Commit ab18e99e authored by Janos Guljas's avatar Janos Guljas

add content type checks to jsonhttp tests

parent ac1fc0a1
......@@ -17,12 +17,15 @@ import (
)
func TestMethodHandler(t *testing.T) {
contentType := "application/swarm"
h := jsonhttp.MethodHandler{
"POST": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
w.Header().Set("Content-Type", contentType)
fmt.Fprint(w, "got: ", string(got))
}),
}
......@@ -46,6 +49,10 @@ func TestMethodHandler(t *testing.T) {
if gotBody != wantBody {
t.Errorf("got body %q, want %q", gotBody, wantBody)
}
if got := w.Header().Get("Content-Type"); got != contentType {
t.Errorf("got content type %q, want %q", got, contentType)
}
})
t.Run("method not allowed", func(t *testing.T) {
......@@ -74,6 +81,8 @@ func TestMethodHandler(t *testing.T) {
if m.Message != wantMessage {
t.Errorf("got message message %q, want %q", m.Message, wantMessage)
}
testContentType(t, w)
})
}
......@@ -102,4 +111,6 @@ func TestNotFoundHandler(t *testing.T) {
if m.Message != wantMessage {
t.Errorf("got message message %q, want %q", m.Message, wantMessage)
}
testContentType(t, w)
}
......@@ -41,6 +41,8 @@ func TestRespond_defaults(t *testing.T) {
if m.Message != wantMessage {
t.Errorf("got message message %q, want %q", m.Message, wantMessage)
}
testContentType(t, w)
}
func TestRespond_statusResponse(t *testing.T) {
......@@ -117,6 +119,8 @@ func TestRespond_statusResponse(t *testing.T) {
if m.Message != wantMessage {
t.Errorf("got message message %q, want %q", m.Message, wantMessage)
}
testContentType(t, w)
}
}
......@@ -187,6 +191,8 @@ func TestRespond_special(t *testing.T) {
if m.Message != tc.wantMessage {
t.Errorf("got message message %q, want %q", m.Message, tc.wantMessage)
}
testContentType(t, w)
})
}
}
......@@ -221,6 +227,8 @@ func TestRespond_custom(t *testing.T) {
if !reflect.DeepEqual(m, r) {
t.Errorf("got response %+v, want %+v", m, r)
}
testContentType(t, w)
}
func TestStandardHTTPResponds(t *testing.T) {
......@@ -290,6 +298,8 @@ func TestStandardHTTPResponds(t *testing.T) {
if m.Message != http.StatusText(tc.code) {
t.Errorf("expected message message \"%s\", got \"%s\"", http.StatusText(tc.code), m.Message)
}
testContentType(t, w)
}
}
......@@ -307,3 +317,11 @@ func TestPanicRespond(t *testing.T) {
true: "",
})
}
func testContentType(t *testing.T, r *httptest.ResponseRecorder) {
t.Helper()
if got := r.Header().Get("Content-Type"); got != jsonhttp.DefaultContentTypeHeader {
t.Errorf("got content type %q, want %q", got, jsonhttp.DefaultContentTypeHeader)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment