| 1 |
// Copyright 2021 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 paths |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
func TestMakePermalink(t *testing.T) { |
| 21 |
type test struct { |
| 22 |
host, link, output string |
| 23 |
} |
| 24 |
|
| 25 |
data := []test{ |
| 26 |
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"}, |
| 27 |
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"}, |
| 28 |
{"http://abc.com", "post/bar", "http://abc.com/post/bar"}, |
| 29 |
{"http://abc.com", "bar", "http://abc.com/bar"}, |
| 30 |
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"}, |
| 31 |
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"}, |
| 32 |
{"http://abc.com/foo", "post/bar?a=b#c", "http://abc.com/foo/post/bar?a=b#c"}, |
| 33 |
} |
| 34 |
|
| 35 |
for i, d := range data { |
| 36 |
output := MakePermalink(d.host, d.link).String() |
| 37 |
if d.output != output { |
| 38 |
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestAddContextRoot(t *testing.T) { |
| 44 |
tests := []struct { |
| 45 |
baseURL string |
| 46 |
url string |
| 47 |
expected string |
| 48 |
}{ |
| 49 |
{"http://example.com/sub/", "/foo", "/sub/foo"}, |
| 50 |
{"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"}, |
| 51 |
{"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"}, |
| 52 |
{"http://example.com", "/foo", "/foo"}, |
| 53 |
// cannot guess that the context root is already added int the example below |
| 54 |
{"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"}, |
| 55 |
{"http://example.com/тря", "/трям/", "/тря/трям/"}, |
| 56 |
{"http://example.com", "/", "/"}, |
| 57 |
{"http://example.com/bar", "//", "/bar/"}, |
| 58 |
} |
| 59 |
|
| 60 |
for _, test := range tests { |
| 61 |
output := AddContextRoot(test.baseURL, test.url) |
| 62 |
if output != test.expected { |
| 63 |
t.Errorf("Expected %#v, got %#v\n", test.expected, output) |
| 64 |
} |
| 65 |
} |
| 66 |
} |