forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
319 lines (245 loc) · 7.27 KB
/
context_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
package gin
import (
"errors"
"html/template"
"net/http"
"net/http/httptest"
"testing"
)
// TestContextParamsGet tests that a parameter can be parsed from the URL.
func TestContextParamsByName(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
w := httptest.NewRecorder()
name := ""
r := Default()
r.GET("/test/:name", func(c *Context) {
name = c.Params.ByName("name")
})
r.ServeHTTP(w, req)
if name != "alexandernyquist" {
t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
}
}
// TestContextSetGet tests that a parameter is set correctly on the
// current context and can be retrieved using Get.
func TestContextSetGet(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test", func(c *Context) {
// Key should be lazily created
if c.Keys != nil {
t.Error("Keys should be nil")
}
// Set
c.Set("foo", "bar")
v, err := c.Get("foo")
if err != nil {
t.Errorf("Error on exist key")
}
if v != "bar" {
t.Errorf("Value should be bar, was %s", v)
}
})
r.ServeHTTP(w, req)
}
// TestContextJSON tests that the response is serialized as JSON
// and Content-Type is set to application/json
func TestContextJSON(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test", func(c *Context) {
c.JSON(200, H{"foo": "bar"})
})
r.ServeHTTP(w, req)
if w.Body.String() != "{\"foo\":\"bar\"}\n" {
t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "application/json" {
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextHTML tests that the response executes the templates
// and responds with Content-Type set to text/html
func TestContextHTML(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
r.SetHTMLTemplate(templ)
type TestData struct{ Name string }
r.GET("/test", func(c *Context) {
c.HTML(200, "t", TestData{"alexandernyquist"})
})
r.ServeHTTP(w, req)
if w.Body.String() != "Hello alexandernyquist" {
t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/html" {
t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextString tests that the response is returned
// with Content-Type set to text/plain
func TestContextString(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test", func(c *Context) {
c.String(200, "test")
})
r.ServeHTTP(w, req)
if w.Body.String() != "test" {
t.Errorf("Response should be test, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/plain" {
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextXML tests that the response is serialized as XML
// and Content-Type is set to application/xml
func TestContextXML(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test", func(c *Context) {
c.XML(200, H{"foo": "bar"})
})
r.ServeHTTP(w, req)
if w.Body.String() != "<map><foo>bar</foo></map>" {
t.Errorf("Response should be <map><foo>bar</foo></map>, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "application/xml" {
t.Errorf("Content-Type should be application/xml, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextData tests that the response can be written from `bytesting`
// with specified MIME type
func TestContextData(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/csv", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test/csv", func(c *Context) {
c.Data(200, "text/csv", []byte(`foo,bar`))
})
r.ServeHTTP(w, req)
if w.Body.String() != "foo,bar" {
t.Errorf("Response should be foo&bar, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/csv" {
t.Errorf("Content-Type should be text/csv, was %s", w.HeaderMap.Get("Content-Type"))
}
}
func TestContextFile(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/file", nil)
w := httptest.NewRecorder()
r := Default()
r.GET("/test/file", func(c *Context) {
c.File("./gin.go")
})
r.ServeHTTP(w, req)
bodyAsString := w.Body.String()
if len(bodyAsString) == 0 {
t.Errorf("Got empty body instead of file data")
}
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
t.Errorf("Content-Type should be text/plain; charset=utf-8, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestHandlerFunc - ensure that custom middleware works properly
func TestHandlerFunc(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r := Default()
var stepsPassed int = 0
r.Use(func(context *Context) {
stepsPassed += 1
context.Next()
stepsPassed += 1
})
r.ServeHTTP(w, req)
if w.Code != 404 {
t.Errorf("Response code should be Not found, was: %s", w.Code)
}
if stepsPassed != 2 {
t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
}
}
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
func TestBadAbortHandlersChain(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r := Default()
var stepsPassed int = 0
r.Use(func(context *Context) {
stepsPassed += 1
context.Next()
stepsPassed += 1
// after check and abort
context.Abort(409)
},
func(context *Context) {
stepsPassed += 1
context.Next()
stepsPassed += 1
context.Abort(403)
},
)
r.ServeHTTP(w, req)
if w.Code != 403 {
t.Errorf("Response code should be Forbiden, was: %s", w.Code)
}
if stepsPassed != 4 {
t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
}
}
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
func TestAbortHandlersChain(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r := Default()
var stepsPassed int = 0
r.Use(func(context *Context) {
stepsPassed += 1
context.Abort(409)
},
func(context *Context) {
stepsPassed += 1
context.Next()
stepsPassed += 1
},
)
r.ServeHTTP(w, req)
if w.Code != 409 {
t.Errorf("Response code should be Conflict, was: %s", w.Code)
}
if stepsPassed != 1 {
t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
}
}
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
// as well as Abort
func TestFailHandlersChain(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r := Default()
var stepsPassed int = 0
r.Use(func(context *Context) {
stepsPassed += 1
context.Fail(500, errors.New("foo"))
},
func(context *Context) {
stepsPassed += 1
context.Next()
stepsPassed += 1
},
)
r.ServeHTTP(w, req)
if w.Code != 500 {
t.Errorf("Response code should be Server error, was: %s", w.Code)
}
if stepsPassed != 1 {
t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
}
}