-
Notifications
You must be signed in to change notification settings - Fork 1
/
jopher_test.go
44 lines (35 loc) · 869 Bytes
/
jopher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package jopher
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSuccessResponse(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
Success(w, M{"Hello": "World"})
}
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body := make(map[string]string)
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Log(err.Error())
}
defer resp.Body.Close()
// parse body as json
if body["Hello"] != "World" {
t.Log("Failed to get correct values from response body")
t.Fail()
}
if resp.Status != "OK" {
t.Log("Response status code != 200")
t.Fail()
}
if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
t.Log("Response header Content-Type should be application/json")
t.Fail()
}
}