@@ -7,23 +7,34 @@ import * as packageJson from '../package.json';
7
7
8
8
describe ( packageJson . name , ( ) => {
9
9
let app = null ;
10
+ let arrayCoercedApp = null ;
10
11
11
12
before ( async ( ) => {
12
13
// Set up the express app
13
14
const apiSpec = path . join ( 'test' , 'resources' , 'coercion.yaml' ) ;
15
+ const routes = express
16
+ . Router ( )
17
+ . post ( `/pets` , ( req , res ) => res . json ( req . body ) )
18
+ . post ( `/pets_string_boolean` , ( req , res ) => res . json ( req . body ) )
19
+ . get ( `/pets_as_array_parameter` , ( req , res ) => res . json ( req . query ) ) ;
20
+
14
21
app = await createApp ( { apiSpec } , 3005 , ( app ) =>
15
- app . use (
16
- `${ app . basePath } /coercion` ,
17
- express
18
- . Router ( )
19
- . post ( `/pets` , ( req , res ) => res . json ( req . body ) )
20
- . post ( `/pets_string_boolean` , ( req , res ) => res . json ( req . body ) ) ,
21
- ) ,
22
+ app . use ( `${ app . basePath } /coercion` , routes ) ,
23
+ ) ;
24
+ arrayCoercedApp = await createApp (
25
+ { apiSpec, validateRequests : { coerceTypes : 'array' } } ,
26
+ 3006 ,
27
+ ( appWithCoerceTypes ) =>
28
+ appWithCoerceTypes . use (
29
+ `${ appWithCoerceTypes . basePath } /coercion` ,
30
+ routes ,
31
+ ) ,
22
32
) ;
23
33
} ) ;
24
34
25
35
after ( ( ) => {
26
36
app . server . close ( ) ;
37
+ arrayCoercedApp . server . close ( ) ;
27
38
} ) ;
28
39
29
40
it ( 'should return 400 since is_cat is passed as string not boolean' , async ( ) =>
@@ -35,7 +46,9 @@ describe(packageJson.name, () => {
35
46
} )
36
47
. expect ( 400 )
37
48
. then ( ( r ) => {
38
- expect ( r . body . message ) . to . contain ( 'request/body/is_cat must be boolean' ) ;
49
+ expect ( r . body . message ) . to . contain (
50
+ 'request/body/is_cat must be boolean' ,
51
+ ) ;
39
52
} ) ) ;
40
53
41
54
it ( 'should return 400 when age is passed as string, but number is expected' , async ( ) =>
@@ -102,4 +115,15 @@ describe(packageJson.name, () => {
102
115
. then ( ( r ) => {
103
116
expect ( r . body . message ) . to . contain ( 'request/body/is_cat must be string' ) ;
104
117
} ) ) ;
118
+
119
+ it ( 'should return 200 when names is a string and coerce names to be an array' , async ( ) =>
120
+ request ( arrayCoercedApp )
121
+ . get ( `${ arrayCoercedApp . basePath } /coercion/pets_as_array_parameter` )
122
+ . query ( {
123
+ filter : { names : 'test' } ,
124
+ } )
125
+ . expect ( 200 )
126
+ . then ( ( r ) => {
127
+ expect ( r . text ) . to . equal ( '{"filter":{"names":["test"]}}' ) ;
128
+ } ) ) ;
105
129
} ) ;
0 commit comments