Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
✅ Add header tests
Browse files Browse the repository at this point in the history
Also set headers correctly, completely fixing #4. 🐛
  • Loading branch information
robintemme committed Apr 6, 2016
1 parent 4de7245 commit 5ce3608
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
32 changes: 27 additions & 5 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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")
Expand All @@ -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()) {
Expand Down

0 comments on commit 5ce3608

Please sign in to comment.