@@ -21,6 +21,17 @@ func TestTestCase(t *testing.T) {
2121 fooRequst := atest.Request {
2222 API : urlFoo ,
2323 }
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+ }
2435
2536 tests := []struct {
2637 name string
@@ -41,11 +52,9 @@ func TestTestCase(t *testing.T) {
4152 name : "normal, response is map" ,
4253 testCase : & atest.TestCase {
4354 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"}` ,
4958 },
5059 Expect : atest.Response {
5160 StatusCode : http .StatusOK ,
@@ -204,10 +213,7 @@ func TestTestCase(t *testing.T) {
204213 },
205214 },
206215 },
207- prepare : func () {
208- gock .New (urlLocalhost ).
209- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
210- },
216+ prepare : defaultPrepare ,
211217 verify : func (t * testing.T , output interface {}, err error ) {
212218 assert .NotNil (t , err )
213219 assert .Contains (t , err .Error (), "failed to get field" )
@@ -225,10 +231,7 @@ func TestTestCase(t *testing.T) {
225231 // },
226232 // },
227233 // },
228- // prepare: func() {
229- // gock.New(urlLocalhost).
230- // Get("/foo").Reply(http.StatusOK).BodyString(`{"items":[]}`)
231- // },
234+ // prepare: defaultPrepare,
232235 // verify: func(t *testing.T, output interface{}, err error) {
233236 // if assert.NotNil(t, err) {
234237 // assert.Contains(t, err.Error(), "failed to verify")
@@ -245,10 +248,7 @@ func TestTestCase(t *testing.T) {
245248 },
246249 },
247250 },
248- prepare : func () {
249- gock .New (urlLocalhost ).
250- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
251- },
251+ prepare : defaultPrepare ,
252252 verify : func (t * testing.T , output interface {}, err error ) {
253253 assert .NotNil (t , err )
254254 assert .Contains (t , err .Error (), "unknown name println" )
@@ -263,10 +263,7 @@ func TestTestCase(t *testing.T) {
263263 },
264264 },
265265 },
266- prepare : func () {
267- gock .New (urlLocalhost ).
268- Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
269- },
266+ prepare : defaultPrepare ,
270267 verify : func (t * testing.T , output interface {}, err error ) {
271268 assert .NotNil (t , err )
272269 assert .Contains (t , err .Error (), "expected bool, but got int" )
@@ -303,16 +300,11 @@ func TestTestCase(t *testing.T) {
303300 Header : map [string ]string {
304301 util .ContentType : "multipart/form-data" ,
305302 },
306- Form : map [string ]string {
307- "key" : "value" ,
308- },
303+ Form : defaultForm ,
309304 },
310305 },
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 ,
316308 }, {
317309 name : "normal form request" ,
318310 testCase : & atest.TestCase {
@@ -322,14 +314,24 @@ func TestTestCase(t *testing.T) {
322314 Header : map [string ]string {
323315 util .ContentType : "application/x-www-form-urlencoded" ,
324316 },
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"}}"}` ,
328329 },
329330 },
330331 prepare : func () {
331332 gock .New (urlLocalhost ).
332- Post ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
333+ Post ("/foo" ).BodyString (`{"name":"hello"}` ).
334+ Reply (http .StatusOK ).BodyString (`{}` )
333335 },
334336 verify : noError ,
335337 }}
0 commit comments