Debian Codegraph @

helpers/url_test.go

raw
1 // Copyright 2024 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 helpers_test
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/config"
23 )
24
25 func TestURLize(t *testing.T) {
26 p := newTestPathSpec()
27
28 tests := []struct {
29 input string
30 expected string
31 }{
32 {" foo bar ", "foo-bar"},
33 {"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
34 {"foo,bar:foobar", "foobarfoobar"},
35 {"foo/bar.html", "foo/bar.html"},
36 {"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
37 {"100%-google", "100-google"},
38 }
39
40 for _, test := range tests {
41 output := p.URLize(test.input)
42 if output != test.expected {
43 t.Errorf("Expected %#v, got %#v\n", test.expected, output)
44 }
45 }
46 }
47
48 func TestAbsURL(t *testing.T) {
49 for _, defaultInSubDir := range []bool{true, false} {
50 for _, addLanguage := range []bool{true, false} {
51 for _, m := range []bool{true, false} {
52 for _, l := range []string{"en", "fr"} {
53 doTestAbsURL(t, defaultInSubDir, addLanguage, m, l)
54 }
55 }
56 }
57 }
58 }
59
60 func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
61 c := qt.New(t)
62
63 tests := []struct {
64 input string
65 baseURL string
66 expected string
67 }{
68 // Issue 9994
69 {"foo/bar", "https://example.org/foo/", "https://example.org/foo/MULTIfoo/bar"},
70 {"/foo/bar", "https://example.org/foo/", "https://example.org/MULTIfoo/bar"},
71
72 {"/test/foo", "http://base/", "http://base/MULTItest/foo"},
73 {"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
74 {"", "http://base/ace/", "http://base/ace/MULTI"},
75 {"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
76 {"http://abs", "http://base/", "http://abs"},
77 {"schema://abs", "http://base/", "schema://abs"},
78 {"//schemaless", "http://base/", "//schemaless"},
79 {"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
80 {lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
81 {"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
82 {"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
83 }
84
85 if multilingual && addLanguage && defaultInSubDir {
86 newTests := []struct {
87 input string
88 baseURL string
89 expected string
90 }{
91 {lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
92 {"/" + lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
93 }
94
95 tests = append(tests, newTests...)
96
97 }
98
99 for _, test := range tests {
100 c.Run(fmt.Sprintf("%v/%t-%t-%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
101 v := config.New()
102 if multilingual {
103 v.Set("languages", map[string]any{
104 "fr": map[string]any{
105 "weight": 20,
106 },
107 "en": map[string]any{
108 "weight": 10,
109 },
110 })
111 v.Set("defaultContentLanguage", "en")
112 } else {
113 v.Set("defaultContentLanguage", lang)
114 v.Set("languages", map[string]any{
115 lang: map[string]any{
116 "weight": 10,
117 },
118 })
119 }
120
121 v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
122 v.Set("baseURL", test.baseURL)
123
124 var configLang string
125 if multilingual {
126 configLang = lang
127 }
128 defaultContentLanguage := lang
129 if multilingual {
130 defaultContentLanguage = "en"
131 }
132
133 p := newTestPathSpecFromCfgAndLang(v, configLang)
134
135 output := p.AbsURL(test.input, addLanguage)
136 expected := test.expected
137 if addLanguage {
138 addLanguage = defaultInSubDir && lang == defaultContentLanguage
139 addLanguage = addLanguage || (lang != defaultContentLanguage && multilingual)
140 }
141 if addLanguage {
142 expected = strings.Replace(expected, "MULTI", lang+"/", 1)
143 } else {
144 expected = strings.Replace(expected, "MULTI", "", 1)
145 }
146
147 c.Assert(output, qt.Equals, expected)
148 })
149 }
150 }
151
152 func TestRelURL(t *testing.T) {
153 for _, defaultInSubDir := range []bool{true, false} {
154 for _, addLanguage := range []bool{true, false} {
155 for _, m := range []bool{true, false} {
156 for _, l := range []string{"en", "fr"} {
157 doTestRelURL(t, defaultInSubDir, addLanguage, m, l)
158 }
159 }
160 }
161 }
162 }
163
164 func doTestRelURL(t testing.TB, defaultInSubDir, addLanguage, multilingual bool, lang string) {
165 t.Helper()
166 c := qt.New(t)
167 v := config.New()
168 if multilingual {
169 v.Set("languages", map[string]any{
170 "fr": map[string]any{
171 "weight": 20,
172 },
173 "en": map[string]any{
174 "weight": 10,
175 },
176 })
177 v.Set("defaultContentLanguage", "en")
178 } else {
179 v.Set("defaultContentLanguage", lang)
180 v.Set("languages", map[string]any{
181 lang: map[string]any{
182 "weight": 10,
183 },
184 })
185 }
186
187 v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
188
189 tests := []struct {
190 input string
191 baseURL string
192 canonify bool
193 expected string
194 }{
195 // Issue 9994
196 {"/foo/bar", "https://example.org/foo/", false, "MULTI/foo/bar"},
197 {"foo/bar", "https://example.org/foo/", false, "/fooMULTI/foo/bar"},
198
199 // Issue 11080
200 {"mailto:[email protected]", "http://base/", false, "mailto:[email protected]"},
201 {"ftp://b.com/a.txt", "http://base/", false, "ftp://b.com/a.txt"},
202
203 {"/test/foo", "http://base/", false, "MULTI/test/foo"},
204 {"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
205 {lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
206 {"test.css", "http://base/sub", false, "/subMULTI/test.css"},
207 {"test.css", "http://base/sub", true, "MULTI/test.css"},
208 {"/test/", "http://base/", false, "MULTI/test/"},
209 {"test/", "http://base/sub/", false, "/subMULTI/test/"},
210 {"/test/", "http://base/sub/", true, "MULTI/test/"},
211 {"", "http://base/ace/", false, "/aceMULTI/"},
212 {"", "http://base/ace", false, "/aceMULTI/"},
213 {"http://abs", "http://base/", false, "http://abs"},
214 {"//schemaless", "http://base/", false, "//schemaless"},
215 }
216
217 if multilingual && addLanguage && defaultInSubDir {
218 newTests := []struct {
219 input string
220 baseURL string
221 canonify bool
222 expected string
223 }{
224 {lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
225 {"/" + lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
226 }
227 tests = append(tests, newTests...)
228 }
229
230 for i, test := range tests {
231 c.Run(fmt.Sprintf("%v/defaultInSubDir=%t;addLanguage=%t;multilingual=%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
232 v.Set("baseURL", test.baseURL)
233 v.Set("canonifyURLs", test.canonify)
234 defaultContentLanguage := lang
235 if multilingual {
236 defaultContentLanguage = "en"
237 }
238 p := newTestPathSpecFromCfgAndLang(v, lang)
239
240 output := p.RelURL(test.input, addLanguage)
241
242 expected := test.expected
243 if addLanguage {
244 addLanguage = defaultInSubDir && lang == defaultContentLanguage
245 addLanguage = addLanguage || (lang != defaultContentLanguage && multilingual)
246 }
247 if addLanguage {
248 expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
249 } else {
250 expected = strings.Replace(expected, "MULTI", "", 1)
251 }
252
253 c.Assert(output, qt.Equals, expected, qt.Commentf("[%d] %s", i, test.input))
254 })
255 }
256 }
257
258 func BenchmarkRelURL(b *testing.B) {
259 v := config.New()
260 v.Set("baseURL", "https://base/")
261 p := newTestPathSpecFromCfgAndLang(v, "")
262
263 for b.Loop() {
264 _ = p.RelURL("https://base/foo/bar", false)
265 }
266 }
267
268 func BenchmarkAbsURL(b *testing.B) {
269 v := config.New()
270 v.Set("baseURL", "https://base/")
271 p := newTestPathSpecFromCfgAndLang(v, "")
272 b.ResetTimer()
273 b.Run("relurl", func(b *testing.B) {
274 for b.Loop() {
275 _ = p.AbsURL("foo/bar", false)
276 }
277 })
278 b.Run("absurl", func(b *testing.B) {
279 for b.Loop() {
280 _ = p.AbsURL("https://base/foo/bar", false)
281 }
282 })
283 }