Skip to content

Commit

Permalink
feat: add requestid into client response when system is into maintena…
Browse files Browse the repository at this point in the history
…nce mode. update unit tests accordingly
  • Loading branch information
jeamon committed Nov 8, 2023
1 parent cf6672d commit 3fe3a78
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions api.handlers.ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ func (api *APIHandler) Maintenance(w http.ResponseWriter, r *http.Request, ps ht

case "show":
response = map[string]interface{}{
"message": "service currently unvailable.",
"reason": api.mode.reason,
"since": api.mode.started.Format(time.RFC1123),
"requestid": requestID,
"message": "service currently unvailable.",
"reason": api.mode.reason,
"since": api.mode.started.Format(time.RFC1123),
}
w.WriteHeader(http.StatusServiceUnavailable)
}
Expand Down
4 changes: 3 additions & 1 deletion tests.middlewares_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -122,6 +123,7 @@ func TestMaintenanceModeMiddleware(t *testing.T) {
ts := NewMockClocker().Now()
api.mode.started = ts
api.mode.reason = "ongoing maintenance."
req = req.WithContext(context.WithValue(req.Context(), ContextRequestID, "abc"))
wrapped := api.MaintenanceModeMiddleware(handler)
wrapped(w, req, nil)
// target handler will not be called but the maintenance handler must kick-in.
Expand All @@ -131,7 +133,7 @@ func TestMaintenanceModeMiddleware(t *testing.T) {
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
require.NoError(t, err)
expected := `{"message":"service currently unvailable.","reason":"ongoing maintenance.", "since":"Sun, 02 Jul 2023 00:00:00 UTC"}`
expected := `{"requestid":"abc","message":"service currently unvailable.","reason":"ongoing maintenance.", "since":"Sun, 02 Jul 2023 00:00:00 UTC"}`
assert.JSONEq(t, expected, string(data))
})
}
Expand Down

0 comments on commit 3fe3a78

Please sign in to comment.