From cd2820e165483609fbe0af99455bf909e6a8cd30 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar Date: Fri, 30 Mar 2018 11:00:22 +0530 Subject: [PATCH 1/2] Fix pathPrefix bug from PR-4025 --- web/web.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/web/web.go b/web/web.go index eae7fa94d..731850cd9 100644 --- a/web/web.go +++ b/web/web.go @@ -666,7 +666,13 @@ func tmplFuncs(consolesPath string, opts *Options) template_text.FuncMap { return time.Since(t) / time.Millisecond * time.Millisecond }, "consolesPath": func() string { return consolesPath }, - "pathPrefix": func() string { return opts.RoutePrefix }, + "pathPrefix": func() string { + if opts.RoutePrefix == "/" { + return "" + } else { + return opts.RoutePrefix + } + }, "buildVersion": func() string { return opts.Version.Revision }, "stripLabels": func(lset map[string]string, labels ...string) map[string]string { for _, ln := range labels { From b44ce11d1b8b7011c1019dbede6b91905f92e27b Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar Date: Fri, 30 Mar 2018 11:35:12 +0530 Subject: [PATCH 2/2] Added test to check pathPrefix --- web/web.go | 2 +- web/web_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/web/web.go b/web/web.go index 731850cd9..fe68e62eb 100644 --- a/web/web.go +++ b/web/web.go @@ -666,7 +666,7 @@ func tmplFuncs(consolesPath string, opts *Options) template_text.FuncMap { return time.Since(t) / time.Millisecond * time.Millisecond }, "consolesPath": func() string { return consolesPath }, - "pathPrefix": func() string { + "pathPrefix": func() string { if opts.RoutePrefix == "/" { return "" } else { diff --git a/web/web_test.go b/web/web_test.go index a9cd7bb02..e0c209967 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -274,6 +274,41 @@ func TestRoutePrefix(t *testing.T) { testutil.Equals(t, http.StatusOK, resp.StatusCode) } +func TestPathPrefix(t *testing.T) { + + tests := []struct { + routePrefix string + pathPrefix string + }{ + { + routePrefix: "/", + // If we have pathPrefix as "/", URL in UI gets "//"" as prefix, + // hence doesn't remain relative path anymore. + pathPrefix: "", + }, + { + routePrefix: "/prometheus", + pathPrefix: "/prometheus", + }, + { + routePrefix: "/p1/p2/p3/p4", + pathPrefix: "/p1/p2/p3/p4", + }, + } + + for _, test := range tests { + opts := &Options{ + RoutePrefix: test.routePrefix, + } + + pathPrefix := tmplFuncs("", opts)["pathPrefix"].(func() string) + pp := pathPrefix() + + testutil.Equals(t, test.pathPrefix, pp) + } + +} + func TestDebugHandler(t *testing.T) { for _, tc := range []struct { prefix, url string