Keep trailing slash when baseUrl contains a sub path
authorbep <bjorn.erik.pedersen@gmail.com>
Sat, 28 Feb 2015 17:45:02 +0000 (18:45 +0100)
committerbep <bjorn.erik.pedersen@gmail.com>
Sat, 28 Feb 2015 17:45:02 +0000 (18:45 +0100)
Before this commit, .Site.BaseUrl ended up as:

http://mysite.com/ => http://mysite.com/
http://mysite.com/sub/ => http://mysite.com/sub

Now it becomes:

http://mysite.com/ => http://mysite.com/
http://mysite.com/sub/ => http://mysite.com/sub/

Fixed #931

helpers/url.go
helpers/url_test.go
hugolib/site.go

index a07a3c6bdf079097638d452af05495db5ffc4ae5..f31e7a4e198b3e42c8ca461c6ace3bc0226356ef 100644 (file)
@@ -52,9 +52,8 @@ func (PathBridge) Separator() string {
 
 var pathBridge PathBridge
 
-// SanitizeUrl sanitizes the input URL string.
-func SanitizeUrl(in string) string {
-       s, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
+func sanitizeUrlWithFlags(in string, f purell.NormalizationFlags) string {
+       s, err := purell.NormalizeURLString(in, f)
        if err != nil {
                return in
        }
@@ -85,6 +84,17 @@ func SanitizeUrl(in string) string {
        // End temporary kludge
 
        //return s
+
+}
+
+// SanitizeUrl sanitizes the input URL string.
+func SanitizeUrl(in string) string {
+       return sanitizeUrlWithFlags(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
+}
+
+// SanitizeUrlKeepTrailingSlash is the same as SanitizeUrl, but will keep any trailing slash.
+func SanitizeUrlKeepTrailingSlash(in string) string {
+       return sanitizeUrlWithFlags(in, purell.FlagsSafe|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
 }
 
 // Similar to MakePath, but with Unicode handling
index e27e2bb02b4aaa821358d5640e39d5c443a09123..c38750a63fe2f4c2849f1a125ad2705c0483c3d2 100644 (file)
@@ -1,9 +1,9 @@
 package helpers
 
 import (
-       "testing"
-
        "github.com/stretchr/testify/assert"
+       "strings"
+       "testing"
 )
 
 func TestUrlize(t *testing.T) {
@@ -26,6 +26,34 @@ func TestUrlize(t *testing.T) {
        }
 }
 
+func TestSanitizeUrl(t *testing.T) {
+       tests := []struct {
+               input    string
+               expected string
+       }{
+               {"http://foo.bar/", "http://foo.bar/"},
+               {"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
+       }
+
+       for _, test := range tests {
+               o1 := SanitizeUrl(test.input)
+               o2 := SanitizeUrlKeepTrailingSlash(test.input)
+
+               expected2 := test.expected
+
+               if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
+                       expected2 += "/"
+               }
+
+               if o1 != test.expected {
+                       t.Errorf("Expected %#v, got %#v\n", test.expected, o1)
+               }
+               if o2 != expected2 {
+                       t.Errorf("Expected %#v, got %#v\n", expected2, o2)
+               }
+       }
+}
+
 func TestMakePermalink(t *testing.T) {
        type test struct {
                host, link, output string
index 0acc287d5251fadf6ccef53e668c835080cfbc1e..c5b5b85c1ed3571dad77a97def12ef5d8a948d12 100644 (file)
@@ -444,7 +444,7 @@ func (s *Site) initializeSiteInfo() {
        }
 
        s.Info = SiteInfo{
-               BaseUrl:         template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))),
+               BaseUrl:         template.URL(helpers.SanitizeUrlKeepTrailingSlash(viper.GetString("BaseUrl"))),
                Title:           viper.GetString("Title"),
                Author:          viper.GetStringMap("author"),
                LanguageCode:    viper.GetString("languagecode"),