gatus/config/web_test.go

100 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-11-20 23:45:25 +01:00
package config
2020-11-21 04:07:38 +01:00
import (
"fmt"
"testing"
)
2020-11-20 23:45:25 +01:00
func TestWebConfig_SocketAddress(t *testing.T) {
web := &webConfig{
Address: "0.0.0.0",
Port: 8081,
}
if web.SocketAddress() != "0.0.0.0:8081" {
t.Errorf("expected %s, got %s", "0.0.0.0:8081", web.SocketAddress())
}
}
2020-11-21 23:53:18 +01:00
func TestWebConfig_PrependWithContextRoot(t *testing.T) {
web := &webConfig{ContextRoot: "/status/"}
2020-11-30 15:23:03 +01:00
if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results" {
t.Errorf("expected %s, got %s", "/status/api/v1/results", result)
2020-11-21 23:53:18 +01:00
}
2020-11-30 15:23:03 +01:00
if result := web.PrependWithContextRoot("/health"); result != "/status/health" {
t.Errorf("expected %s, got %s", "/status/health", result)
2020-11-21 23:53:18 +01:00
}
2020-11-30 15:23:03 +01:00
if result := web.PrependWithContextRoot("/health/"); result != "/status/health" {
t.Errorf("expected %s, got %s", "/status/health", result)
2020-11-21 23:53:18 +01:00
}
}
2020-11-21 13:11:37 +01:00
// validContextRootTest specifies all test case which should end up in
// a valid context root used to bind the web interface to
var validContextRootTests = []struct {
name string
path string
expectedPath string
}{
{"Empty", "", "/"},
{"/", "/", "/"},
{"///", "///", "/"},
{"Single character 'a'", "a", "/a/"},
{"Slash at the beginning", "/status", "/status/"},
{"Slashes at start and end", "/status/", "/status/"},
{"Multiple slashes at start", "//status", "/status/"},
2020-11-30 15:23:03 +01:00
{"Multiple slashes at start and end", "///status////", "/status/"},
2020-11-21 13:11:37 +01:00
{"Contains '@' in path'", "me@/status/gatus", "/me@/status/gatus/"},
2020-11-30 15:23:03 +01:00
{"Nested context with trailing slash", "/status/gatus/", "/status/gatus/"},
{"Nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"},
2020-11-21 03:42:42 +01:00
}
2020-11-21 13:11:37 +01:00
func TestWebConfig_ValidContextRoots(t *testing.T) {
for idx, test := range validContextRootTests {
t.Run(fmt.Sprintf("%d: %s", idx, test.name), func(t *testing.T) {
expectValidResultForContextRoot(t, test.path, test.expectedPath)
})
}
}
2020-11-30 15:23:03 +01:00
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)
}
}
2020-11-21 13:11:37 +01:00
// invalidContextRootTests contains all tests for context root which are
2020-11-21 22:41:55 +01:00
// expected to fail and stop program execution
2020-11-21 13:11:37 +01:00
var invalidContextRootTests = []struct {
name string
path string
}{
{"Only a fragment identifier", "#"},
2020-11-30 15:23:03 +01:00
{"Invalid character in path", "/invalid" + string([]byte{0x7F})},
2020-11-21 13:11:37 +01:00
{"Starts with protocol", "http://status/gatus"},
{"Path with fragment", "/status/gatus#here"},
2020-11-30 15:23:03 +01:00
{"Starts with '://'", "://status"},
{"Contains query parameter", "/status/h?ello=world"},
{"Contains '?'", "/status?"},
2020-11-21 13:11:37 +01:00
}
2020-11-21 13:11:37 +01:00
func TestWebConfig_InvalidContextRoots(t *testing.T) {
for idx, test := range invalidContextRootTests {
t.Run(fmt.Sprintf("%d: %s", idx, test.name), func(t *testing.T) {
expectInvalidResultForContextRoot(t, test.path)
})
}
}
2020-11-21 13:11:37 +01:00
func expectInvalidResultForContextRoot(t *testing.T, path string) {
2020-11-21 04:07:38 +01:00
defer func() { recover() }()
2020-11-21 13:11:37 +01:00
web := &webConfig{ContextRoot: path}
2020-11-21 04:07:38 +01:00
web.validateAndSetDefaults()
2020-11-21 13:11:37 +01:00
t.Fatal(fmt.Sprintf("Should've panicked because the configuration specifies an invalid context root: %s", path))
2020-11-21 04:07:38 +01:00
}