url_test.go (3906B)
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 qt "github.com/frankban/quicktest"
20 )
21
22 func TestMakePermalink(t *testing.T) {
23 type test struct {
24 host, link, output string
25 }
26
27 data := []test{
28 {"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
29 {"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
30 {"http://abc.com", "post/bar", "http://abc.com/post/bar"},
31 {"http://abc.com", "bar", "http://abc.com/bar"},
32 {"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
33 {"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
34 }
35
36 for i, d := range data {
37 output := MakePermalink(d.host, d.link).String()
38 if d.output != output {
39 t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
40 }
41 }
42 }
43
44 func TestAddContextRoot(t *testing.T) {
45 tests := []struct {
46 baseURL string
47 url string
48 expected string
49 }{
50 {"http://example.com/sub/", "/foo", "/sub/foo"},
51 {"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"},
52 {"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"},
53 {"http://example.com", "/foo", "/foo"},
54 // cannot guess that the context root is already added int the example below
55 {"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"},
56 {"http://example.com/тря", "/трям/", "/тря/трям/"},
57 {"http://example.com", "/", "/"},
58 {"http://example.com/bar", "//", "/bar/"},
59 }
60
61 for _, test := range tests {
62 output := AddContextRoot(test.baseURL, test.url)
63 if output != test.expected {
64 t.Errorf("Expected %#v, got %#v\n", test.expected, output)
65 }
66 }
67 }
68
69 func TestPretty(t *testing.T) {
70 c := qt.New(t)
71 c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name.html"))
72 c.Assert("/section/sub/name/index.html", qt.Equals, PrettifyURLPath("/section/sub/name.html"))
73 c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/"))
74 c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/index.html"))
75 c.Assert("/index.html", qt.Equals, PrettifyURLPath("/index.html"))
76 c.Assert("/name/index.xml", qt.Equals, PrettifyURLPath("/name.xml"))
77 c.Assert("/", qt.Equals, PrettifyURLPath("/"))
78 c.Assert("/", qt.Equals, PrettifyURLPath(""))
79 c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name.html"))
80 c.Assert("/section/sub/name", qt.Equals, PrettifyURL("/section/sub/name.html"))
81 c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/"))
82 c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/index.html"))
83 c.Assert("/", qt.Equals, PrettifyURL("/index.html"))
84 c.Assert("/name/index.xml", qt.Equals, PrettifyURL("/name.xml"))
85 c.Assert("/", qt.Equals, PrettifyURL("/"))
86 c.Assert("/", qt.Equals, PrettifyURL(""))
87 }
88
89 func TestUgly(t *testing.T) {
90 c := qt.New(t)
91 c.Assert("/section/name.html", qt.Equals, Uglify("/section/name.html"))
92 c.Assert("/section/sub/name.html", qt.Equals, Uglify("/section/sub/name.html"))
93 c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/"))
94 c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/index.html"))
95 c.Assert("/index.html", qt.Equals, Uglify("/index.html"))
96 c.Assert("/name.xml", qt.Equals, Uglify("/name.xml"))
97 c.Assert("/", qt.Equals, Uglify("/"))
98 c.Assert("/", qt.Equals, Uglify(""))
99 }