Don't append / at the end of the path
This commit is contained in:
parent
43504913b4
commit
971967ae78
@ -59,5 +59,5 @@ func (web *webConfig) SocketAddress() string {
|
|||||||
|
|
||||||
// PrependWithContextRoot appends the given path to the ContextRoot
|
// PrependWithContextRoot appends the given path to the ContextRoot
|
||||||
func (web *webConfig) PrependWithContextRoot(path string) string {
|
func (web *webConfig) PrependWithContextRoot(path string) string {
|
||||||
return web.ContextRoot + strings.Trim(path, "/") + "/"
|
return web.ContextRoot + strings.Trim(path, "/")
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,14 @@ func TestWebConfig_SocketAddress(t *testing.T) {
|
|||||||
|
|
||||||
func TestWebConfig_PrependWithContextRoot(t *testing.T) {
|
func TestWebConfig_PrependWithContextRoot(t *testing.T) {
|
||||||
web := &webConfig{ContextRoot: "/status/"}
|
web := &webConfig{ContextRoot: "/status/"}
|
||||||
if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results/" {
|
if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results" {
|
||||||
t.Errorf("expected %s, got %s", "/status/api/v1/results/", result)
|
t.Errorf("expected %s, got %s", "/status/api/v1/results", result)
|
||||||
}
|
}
|
||||||
if result := web.PrependWithContextRoot("/health"); result != "/status/health/" {
|
if result := web.PrependWithContextRoot("/health"); result != "/status/health" {
|
||||||
t.Errorf("expected %s, got %s", "/status/health/", result)
|
t.Errorf("expected %s, got %s", "/status/health", result)
|
||||||
}
|
}
|
||||||
if result := web.PrependWithContextRoot("/health/"); result != "/status/health/" {
|
if result := web.PrependWithContextRoot("/health/"); result != "/status/health" {
|
||||||
t.Errorf("expected %s, got %s", "/status/health/", result)
|
t.Errorf("expected %s, got %s", "/status/health", result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +42,10 @@ var validContextRootTests = []struct {
|
|||||||
{"Slash at the beginning", "/status", "/status/"},
|
{"Slash at the beginning", "/status", "/status/"},
|
||||||
{"Slashes at start and end", "/status/", "/status/"},
|
{"Slashes at start and end", "/status/", "/status/"},
|
||||||
{"Multiple slashes at start", "//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/"},
|
{"Contains '@' in path'", "me@/status/gatus", "/me@/status/gatus/"},
|
||||||
{"nested context with trailing slash", "/status/gatus/", "/status/gatus/"},
|
{"Nested context with trailing slash", "/status/gatus/", "/status/gatus/"},
|
||||||
{"nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"},
|
{"Nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWebConfig_ValidContextRoots(t *testing.T) {
|
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
|
// invalidContextRootTests contains all tests for context root which are
|
||||||
// expected to fail and stop program execution
|
// expected to fail and stop program execution
|
||||||
var invalidContextRootTests = []struct {
|
var invalidContextRootTests = []struct {
|
||||||
@ -63,12 +73,12 @@ var invalidContextRootTests = []struct {
|
|||||||
path string
|
path string
|
||||||
}{
|
}{
|
||||||
{"Only a fragment identifier", "#"},
|
{"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"},
|
{"Starts with protocol", "http://status/gatus"},
|
||||||
{"Path with fragment", "/status/gatus#here"},
|
{"Path with fragment", "/status/gatus#here"},
|
||||||
{"starts with '://'", "://status"},
|
{"Starts with '://'", "://status"},
|
||||||
{"contains query parameter", "/status/h?ello=world"},
|
{"Contains query parameter", "/status/h?ello=world"},
|
||||||
{"contains '?'", "/status?/"},
|
{"Contains '?'", "/status?"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWebConfig_InvalidContextRoots(t *testing.T) {
|
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))
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user