ref: 9d80ecb4d8efa1a45f649d8b75d7e2c946c09070
parent: 176ce5deab2b6a71789c14a401b8f9ed10399fde
author: bep <[email protected]>
date: Sat Feb 28 13:45:02 EST 2015
Keep trailing slash when baseUrl contains a sub path 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
--- a/helpers/url.go
+++ b/helpers/url.go
@@ -52,9 +52,8 @@
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 @@
// 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
--- a/helpers/url_test.go
+++ b/helpers/url_test.go
@@ -1,9 +1,9 @@
package helpers
import (
- "testing"
-
"github.com/stretchr/testify/assert"
+ "strings"
+ "testing"
)
func TestUrlize(t *testing.T) {
@@ -22,6 +22,34 @@
output := Urlize(test.input)
if output != test.expected {
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
+ }
+ }
+}
+
+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)
}
}
}
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -444,7 +444,7 @@
}
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"),