2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
package helpers_test
|
2014-09-10 13:43:02 -04:00
|
|
|
|
|
|
|
import (
|
2023-01-04 12:24:36 -05:00
|
|
|
"fmt"
|
2015-02-28 12:45:02 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2022-06-13 03:52:02 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2014-09-10 13:43:02 -04:00
|
|
|
)
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestURLize(t *testing.T) {
|
2023-01-04 12:24:36 -05:00
|
|
|
p := newTestPathSpec()
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" foo bar ", "foo-bar"},
|
|
|
|
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"foo,bar:foobar", "foobarfoobar"},
|
2014-09-10 13:43:02 -04:00
|
|
|
{"foo/bar.html", "foo/bar.html"},
|
|
|
|
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
|
2016-07-11 04:06:40 -04:00
|
|
|
{"100%-google", "100-google"},
|
2014-09-10 13:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.URLize(test.input)
|
2014-09-10 13:43:02 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 06:28:44 -04:00
|
|
|
func TestAbsURL(t *testing.T) {
|
2016-08-08 07:55:18 -04:00
|
|
|
for _, defaultInSubDir := range []bool{true, false} {
|
|
|
|
for _, addLanguage := range []bool{true, false} {
|
|
|
|
for _, m := range []bool{true, false} {
|
|
|
|
for _, l := range []string{"en", "fr"} {
|
|
|
|
doTestAbsURL(t, defaultInSubDir, addLanguage, m, l)
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
2022-06-13 03:52:02 -04:00
|
|
|
c := qt.New(t)
|
2016-08-08 07:55:18 -04:00
|
|
|
|
2015-05-11 06:28:44 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
expected string
|
|
|
|
}{
|
2022-06-13 03:52:02 -04:00
|
|
|
// Issue 9994
|
|
|
|
{"foo/bar", "https://example.org/foo/", "https://example.org/foo/MULTIfoo/bar"},
|
|
|
|
{"/foo/bar", "https://example.org/foo/", "https://example.org/MULTIfoo/bar"},
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/foo", "http://base/", "http://base/MULTItest/foo"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"", "http://base/ace/", "http://base/ace/MULTI"},
|
|
|
|
{"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
|
2015-05-11 06:28:44 -04:00
|
|
|
{"http://abs", "http://base/", "http://abs"},
|
2016-04-27 10:54:44 -04:00
|
|
|
{"schema://abs", "http://base/", "schema://abs"},
|
2015-05-11 06:28:44 -04:00
|
|
|
{"//schemaless", "http://base/", "//schemaless"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
|
|
|
|
{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
|
2015-05-11 06:28:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:47:01 -05:00
|
|
|
if multilingual && addLanguage && defaultInSubDir {
|
|
|
|
newTests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
|
|
|
|
{"/" + lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:27:27 -04:00
|
|
|
tests = append(tests, newTests...)
|
|
|
|
|
2020-12-02 06:47:01 -05:00
|
|
|
}
|
|
|
|
|
2015-05-11 06:28:44 -04:00
|
|
|
for _, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Run(fmt.Sprintf("%v/%t-%t-%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
|
|
|
|
v := config.New()
|
|
|
|
if multilingual {
|
|
|
|
v.Set("languages", map[string]any{
|
|
|
|
"fr": map[string]interface{}{
|
|
|
|
"weight": 20,
|
|
|
|
},
|
|
|
|
"en": map[string]interface{}{
|
|
|
|
"weight": 10,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
v.Set("defaultContentLanguage", "en")
|
|
|
|
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
|
|
|
|
v.Set("baseURL", test.baseURL)
|
|
|
|
|
|
|
|
var configLang string
|
|
|
|
if multilingual {
|
|
|
|
configLang = lang
|
|
|
|
}
|
|
|
|
p := newTestPathSpecFromCfgAndLang(v, configLang)
|
|
|
|
|
|
|
|
output := p.AbsURL(test.input, addLanguage)
|
|
|
|
expected := test.expected
|
|
|
|
if multilingual && addLanguage {
|
|
|
|
if !defaultInSubDir && lang == "en" {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
|
|
|
|
}
|
2016-08-08 07:55:18 -04:00
|
|
|
} else {
|
2023-01-04 12:24:36 -05:00
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
2016-08-08 07:55:18 -04:00
|
|
|
}
|
2022-06-13 03:52:02 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(output, qt.Equals, expected)
|
|
|
|
})
|
2015-05-11 06:28:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
func TestRelURL(t *testing.T) {
|
2016-08-08 07:55:18 -04:00
|
|
|
for _, defaultInSubDir := range []bool{true, false} {
|
|
|
|
for _, addLanguage := range []bool{true, false} {
|
|
|
|
for _, m := range []bool{true, false} {
|
|
|
|
for _, l := range []string{"en", "fr"} {
|
|
|
|
doTestRelURL(t, defaultInSubDir, addLanguage, m, l)
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
2023-01-04 12:24:36 -05:00
|
|
|
t.Helper()
|
2022-06-13 03:52:02 -04:00
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
v := config.New()
|
|
|
|
if multilingual {
|
|
|
|
v.Set("languages", map[string]any{
|
|
|
|
"fr": map[string]interface{}{
|
|
|
|
"weight": 20,
|
|
|
|
},
|
|
|
|
"en": map[string]interface{}{
|
|
|
|
"weight": 10,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("defaultContentLanguage", "en")
|
|
|
|
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
|
2016-08-07 16:01:55 -04:00
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
canonify bool
|
|
|
|
expected string
|
|
|
|
}{
|
2022-06-13 03:52:02 -04:00
|
|
|
|
|
|
|
// Issue 9994
|
|
|
|
{"/foo/bar", "https://example.org/foo/", false, "MULTI/foo/bar"},
|
|
|
|
{"foo/bar", "https://example.org/foo/", false, "/fooMULTI/foo/bar"},
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/foo", "http://base/", false, "MULTI/test/foo"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
|
|
|
|
{lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"test.css", "http://base/sub", false, "/subMULTI/test.css"},
|
|
|
|
{"test.css", "http://base/sub", true, "MULTI/test.css"},
|
|
|
|
{"/test/", "http://base/", false, "MULTI/test/"},
|
2022-06-13 03:52:02 -04:00
|
|
|
{"test/", "http://base/sub/", false, "/subMULTI/test/"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/", "http://base/sub/", true, "MULTI/test/"},
|
|
|
|
{"", "http://base/ace/", false, "/aceMULTI/"},
|
|
|
|
{"", "http://base/ace", false, "/aceMULTI"},
|
2015-05-11 07:59:06 -04:00
|
|
|
{"http://abs", "http://base/", false, "http://abs"},
|
|
|
|
{"//schemaless", "http://base/", false, "//schemaless"},
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:47:01 -05:00
|
|
|
if multilingual && addLanguage && defaultInSubDir {
|
|
|
|
newTests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
canonify bool
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
|
|
|
|
{"/" + lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
|
|
|
|
}
|
2021-06-18 04:27:27 -04:00
|
|
|
tests = append(tests, newTests...)
|
2020-12-02 06:47:01 -05:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
for i, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Run(fmt.Sprintf("%v/%t%t%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
|
2015-05-11 07:59:06 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
v.Set("baseURL", test.baseURL)
|
|
|
|
v.Set("canonifyURLs", test.canonify)
|
|
|
|
var configLang string
|
|
|
|
if multilingual {
|
|
|
|
configLang = lang
|
|
|
|
}
|
|
|
|
p := newTestPathSpecFromCfgAndLang(v, configLang)
|
2016-08-07 16:01:55 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
output := p.RelURL(test.input, addLanguage)
|
|
|
|
|
|
|
|
expected := test.expected
|
|
|
|
if multilingual && addLanguage {
|
|
|
|
if !defaultInSubDir && lang == "en" {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
|
|
|
|
}
|
2016-08-08 07:55:18 -04:00
|
|
|
} else {
|
2023-01-04 12:24:36 -05:00
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
2016-08-08 07:55:18 -04:00
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(output, qt.Equals, expected, qt.Commentf("[%d] %s", i, test.input))
|
|
|
|
})
|
2022-06-13 03:52:02 -04:00
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestSanitizeURL(t *testing.T) {
|
2015-02-28 12:45:02 -05:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
2015-05-05 10:02:52 -04:00
|
|
|
{"http://foo.bar/", "http://foo.bar"},
|
|
|
|
{"http://foo.bar", "http://foo.bar"}, // issue #1105
|
2015-02-28 12:45:02 -05:00
|
|
|
{"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:02:52 -04:00
|
|
|
for i, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
o1 := helpers.SanitizeURL(test.input)
|
|
|
|
o2 := helpers.SanitizeURLKeepTrailingSlash(test.input)
|
2015-02-28 12:45:02 -05:00
|
|
|
|
|
|
|
expected2 := test.expected
|
|
|
|
|
|
|
|
if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
|
|
|
|
expected2 += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if o1 != test.expected {
|
2015-05-05 10:02:52 -04:00
|
|
|
t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1)
|
2015-02-28 12:45:02 -05:00
|
|
|
}
|
|
|
|
if o2 != expected2 {
|
2015-05-05 10:02:52 -04:00
|
|
|
t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2)
|
2015-02-28 12:45:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|