1- " use strict" ;
1+ ' use strict'
22
3- const Fastify = require ( " fastify" ) ;
4- const FormData = require ( " form-data" ) ;
5- const http = require ( " node:http" ) ;
6- const multipart = require ( ".." ) ;
7- const { once } = require ( " node:events" ) ;
8- const fs = require ( " node:fs" ) ;
9- const path = require ( " node:path" ) ;
10- const test = require ( " node:test" ) ;
3+ const Fastify = require ( ' fastify' )
4+ const FormData = require ( ' form-data' )
5+ const http = require ( ' node:http' )
6+ const multipart = require ( '..' )
7+ const { once } = require ( ' node:events' )
8+ const fs = require ( ' node:fs' )
9+ const path = require ( ' node:path' )
10+ const test = require ( ' node:test' )
1111
12- const filePath = path . join ( __dirname , " ../README.md" ) ;
12+ const filePath = path . join ( __dirname , ' ../README.md' )
1313
1414test ( 'show modify the generated schema' , async t => {
1515 t . plan ( 6 )
1616
1717 const fastify = Fastify ( {
1818 ajv : {
19- plugins : [ multipart . ajvFilePlugin ] ,
20- } ,
21- } ) ;
19+ plugins : [ multipart . ajvFilePlugin ]
20+ }
21+ } )
2222
23- t . after ( ( ) => fastify . close ( ) ) ;
23+ t . after ( ( ) => fastify . close ( ) )
2424
25- await fastify . register ( multipart , { attachFieldsToBody : true } ) ;
26- await fastify . register ( require ( " @fastify/swagger" ) , {
27- mode : " dynamic" ,
25+ await fastify . register ( multipart , { attachFieldsToBody : true } )
26+ await fastify . register ( require ( ' @fastify/swagger' ) , {
27+ mode : ' dynamic' ,
2828
2929 openapi : {
30- openapi : " 3.1.0" ,
31- } ,
32- } ) ;
30+ openapi : ' 3.1.0'
31+ }
32+ } )
3333
3434 fastify . post (
35- "/" ,
35+ '/' ,
3636 {
3737 schema : {
38- operationId : " test" ,
39- consumes : [ " multipart/form-data" ] ,
38+ operationId : ' test' ,
39+ consumes : [ ' multipart/form-data' ] ,
4040 body : {
41- type : " object" ,
41+ type : ' object' ,
4242 properties : {
43- field : { isFile : true } ,
44- } ,
45- } ,
46- } ,
43+ field : { isFile : true }
44+ }
45+ }
46+ }
4747 } ,
4848 async function ( _req , reply ) {
49- reply . code ( 200 ) . send ( ) ;
50- } ,
51- ) ;
49+ reply . code ( 200 ) . send ( )
50+ }
51+ )
5252
53- await fastify . ready ( ) ;
53+ await fastify . ready ( )
5454
5555 const { post } = fastify . swagger ( ) . paths [ '/' ]
5656
@@ -63,52 +63,52 @@ test('show modify the generated schema', async t => {
6363 } )
6464 t . assert . strictEqual ( post . responses [ 200 ] . description , 'Default Response' )
6565
66- await fastify . listen ( { port : 0 } ) ;
66+ await fastify . listen ( { port : 0 } )
6767
6868 // request without file
6969 {
70- const form = new FormData ( ) ;
70+ const form = new FormData ( )
7171 const req = http . request ( {
72- protocol : " http:" ,
73- hostname : " localhost" ,
72+ protocol : ' http:' ,
73+ hostname : ' localhost' ,
7474 port : fastify . server . address ( ) . port ,
75- path : "/" ,
75+ path : '/' ,
7676 headers : form . getHeaders ( ) ,
77- method : " POST" ,
78- } ) ;
79-
80- form . append ( " field" , JSON . stringify ( { } ) , {
81- contentType : " application/json" ,
82- } ) ;
83- form . pipe ( req ) ;
84-
85- const [ res ] = await once ( req , " response" ) ;
86- res . resume ( ) ;
87- await once ( res , " end" ) ;
88- t . assert . strictEqual ( res . statusCode , 400 ) ; // body/field should be a file
77+ method : ' POST'
78+ } )
79+
80+ form . append ( ' field' , JSON . stringify ( { } ) , {
81+ contentType : ' application/json'
82+ } )
83+ form . pipe ( req )
84+
85+ const [ res ] = await once ( req , ' response' )
86+ res . resume ( )
87+ await once ( res , ' end' )
88+ t . assert . strictEqual ( res . statusCode , 400 ) // body/field should be a file
8989 }
9090
9191 // request with file
9292 {
93- const form = new FormData ( ) ;
93+ const form = new FormData ( )
9494 const req = http . request ( {
95- protocol : " http:" ,
96- hostname : " localhost" ,
95+ protocol : ' http:' ,
96+ hostname : ' localhost' ,
9797 port : fastify . server . address ( ) . port ,
98- path : "/" ,
98+ path : '/' ,
9999 headers : form . getHeaders ( ) ,
100- method : " POST" ,
101- } ) ;
102-
103- form . append ( " field" , fs . createReadStream ( filePath ) , {
104- contentType : " multipart/form-data" ,
105- } ) ;
106- form . pipe ( req ) ;
107-
108- const [ res ] = await once ( req , " response" ) ;
109- res . resume ( ) ;
110- await once ( res , " end" ) ;
111- t . assert . strictEqual ( res . statusCode , 200 ) ;
100+ method : ' POST'
101+ } )
102+
103+ form . append ( ' field' , fs . createReadStream ( filePath ) , {
104+ contentType : ' multipart/form-data'
105+ } )
106+ form . pipe ( req )
107+
108+ const [ res ] = await once ( req , ' response' )
109+ res . resume ( )
110+ await once ( res , ' end' )
111+ t . assert . strictEqual ( res . statusCode , 200 )
112112 }
113- t . assert . ok ( " res ended successfully" ) ;
114- } ) ;
113+ t . assert . ok ( ' res ended successfully' )
114+ } )
0 commit comments