Debian Codegraph @

tpl/urls/urls_test.go

raw
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 "net/url"
18 "regexp"
19 "testing"
20
21 "github.com/gohugoio/hugo/config/testconfig"
22 "github.com/gohugoio/hugo/htesting/hqt"
23
24 qt "github.com/frankban/quicktest"
25 )
26
27 func newNs() *Namespace {
28 return New(testconfig.GetTestDeps(nil, nil))
29 }
30
31 type tstNoStringer struct{}
32
33 func TestParse(t *testing.T) {
34 t.Parallel()
35 c := qt.New(t)
36 ns := newNs()
37
38 for _, test := range []struct {
39 rawurl any
40 expect any
41 }{
42 {
43 "http://www.google.com",
44 &url.URL{
45 Scheme: "http",
46 Host: "www.google.com",
47 },
48 },
49 {
50 "http://j@ne:[email protected]",
51 &url.URL{
52 Scheme: "http",
53 User: url.UserPassword("j@ne", "password"),
54 Host: "google.com",
55 },
56 },
57 // errors
58 {tstNoStringer{}, false},
59 } {
60
61 result, err := ns.Parse(test.rawurl)
62
63 if b, ok := test.expect.(bool); ok && !b {
64 c.Assert(err, qt.Not(qt.IsNil))
65 continue
66 }
67
68 c.Assert(err, qt.IsNil)
69 c.Assert(result,
70 qt.CmpEquals(hqt.DeepAllowUnexported(&url.URL{}, url.Userinfo{})), test.expect)
71 }
72 }
73
74 func TestJoinPath(t *testing.T) {
75 t.Parallel()
76 c := qt.New(t)
77 ns := newNs()
78
79 for _, test := range []struct {
80 elements any
81 expect any
82 }{
83 {"", `/`},
84 {"a", `a`},
85 {"/a/b", `/a/b`},
86 {"./../a/b", `a/b`},
87 {[]any{""}, `/`},
88 {[]any{"a"}, `a`},
89 {[]any{"/a", "b"}, `/a/b`},
90 {[]any{".", "..", "/a", "b"}, `a/b`},
91 {[]any{"https://example.org", "a"}, `https://example.org/a`},
92 {[]any{nil}, `/`},
93 // errors
94 {tstNoStringer{}, false},
95 {[]any{tstNoStringer{}}, false},
96 } {
97
98 result, err := ns.JoinPath(test.elements)
99
100 if b, ok := test.expect.(bool); ok && !b {
101 c.Assert(err, qt.Not(qt.IsNil))
102 continue
103 }
104
105 c.Assert(err, qt.IsNil)
106 c.Assert(result, qt.Equals, test.expect)
107 }
108 }
109
110 func TestPathEscape(t *testing.T) {
111 t.Parallel()
112 c := qt.New(t)
113 ns := newNs()
114
115 tests := []struct {
116 name string
117 input any
118 want string
119 wantErr bool
120 errCheck string
121 }{
122 {"string", "A/b/c?d=é&f=g+h", "A%2Fb%2Fc%3Fd=%C3%A9&f=g+h", false, ""},
123 {"empty string", "", "", false, ""},
124 {"integer", 6, "6", false, ""},
125 {"float", 7.42, "7.42", false, ""},
126 {"nil", nil, "", false, ""},
127 {"slice", []int{}, "", true, "unable to cast"},
128 {"map", map[string]string{}, "", true, "unable to cast"},
129 {"struct", tstNoStringer{}, "", true, "unable to cast"},
130 }
131
132 for _, tt := range tests {
133 c.Run(tt.name, func(c *qt.C) {
134 got, err := ns.PathEscape(tt.input)
135 if tt.wantErr {
136 c.Assert(err, qt.IsNotNil, qt.Commentf("PathEscape(%v) should have failed", tt.input))
137 if tt.errCheck != "" {
138 c.Assert(err, qt.ErrorMatches, ".*"+regexp.QuoteMeta(tt.errCheck)+".*")
139 }
140 } else {
141 c.Assert(err, qt.IsNil)
142 c.Assert(got, qt.Equals, tt.want)
143 }
144 })
145 }
146 }
147
148 func TestPathUnescape(t *testing.T) {
149 t.Parallel()
150 c := qt.New(t)
151 ns := newNs()
152
153 tests := []struct {
154 name string
155 input any
156 want string
157 wantErr bool
158 errCheck string
159 }{
160 {"string", "A%2Fb%2Fc%3Fd=%C3%A9&f=g+h", "A/b/c?d=é&f=g+h", false, ""},
161 {"empty string", "", "", false, ""},
162 {"integer", 6, "6", false, ""},
163 {"float", 7.42, "7.42", false, ""},
164 {"nil", nil, "", false, ""},
165 {"slice", []int{}, "", true, "unable to cast"},
166 {"map", map[string]string{}, "", true, "unable to cast"},
167 {"struct", tstNoStringer{}, "", true, "unable to cast"},
168 {"malformed hex", "bad%g0escape", "", true, "invalid URL escape"},
169 {"incomplete hex", "trailing%", "", true, "invalid URL escape"},
170 {"single hex digit", "trail%1", "", true, "invalid URL escape"},
171 }
172
173 for _, tt := range tests {
174 c.Run(tt.name, func(c *qt.C) {
175 got, err := ns.PathUnescape(tt.input)
176 if tt.wantErr {
177 c.Assert(err, qt.Not(qt.IsNil), qt.Commentf("PathUnescape(%v) should have failed", tt.input))
178 if tt.errCheck != "" {
179 c.Assert(err, qt.ErrorMatches, ".*"+regexp.QuoteMeta(tt.errCheck)+".*")
180 }
181 } else {
182 c.Assert(err, qt.IsNil)
183 c.Assert(got, qt.Equals, tt.want)
184 }
185 })
186 }
187 }