github.com/gohugoio/hugo@v0.88.1/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  	"strings"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  func TestSanitizeURL(t *testing.T) {
    24  	tests := []struct {
    25  		input    string
    26  		expected string
    27  	}{
    28  		{"http://foo.bar/", "http://foo.bar"},
    29  		{"http://foo.bar", "http://foo.bar"},          // issue #1105
    30  		{"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
    31  	}
    32  
    33  	for i, test := range tests {
    34  		o1 := SanitizeURL(test.input)
    35  		o2 := SanitizeURLKeepTrailingSlash(test.input)
    36  
    37  		expected2 := test.expected
    38  
    39  		if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
    40  			expected2 += "/"
    41  		}
    42  
    43  		if o1 != test.expected {
    44  			t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1)
    45  		}
    46  		if o2 != expected2 {
    47  			t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2)
    48  		}
    49  	}
    50  }
    51  
    52  func TestMakePermalink(t *testing.T) {
    53  	type test struct {
    54  		host, link, output string
    55  	}
    56  
    57  	data := []test{
    58  		{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
    59  		{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
    60  		{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
    61  		{"http://abc.com", "bar", "http://abc.com/bar"},
    62  		{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
    63  		{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
    64  	}
    65  
    66  	for i, d := range data {
    67  		output := MakePermalink(d.host, d.link).String()
    68  		if d.output != output {
    69  			t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
    70  		}
    71  	}
    72  }
    73  
    74  func TestAddContextRoot(t *testing.T) {
    75  	tests := []struct {
    76  		baseURL  string
    77  		url      string
    78  		expected string
    79  	}{
    80  		{"http://example.com/sub/", "/foo", "/sub/foo"},
    81  		{"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"},
    82  		{"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"},
    83  		{"http://example.com", "/foo", "/foo"},
    84  		// cannot guess that the context root is already added int the example below
    85  		{"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"},
    86  		{"http://example.com/тря", "/трям/", "/тря/трям/"},
    87  		{"http://example.com", "/", "/"},
    88  		{"http://example.com/bar", "//", "/bar/"},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		output := AddContextRoot(test.baseURL, test.url)
    93  		if output != test.expected {
    94  			t.Errorf("Expected %#v, got %#v\n", test.expected, output)
    95  		}
    96  	}
    97  }
    98  
    99  func TestPretty(t *testing.T) {
   100  	c := qt.New(t)
   101  	c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name.html"))
   102  	c.Assert("/section/sub/name/index.html", qt.Equals, PrettifyURLPath("/section/sub/name.html"))
   103  	c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/"))
   104  	c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/index.html"))
   105  	c.Assert("/index.html", qt.Equals, PrettifyURLPath("/index.html"))
   106  	c.Assert("/name/index.xml", qt.Equals, PrettifyURLPath("/name.xml"))
   107  	c.Assert("/", qt.Equals, PrettifyURLPath("/"))
   108  	c.Assert("/", qt.Equals, PrettifyURLPath(""))
   109  	c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name.html"))
   110  	c.Assert("/section/sub/name", qt.Equals, PrettifyURL("/section/sub/name.html"))
   111  	c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/"))
   112  	c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/index.html"))
   113  	c.Assert("/", qt.Equals, PrettifyURL("/index.html"))
   114  	c.Assert("/name/index.xml", qt.Equals, PrettifyURL("/name.xml"))
   115  	c.Assert("/", qt.Equals, PrettifyURL("/"))
   116  	c.Assert("/", qt.Equals, PrettifyURL(""))
   117  }
   118  
   119  func TestUgly(t *testing.T) {
   120  	c := qt.New(t)
   121  	c.Assert("/section/name.html", qt.Equals, Uglify("/section/name.html"))
   122  	c.Assert("/section/sub/name.html", qt.Equals, Uglify("/section/sub/name.html"))
   123  	c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/"))
   124  	c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/index.html"))
   125  	c.Assert("/index.html", qt.Equals, Uglify("/index.html"))
   126  	c.Assert("/name.xml", qt.Equals, Uglify("/name.xml"))
   127  	c.Assert("/", qt.Equals, Uglify("/"))
   128  	c.Assert("/", qt.Equals, Uglify(""))
   129  }