Do not add trailing slash to baseURL
authorbep <bjorn.erik.pedersen@gmail.com>
Tue, 5 May 2015 14:02:52 +0000 (16:02 +0200)
committerbep <bjorn.erik.pedersen@gmail.com>
Tue, 5 May 2015 14:02:44 +0000 (16:02 +0200)
Fixes #1105

helpers/url.go
helpers/url_test.go
hugolib/site_url_test.go

index 13421632f725cc9e97a0894134c442e2c88fc759..c780579d6c3c8b721889df4b45ca3c6993293f9a 100644 (file)
@@ -77,7 +77,7 @@ func sanitizeURLWithFlags(in string, f purell.NormalizationFlags) string {
        if err != nil {
                panic(err)
        }
-       if !strings.HasPrefix(u.Path, "/") {
+       if len(u.Path) > 0 && !strings.HasPrefix(u.Path, "/") {
                u.Path = "/" + u.Path
        }
        return u.String()
index b11be3521190d6ff82f5926ac39c7ca917786cf5..1dabda273c197143a6bd0f0b0c91b0030fd9d36f 100644 (file)
@@ -31,11 +31,12 @@ func TestSanitizeURL(t *testing.T) {
                input    string
                expected string
        }{
-               {"http://foo.bar/", "http://foo.bar/"},
+               {"http://foo.bar/", "http://foo.bar"},
+               {"http://foo.bar", "http://foo.bar"},          // issue #1105
                {"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
        }
 
-       for _, test := range tests {
+       for i, test := range tests {
                o1 := SanitizeURL(test.input)
                o2 := SanitizeURLKeepTrailingSlash(test.input)
 
@@ -46,10 +47,10 @@ func TestSanitizeURL(t *testing.T) {
                }
 
                if o1 != test.expected {
-                       t.Errorf("Expected %#v, got %#v\n", test.expected, o1)
+                       t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1)
                }
                if o2 != expected2 {
-                       t.Errorf("Expected %#v, got %#v\n", expected2, o2)
+                       t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2)
                }
        }
 }
index f2f25f80d440bdff880f3e85b0e57290ac23156b..c7fc64e44b9a8b3e60ee1868cce06086bf987215 100644 (file)
@@ -1,7 +1,6 @@
 package hugolib
 
 import (
-       "html/template"
        "path/filepath"
        "testing"
 
@@ -10,6 +9,7 @@ import (
        "github.com/spf13/hugo/source"
        "github.com/spf13/hugo/target"
        "github.com/spf13/viper"
+       "html/template"
 )
 
 const SLUG_DOC_1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - sd1/foo/\n - sd2\n - sd3/\n - sd4.html\n---\nslug doc 1 content\n"
@@ -52,6 +52,29 @@ var urlFakeSource = []source.ByteSource{
        {filepath.FromSlash("content/blue/doc2.md"), []byte(SLUG_DOC_2)},
 }
 
+// Issue #1105
+func TestShouldNotAddTrailingSlashToBaseURL(t *testing.T) {
+
+       for i, this := range []struct {
+               in       string
+               expected string
+       }{
+               {"http://base.com/", "http://base.com/"},
+               {"http://base.com/sub/", "http://base.com/sub/"},
+               {"http://base.com/sub", "http://base.com/sub"},
+               {"http://base.com", "http://base.com"}} {
+
+               viper.Set("BaseURL", this.in)
+               s := &Site{}
+               s.initializeSiteInfo()
+
+               if s.Info.BaseURL != template.URL(this.expected) {
+                       t.Errorf("[%d] got %s expected %s", i, s.Info.BaseURL, this.expected)
+               }
+       }
+
+}
+
 func TestPageCount(t *testing.T) {
        hugofs.DestinationFS = new(afero.MemMapFs)