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
This commit is contained in:
bep 2015-02-28 18:45:02 +01:00
parent 176ce5deab
commit 9d80ecb4d8
3 changed files with 44 additions and 6 deletions

View file

@ -52,9 +52,8 @@ func (PathBridge) Separator() string {
var pathBridge PathBridge var pathBridge PathBridge
// SanitizeUrl sanitizes the input URL string. func sanitizeUrlWithFlags(in string, f purell.NormalizationFlags) string {
func SanitizeUrl(in string) string { s, err := purell.NormalizeURLString(in, f)
s, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
if err != nil { if err != nil {
return in return in
} }
@ -85,6 +84,17 @@ func SanitizeUrl(in string) string {
// End temporary kludge // End temporary kludge
//return s //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 // Similar to MakePath, but with Unicode handling

View file

@ -1,9 +1,9 @@
package helpers package helpers
import ( import (
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"strings"
"testing"
) )
func TestUrlize(t *testing.T) { 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) { func TestMakePermalink(t *testing.T) {
type test struct { type test struct {
host, link, output string host, link, output string

View file

@ -444,7 +444,7 @@ func (s *Site) initializeSiteInfo() {
} }
s.Info = SiteInfo{ s.Info = SiteInfo{
BaseUrl: template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))), BaseUrl: template.URL(helpers.SanitizeUrlKeepTrailingSlash(viper.GetString("BaseUrl"))),
Title: viper.GetString("Title"), Title: viper.GetString("Title"),
Author: viper.GetStringMap("author"), Author: viper.GetStringMap("author"),
LanguageCode: viper.GetString("languagecode"), LanguageCode: viper.GetString("languagecode"),