Skip to content

Commit

Permalink
add: /healthz and /readyz endpoints for HTTP server (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPsychick committed Apr 8, 2023
1 parent 584a98e commit a0517d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
7 changes: 7 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 33 additions & 7 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,28 +32,53 @@ 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)
assert.Equal(t, http.StatusOK, rw.Result().StatusCode)
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)
Expand All @@ -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)
Expand Down

0 comments on commit a0517d3

Please sign in to comment.