-
Notifications
You must be signed in to change notification settings - Fork 24
/
parameters.test.js
373 lines (333 loc) · 11.5 KB
/
parameters.test.js
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
'use strict'
var _ = require('lodash')
describe('parameters', function () {
it('ignores optional parameters that are missing', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.end(done)
})
it('replaces path parameters with provided variables', function (done) {
hippie(app, swaggerSchema)
.get('/foos/{fooId}')
.pathParams({ fooId: data.firstFoo.id })
.end(function (err, res) {
expect(err).to.be.undefined()
expect(res.req.path).to.equal('/foos/' + data.firstFoo.id)
done()
})
})
it('replaces query string variables', function (done) {
var limit = 10
var offset = 2
hippie(app, swaggerSchema)
.get('/foos')
.qs({ limit: limit, offset: offset })
.end(function (err, res) {
expect(err).to.be.undefined()
expect(res.req.path).to.equal('/foos?limit=' + limit + '&offset=' + offset)
done()
})
})
describe('header variables', function () {
it('errors if the header is required', function () {
var headerSchema = cloneSwagger(swaggerSchema)
// set X-Total-Count to be required for this test
headerSchema.paths['/foos'].get.parameters.filter(function (param) {
return param.name === 'X-Total-Count'
})[0].required = true
expect(hippie(app, headerSchema)
.get('/foos')
.end()
).to.be.rejectedWith(/Missing required parameter in header: X-Total-Count/)
})
it('allows multiple headers to be given', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.headers({ 'X-Total-Count': 1, 'New-Header': 0 })
.end(function (err, res) {
expect(err).to.be.undefined()
expect(res.request.headers['X-Total-Count']).to.exist()
expect(res.request.headers['New-Header']).to.exist()
done()
})
})
it('replaces header variables', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.header('X-Total-Count', 1)
.end(function (err, res) {
expect(err).to.be.undefined()
expect(res.request.headers['X-Total-Count']).to.exist()
expect(res.request.headers['X-Total-Count']).to.equal(1)
done()
})
})
})
describe('forms', function () {
describe('form-data', function () {
it('works if formData is set to type "file"', function (done) {
var file = 'Content-Disposition: form-data; name="uploadedFile"'
hippie(app, swaggerSchema)
.header('Content-Type', 'multipart/form-data')
.send(file)
.post('/foos/{fooId}')
.pathParams({ fooId: data.firstFoo.id })
.end(done)
})
describe('required file', function () {
var formSchema
before(function () {
formSchema = cloneSwagger(swaggerSchema)
// set parameter to be required for this test
formSchema.paths['/foos/{fooId}'].post.parameters.filter(function (param) {
return param.name === 'uploadedFile'
})[0].required = true
})
it('errors if file is required & missing header and body', function () {
expect(hippie(app, formSchema)
.post('/foos/{fooId}')
.pathParams({ fooId: data.firstFoo.id })
.end()
).to.be.rejectedWith(/Missing required parameter in formData: uploadedFile/)
})
it('errors if file is required & missing body', function () {
expect(hippie(app, formSchema)
.header('Content-Type', 'multipart/form-data')
.post('/foos/{fooId}')
.pathParams({ fooId: data.firstFoo.id })
.end()
).to.be.rejectedWith(/Missing required parameter in formData: uploadedFile/)
})
})
})
describe('urlencoded', function () {
it('works if formData is optional & not provided', function (done) {
hippie(app, swaggerSchema)
.form()
.get('/foos')
.end(done)
})
it('works if formData is required & present', function (done) {
// set formMetadata to be required for this test
var formSchema = cloneSwagger(swaggerSchema)
formSchema.paths['/foos'].get.parameters.filter(function (param) {
return param.name === 'formMetadata'
})[0].required = true
hippie(app, formSchema)
.form()
.send({ formMetadata: 'formMetadataValue' })
.get('/foos')
.end(done)
})
it('errors if formData is required & missing', function () {
var formSchema = cloneSwagger(swaggerSchema)
// set parameter to be required for this test
formSchema.paths['/foos'].get.parameters.filter(function (param) {
return param.name === 'formMetadata'
})[0].required = true
expect(
hippie(app, formSchema)
.form()
.get('/foos')
.end()
).to.be.rejectedWith(/Missing required parameter in formData: formMetadata/)
})
})
})
describe('integers', function () {
describe('when using get', function () {
it('when requesting with valid integers, validation is ok', function (done) {
hippie(app, swaggerSchema)
.get('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end(done)
})
describe('int32', function () {
it('when requesting with valid integer, validation is ok', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.qs({ int32: 1 })
.end(done)
})
})
describe('int64', function () {
it('when requesting with valid integer, validation is ok', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.qs({ int64: 2147483647 + 1 })
.end(done)
})
})
describe('floats', function () {
it('when requesting with valid float, validation is ok', function (done) {
hippie(app, swaggerSchema)
.get('/foos')
.qs({ float: 1.1211 })
.end(done)
})
})
it('when requesting with non-integer values, validation is rejected', function (done) {
try {
hippie(app, swaggerSchema)
.get('/integerTest/{fooId}')
.pathParams({ fooId: '137' })
.end(done)
} catch (e) {
expect(e).to.match(/Invalid format for parameter {fooId}/)
done()
}
})
})
describe('when using delete', function () {
it('when requesting with valid integers, validation is ok', function (done) {
hippie(app, swaggerSchema)
.del('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end(done)
})
it('when requesting with non-integer values, validation is rejected', function (done) {
hippie(app, swaggerSchema)
.del('/integerTest/{fooId}')
.pathParams({ fooId: 'c' })
.end()
.catch(function (err) {
expect(err).to.match(/Invalid format for parameter {fooId}/)
done()
})
})
})
describe('when using post', function () {
it('when sending valid integers, validation is ok', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 1
})
.post('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end(done)
})
it('when sending non-integer values in path, validation is rejected', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 1
})
.post('/integerTest/{fooId}')
.pathParams({ fooId: 'c' })
.end()
.catch(function (err) {
expect(err).to.match(/Invalid format for parameter {fooId}/)
done()
})
})
it('when sending non-integer values in formData, validation is rejected', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 'c'
})
.post('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end()
.catch(function (err) {
expect(err).to.match(/Invalid format for parameter {barId}/)
done()
})
})
})
describe('when using patch', function () {
it('when sending valid integers, validation is ok', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 1
})
.patch('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end(done)
})
it('when sending non-integer values in path, validation is rejected', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 1
})
.patch('/integerTest/{fooId}')
.pathParams({ fooId: 'c' })
.end()
.catch(function (err) {
expect(err).to.match(/Invalid format for parameter {fooId}/)
done()
})
})
it('when sending non-integer values in formData, validation is rejected', function (done) {
hippie(app, swaggerSchema)
.form()
.send({
barId: 'c'
})
.patch('/integerTest/{fooId}')
.pathParams({ fooId: 137 })
.end()
.catch(function (err) {
expect(err).to.match(/Invalid format for parameter {barId}/)
done()
})
})
})
})
describe('errors', function () {
it('when a required parameter is missing', function () {
expect(hippie(app, swaggerSchema)
.get('/foos/{fooId}')
.end()
).to.be.rejectedWith(/Missing required parameter in path: fooId/)
})
it('when a parameter fails json-schema validation', function () {
expect(
hippie(app, swaggerSchema)
.get('/foos/{fooId}')
.pathParams({ fooId: 45 })
.end()
).to.be.rejectedWith(/Invalid format for parameter {fooId}/)
})
})
describe('settings', function () {
it('when validateParameterSchema is off, it does not error if parameter fails json-schema validation', function () {
return expect(hippie(app, swaggerSchema, { validateParameterSchema: false })
.get('/foos/{fooId}')
.pathParams({ fooId: 45 })
.end()
).to.be.fulfilled()
})
it('when validateRequiredParameters is off, it does not error if required parameter is not provided', function (done) {
var schema = _.cloneDeep(swaggerSchema)
schema.paths['/foos'].get.parameters.forEach(function (p) {
if (p.name === 'limit') p.required = true
})
hippie(app, schema, { validateRequiredParameters: false })
.get('/foos')
.end(done)
})
})
describe('methods parameters override path parameters', function () {
it('rejects when parameter fails json-schema validation', function () {
expect(hippie(app, swaggerSchema)
.patch('/foos/{fooId}')
.pathParams({ fooId: data.firstFoo.id }) // uuid valid at path level, but overriden to integer at method level
.end()
).to.be.rejectedWith(/Invalid format for parameter {fooId}/)
})
it('accepts valid method parameters', function (done) {
hippie(app, swaggerSchema)
.patch('/foos/{fooId}')
.pathParams({ fooId: 45 })
.end(function (err, res) {
expect(err).to.be.undefined()
expect(res.req.path).to.equal('/foos/' + 45)
done()
})
})
})
})