/
dou_test.go
343 lines (248 loc) · 7.82 KB
/
dou_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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package dou
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
type recorder struct {
calledTime int
}
func newRecorder() *recorder {
return &recorder{calledTime: 0}
}
type testAPI struct {
beforeDispatchCalled bool
afterDispatchCalled bool
recoverCalled bool
}
// Enable to stub
var testAPIMarshal = func(v interface{}) ([]byte, error) {
return nil, nil
}
// Enable to stub
var testAPIUnmarshal = func(data []byte, v interface{}) error {
return nil
}
func (p *testAPI) BeforeDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
p.beforeDispatchCalled = true
return w, r
}
func (p *testAPI) AfterDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
p.afterDispatchCalled = true
return w, r
}
func (p *testAPI) OnPanic(w http.ResponseWriter, r *http.Request) {
p.recoverCalled = true
}
func (p *testAPI) Marshal(v interface{}) ([]byte, error) {
return testAPIMarshal(v)
}
func (p *testAPI) Unmarshal(data []byte, v interface{}) error {
return testAPIUnmarshal(data, v)
}
func (p *testAPI) APIStatus(w http.ResponseWriter, code int) {
}
func TestNewAPIWithUnregisteredPluginName(t *testing.T) {
api, err := NewAPI("unknown")
if api != nil {
t.Error("If NewAPI called with unknown plugin name, api should be nil")
}
if err == nil {
t.Error("If NewAPI called with unknown plugin name, api should not be nil")
}
}
func TestDeregister(t *testing.T) {
Register("testapi", &testAPI{})
Deregister("testapi")
if _, ok := plugins["testapi"]; ok != false {
t.Error("Deregister should delete registered plugin")
}
}
func TestCallBeforeDispatchAndAfterDispatch(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
defer delete(plugins, "testapi")
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
a.LogStackTrace = false
a.ServeHTTP(response, request)
plugin, ok := a.Plugin.(*testAPI)
if !ok {
panic("a.plugin should be able to cast *testAPI")
}
if !plugin.beforeDispatchCalled {
t.Error("Plugin.BeforeDispatch should be called")
}
if !plugin.afterDispatchCalled {
t.Error("Plugin.AfterDispatch should be called")
}
}
func TestCallOnPanicIfOccurPanicInHandler(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a := newTestAPI()
a.LogStackTrace = false
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("<test panic>")
})
a.ServeHTTP(response, request)
plugin, ok := a.Plugin.(*testAPI)
if !ok {
panic("a.plugin should be able to cast *testAPI")
}
if !plugin.recoverCalled {
t.Error("Plugin.OnPanic should be called")
}
}
func TestCallAfterDispatchIfOccurPanicInHandler(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a := newTestAPI()
a.LogStackTrace = false
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("<test panic>")
})
a.ServeHTTP(response, request)
plugin, ok := a.Plugin.(*testAPI)
if !ok {
panic("a.plugin should be able to cast *testAPI")
}
if !plugin.afterDispatchCalled {
t.Error("Plugin.AfterDispatch should be called")
}
}
func TestNewSafeWriter(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sw := NewSafeWriter(w)
if !reflect.DeepEqual(sw.ResponseWriter, w) {
t.Error("NewSafeWriter should set given value to .ResponseWriter")
}
if sw.Wrote != false {
t.Error("NewSafeWriter should set false to .wrote")
}
}).ServeHTTP(response, request)
}
func TestNewSafeWriterWrite(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sw := NewSafeWriter(w)
sw.Write([]byte("hello"))
if sw.Wrote != true {
t.Error("NewSafeWriter.Wrote should be true after called Write")
}
}).ServeHTTP(response, request)
}
func TestAPIUnmarshal(t *testing.T) {
request, _ := http.NewRequest("GET", `/?json={"name": "ToQoz"}`, nil)
response := httptest.NewRecorder()
// stub testAPI.Marshal
testAPIUnmarshal = func(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := map[string]string{}
expected := map[string]string{"name": "ToQoz"}
a.Unmarshal([]byte(request.FormValue("json")), &v)
if !reflect.DeepEqual(v, expected) {
t.Errorf("fail to Unmarshal.\nexpected: %v\ngot: %v\n", expected, v)
}
})
a.ServeHTTP(response, request)
}
func TestAPIOkSetGivenHTTPStatusCodeAndResponseBody(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
expectedBodyString := "stubed testAPIMarshal"
expectedCode := http.StatusCreated
// stub testAPI.Marshal
testAPIMarshal = func(v interface{}) ([]byte, error) {
return []byte(expectedBodyString), nil
}
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.Ok(w, "", expectedCode)
})
a.ServeHTTP(response, request)
// [Test] http status code
if response.Code != expectedCode {
t.Errorf("API.OK should set given status code\nexpected: %v\ngot: %v\n", expectedCode, response.Code)
}
// [Test] responseBody
gotBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
gotBodyString := strings.TrimSuffix(string(gotBody), "\n")
if gotBodyString != expectedBodyString {
t.Errorf("API.OK should marshal given resource and write it\nexpected: %v\ngot: %v\n", expectedBodyString, gotBodyString)
}
}
func TestAPIOkSet200IfGiven0AsHTTPStatusCode(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.Ok(w, "", 0)
})
a.ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Errorf("API.OK should set 200 if given 0\nexpected: %v\ngot: %v\n", http.StatusOK, response.Code)
}
}
func TestAPIErrorSetGivenHTTPStatusCodeAndResponseBody(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
expectedBodyString := "stubed testAPIMarshal"
expectedCode := http.StatusNotFound
// stub testAPI.Marshal
testAPIMarshal = func(v interface{}) ([]byte, error) {
return []byte(expectedBodyString), nil
}
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.Error(w, "", expectedCode)
})
a.ServeHTTP(response, request)
// [Test] http status code
if response.Code != expectedCode {
t.Errorf("API.Error should set given status code\nexpected: %v\ngot: %v\n", expectedCode, response.Code)
}
// [Test] responseBody
gotBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
gotBodyString := strings.TrimSuffix(string(gotBody), "\n")
if gotBodyString != expectedBodyString {
t.Errorf("API.Error should marshal given resource and write it\nexpected: \"%v\"\ngot: \"%v\"\n", expectedBodyString, gotBodyString)
}
}
func TestAPIErrorSet500IfGiven0AsHTTPStatusCode(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a := newTestAPI()
a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.Error(w, "", 0)
})
a.ServeHTTP(response, request)
if response.Code != http.StatusInternalServerError {
t.Errorf("API.OK should set 200 if given 0\nexpected: %v\ngot: %v\n", http.StatusOK, response.Code)
}
}
func newTestAPI() *API {
Register("testapi", &testAPI{})
defer Deregister("testapi")
a, err := NewAPI("testapi")
if err != nil {
panic(err)
}
return a
}