httputil : wrap handlers for additional context (#413)

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2019-12-06 11:07:45 -08:00 committed by GitHub
parent 487fc655d6
commit b3d3159185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 495 additions and 463 deletions

View file

@ -1,9 +1,12 @@
package httputil
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestHealthCheck(t *testing.T) {
@ -66,3 +69,26 @@ func TestRedirect(t *testing.T) {
})
}
}
func TestHandlerFunc_ServeHTTP(t *testing.T) {
tests := []struct {
name string
f HandlerFunc
wantBody string
}{
{"good http error", func(w http.ResponseWriter, r *http.Request) error { return NewError(404, errors.New("404")) }, "{\"Status\":404,\"Error\":\"Not Found: 404\"}\n"},
{"good std error", func(w http.ResponseWriter, r *http.Request) error { return errors.New("404") }, "{\"Status\":500,\"Error\":\"Internal Server Error: 404\"}\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept", "application/json")
w := httptest.NewRecorder()
tt.f.ServeHTTP(w, r)
if diff := cmp.Diff(tt.wantBody, w.Body.String()); diff != "" {
t.Errorf("ErrorResponse status:\n %s", diff)
}
})
}
}