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