diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2021-08-20 04:35:36 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2021-08-20 04:39:10 +0300 |
commit | de9c4c78cdba058ea7ca2c08cd55bddfd7dbd9b4 (patch) | |
tree | c8d17c1a7c073c4cf761d2a521dac621fe82a57b /app_test.go | |
parent | b16bf8296b4d3319b32e74046b1aae3e21e2a947 (diff) |
test: update healthcheck test for disk source removal
Diffstat (limited to 'app_test.go')
-rw-r--r-- | app_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/app_test.go b/app_test.go index d78d6737..483ffb39 100644 --- a/app_test.go +++ b/app_test.go @@ -3,12 +3,17 @@ package main import ( "crypto/tls" "fmt" + "io" "net/http" + "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/request" + "gitlab.com/gitlab-org/gitlab-pages/internal/source" ) func Test_setRequestScheme(t *testing.T) { @@ -58,3 +63,65 @@ 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.StatusOK, + body: "success\n", + }, + } + + validCfg := config.GitLab{ + InternalServer: "server", + APISecretKey: []byte("secret"), + ClientHTTPTimeout: time.Second, + JWTTokenExpiration: time.Second, + } + + domains, err := source.NewDomains(&validCfg) + require.NoError(t, err) + + cfg := config.Config{ + General: config.General{ + StatusPath: "/-/healthcheck", + }, + } + + app := theApp{ + config: &cfg, + 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()) + }) + } +} |