From a0517d32f84cc7887d69b8ca1a5f5b058ac4b6b3 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Sun, 9 Apr 2023 00:00:33 +0200 Subject: [PATCH] add: /healthz and /readyz endpoints for HTTP server (#32) --- server.go | 7 +++++++ server_test.go | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/server.go b/server.go index 0a26ef3..2d72be7 100644 --- a/server.go +++ b/server.go @@ -197,6 +197,13 @@ func (m *ServeMux) Serve(b *ResponseBuilder, r *RequestEnvelope) { // ServeHTTP dispatches the request to the handler whose // alexa intent matches the request URL. func (m *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.URL != nil && (r.URL.Path == "/healthz" || r.URL.Path == "/readyz") { + if _, err := w.Write([]byte("ok")); err != nil { + m.logger.Debug("failed to write response") + } + return + } + var h Handler req, err := parseRequest(r.Body) if err != nil { diff --git a/server_test.go b/server_test.go index 412e421..515ea6f 100644 --- a/server_test.go +++ b/server_test.go @@ -6,9 +6,10 @@ import ( log "github.com/hamba/logger/v2" jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" - "io/ioutil" + "io" "net/http" "net/http/httptest" + "net/url" "reflect" "runtime" "testing" @@ -31,15 +32,40 @@ func TestServer(t *testing.T) { assert.NotEmpty(t, resp) } +func TestMuxServeHTTPProbes(t *testing.T) { + mux := NewServerMux(log.New(nil, log.ConsoleFormat(), log.Info)) + rw := httptest.NewRecorder() + u, _ := url.Parse("http://anything/healthz") + r := &http.Request{Method: http.MethodGet, URL: u} + + mux.ServeHTTP(rw, r) + + res, err := io.ReadAll(rw.Result().Body) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rw.Result().StatusCode) + assert.Contains(t, string(res), "ok") + + rw = httptest.NewRecorder() + u, _ = url.Parse("http://anything/readyz") + r = &http.Request{Method: http.MethodGet, URL: u} + + mux.ServeHTTP(rw, r) + + res, err = io.ReadAll(rw.Result().Body) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rw.Result().StatusCode) + assert.Contains(t, string(res), "ok") +} + func TestMuxServeHTTP(t *testing.T) { mux := NewServerMux(log.New(nil, log.ConsoleFormat(), log.Info)) rw := httptest.NewRecorder() - b := ioutil.NopCloser(bytes.NewReader([]byte(`{}`))) + b := io.NopCloser(bytes.NewReader([]byte(`{}`))) r := &http.Request{Method: http.MethodGet, Body: b} mux.ServeHTTP(rw, r) - res, _ := ioutil.ReadAll(rw.Result().Body) + res, _ := io.ReadAll(rw.Result().Body) resp := &ResponseEnvelope{} err := jsoniter.Unmarshal(res, resp) assert.NoError(t, err) @@ -47,12 +73,12 @@ func TestMuxServeHTTP(t *testing.T) { assert.Contains(t, string(res), "error") rw = httptest.NewRecorder() - b = ioutil.NopCloser(bytes.NewReader([]byte(`foo`))) + b = io.NopCloser(bytes.NewReader([]byte(`foo`))) r = &http.Request{Method: http.MethodGet, Body: b} mux.ServeHTTP(rw, r) - res, _ = ioutil.ReadAll(rw.Result().Body) + res, _ = io.ReadAll(rw.Result().Body) resp = &ResponseEnvelope{} err = jsoniter.Unmarshal(res, resp) assert.NoError(t, err) @@ -63,12 +89,12 @@ func TestMuxServeHTTP(t *testing.T) { req := &RequestEnvelope{Request: &Request{Type: TypeIntentRequest, Intent: Intent{Name: HelpIntent}}} content, err := jsoniter.Marshal(req) assert.NoError(t, err) - b = ioutil.NopCloser(bytes.NewReader(content)) + b = io.NopCloser(bytes.NewReader(content)) r = &http.Request{Method: http.MethodGet, Body: b} mux.ServeHTTP(rw, r) - res, _ = ioutil.ReadAll(rw.Result().Body) + res, _ = io.ReadAll(rw.Result().Body) resp = &ResponseEnvelope{} err = jsoniter.Unmarshal(res, resp) assert.NoError(t, err)