This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
forked from ishehata/jopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jopher_test.go
75 lines (62 loc) · 1.69 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package jopher
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSuccess(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.Errorf("failed to get correct values from response body")
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected response status code to be 200, found %d", resp.StatusCode)
return
}
if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
t.Errorf("Response header Content-Type should be application/json")
return
}
}
func TestCreated(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
Created(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.Errorf("failed to get correct values from response body")
return
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("expected response status code to be 201, found %d", resp.StatusCode)
return
}
if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
t.Errorf("Response header Content-Type should be application/json")
return
}
}