-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
271 lines (266 loc) · 7.71 KB
/
integration_test.go
File metadata and controls
271 lines (266 loc) · 7.71 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package lite
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/tiny-go/codec/driver"
_ "github.com/tiny-go/codec/driver/json"
)
func Test_All(t *testing.T) {
t.Run("Given an HTTP handler with registered module", func(t *testing.T) {
driver.Default("application/json")
module := NewBaseModule()
module.Register("pass", newPassController())
module.Register("fail", newFailController())
handler := NewHandler()
handler.Use("test", module)
ts := httptest.NewServer(handler)
defer ts.Close()
type testCase struct {
title string
request *http.Request
code int
header http.Header
body string
}
testCases := []testCase{
{
title: "plural OPTIONS request",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodOptions, ts.URL+"/test/pass", nil)
return r
}(),
header: http.Header{"Access-Control-Allow-Methods": []string{"GET,POST,PATCH,PUT,DELETE"}},
code: http.StatusOK,
},
{
title: "single OPTIONS request",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodOptions, ts.URL+"/test/pass/abcd", nil)
return r
}(),
header: http.Header{"Access-Control-Allow-Methods": []string{"GET,POST,PATCH,PUT,DELETE"}},
code: http.StatusOK,
},
{
title: "plural GET with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodGet, ts.URL+"/test/pass?foo=bar", nil)
return r
}(),
code: http.StatusOK,
body: "{\"foo\":[\"bar\"]}\n",
},
{
title: "plural GET with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodGet, ts.URL+"/test/fail", nil)
return r
}(),
code: http.StatusBadRequest,
body: "plural GET error\n",
},
{
title: "single GET with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodGet, ts.URL+"/test/pass/abcd", nil)
return r
}(),
code: http.StatusOK,
body: "\"abcd\"\n",
},
{
title: "single GET with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodGet, ts.URL+"/test/fail/abcd", nil)
return r
}(),
code: http.StatusBadRequest,
body: "single GET error\n",
},
{
title: "plural POST with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPost, ts.URL+"/test/pass", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\"}\n",
},
{
title: "plural POST with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPost, ts.URL+"/test/fail", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "plural POST error\n",
},
{
title: "single POST with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPost, ts.URL+"/test/pass/abcd", strings.NewReader("{\"foo\":\"bar\",\"pk\":\"abcd\"}\n"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\",\"pk\":\"abcd\"}\n",
},
{
title: "single POST with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPost, ts.URL+"/test/fail/abcd", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "single POST error\n",
},
{
title: "plural PATCH with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPatch, ts.URL+"/test/pass", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\"}\n",
},
{
title: "plural PATCH with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPatch, ts.URL+"/test/fail", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "plural PATCH error\n",
},
{
title: "single PATCH with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPatch, ts.URL+"/test/pass/abcd", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\",\"pk\":\"abcd\"}\n",
},
{
title: "single PATCH with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPatch, ts.URL+"/test/fail/abcd", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "single PATCH error\n",
},
{
title: "plural PUT with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPut, ts.URL+"/test/pass", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\"}\n",
},
{
title: "plural PUT with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPut, ts.URL+"/test/pass", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\"}\n",
},
{
title: "plural PUT with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPut, ts.URL+"/test/fail", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "plural PUT error\n",
},
{
title: "single PUT with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPut, ts.URL+"/test/pass/abcd", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusOK,
body: "{\"foo\":\"bar\",\"pk\":\"abcd\"}\n",
},
{
title: "single PUT with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodPut, ts.URL+"/test/fail/abcd", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "single PUT error\n",
},
{
title: "plural DELETE with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodDelete, ts.URL+"/test/pass?foo=bar", nil)
return r
}(),
code: http.StatusOK,
body: "{\"foo\":[\"bar\"]}\n",
},
{
title: "plural DELETE with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodDelete, ts.URL+"/test/fail", strings.NewReader("{\"foo\":\"bar\"}"))
return r
}(),
code: http.StatusBadRequest,
body: "plural DELETE error\n",
},
{
title: "single DELETE with success",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodDelete, ts.URL+"/test/pass/abcd", nil)
return r
}(),
code: http.StatusOK,
body: "\"abcd\"\n",
},
{
title: "single DELETE with failure",
request: func() *http.Request {
r, _ := http.NewRequest(http.MethodDelete, ts.URL+"/test/fail/abcd", nil)
return r
}(),
code: http.StatusBadRequest,
body: "single DELETE error\n",
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
client := &http.Client{}
res, err := client.Do(tc.request)
if err != nil {
t.Error("should not return an error")
}
if res.StatusCode != tc.code {
t.Errorf("status code %d was expected to be %d", res.StatusCode, tc.code)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Error("unable to read response body")
}
if string(body) != tc.body {
t.Errorf("the output %q was expected to be %q", body, tc.body)
}
for key, val := range tc.header {
if _, ok := res.Header[key]; !ok {
t.Errorf("header %q is missing", key)
} else if !reflect.DeepEqual(res.Header[key], val) {
t.Errorf("unexpected value %q for header %q", val, key)
}
}
})
}
})
}