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

@ -9,68 +9,67 @@ import (
"github.com/google/go-cmp/cmp"
)
func TestErrorResponse(t *testing.T) {
tests := []struct {
name string
rw http.ResponseWriter
r *http.Request
e *httpError
}{
{"good", httptest.NewRecorder(), &http.Request{Method: http.MethodGet}, &httpError{Code: http.StatusBadRequest, Message: "missing id token"}},
{"good json", httptest.NewRecorder(), &http.Request{Method: http.MethodGet, Header: http.Header{"Accept": []string{"application/json"}}}, &httpError{Code: http.StatusBadRequest, Message: "missing id token"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ErrorResponse(tt.rw, tt.r, tt.e)
})
}
}
func TestHTTPError_ErrorResponse(t *testing.T) {
func TestError_Error(t *testing.T) {
tests := []struct {
name string
Message string
Code int
InnerErr error
want string
}{
{"good", "short and stout", http.StatusTeapot, nil, "418 I'm a teapot: short and stout"},
{"nested error", "short and stout", http.StatusTeapot, errors.New("another error"), "418 I'm a teapot: short and stout: another error"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := httpError{
Message: tt.Message,
Code: tt.Code,
Err: tt.InnerErr,
}
got := h.Error()
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("Error.Error() = %s", diff)
}
})
}
}
func Test_httpError_Error(t *testing.T) {
tests := []struct {
name string
message string
code int
err error
want string
Status int
Err error
reqType string
wantStatus int
wantBody string
}{
{"good", "foobar", 200, nil, "200 OK: foobar"},
{"no code", "foobar", 0, nil, "500 Internal Server Error: foobar"},
{"no message or code", "", 0, nil, "500 Internal Server Error: Internal Server Error"},
{"404 json", http.StatusNotFound, errors.New("route not known"), "application/json", http.StatusNotFound, "{\"Status\":404,\"Error\":\"Not Found: route not known\"}\n"},
{"404 html", http.StatusNotFound, errors.New("route not known"), "", http.StatusNotFound, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Error(tt.message, tt.code, tt.err)
if got := e.Error(); got != tt.want {
t.Errorf("httpError.Error() = %v, want %v", got, tt.want)
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := NewError(tt.Status, tt.Err)
var e *HTTPError
if errors.As(err, &e) {
e.ErrorResponse(w, r)
} else {
http.Error(w, "coulnd't convert error type", http.StatusTeapot)
}
})
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("Accept", tt.reqType)
w := httptest.NewRecorder()
fn(w, r)
if diff := cmp.Diff(tt.wantStatus, w.Code); diff != "" {
t.Errorf("ErrorResponse status:\n %s", diff)
}
if tt.reqType == "application/json" {
if diff := cmp.Diff(tt.wantBody, w.Body.String()); diff != "" {
t.Errorf("ErrorResponse status:\n %s", diff)
}
}
})
}
}
func TestNewError(t *testing.T) {
tests := []struct {
name string
status int
err error
wantErr bool
}{
{"good", 404, errors.New("error"), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewError(tt.status, tt.err)
if (err != nil) != tt.wantErr {
t.Errorf("NewError() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && !errors.Is(err, tt.err) {
t.Errorf("NewError() unwrap fail = %v, wantErr %v", err, tt.wantErr)
}
})
}
}