|
| 1 | +// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package urls |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "net/url" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/gohugoio/hugo/deps" |
| 22 | + "github.com/spf13/viper" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +var ns = New(&deps.Deps{Cfg: viper.New()}) |
| 28 | + |
| 29 | +type tstNoStringer struct{} |
| 30 | + |
| 31 | +func TestParse(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + for i, test := range []struct { |
| 35 | + rawurl interface{} |
| 36 | + expect interface{} |
| 37 | + }{ |
| 38 | + { |
| 39 | + "http://www.google.com", |
| 40 | + &url.URL{ |
| 41 | + Scheme: "http", |
| 42 | + Host: "www.google.com", |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + "http://j@ne:password@google.com", |
| 47 | + &url.URL{ |
| 48 | + Scheme: "http", |
| 49 | + User: url.UserPassword("j@ne", "password"), |
| 50 | + Host: "google.com", |
| 51 | + }, |
| 52 | + }, |
| 53 | + // errors |
| 54 | + {tstNoStringer{}, false}, |
| 55 | + } { |
| 56 | + errMsg := fmt.Sprintf("[%d] %v", i, test) |
| 57 | + |
| 58 | + result, err := ns.Parse(test.rawurl) |
| 59 | + |
| 60 | + if b, ok := test.expect.(bool); ok && !b { |
| 61 | + require.Error(t, err, errMsg) |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + require.NoError(t, err, errMsg) |
| 66 | + assert.Equal(t, test.expect, result, errMsg) |
| 67 | + } |
| 68 | +} |
0 commit comments