-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
144 lines (121 loc) · 3.23 KB
/
json_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package controller
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func initialize(t *testing.T, handler RequestHandler) *http.ServeMux {
m := http.NewServeMux()
return m
}
type id struct {
ID string `json:"id"`
}
func TestOK(t *testing.T) {
testCases := []struct {
Method string
Code int
Response interface{}
}{
{"GET", http.StatusTeapot, nil},
{"GET", http.StatusNotFound, id{"1234"}},
//
{"POST", http.StatusCreated, id{"4321"}},
}
for idx, tc := range testCases {
var (
err error
req *http.Request
jsonExpected = []byte{}
)
if tc.Response != nil {
jsonExpected, err = json.Marshal(tc.Response)
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
}
m := http.NewServeMux()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
j := Json{}
if tc.Method == "GET" {
// Simply send the code and the response
j.Handle(w, r, func() (status int, response interface{}) {
return tc.Code, tc.Response
})
} else {
// Parse json body and send it as response
j.Handle(w, r, func() (status int, response interface{}) {
var d id
j.ParseJsonBody(r, &d)
return tc.Code, d
})
}
})
if tc.Method == "GET" {
req, err = http.NewRequest(tc.Method, "/", nil)
} else {
req, err = http.NewRequest(tc.Method, "/", bytes.NewReader(jsonExpected))
}
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
res := httptest.NewRecorder()
m.ServeHTTP(res, req)
if res.Code != tc.Code {
t.Fatalf("[%d] Expected %d, but got: %d", idx, tc.Code, res.Code)
}
jsonReceived, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
if !reflect.DeepEqual(jsonExpected, jsonReceived) {
t.Fatalf("[%d] Expected %+v, but got: %+v", idx, jsonExpected, jsonReceived)
}
}
}
func TestPostErrReq(t *testing.T) {
testCases := []struct {
Code int
Body string
Response interface{}
}{
{http.StatusBadRequest, `{"]`, &ErrorResponse{http.StatusBadRequest, JSONErr, "unexpected end of JSON input"}},
}
for idx, tc := range testCases {
m := http.NewServeMux()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
j := Json{}
// Parse json body and send it as response
j.Handle(w, r, func() (status int, response interface{}) {
var d id
j.ParseJsonBody(r, &d)
return tc.Code, d
})
})
req, err := http.NewRequest("POST", "/", strings.NewReader(tc.Body))
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
res := httptest.NewRecorder()
m.ServeHTTP(res, req)
if res.Code != tc.Code {
t.Fatalf("[%d] Expected %d, but got: %d", idx, tc.Code, res.Code)
}
jsonExpected, err := json.Marshal(tc.Response)
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
jsonReceived, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("[%d] Expected nil, but got: %s", idx, err.Error())
}
if !reflect.DeepEqual(jsonExpected, jsonReceived) {
t.Fatalf("[%d] Expected %+v, but got: %+v", idx, jsonExpected, jsonReceived)
}
}
}