github.com/pietrocarrara/hugo@v0.47.1/helpers/url_test.go (about)

     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  
    14  package helpers
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/hugofs"
    22  	"github.com/gohugoio/hugo/langs"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestURLize(t *testing.T) {
    28  
    29  	v := newTestCfg()
    30  	l := langs.NewDefaultLanguage(v)
    31  	p, _ := NewPathSpec(hugofs.NewMem(v), l)
    32  
    33  	tests := []struct {
    34  		input    string
    35  		expected string
    36  	}{
    37  		{"  foo bar  ", "foo-bar"},
    38  		{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
    39  		{"foo,bar:foobar", "foobarfoobar"},
    40  		{"foo/bar.html", "foo/bar.html"},
    41  		{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
    42  		{"100%-google", "100-google"},
    43  	}
    44  
    45  	for _, test := range tests {
    46  		output := p.URLize(test.input)
    47  		if output != test.expected {
    48  			t.Errorf("Expected %#v, got %#v\n", test.expected, output)
    49  		}
    50  	}
    51  }
    52  
    53  func TestAbsURL(t *testing.T) {
    54  	for _, defaultInSubDir := range []bool{true, false} {
    55  		for _, addLanguage := range []bool{true, false} {
    56  			for _, m := range []bool{true, false} {
    57  				for _, l := range []string{"en", "fr"} {
    58  					doTestAbsURL(t, defaultInSubDir, addLanguage, m, l)
    59  				}
    60  			}
    61  		}
    62  	}
    63  }
    64  
    65  func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
    66  	v := newTestCfg()
    67  	v.Set("multilingual", multilingual)
    68  	v.Set("defaultContentLanguage", "en")
    69  	v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
    70  
    71  	tests := []struct {
    72  		input    string
    73  		baseURL  string
    74  		expected string
    75  	}{
    76  		{"/test/foo", "http://base/", "http://base/MULTItest/foo"},
    77  		{"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
    78  		{"", "http://base/ace/", "http://base/ace/MULTI"},
    79  		{"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
    80  		{"http://abs", "http://base/", "http://abs"},
    81  		{"schema://abs", "http://base/", "schema://abs"},
    82  		{"//schemaless", "http://base/", "//schemaless"},
    83  		{"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
    84  		{lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
    85  		{"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
    86  		{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		v.Set("baseURL", test.baseURL)
    91  		v.Set("contentDir", "content")
    92  		l := langs.NewLanguage(lang, v)
    93  		p, _ := NewPathSpec(hugofs.NewMem(v), l)
    94  
    95  		output := p.AbsURL(test.input, addLanguage)
    96  		expected := test.expected
    97  		if multilingual && addLanguage {
    98  			if !defaultInSubDir && lang == "en" {
    99  				expected = strings.Replace(expected, "MULTI", "", 1)
   100  			} else {
   101  				expected = strings.Replace(expected, "MULTI", lang+"/", 1)
   102  			}
   103  
   104  		} else {
   105  			expected = strings.Replace(expected, "MULTI", "", 1)
   106  		}
   107  		if output != expected {
   108  			t.Fatalf("Expected %#v, got %#v\n", expected, output)
   109  		}
   110  	}
   111  }
   112  
   113  func TestIsAbsURL(t *testing.T) {
   114  	for i, this := range []struct {
   115  		a string
   116  		b bool
   117  	}{
   118  		{"http://gohugo.io", true},
   119  		{"https://gohugo.io", true},
   120  		{"//gohugo.io", true},
   121  		{"http//gohugo.io", false},
   122  		{"/content", false},
   123  		{"content", false},
   124  	} {
   125  		require.True(t, IsAbsURL(this.a) == this.b, fmt.Sprintf("Test %d", i))
   126  	}
   127  }
   128  
   129  func TestRelURL(t *testing.T) {
   130  	for _, defaultInSubDir := range []bool{true, false} {
   131  		for _, addLanguage := range []bool{true, false} {
   132  			for _, m := range []bool{true, false} {
   133  				for _, l := range []string{"en", "fr"} {
   134  					doTestRelURL(t, defaultInSubDir, addLanguage, m, l)
   135  				}
   136  			}
   137  		}
   138  	}
   139  }
   140  
   141  func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
   142  	v := newTestCfg()
   143  	v.Set("multilingual", multilingual)
   144  	v.Set("defaultContentLanguage", "en")
   145  	v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
   146  
   147  	tests := []struct {
   148  		input    string
   149  		baseURL  string
   150  		canonify bool
   151  		expected string
   152  	}{
   153  		{"/test/foo", "http://base/", false, "MULTI/test/foo"},
   154  		{"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
   155  		{lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
   156  		{"test.css", "http://base/sub", false, "/subMULTI/test.css"},
   157  		{"test.css", "http://base/sub", true, "MULTI/test.css"},
   158  		{"/test/", "http://base/", false, "MULTI/test/"},
   159  		{"/test/", "http://base/sub/", false, "/subMULTI/test/"},
   160  		{"/test/", "http://base/sub/", true, "MULTI/test/"},
   161  		{"", "http://base/ace/", false, "/aceMULTI/"},
   162  		{"", "http://base/ace", false, "/aceMULTI"},
   163  		{"http://abs", "http://base/", false, "http://abs"},
   164  		{"//schemaless", "http://base/", false, "//schemaless"},
   165  	}
   166  
   167  	for i, test := range tests {
   168  		v.Set("baseURL", test.baseURL)
   169  		v.Set("canonifyURLs", test.canonify)
   170  		l := langs.NewLanguage(lang, v)
   171  		p, _ := NewPathSpec(hugofs.NewMem(v), l)
   172  
   173  		output := p.RelURL(test.input, addLanguage)
   174  
   175  		expected := test.expected
   176  		if multilingual && addLanguage {
   177  			if !defaultInSubDir && lang == "en" {
   178  				expected = strings.Replace(expected, "MULTI", "", 1)
   179  			} else {
   180  				expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
   181  			}
   182  		} else {
   183  			expected = strings.Replace(expected, "MULTI", "", 1)
   184  		}
   185  
   186  		if output != expected {
   187  			t.Errorf("[%d][%t] Expected %#v, got %#v\n", i, test.canonify, expected, output)
   188  		}
   189  	}
   190  }
   191  
   192  func TestSanitizeURL(t *testing.T) {
   193  	tests := []struct {
   194  		input    string
   195  		expected string
   196  	}{
   197  		{"http://foo.bar/", "http://foo.bar"},
   198  		{"http://foo.bar", "http://foo.bar"},          // issue #1105
   199  		{"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
   200  	}
   201  
   202  	for i, test := range tests {
   203  		o1 := SanitizeURL(test.input)
   204  		o2 := SanitizeURLKeepTrailingSlash(test.input)
   205  
   206  		expected2 := test.expected
   207  
   208  		if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
   209  			expected2 += "/"
   210  		}
   211  
   212  		if o1 != test.expected {
   213  			t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1)
   214  		}
   215  		if o2 != expected2 {
   216  			t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2)
   217  		}
   218  	}
   219  }
   220  
   221  func TestMakePermalink(t *testing.T) {
   222  	type test struct {
   223  		host, link, output string
   224  	}
   225  
   226  	data := []test{
   227  		{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
   228  		{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
   229  		{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
   230  		{"http://abc.com", "bar", "http://abc.com/bar"},
   231  		{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
   232  		{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
   233  	}
   234  
   235  	for i, d := range data {
   236  		output := MakePermalink(d.host, d.link).String()
   237  		if d.output != output {
   238  			t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
   239  		}
   240  	}
   241  }
   242  
   243  func TestURLPrep(t *testing.T) {
   244  	type test struct {
   245  		ugly   bool
   246  		input  string
   247  		output string
   248  	}
   249  
   250  	data := []test{
   251  		{false, "/section/name.html", "/section/name/"},
   252  		{true, "/section/name/index.html", "/section/name.html"},
   253  	}
   254  
   255  	for i, d := range data {
   256  		v := newTestCfg()
   257  		v.Set("uglyURLs", d.ugly)
   258  		l := langs.NewDefaultLanguage(v)
   259  		p, _ := NewPathSpec(hugofs.NewMem(v), l)
   260  
   261  		output := p.URLPrep(d.input)
   262  		if d.output != output {
   263  			t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
   264  		}
   265  	}
   266  
   267  }
   268  
   269  func TestAddContextRoot(t *testing.T) {
   270  	tests := []struct {
   271  		baseURL  string
   272  		url      string
   273  		expected string
   274  	}{
   275  		{"http://example.com/sub/", "/foo", "/sub/foo"},
   276  		{"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"},
   277  		{"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"},
   278  		{"http://example.com", "/foo", "/foo"},
   279  		// cannot guess that the context root is already added int the example below
   280  		{"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"},
   281  		{"http://example.com/тря", "/трям/", "/тря/трям/"},
   282  		{"http://example.com", "/", "/"},
   283  		{"http://example.com/bar", "//", "/bar/"},
   284  	}
   285  
   286  	for _, test := range tests {
   287  		output := AddContextRoot(test.baseURL, test.url)
   288  		if output != test.expected {
   289  			t.Errorf("Expected %#v, got %#v\n", test.expected, output)
   290  		}
   291  	}
   292  }
   293  
   294  func TestPretty(t *testing.T) {
   295  	assert.Equal(t, PrettifyURLPath("/section/name.html"), "/section/name/index.html")
   296  	assert.Equal(t, PrettifyURLPath("/section/sub/name.html"), "/section/sub/name/index.html")
   297  	assert.Equal(t, PrettifyURLPath("/section/name/"), "/section/name/index.html")
   298  	assert.Equal(t, PrettifyURLPath("/section/name/index.html"), "/section/name/index.html")
   299  	assert.Equal(t, PrettifyURLPath("/index.html"), "/index.html")
   300  	assert.Equal(t, PrettifyURLPath("/name.xml"), "/name/index.xml")
   301  	assert.Equal(t, PrettifyURLPath("/"), "/")
   302  	assert.Equal(t, PrettifyURLPath(""), "/")
   303  	assert.Equal(t, PrettifyURL("/section/name.html"), "/section/name")
   304  	assert.Equal(t, PrettifyURL("/section/sub/name.html"), "/section/sub/name")
   305  	assert.Equal(t, PrettifyURL("/section/name/"), "/section/name")
   306  	assert.Equal(t, PrettifyURL("/section/name/index.html"), "/section/name")
   307  	assert.Equal(t, PrettifyURL("/index.html"), "/")
   308  	assert.Equal(t, PrettifyURL("/name.xml"), "/name/index.xml")
   309  	assert.Equal(t, PrettifyURL("/"), "/")
   310  	assert.Equal(t, PrettifyURL(""), "/")
   311  }
   312  
   313  func TestUgly(t *testing.T) {
   314  	assert.Equal(t, Uglify("/section/name.html"), "/section/name.html")
   315  	assert.Equal(t, Uglify("/section/sub/name.html"), "/section/sub/name.html")
   316  	assert.Equal(t, Uglify("/section/name/"), "/section/name.html")
   317  	assert.Equal(t, Uglify("/section/name/index.html"), "/section/name.html")
   318  	assert.Equal(t, Uglify("/index.html"), "/index.html")
   319  	assert.Equal(t, Uglify("/name.xml"), "/name.xml")
   320  	assert.Equal(t, Uglify("/"), "/")
   321  	assert.Equal(t, Uglify(""), "/")
   322  }