-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_test.go
358 lines (289 loc) · 12.2 KB
/
view_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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package helios
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type sampleRequest struct {
A string `json:"a"`
B int `json:"b"`
C bool `json:"c"`
D string `json:"d"`
E int `json:"e"`
F bool `json:"f"`
}
func TestHandle(t *testing.T) {
App.BeforeTest()
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/def", nil)
f := func(req Request) {
json := make(map[string]int)
json["abc"] = 2
json["def"] = 3
req.SendJSON(json, 201)
}
Handle(f)(recorder, request)
actualResponse := make([]byte, 17)
expectedResponse := []byte("{\"abc\":2,\"def\":3}")
n, err := recorder.Result().Body.Read(actualResponse)
assert.Nil(t, err, "Fail on reading body")
assert.Equal(t, n, len(expectedResponse), "Different number of bytes")
assert.Equal(t, expectedResponse, actualResponse, "Different body")
}
func TestMockRequest(t *testing.T) {
App.BeforeTest()
req := NewMockRequest()
req.URLParam["abc"] = "def"
req.URLParam["one"] = "1"
req.URLParam["long"] = "123456789012345678901234567890"
assert.Equal(t, "def", req.GetURLParam("abc"), "Failed to retrieve url param")
assert.Equal(t, "", req.GetURLParam("def"), "Not found url param should return empty string")
oneParam, errOneParam := req.GetURLParamUint("one")
_, errTwoParam := req.GetURLParamUint("two")
_, errAbcParam := req.GetURLParamUint("abc")
_, errLongParam := req.GetURLParamUint("long")
assert.Equal(t, uint(1), oneParam, "Different result on uint url param")
assert.Nil(t, errOneParam, "Failed to parse uint url param")
assert.NotNil(t, errTwoParam, "Empty uint param will return error")
assert.NotNil(t, errAbcParam, "Not number param will return error")
assert.NotNil(t, errLongParam, "Long number param will return error")
var expected sampleRequest = sampleRequest{A: "def"}
var actual sampleRequest
req.RequestData = nil
errDeserialize1 := req.DeserializeRequestData(&actual)
assert.NotNil(t, errDeserialize1, "If request data is nil, DeserializeRequestData purposely throw error")
req.RequestData = expected
errDeserialize2 := req.DeserializeRequestData(&actual)
assert.Nil(t, errDeserialize2, "Failed to deserialize request data")
assert.Equal(t, expected, actual, "Differenet request data")
req.RequestData = "{\"a\":\"def\"}"
errDeserialize3 := req.DeserializeRequestData(&actual)
assert.Nil(t, errDeserialize3, "Failed to deserialize request data")
assert.Equal(t, expected, actual, "Different request data")
req.RequestData = "{\"b\":\"def\"}"
errDeserialize4 := req.DeserializeRequestData(&actual)
assert.Equal(t, ErrJSONParseFailed, errDeserialize4, "Failed to deserialize request data")
req.RequestData = "{\"a\":\"def"
errDeserialize5 := req.DeserializeRequestData(&actual)
assert.Equal(t, ErrJSONParseFailed, errDeserialize5, "Invalid JSON")
req.SetContextData("abc", 2)
req.SetContextData("abc", 3)
req.SetContextData("def", "ghi")
assert.Equal(t, 3, req.GetContextData("abc"), "Fail to use context data")
assert.Equal(t, "ghi", req.GetContextData("def"), "Fail to use context data")
assert.Nil(t, req.GetContextData("ghi"), "Context data with unexist key should return nil")
req.SetSessionData("abc", 4)
req.SetSessionData("abc", 5)
req.SetSessionData("def", true)
req.SaveSession()
assert.Equal(t, 5, req.GetSessionData("abc"), "Fail to use session data")
assert.Equal(t, true, req.GetSessionData("def"), "Fail to use session data")
assert.Nil(t, req.GetSessionData("ghi"), "Session data with unexist key should return nil")
req.RequestHeader["header-a"] = "a"
assert.Empty(t, req.GetHeader("header-b"), "Missing header should return empty string")
assert.Equal(t, "a", req.GetHeader("header-a"), "Different request header value")
assert.Equal(t, "a", req.GetHeader("HEADER-A"), "Request header should be case insensitive")
req.SetHeader("header-x", "x")
req.SetHeader("header-x", "y")
assert.Empty(t, req.ResponseHeader["header-y"], "Unset header should return empty string")
assert.Equal(t, "y", req.ResponseHeader["header-x"], "Different header set")
req.SendJSON(sampleRequest{A: "abcde", B: 2, C: true}, 499)
expectedResponse := "{\"a\":\"abcde\",\"b\":2,\"c\":true,\"d\":\"\",\"e\":0,\"f\":false}"
assert.Equal(t, expectedResponse, string(req.JSONResponse), "Different JSON Response")
assert.Equal(t, 499, req.StatusCode, "Different Response status code")
req.SendJSON(make(chan int), 200)
assert.Equal(t, http.StatusInternalServerError, req.StatusCode, "Failed to marshalling json")
assert.Equal(t, "127.0.0.1", req.ClientIP(), "Default for ClientIP is 127.0.0.1")
req.RemoteAddr = "1.2.3.4"
assert.Equal(t, "1.2.3.4", req.ClientIP(), "ClientIP() should returns the RemoteAddr attribute")
}
func TestNewHTTPRequest(t *testing.T) {
App.BeforeTest()
recorder := httptest.NewRecorder()
response := []byte("abc")
request, _ := http.NewRequest("GET", "/def", nil)
req := NewHTTPRequest(recorder, request)
_, err := req.w.Write(response)
assert.Nil(t, err, "Fail on writing response")
actualResponse := make([]byte, 5)
n, err := recorder.Result().Body.Read(actualResponse)
assert.Nil(t, err, "Fail on reading body")
assert.Equal(t, n, 3, "Different number of bytes")
assert.Equal(t, []byte{0x61, 0x62, 0x63, 0x0, 0x0}, actualResponse, "Different body")
}
func TestHTTPRequest(t *testing.T) {
App.BeforeTest()
request, _ := http.NewRequest("POST", "/def", strings.NewReader("{\"a\":\"abcde\",\"b\":2,\"c\":true}"))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("header-a", "a")
recorder := httptest.NewRecorder()
urlParam := make(map[string]string)
urlParam["id"] = "3"
urlParam["abc"] = "def"
urlParam["long"] = "123456789012345678901234567890"
contextData := make(map[string]interface{})
contextData["abc"] = "random_context_data"
contextData["def"] = "def"
req := HTTPRequest{
r: request,
w: recorder,
s: nil,
c: contextData,
u: urlParam,
}
req.SetContextData("def", "ghi")
assert.Empty(t, req.GetHeader("header-b"), "Missing header should return empty string")
assert.Equal(t, "a", req.GetHeader("header-a"), "Different request header returned")
assert.Equal(t, "a", req.GetHeader("HEADER-A"), "Header should be case insensitive")
req.SetHeader("header-x", "x")
req.SetHeader("header-x", "y")
assert.Empty(t, req.w.Header().Get("header-y"), "Empty header shoul be empty string")
assert.Equal(t, "y", req.w.Header().Get("header-x"), "Different header set")
assert.Equal(t, "3", req.GetURLParam("id"), "Failed to retrive url param")
idParam, errIDParam := req.GetURLParamUint("id")
_, errOtherIDParam := req.GetURLParamUint("id2")
_, errAbcParam := req.GetURLParamUint("abc")
_, errLongParam := req.GetURLParamUint("long")
assert.Equal(t, uint(3), idParam, "Different result on uint url param")
assert.Nil(t, errIDParam, "Failed to parse uint url param")
assert.NotNil(t, errOtherIDParam, "Empty uint param will return error")
assert.NotNil(t, errAbcParam, "Not number param will return error")
assert.NotNil(t, errLongParam, "Long number param will return error")
assert.Equal(t, "random_context_data", req.GetContextData("abc"), "Failed to retrive context data")
assert.Equal(t, "ghi", req.GetContextData("def"), "Failed to retrive modified context data")
assert.Nil(t, req.GetContextData("ghi"), "Missing context data should treated as nil")
}
func TestHTTPRequestJSONEncoded(t *testing.T) {
App.BeforeTest()
request, _ := http.NewRequest("POST", "/def", strings.NewReader("{\"a\":\"abcde\",\"b\":2,\"c\":true}"))
request.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
req := HTTPRequest{
r: request,
w: recorder,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
var requestData sampleRequest
err := req.DeserializeRequestData(&requestData)
assert.Nil(t, err, "Fail on deserializing request data")
assert.Equal(t, "abcde", requestData.A, "Different string request data")
assert.Equal(t, 2, requestData.B, "Different integer request data")
assert.Equal(t, true, requestData.C, "Different boolean request data")
assert.Equal(t, "", requestData.D, "Different empty string request data")
assert.Equal(t, 0, requestData.E, "Different empty integer request data")
assert.Equal(t, false, requestData.F, "Different empty boolean request data")
}
func TestHTTPRequestJSONPoorlyEncoded(t *testing.T) {
App.BeforeTest()
request, _ := http.NewRequest("POST", "/def", strings.NewReader("{\"a\":\"abcde\",\"b\":2,\"c\":true"))
request.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
req := HTTPRequest{
r: request,
w: recorder,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
var requestData sampleRequest
err := req.DeserializeRequestData(&requestData)
assert.Equal(t, ErrJSONParseFailed, err, "Poorly encoded JSON should return Bad Request error")
}
func TestHTTPRequestUrlFormEncoded(t *testing.T) {
App.BeforeTest()
request, _ := http.NewRequest("POST", "/def", strings.NewReader("a=abcde&b=2&c=true}"))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
recorder := httptest.NewRecorder()
req := HTTPRequest{
r: request,
w: recorder,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
var requestData sampleRequest
err := req.DeserializeRequestData(&requestData)
assert.Equal(t, ErrUnsupportedContentType, err, "application/x-www-form-urlencoded is not supported yet")
}
func TestHTTPRequestMultipartFormData(t *testing.T) {
App.BeforeTest()
request, _ := http.NewRequest("POST", "/def", strings.NewReader(`
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="a"
abcde
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="b"
2
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="c"
true
-----------------------------9051914041544843365972754266-
`))
request.Header.Set("Content-Type", "multipart/form-data")
recorder := httptest.NewRecorder()
req := HTTPRequest{
r: request,
w: recorder,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
var requestData sampleRequest
err := req.DeserializeRequestData(&requestData)
assert.Equal(t, ErrUnsupportedContentType, err, "multipart/form-data is not supported yet")
}
func TestHTTPRequestClientIP(t *testing.T) {
App.BeforeTest()
requestXFF, _ := http.NewRequest("POST", "/def", nil)
requestXFF.Header.Set("X-Forwarded-For", "1.2.3.4, 5.6.7.8")
requestXFF.Header.Set("X-Real-Ip", "11.22.33.44")
requestXFF.RemoteAddr = "55.66.77.88:12345"
reqXFF := HTTPRequest{
r: requestXFF,
w: nil,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
assert.Equal(t, "1.2.3.4", reqXFF.ClientIP(), "If X-Forwarded-For header is present, ClientIP should return the first entry of the header")
requestXRI, _ := http.NewRequest("POST", "/def", nil)
requestXRI.Header.Set("X-Forwarded-For", "")
requestXRI.Header.Set("X-Real-Ip", "11.22.33.44")
requestXRI.RemoteAddr = "55.66.77.88:12345"
reqXRI := HTTPRequest{
r: requestXRI,
w: nil,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
assert.Equal(t, "11.22.33.44", reqXRI.ClientIP(), "If X-Forwarded-For header is not present and X-Real-Ip is present, ClientIP should return the X-Real-Ip")
requestRemoteAddr, _ := http.NewRequest("POST", "/def", nil)
requestRemoteAddr.Header.Set("X-Forwarded-For", "")
requestRemoteAddr.Header.Set("X-Real-Ip", "")
requestRemoteAddr.RemoteAddr = "55.66.77.88:12345"
reqRemoteAddr := HTTPRequest{
r: requestRemoteAddr,
w: nil,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
assert.Equal(t, "55.66.77.88", reqRemoteAddr.ClientIP(), "If X-Forwarded-For and X-Real-Ip headers are not present, ClientIP should return RemoteAddr")
requestBadIP, _ := http.NewRequest("POST", "/def", nil)
requestBadIP.Header.Set("X-Forwarded-For", "")
requestBadIP.Header.Set("X-Real-Ip", "")
requestBadIP.RemoteAddr = "55.66.77.88"
reqBadIP := HTTPRequest{
r: requestBadIP,
w: nil,
s: nil,
c: make(map[string]interface{}),
u: make(map[string]string),
}
assert.Empty(t, reqBadIP.ClientIP(), "Bad IP format on RemoteAddr will return empty ip")
}