From 01bc799b99fcc5e60292293dfcafda8013cf60ff Mon Sep 17 00:00:00 2001 From: feistel <6742251-feistel@users.noreply.gitlab.com> Date: Mon, 7 Mar 2022 19:29:05 +0100 Subject: Move healthcheck middleware to a separate package add Cache-Control: no-store to status response --- internal/healthcheck/middleware.go | 19 +++++++++++ internal/healthcheck/middleware_test.go | 56 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 internal/healthcheck/middleware.go create mode 100644 internal/healthcheck/middleware_test.go (limited to 'internal') diff --git a/internal/healthcheck/middleware.go b/internal/healthcheck/middleware.go new file mode 100644 index 00000000..cd607f5a --- /dev/null +++ b/internal/healthcheck/middleware.go @@ -0,0 +1,19 @@ +package healthcheck + +import ( + "net/http" +) + +// NewMiddleware is serving the application status check +func NewMiddleware(handler http.Handler, statusPath string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == statusPath { + w.Header().Set("Cache-Control", "no-store") + w.Write([]byte("success\n")) + + return + } + + handler.ServeHTTP(w, r) + }) +} diff --git a/internal/healthcheck/middleware_test.go b/internal/healthcheck/middleware_test.go new file mode 100644 index 00000000..dd189b92 --- /dev/null +++ b/internal/healthcheck/middleware_test.go @@ -0,0 +1,56 @@ +package healthcheck_test + +import ( + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-pages/internal/config" + "gitlab.com/gitlab-org/gitlab-pages/internal/healthcheck" +) + +func TestHealthCheckMiddleware(t *testing.T) { + tests := []struct { + name string + path string + body string + }{ + { + name: "Not a healthcheck request", + path: "/foo/bar", + body: "Hello from inner handler", + }, + { + name: "Healthcheck request", + path: "/-/healthcheck", + body: "success\n", + }, + } + + cfg := config.Config{ + General: config.General{ + StatusPath: "/-/healthcheck", + }, + } + + 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(http.MethodGet, tc.path, nil) + rr := httptest.NewRecorder() + + middleware := healthcheck.NewMiddleware(handler, cfg.General.StatusPath) + middleware.ServeHTTP(rr, r) + + require.Equal(t, http.StatusOK, rr.Code) + require.Equal(t, tc.body, rr.Body.String()) + }) + } +} -- cgit v1.2.3 From 60ef8fa86cfe3d67c2741f22ffaceb6d9a32731f Mon Sep 17 00:00:00 2001 From: feistel <6742251-feistel@users.noreply.gitlab.com> Date: Tue, 26 Apr 2022 20:40:06 +0200 Subject: Rework healthcheck tests --- internal/healthcheck/middleware_test.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'internal') diff --git a/internal/healthcheck/middleware_test.go b/internal/healthcheck/middleware_test.go index dd189b92..124bb0ff 100644 --- a/internal/healthcheck/middleware_test.go +++ b/internal/healthcheck/middleware_test.go @@ -3,7 +3,6 @@ package healthcheck_test import ( "io" "net/http" - "net/http/httptest" "testing" "github.com/stretchr/testify/require" @@ -13,18 +12,15 @@ import ( ) func TestHealthCheckMiddleware(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { path string body string }{ - { - name: "Not a healthcheck request", + "Not a healthcheck request": { path: "/foo/bar", body: "Hello from inner handler", }, - { - name: "Healthcheck request", + "Healthcheck request": { path: "/-/healthcheck", body: "success\n", }, @@ -41,16 +37,14 @@ func TestHealthCheckMiddleware(t *testing.T) { io.WriteString(w, "Hello from inner handler") }) - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - r := httptest.NewRequest(http.MethodGet, tc.path, nil) - rr := httptest.NewRecorder() - + for name, tc := range tests { + t.Run(name, func(t *testing.T) { middleware := healthcheck.NewMiddleware(handler, cfg.General.StatusPath) - middleware.ServeHTTP(rr, r) - require.Equal(t, http.StatusOK, rr.Code) - require.Equal(t, tc.body, rr.Body.String()) + u := "https://example.com" + tc.path + + require.HTTPStatusCode(t, middleware.ServeHTTP, http.MethodGet, u, nil, http.StatusOK) + require.HTTPBodyContains(t, middleware.ServeHTTP, http.MethodGet, u, nil, tc.body) }) } } -- cgit v1.2.3 From cce3706f57ec6424ea7c62ebffd19a3ccbe49c0e Mon Sep 17 00:00:00 2001 From: feistel <6742251-feistel@users.noreply.gitlab.com> Date: Tue, 26 Apr 2022 20:40:28 +0200 Subject: Compare status path against request path --- internal/healthcheck/middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/healthcheck/middleware.go b/internal/healthcheck/middleware.go index cd607f5a..2ddd35b7 100644 --- a/internal/healthcheck/middleware.go +++ b/internal/healthcheck/middleware.go @@ -7,7 +7,7 @@ import ( // NewMiddleware is serving the application status check func NewMiddleware(handler http.Handler, statusPath string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == statusPath { + if r.URL.Path == statusPath { w.Header().Set("Cache-Control", "no-store") w.Write([]byte("success\n")) -- cgit v1.2.3