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