@@ -21,6 +21,17 @@ func TestTestCase(t *testing.T) {
21
21
fooRequst := atest.Request {
22
22
API : urlFoo ,
23
23
}
24
+ defaultForm := map [string ]string {
25
+ "key" : "value" ,
26
+ }
27
+ defaultPrepare := func () {
28
+ gock .New (urlLocalhost ).
29
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
30
+ }
31
+ defaultPostPrepare := func () {
32
+ gock .New (urlLocalhost ).
33
+ Post ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
34
+ }
24
35
25
36
tests := []struct {
26
37
name string
@@ -41,11 +52,9 @@ func TestTestCase(t *testing.T) {
41
52
name : "normal, response is map" ,
42
53
testCase : & atest.TestCase {
43
54
Request : atest.Request {
44
- API : urlFoo ,
45
- Header : map [string ]string {
46
- "key" : "value" ,
47
- },
48
- Body : `{"foo":"bar"}` ,
55
+ API : urlFoo ,
56
+ Header : defaultForm ,
57
+ Body : `{"foo":"bar"}` ,
49
58
},
50
59
Expect : atest.Response {
51
60
StatusCode : http .StatusOK ,
@@ -204,10 +213,7 @@ func TestTestCase(t *testing.T) {
204
213
},
205
214
},
206
215
},
207
- prepare : func () {
208
- gock .New (urlLocalhost ).
209
- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
210
- },
216
+ prepare : defaultPrepare ,
211
217
verify : func (t * testing.T , output interface {}, err error ) {
212
218
assert .NotNil (t , err )
213
219
assert .Contains (t , err .Error (), "failed to get field" )
@@ -225,10 +231,7 @@ func TestTestCase(t *testing.T) {
225
231
// },
226
232
// },
227
233
// },
228
- // prepare: func() {
229
- // gock.New(urlLocalhost).
230
- // Get("/foo").Reply(http.StatusOK).BodyString(`{"items":[]}`)
231
- // },
234
+ // prepare: defaultPrepare,
232
235
// verify: func(t *testing.T, output interface{}, err error) {
233
236
// if assert.NotNil(t, err) {
234
237
// assert.Contains(t, err.Error(), "failed to verify")
@@ -245,10 +248,7 @@ func TestTestCase(t *testing.T) {
245
248
},
246
249
},
247
250
},
248
- prepare : func () {
249
- gock .New (urlLocalhost ).
250
- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
251
- },
251
+ prepare : defaultPrepare ,
252
252
verify : func (t * testing.T , output interface {}, err error ) {
253
253
assert .NotNil (t , err )
254
254
assert .Contains (t , err .Error (), "unknown name println" )
@@ -263,10 +263,7 @@ func TestTestCase(t *testing.T) {
263
263
},
264
264
},
265
265
},
266
- prepare : func () {
267
- gock .New (urlLocalhost ).
268
- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
269
- },
266
+ prepare : defaultPrepare ,
270
267
verify : func (t * testing.T , output interface {}, err error ) {
271
268
assert .NotNil (t , err )
272
269
assert .Contains (t , err .Error (), "expected bool, but got int" )
@@ -303,16 +300,11 @@ func TestTestCase(t *testing.T) {
303
300
Header : map [string ]string {
304
301
util .ContentType : "multipart/form-data" ,
305
302
},
306
- Form : map [string ]string {
307
- "key" : "value" ,
308
- },
303
+ Form : defaultForm ,
309
304
},
310
305
},
311
- prepare : func () {
312
- gock .New (urlLocalhost ).
313
- Post ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
314
- },
315
- verify : noError ,
306
+ prepare : defaultPostPrepare ,
307
+ verify : noError ,
316
308
}, {
317
309
name : "normal form request" ,
318
310
testCase : & atest.TestCase {
@@ -322,14 +314,24 @@ func TestTestCase(t *testing.T) {
322
314
Header : map [string ]string {
323
315
util .ContentType : "application/x-www-form-urlencoded" ,
324
316
},
325
- Form : map [string ]string {
326
- "key" : "value" ,
327
- },
317
+ Form : defaultForm ,
318
+ },
319
+ },
320
+ prepare : defaultPostPrepare ,
321
+ verify : noError ,
322
+ }, {
323
+ name : "body is a template" ,
324
+ testCase : & atest.TestCase {
325
+ Request : atest.Request {
326
+ API : urlFoo ,
327
+ Method : http .MethodPost ,
328
+ Body : `{"name":"{{lower "HELLO"}}"}` ,
328
329
},
329
330
},
330
331
prepare : func () {
331
332
gock .New (urlLocalhost ).
332
- Post ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
333
+ Post ("/foo" ).BodyString (`{"name":"hello"}` ).
334
+ Reply (http .StatusOK ).BodyString (`{}` )
333
335
},
334
336
verify : noError ,
335
337
}}
0 commit comments