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