From 971967ae78c90d786f3cbdb5c7e36bfc1ee8517a Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Mon, 30 Nov 2020 09:23:03 -0500 Subject: [PATCH] Don't append / at the end of the path --- config/web.go | 2 +- config/web_test.go | 48 ++++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/config/web.go b/config/web.go index 342929ff..5ea0daf0 100644 --- a/config/web.go +++ b/config/web.go @@ -59,5 +59,5 @@ func (web *webConfig) SocketAddress() string { // PrependWithContextRoot appends the given path to the ContextRoot func (web *webConfig) PrependWithContextRoot(path string) string { - return web.ContextRoot + strings.Trim(path, "/") + "/" + return web.ContextRoot + strings.Trim(path, "/") } diff --git a/config/web_test.go b/config/web_test.go index fa61a3b3..02b1bca0 100644 --- a/config/web_test.go +++ b/config/web_test.go @@ -17,14 +17,14 @@ func TestWebConfig_SocketAddress(t *testing.T) { func TestWebConfig_PrependWithContextRoot(t *testing.T) { web := &webConfig{ContextRoot: "/status/"} - if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results/" { - t.Errorf("expected %s, got %s", "/status/api/v1/results/", result) + if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results" { + t.Errorf("expected %s, got %s", "/status/api/v1/results", result) } - if result := web.PrependWithContextRoot("/health"); result != "/status/health/" { - t.Errorf("expected %s, got %s", "/status/health/", result) + if result := web.PrependWithContextRoot("/health"); result != "/status/health" { + t.Errorf("expected %s, got %s", "/status/health", result) } - if result := web.PrependWithContextRoot("/health/"); result != "/status/health/" { - t.Errorf("expected %s, got %s", "/status/health/", result) + if result := web.PrependWithContextRoot("/health/"); result != "/status/health" { + t.Errorf("expected %s, got %s", "/status/health", result) } } @@ -42,10 +42,10 @@ var validContextRootTests = []struct { {"Slash at the beginning", "/status", "/status/"}, {"Slashes at start and end", "/status/", "/status/"}, {"Multiple slashes at start", "//status", "/status/"}, - {"Mutliple slashes at start and end", "///status////", "/status/"}, + {"Multiple slashes at start and end", "///status////", "/status/"}, {"Contains '@' in path'", "me@/status/gatus", "/me@/status/gatus/"}, - {"nested context with trailing slash", "/status/gatus/", "/status/gatus/"}, - {"nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"}, + {"Nested context with trailing slash", "/status/gatus/", "/status/gatus/"}, + {"Nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"}, } func TestWebConfig_ValidContextRoots(t *testing.T) { @@ -56,6 +56,16 @@ func TestWebConfig_ValidContextRoots(t *testing.T) { } } +func expectValidResultForContextRoot(t *testing.T, path string, expected string) { + web := &webConfig{ + ContextRoot: path, + } + web.validateAndSetDefaults() + if web.ContextRoot != expected { + t.Errorf("expected %s, got %s", expected, web.ContextRoot) + } +} + // invalidContextRootTests contains all tests for context root which are // expected to fail and stop program execution var invalidContextRootTests = []struct { @@ -63,12 +73,12 @@ var invalidContextRootTests = []struct { path string }{ {"Only a fragment identifier", "#"}, - {"Invalid character in path", "/invalid" + string([]byte{0x7F}) + "/"}, + {"Invalid character in path", "/invalid" + string([]byte{0x7F})}, {"Starts with protocol", "http://status/gatus"}, {"Path with fragment", "/status/gatus#here"}, - {"starts with '://'", "://status"}, - {"contains query parameter", "/status/h?ello=world"}, - {"contains '?'", "/status?/"}, + {"Starts with '://'", "://status"}, + {"Contains query parameter", "/status/h?ello=world"}, + {"Contains '?'", "/status?"}, } func TestWebConfig_InvalidContextRoots(t *testing.T) { @@ -87,15 +97,3 @@ func expectInvalidResultForContextRoot(t *testing.T, path string) { t.Fatal(fmt.Sprintf("Should've panicked because the configuration specifies an invalid context root: %s", path)) } - -func expectValidResultForContextRoot(t *testing.T, path string, expected string) { - web := &webConfig{ - ContextRoot: path, - } - - web.validateAndSetDefaults() - - if web.ContextRoot != expected { - t.Errorf("expected %s, got %s", expected, web.ContextRoot) - } -}