From 5ce3608260a4c66a48e51fe7dc90f99bf4a210ad Mon Sep 17 00:00:00 2001 From: Robin Temme Date: Thu, 7 Apr 2016 01:44:48 +0200 Subject: [PATCH] :white_check_mark: Add header tests Also set headers correctly, completely fixing #4. :bug: --- json.go | 2 ++ json_test.go | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/json.go b/json.go index 548f491..5c1d063 100644 --- a/json.go +++ b/json.go @@ -7,6 +7,7 @@ import ( // WriteError writes a string as JSON encoded error func WriteError(w http.ResponseWriter, code int, err string) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(code) WriteJSON(w, map[string]string{ @@ -29,6 +30,7 @@ func WriteJSON(w http.ResponseWriter, v interface{}) error { // WriteJSONWithStatus writes the given statuscode into the header and the // given interface as JSON or returns an error func WriteJSONWithStatus(w http.ResponseWriter, code int, v interface{}) error { + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(code) return WriteJSON(w, v) diff --git a/json_test.go b/json_test.go index cca60c8..a56362d 100644 --- a/json_test.go +++ b/json_test.go @@ -21,12 +21,19 @@ func TestWriteError(t *testing.T) { w := httptest.NewRecorder() WriteError(w, code, err) - // Test code + // Test Code if w.Code != code { t.Errorf("WriteError should set Code to %i, but did set it to %i", code, w.Code) } - // Test body + // Test Header + expectedContentTypeHeader := "application/json; charset=utf-8" + contentTypeHeader := w.Header().Get("Content-Type") + if contentTypeHeader != expectedContentTypeHeader { + t.Errorf("WriteError should set Content-Type header to %s, but did set it to %s", expectedContentTypeHeader, contentTypeHeader) + } + + // Test Body if w.Body == nil { t.Errorf("WriteError should set Body to %s, but didn't", json) } else if string(w.Body.Bytes()) == string(buf.Bytes()) { @@ -49,13 +56,21 @@ func TestWriteJSON(t *testing.T) { w := httptest.NewRecorder() WriteJSON(w, in) + // Test Body if w.Body == nil { t.Errorf("WriteJSON should set the Body to %s, but didn't", json) } else if string(w.Body.Bytes()) == string(buf.Bytes()) { t.Errorf("WriteJSON set the Body to %v, but should set it to %v", buf, w.Body) } - // Test error + // Test Header + expectedContentTypeHeader := "application/json; charset=utf-8" + contentTypeHeader := w.Header().Get("Content-Type") + if contentTypeHeader != expectedContentTypeHeader { + t.Errorf("WriteJSON should set Content-Type header to %s, but did set it to %s", expectedContentTypeHeader, contentTypeHeader) + } + + // Test Error w = httptest.NewRecorder() if err := WriteJSON(w, WriteJSON); err == nil { t.Errorf("WriteJSON should return an error, but didn't") @@ -79,12 +94,19 @@ func TestWriteJSONWithStatus(t *testing.T) { w := httptest.NewRecorder() WriteJSONWithStatus(w, code, in) - // test code + // Test Code if w.Code != code { t.Errorf("WriteJSONWithStatus should set Code to %i, but did set it to %i", code, w.Code) } - // test body + // Test Header + expectedContentTypeHeader := "application/json; charset=utf-8" + contentTypeHeader := w.Header().Get("Content-Type") + if contentTypeHeader != expectedContentTypeHeader { + t.Errorf("WriteJSONWithStatus should set Content-Type header to %s, but did set it to %s", expectedContentTypeHeader, contentTypeHeader) + } + + // Test Body if w.Body == nil { t.Errorf("WriteJSONWithStatus should set the Body to %s, but didn't", json) } else if string(w.Body.Bytes()) == string(buf.Bytes()) {