mirror of https://github.com/status-im/fathom.git
add test for respond func
This commit is contained in:
parent
3a46eda0ee
commit
9d0bee4372
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue