1
1
const { assert } = require ( 'chai' ) ;
2
2
const { validate } = require ( '../../lib/validate' ) ;
3
3
4
+ const isValid = ( result , expectedValid = true ) => {
5
+ it ( `sets "isValid" to ${ JSON . stringify ( expectedValid ) } ` , ( ) => {
6
+ assert . propertyVal ( result , 'isValid' , expectedValid ) ;
7
+ } ) ;
8
+ } ;
9
+
4
10
const validator = ( obj , expected ) => {
5
11
it ( `has "${ expected } " validator` , ( ) => {
6
12
assert . propertyVal ( obj , 'validator' , expected ) ;
@@ -32,26 +38,22 @@ describe('validate', () => {
32
38
} ;
33
39
const result = validate ( request , request ) ;
34
40
35
- it ( 'returns validation result object' , ( ) => {
36
- assert . isObject ( result ) ;
37
- } ) ;
38
-
39
- it ( 'has "isValid" set to true' , ( ) => {
40
- assert . propertyVal ( result , 'isValid' , true ) ;
41
- } ) ;
41
+ isValid ( result ) ;
42
42
43
43
it ( 'contains all validatable keys' , ( ) => {
44
44
assert . hasAllKeys ( result . fields , [ 'method' , 'headers' , 'body' ] ) ;
45
45
} ) ;
46
46
47
47
describe ( 'method' , ( ) => {
48
+ isValid ( result . fields . method ) ;
48
49
validator ( result . fields . method , null ) ;
49
50
expectedType ( result . fields . method , 'text/vnd.apiary.method' ) ;
50
51
realType ( result . fields . method , 'text/vnd.apiary.method' ) ;
51
52
noErrors ( result . fields . method ) ;
52
53
} ) ;
53
54
54
55
describe ( 'headers' , ( ) => {
56
+ isValid ( result . fields . headers ) ;
55
57
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
56
58
expectedType (
57
59
result . fields . headers ,
@@ -65,6 +67,7 @@ describe('validate', () => {
65
67
} ) ;
66
68
67
69
describe ( 'body' , ( ) => {
70
+ isValid ( result . fields . body ) ;
68
71
validator ( result . fields . body , 'JsonExample' ) ;
69
72
expectedType ( result . fields . body , 'application/json' ) ;
70
73
realType ( result . fields . body , 'application/json' ) ;
@@ -88,19 +91,14 @@ describe('validate', () => {
88
91
}
89
92
) ;
90
93
91
- it ( 'returns validation result object' , ( ) => {
92
- assert . isObject ( result ) ;
93
- } ) ;
94
-
95
- it ( 'has "isValid" set to false' , ( ) => {
96
- assert . propertyVal ( result , 'isValid' , false ) ;
97
- } ) ;
94
+ isValid ( result , false ) ;
98
95
99
96
it ( 'contains all validatable keys' , ( ) => {
100
97
assert . hasAllKeys ( result . fields , [ 'method' , 'headers' , 'body' ] ) ;
101
98
} ) ;
102
99
103
100
describe ( 'method' , ( ) => {
101
+ isValid ( result . fields . method , false ) ;
104
102
validator ( result . fields . method , null ) ;
105
103
expectedType ( result . fields . method , 'text/vnd.apiary.method' ) ;
106
104
realType ( result . fields . method , 'text/vnd.apiary.method' ) ;
@@ -121,6 +119,7 @@ describe('validate', () => {
121
119
} ) ;
122
120
123
121
describe ( 'headers' , ( ) => {
122
+ isValid ( result . fields . headers ) ;
124
123
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
125
124
expectedType (
126
125
result . fields . headers ,
@@ -134,6 +133,7 @@ describe('validate', () => {
134
133
} ) ;
135
134
136
135
describe ( 'body' , ( ) => {
136
+ isValid ( result . fields . body , false ) ;
137
137
validator ( result . fields . body , 'JsonExample' ) ;
138
138
expectedType ( result . fields . body , 'application/json' ) ;
139
139
realType ( result . fields . body , 'application/json' ) ;
@@ -177,13 +177,15 @@ describe('validate', () => {
177
177
} ) ;
178
178
179
179
describe ( 'statusCode' , ( ) => {
180
+ isValid ( result . fields . statusCode ) ;
180
181
validator ( result . fields . statusCode , 'TextDiff' ) ;
181
182
expectedType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
182
183
realType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
183
184
noErrors ( result . fields . statusCode ) ;
184
185
} ) ;
185
186
186
187
describe ( 'headers' , ( ) => {
188
+ isValid ( result . fields . headers ) ;
187
189
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
188
190
expectedType (
189
191
result . fields . headers ,
@@ -197,6 +199,7 @@ describe('validate', () => {
197
199
} ) ;
198
200
199
201
describe ( 'body' , ( ) => {
202
+ isValid ( result . fields . body ) ;
200
203
validator ( result . fields . body , 'JsonExample' ) ;
201
204
expectedType ( result . fields . body , 'application/json' ) ;
202
205
realType ( result . fields . body , 'application/json' ) ;
@@ -219,19 +222,14 @@ describe('validate', () => {
219
222
} ;
220
223
const result = validate ( expectedResponse , realResponse ) ;
221
224
222
- it ( 'returns validation result object' , ( ) => {
223
- assert . isObject ( result ) ;
224
- } ) ;
225
-
226
- it ( 'has "isValid" as false' , ( ) => {
227
- assert . propertyVal ( result , 'isValid' , false ) ;
228
- } ) ;
225
+ isValid ( result , false ) ;
229
226
230
227
it ( 'contains all validatable keys' , ( ) => {
231
228
assert . hasAllKeys ( result . fields , [ 'statusCode' , 'headers' ] ) ;
232
229
} ) ;
233
230
234
231
describe ( 'statusCode' , ( ) => {
232
+ isValid ( result . fields . statusCode , false ) ;
235
233
validator ( result . fields . statusCode , 'TextDiff' ) ;
236
234
expectedType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
237
235
realType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
@@ -252,6 +250,7 @@ describe('validate', () => {
252
250
} ) ;
253
251
254
252
describe ( 'headers' , ( ) => {
253
+ isValid ( result . fields . headers , false ) ;
255
254
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
256
255
expectedType (
257
256
result . fields . headers ,
@@ -291,26 +290,22 @@ describe('validate', () => {
291
290
}
292
291
) ;
293
292
294
- it ( 'returns validation result object' , ( ) => {
295
- assert . isObject ( result ) ;
296
- } ) ;
297
-
298
- it ( 'has "isValid" as false' , ( ) => {
299
- assert . propertyVal ( result , 'isValid' , false ) ;
300
- } ) ;
293
+ isValid ( result , false ) ;
301
294
302
295
it ( 'contains all validatable keys' , ( ) => {
303
296
assert . hasAllKeys ( result . fields , [ 'statusCode' , 'headers' ] ) ;
304
297
} ) ;
305
298
306
299
describe ( 'statusCode' , ( ) => {
300
+ isValid ( result . fields . statusCode ) ;
307
301
validator ( result . fields . statusCode , 'TextDiff' ) ;
308
302
expectedType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
309
303
realType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
310
304
noErrors ( result . fields . statusCode ) ;
311
305
} ) ;
312
306
313
307
describe ( 'headers' , ( ) => {
308
+ isValid ( result . fields . headers , false ) ;
314
309
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
315
310
expectedType (
316
311
result . fields . headers ,
@@ -348,41 +343,46 @@ describe('validate', () => {
348
343
describe ( 'always validates expected properties' , ( ) => {
349
344
const result = validate (
350
345
{
346
+ method : 'POST' ,
351
347
statusCode : 200 ,
352
348
headers : {
353
349
'Content-Type' : 'application/json'
354
350
} ,
355
351
body : '{ "foo": "bar" }'
356
352
} ,
357
353
{
358
- body : 'doe '
354
+ method : 'PUT '
359
355
}
360
356
) ;
361
357
362
- it ( 'has "isValid" as false' , ( ) => {
363
- assert . propertyVal ( result , 'isValid' , false ) ;
364
- } ) ;
358
+ isValid ( result , false ) ;
365
359
366
360
it ( 'contains all validatable keys' , ( ) => {
367
- assert . hasAllKeys ( result . fields , [ 'statusCode' , 'headers' , 'body' ] ) ;
361
+ assert . hasAllKeys ( result . fields , [
362
+ 'method' ,
363
+ 'statusCode' ,
364
+ 'headers' ,
365
+ 'body'
366
+ ] ) ;
368
367
} ) ;
369
368
370
369
describe ( 'for properties present in both expected and real' , ( ) => {
371
- describe ( 'body' , ( ) => {
372
- validator ( result . fields . body , null ) ;
373
- expectedType ( result . fields . body , 'application/json' ) ;
374
- realType ( result . fields . body , 'text/plain' ) ;
370
+ describe ( 'method' , ( ) => {
371
+ isValid ( result . fields . method , false ) ;
372
+ validator ( result . fields . method , null ) ;
373
+ expectedType ( result . fields . method , 'text/vnd.apiary.method' ) ;
374
+ realType ( result . fields . method , 'text/vnd.apiary.method' ) ;
375
375
376
376
describe ( 'produces an error' , ( ) => {
377
377
it ( 'exactly one error' , ( ) => {
378
- assert . lengthOf ( result . fields . body . errors , 1 ) ;
378
+ assert . lengthOf ( result . fields . method . errors , 1 ) ;
379
379
} ) ;
380
380
381
381
it ( 'has explanatory message' , ( ) => {
382
382
assert . propertyVal (
383
- result . fields . body . errors [ 0 ] ,
383
+ result . fields . method . errors [ 0 ] ,
384
384
'message' ,
385
- `Can't validate real media type 'text/plain' against expected media type 'application/json'.`
385
+ 'Expected "method" field to equal "POST", but got "PUT".'
386
386
) ;
387
387
} ) ;
388
388
} ) ;
@@ -391,6 +391,7 @@ describe('validate', () => {
391
391
392
392
describe ( 'for properties present in expected, but not in real' , ( ) => {
393
393
describe ( 'statusCode' , ( ) => {
394
+ isValid ( result . fields . statusCode , false ) ;
394
395
validator ( result . fields . statusCode , 'TextDiff' ) ;
395
396
expectedType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
396
397
realType ( result . fields . statusCode , 'text/vnd.apiary.status-code' ) ;
@@ -411,6 +412,7 @@ describe('validate', () => {
411
412
} ) ;
412
413
413
414
describe ( 'headers' , ( ) => {
415
+ isValid ( result . fields . headers , false ) ;
414
416
validator ( result . fields . headers , 'HeadersJsonExample' ) ;
415
417
expectedType (
416
418
result . fields . headers ,
@@ -435,6 +437,27 @@ describe('validate', () => {
435
437
} ) ;
436
438
} ) ;
437
439
} ) ;
440
+
441
+ describe ( 'body' , ( ) => {
442
+ isValid ( result . fields . body , false ) ;
443
+ validator ( result . fields . body , null ) ;
444
+ expectedType ( result . fields . body , 'application/json' ) ;
445
+ realType ( result . fields . body , 'text/plain' ) ;
446
+
447
+ describe ( 'produces an error' , ( ) => {
448
+ it ( 'exactly one error' , ( ) => {
449
+ assert . lengthOf ( result . fields . body . errors , 1 ) ;
450
+ } ) ;
451
+
452
+ it ( 'has explanatory message' , ( ) => {
453
+ assert . propertyVal (
454
+ result . fields . body . errors [ 0 ] ,
455
+ 'message' ,
456
+ 'Expected "body" of "application/json" media type, but actual "body" is missing.'
457
+ ) ;
458
+ } ) ;
459
+ } ) ;
460
+ } ) ;
438
461
} ) ;
439
462
} ) ;
440
463
} ) ;
0 commit comments