diff options
author | Krasimir Angelov <kangelov@gitlab.com> | 2020-02-27 03:56:42 +0300 |
---|---|---|
committer | Krasimir Angelov <kangelov@gitlab.com> | 2020-02-28 04:38:10 +0300 |
commit | d12e59eefc3ce15ba9dac808c5e345bf26ad5d45 (patch) | |
tree | 542ab5ae19f0c8a8696f0761e307f5ae7b993cba /app_test.go | |
parent | 70879969bde8aaf97d5ed97d15b904747a44bfb0 (diff) |
Extract health check in its own middleware
This way we short-circuit health check requests and avoid doing domain
lookup for them. We also do not report them in exported Prometheus
metrics and this way avoid trigger alerts during deploys.
Related to:
* https://gitlab.com/gitlab-org/gitlab-pages/issues/350
* https://gitlab.com/gitlab-com/gl-infra/production/issues/1681
Diffstat (limited to 'app_test.go')
-rw-r--r-- | app_test.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app_test.go b/app_test.go index d78d6737..3c485804 100644 --- a/app_test.go +++ b/app_test.go @@ -3,12 +3,15 @@ package main import ( "crypto/tls" "fmt" + "io" "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitlab-pages/internal/request" + "gitlab.com/gitlab-org/gitlab-pages/internal/source" ) func Test_setRequestScheme(t *testing.T) { @@ -58,3 +61,54 @@ func newGetRequestWithScheme(t *testing.T, scheme string, withTLS bool) *http.Re return req } + +func TestHealthCheckMiddleware(t *testing.T) { + tests := []struct { + name string + path string + status int + body string + }{ + { + name: "Not a healthcheck request", + path: "/foo/bar", + status: http.StatusOK, + body: "Hello from inner handler", + }, + { + name: "Healthcheck request", + path: "/-/healthcheck", + status: http.StatusServiceUnavailable, + body: "not yet ready\n", + }, + } + + app := theApp{ + appConfig: appConfig{ + StatusPath: "/-/healthcheck", + }, + } + + domains, err := source.NewDomains(app.appConfig) + require.NoError(t, err) + app.domains = domains + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + io.WriteString(w, "Hello from inner handler") + }) + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + r := httptest.NewRequest("GET", tc.path, nil) + rr := httptest.NewRecorder() + + middleware, err := app.healthCheckMiddleware(handler) + require.NoError(t, err) + middleware.ServeHTTP(rr, r) + + require.Equal(t, tc.status, rr.Code) + require.Equal(t, tc.body, rr.Body.String()) + }) + } +} |