add test for respond func

This commit is contained in:
Danny van Kooten 2017-01-15 16:12:14 +01:00
parent 3a46eda0ee
commit 9d0bee4372
2 changed files with 22 additions and 1 deletions

View File

@ -20,7 +20,8 @@ type envelope struct {
func respond(w http.ResponseWriter, d interface{}) {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.Encode(d)
err := enc.Encode(d)
checkError(err)
}
// log fatal errors

View File

@ -1,7 +1,9 @@
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
@ -62,3 +64,21 @@ func TestParseMajorMinor(t *testing.T) {
t.Errorf("Return value should be %s is %s instead", expected, actual)
}
}
func TestRespond(t *testing.T) {
w := httptest.NewRecorder()
respond(w, 15)
// assert json header
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("Invalid Content-Type header")
}
// assert json response
var d int
err := json.NewDecoder(w.Body).Decode(&d)
if err != nil {
t.Errorf("Invalid response body: %s", err)
}
}