From 74decb4bdc2a5570ccde44722b9516ae4171dba9 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Fri, 29 Jan 2016 18:31:00 +0100 Subject: [PATCH] fix(dataproxy): no trailing slash if proxypath is empty The JoinUrlFragments function adds a trailing slash if to the proxy url if the proxy path is an empty string. This fix removes that trailing slash. Fixes #3847 --- pkg/util/url.go | 5 +++++ pkg/util/url_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 pkg/util/url_test.go diff --git a/pkg/util/url.go b/pkg/util/url.go index 758e6864fe4..ba452596a2b 100644 --- a/pkg/util/url.go +++ b/pkg/util/url.go @@ -27,6 +27,11 @@ func (r *UrlQueryReader) Get(name string, def string) string { func JoinUrlFragments(a, b string) string { aslash := strings.HasSuffix(a, "/") bslash := strings.HasPrefix(b, "/") + + if len(b) == 0 { + return a + } + switch { case aslash && bslash: return a + b[1:] diff --git a/pkg/util/url_test.go b/pkg/util/url_test.go new file mode 100644 index 00000000000..4dd221b9e0b --- /dev/null +++ b/pkg/util/url_test.go @@ -0,0 +1,46 @@ +package util + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestUrl(t *testing.T) { + + Convey("When joining two urls where right hand side is empty", t, func() { + result := JoinUrlFragments("http://localhost:8080", "") + + So(result, ShouldEqual, "http://localhost:8080") + }) + + Convey("When joining two urls where right hand side is empty and lefthand side has a trailing slash", t, func() { + result := JoinUrlFragments("http://localhost:8080/", "") + + So(result, ShouldEqual, "http://localhost:8080/") + }) + + Convey("When joining two urls where neither has a trailing slash", t, func() { + result := JoinUrlFragments("http://localhost:8080", "api") + + So(result, ShouldEqual, "http://localhost:8080/api") + }) + + Convey("When joining two urls where lefthand side has a trailing slash", t, func() { + result := JoinUrlFragments("http://localhost:8080/", "api") + + So(result, ShouldEqual, "http://localhost:8080/api") + }) + + Convey("When joining two urls where righthand side has preceding slash", t, func() { + result := JoinUrlFragments("http://localhost:8080", "/api") + + So(result, ShouldEqual, "http://localhost:8080/api") + }) + + Convey("When joining two urls where righthand side has trailing slash", t, func() { + result := JoinUrlFragments("http://localhost:8080", "api/") + + So(result, ShouldEqual, "http://localhost:8080/api/") + }) +}