1
1
package runner
2
2
3
3
import (
4
+ "errors"
4
5
"net/http"
5
6
"testing"
6
7
8
+ _ "embed"
7
9
"github.com/h2non/gock"
8
10
atest "github.com/linuxsuren/api-testing/pkg/testing"
9
11
"github.com/stretchr/testify/assert"
@@ -17,7 +19,7 @@ func TestTestCase(t *testing.T) {
17
19
prepare func ()
18
20
verify func (t * testing.T , output interface {}, err error )
19
21
}{{
20
- name : "normal" ,
22
+ name : "normal, response is map " ,
21
23
testCase : & atest.TestCase {
22
24
Request : atest.Request {
23
25
API : "http://localhost/foo" ,
@@ -51,13 +53,279 @@ func TestTestCase(t *testing.T) {
51
53
assert .Nil (t , err )
52
54
assert .Equal (t , map [string ]interface {}{"name" : "linuxsuren" }, output )
53
55
},
56
+ }, {
57
+ name : "normal, response is slice" ,
58
+ testCase : & atest.TestCase {
59
+ Request : atest.Request {
60
+ API : "http://localhost/foo" ,
61
+ },
62
+ Expect : atest.Response {
63
+ StatusCode : http .StatusOK ,
64
+ Body : `["foo", "bar"]` ,
65
+ },
66
+ },
67
+ prepare : func () {
68
+ gock .New ("http://localhost" ).
69
+ Get ("/foo" ).
70
+ Reply (http .StatusOK ).
71
+ BodyString (`["foo", "bar"]` )
72
+ },
73
+ verify : func (t * testing.T , output interface {}, err error ) {
74
+ assert .Nil (t , err )
75
+ assert .Equal (t , []interface {}{"foo" , "bar" }, output )
76
+ },
77
+ }, {
78
+ name : "normal, response from file" ,
79
+ testCase : & atest.TestCase {
80
+ Request : atest.Request {
81
+ API : "http://localhost/foo" ,
82
+ Method : http .MethodPost ,
83
+ BodyFromFile : "testdata/generic_response.json" ,
84
+ },
85
+ Expect : atest.Response {
86
+ StatusCode : http .StatusOK ,
87
+ },
88
+ },
89
+ prepare : func () {
90
+ gock .New ("http://localhost" ).
91
+ Post ("/foo" ).BodyString (genericBody ).
92
+ Reply (http .StatusOK ).BodyString ("123" )
93
+ },
94
+ verify : func (t * testing.T , output interface {}, err error ) {
95
+ assert .NotNil (t , err )
96
+ },
97
+ }, {
98
+ name : "response from a not found file" ,
99
+ testCase : & atest.TestCase {
100
+ Request : atest.Request {
101
+ API : "http://localhost/foo" ,
102
+ Method : http .MethodPost ,
103
+ BodyFromFile : "testdata/fake.json" ,
104
+ },
105
+ },
106
+ verify : func (t * testing.T , output interface {}, err error ) {
107
+ assert .NotNil (t , err )
108
+ },
109
+ }, {
110
+ name : "bad request" ,
111
+ testCase : & atest.TestCase {
112
+ Request : atest.Request {
113
+ API : "http://localhost/foo" ,
114
+ },
115
+ Expect : atest.Response {
116
+ StatusCode : http .StatusOK ,
117
+ },
118
+ },
119
+ prepare : func () {
120
+ gock .New ("http://localhost" ).
121
+ Get ("/foo" ).Reply (http .StatusBadRequest )
122
+ },
123
+ verify : func (t * testing.T , output interface {}, err error ) {
124
+ assert .NotNil (t , err )
125
+ },
126
+ }, {
127
+ name : "error with request" ,
128
+ testCase : & atest.TestCase {
129
+ Request : atest.Request {
130
+ API : "http://localhost/foo" ,
131
+ },
132
+ },
133
+ prepare : func () {
134
+ gock .New ("http://localhost" ).
135
+ Get ("/foo" ).ReplyError (errors .New ("error" ))
136
+ },
137
+ verify : func (t * testing.T , output interface {}, err error ) {
138
+ assert .NotNil (t , err )
139
+ },
140
+ }, {
141
+ name : "not match with body" ,
142
+ testCase : & atest.TestCase {
143
+ Request : atest.Request {
144
+ API : "http://localhost/foo" ,
145
+ },
146
+ Expect : atest.Response {
147
+ Body : "bar" ,
148
+ },
149
+ },
150
+ prepare : func () {
151
+ gock .New ("http://localhost" ).
152
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString ("foo" )
153
+ },
154
+ verify : func (t * testing.T , output interface {}, err error ) {
155
+ assert .NotNil (t , err )
156
+ },
157
+ }, {
158
+ name : "not match with header" ,
159
+ testCase : & atest.TestCase {
160
+ Request : atest.Request {
161
+ API : "http://localhost/foo" ,
162
+ },
163
+ Expect : atest.Response {
164
+ Header : map [string ]string {
165
+ "foo" : "bar" ,
166
+ },
167
+ },
168
+ },
169
+ prepare : func () {
170
+ gock .New ("http://localhost" ).
171
+ Get ("/foo" ).Reply (http .StatusOK ).SetHeader ("foo" , "value" )
172
+ },
173
+ verify : func (t * testing.T , output interface {}, err error ) {
174
+ assert .NotNil (t , err )
175
+ },
176
+ }, {
177
+ name : "not found from fields" ,
178
+ testCase : & atest.TestCase {
179
+ Request : atest.Request {
180
+ API : "http://localhost/foo" ,
181
+ },
182
+ Expect : atest.Response {
183
+ BodyFieldsExpect : map [string ]string {
184
+ "foo" : "bar" ,
185
+ },
186
+ },
187
+ },
188
+ prepare : func () {
189
+ gock .New ("http://localhost" ).
190
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (genericBody )
191
+ },
192
+ verify : func (t * testing.T , output interface {}, err error ) {
193
+ assert .NotNil (t , err )
194
+ },
195
+ }, {
196
+ name : "body filed not match" ,
197
+ testCase : & atest.TestCase {
198
+ Request : atest.Request {
199
+ API : "http://localhost/foo" ,
200
+ },
201
+ Expect : atest.Response {
202
+ BodyFieldsExpect : map [string ]string {
203
+ "name" : "bar" ,
204
+ },
205
+ },
206
+ },
207
+ prepare : func () {
208
+ gock .New ("http://localhost" ).
209
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (genericBody )
210
+ },
211
+ verify : func (t * testing.T , output interface {}, err error ) {
212
+ assert .NotNil (t , err )
213
+ },
214
+ }, {
215
+ name : "invalid filed finding" ,
216
+ testCase : & atest.TestCase {
217
+ Request : atest.Request {
218
+ API : "http://localhost/foo" ,
219
+ },
220
+ Expect : atest.Response {
221
+ BodyFieldsExpect : map [string ]string {
222
+ "items[1]" : "bar" ,
223
+ },
224
+ },
225
+ },
226
+ prepare : func () {
227
+ gock .New ("http://localhost" ).
228
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
229
+ },
230
+ verify : func (t * testing.T , output interface {}, err error ) {
231
+ assert .NotNil (t , err )
232
+ assert .Contains (t , err .Error (), "failed to get field" )
233
+ },
234
+ }, {
235
+ name : "verify failed" ,
236
+ testCase : & atest.TestCase {
237
+ Request : atest.Request {
238
+ API : "http://localhost/foo" ,
239
+ },
240
+ Expect : atest.Response {
241
+ Verify : []string {
242
+ "len(items) > 0" ,
243
+ },
244
+ },
245
+ },
246
+ prepare : func () {
247
+ gock .New ("http://localhost" ).
248
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
249
+ },
250
+ verify : func (t * testing.T , output interface {}, err error ) {
251
+ assert .NotNil (t , err )
252
+ assert .Contains (t , err .Error (), "failed to verify" )
253
+ },
254
+ }, {
255
+ name : "failed to compile" ,
256
+ testCase : & atest.TestCase {
257
+ Request : atest.Request {
258
+ API : "http://localhost/foo" ,
259
+ },
260
+ Expect : atest.Response {
261
+ Verify : []string {
262
+ `println("12")` ,
263
+ },
264
+ },
265
+ },
266
+ prepare : func () {
267
+ gock .New ("http://localhost" ).
268
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
269
+ },
270
+ verify : func (t * testing.T , output interface {}, err error ) {
271
+ assert .NotNil (t , err )
272
+ assert .Contains (t , err .Error (), "unknown name println" )
273
+ },
274
+ }, {
275
+ name : "failed to compile" ,
276
+ testCase : & atest.TestCase {
277
+ Request : atest.Request {
278
+ API : "http://localhost/foo" ,
279
+ },
280
+ Expect : atest.Response {
281
+ Verify : []string {
282
+ `1 + 1` ,
283
+ },
284
+ },
285
+ },
286
+ prepare : func () {
287
+ gock .New ("http://localhost" ).
288
+ Get ("/foo" ).Reply (http .StatusOK ).BodyString (`{"items":[]}` )
289
+ },
290
+ verify : func (t * testing.T , output interface {}, err error ) {
291
+ assert .NotNil (t , err )
292
+ assert .Contains (t , err .Error (), "expected bool, but got int" )
293
+ },
294
+ }, {
295
+ name : "wrong API format" ,
296
+ testCase : & atest.TestCase {
297
+ Request : atest.Request {
298
+ API : "ssh://localhost/foo" ,
299
+ Method : "fake,fake" ,
300
+ },
301
+ },
302
+ verify : func (t * testing.T , output interface {}, err error ) {
303
+ assert .NotNil (t , err )
304
+ assert .Contains (t , err .Error (), "invalid method" )
305
+ },
306
+ }, {
307
+ name : "failed to render API" ,
308
+ testCase : & atest.TestCase {
309
+ Request : atest.Request {
310
+ API : "http://localhost/foo/{{.abc}" ,
311
+ },
312
+ },
313
+ verify : func (t * testing.T , output interface {}, err error ) {
314
+ assert .NotNil (t , err )
315
+ assert .Contains (t , err .Error (), "template: api:1:" )
316
+ },
54
317
}}
55
318
for _ , tt := range tests {
56
319
t .Run (tt .name , func (t * testing.T ) {
57
320
defer gock .Clean ()
58
- tt .prepare ()
321
+ if tt .prepare != nil {
322
+ tt .prepare ()
323
+ }
59
324
output , err := RunTestCase (tt .testCase , tt .ctx )
60
325
tt .verify (t , output , err )
61
326
})
62
327
}
63
328
}
329
+
330
+ //go:embed testdata/generic_response.json
331
+ var genericBody string
0 commit comments