diff options
author | Nate Finch <nate.finch@gmail.com> | 2014-08-22 07:59:59 -0400 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2014-08-25 11:51:51 -0400 |
commit | a31edb3388606eb231261780d91e1324c50b2953 (patch) | |
tree | 3b4189b0240765210b3fce4525665b28d209f5d4 /helpers | |
parent | 4b979b17cc3af0d6da3026a2ca02b525133b6d3f (diff) |
Support subdir in baseurl.
Mainly this was a change to helpers.MakePermalink, but to get the local server to run correctly,
we needed to redirect the path of the request from /foo to /. In addition, I added tests for the
server's code for fixing up the base url with different config file & CLI options.
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/helpers_test.go | 22 | ||||
-rw-r--r-- | helpers/url.go | 18 |
2 files changed, 38 insertions, 2 deletions
diff --git a/helpers/helpers_test.go b/helpers/helpers_test.go index 822783025..6d3993be4 100644 --- a/helpers/helpers_test.go +++ b/helpers/helpers_test.go @@ -97,3 +97,25 @@ func TestUrlize(t *testing.T) { } } } + +func TestMakePermalink(t *testing.T) { + type test struct { + host, link, output string + } + + data := []test{ + {"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"}, + {"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"}, + {"http://abc.com", "post/bar", "http://abc.com/post/bar"}, + {"http://abc.com", "bar", "http://abc.com/bar"}, + {"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"}, + {"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"}, + } + + for i, d := range data { + output := MakePermalink(d.host, d.link).String() + if d.output != output { + t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output) + } + } +} diff --git a/helpers/url.go b/helpers/url.go index d7f99f4f5..8a33c5b86 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -14,8 +14,10 @@ package helpers import ( + "fmt" "net/url" "path" + "strings" "github.com/PuerkitoBio/purell" ) @@ -57,11 +59,23 @@ func MakePermalink(host, plink string) *url.URL { panic(err) } - path, err := url.Parse(plink) + p, err := url.Parse(plink) if err != nil { panic(err) } - return base.ResolveReference(path) + + if p.Host != "" { + panic(fmt.Errorf("Can't make permalink from absolute link %q", plink)) + } + + base.Path = path.Join(base.Path, p.Path) + + // path.Join will strip off the last /, so put it back if it was there. + if strings.HasSuffix(p.Path, "/") && !strings.HasSuffix(base.Path, "/") { + base.Path = base.Path + "/" + } + + return base } func UrlPrep(ugly bool, in string) string { |