1- import { strictEqual , ok } from 'node:assert' ;
2- import { createServer , request } from 'node:http' ;
3- import formidable , { errors } from '../../src/index.js' ;
1+ import { ok , strictEqual } from 'node:assert' ;
2+ import { createServer } from 'node:http' ;
43import test from 'node:test' ;
5-
6- const PORT = 13539 ;
4+ import formidable , { errors } from '../../src/index.js' ;
75
86const isPromise = ( x ) => {
9- return x && typeof x === `object` && typeof x . then === `function` ;
7+ return x && typeof x === `object` && typeof x . then === `function` ;
108} ;
119
12- test ( 'parse returns promise if no callback is provided' , ( t , done ) => {
13- const server = createServer ( ( req , res ) => {
10+ let server ;
11+ let port = 13540 ;
12+
13+ test . beforeEach ( ( ) => {
14+ // Increment port to avoid conflicts between tests
15+ port += 1 ;
16+ server = createServer ( ) ;
17+ } ) ;
18+
19+ test . afterEach ( ( ) => {
20+ return new Promise ( ( resolve ) => {
21+ if ( server . listening ) {
22+ server . close ( ( ) => resolve ( ) ) ;
23+ } else {
24+ resolve ( ) ;
25+ }
26+ } ) ;
27+ } ) ;
28+
29+ test ( 'parse returns promise if no callback is provided' , async ( t ) => {
30+ server . on ( 'request' , ( req , res ) => {
1431 const form = formidable ( ) ;
1532
1633 const promise = form . parse ( req ) ;
@@ -19,15 +36,16 @@ test('parse returns promise if no callback is provided', (t,done) => {
1936 ok ( typeof fields === 'object' ) ;
2037 ok ( typeof files === 'object' ) ;
2138 res . writeHead ( 200 ) ;
22- res . end ( "ok" )
39+ res . end ( "ok" ) ;
2340 } ) . catch ( e => {
24- done ( e )
25- } )
41+ res . writeHead ( 500 ) ;
42+ res . end ( String ( e ) ) ;
43+ } ) ;
2644 } ) ;
2745
28- server . listen ( PORT , ( ) => {
29- const chosenPort = server . address ( ) . port ;
30- const body = `----13068458571765726332503797717\r
46+ await new Promise ( resolve => server . listen ( port , resolve ) ) ;
47+
48+ const body = `----13068458571765726332503797717\r
3149Content-Disposition: form-data; name="title"\r
3250\r
3351a\r
4462\r
4563----13068458571765726332503797717--\r
4664` ;
47- fetch ( String ( new URL ( `http:localhost:${ chosenPort } /` ) ) , {
48- method : 'POST' ,
49-
50- headers : {
51- 'Content-Length' : body . length ,
52- Host : `localhost:${ chosenPort } ` ,
53- 'Content-Type' : 'multipart/form-data; boundary=--13068458571765726332503797717' ,
54- } ,
55- body
56- } ) . then ( res => {
57- strictEqual ( res . status , 200 ) ;
58- server . close ( ) ;
59- done ( ) ;
60- } ) ;
61-
65+
66+ const res = await fetch ( String ( new URL ( `http:localhost:${ port } /` ) ) , {
67+ method : 'POST' ,
68+ headers : {
69+ 'Content-Length' : body . length ,
70+ Host : `localhost:${ port } ` ,
71+ 'Content-Type' : 'multipart/form-data; boundary=--13068458571765726332503797717' ,
72+ } ,
73+ body
6274 } ) ;
75+
76+ strictEqual ( res . status , 200 ) ;
6377} ) ;
6478
65- test ( 'parse rejects with promise if it fails' , ( t , done ) => {
66- const server = createServer ( ( req , res ) => {
79+ test ( 'parse rejects with promise if it fails' , async ( t ) => {
80+ server . on ( 'request' , ( req , res ) => {
6781 const form = formidable ( { minFileSize : 10 ** 6 } ) ; // create condition to fail
6882
6983 const promise = form . parse ( req ) ;
7084 strictEqual ( isPromise ( promise ) , true ) ;
7185 promise . then ( ( ) => {
72- done ( 'should have failed' )
86+ res . writeHead ( 500 ) ;
87+ res . end ( 'should have failed' ) ;
7388 } ) . catch ( e => {
7489 res . writeHead ( e . httpCode ) ;
7590 strictEqual ( e . code , errors . smallerThanMinFileSize ) ;
76- res . end ( String ( e ) )
77- } )
91+ res . end ( String ( e ) ) ;
92+ } ) ;
7893 } ) ;
7994
80- server . listen ( PORT , ( ) => {
81- const chosenPort = server . address ( ) . port ;
82- const body = `----13068458571765726332503797717\r
95+ await new Promise ( resolve => server . listen ( port , resolve ) ) ;
96+
97+ const body = `----13068458571765726332503797717\r
8398Content-Disposition: form-data; name="title"\r
8499\r
85100a\r
96111\r
97112----13068458571765726332503797717--\r
98113` ;
99- fetch ( String ( new URL ( `http:localhost:${ chosenPort } /` ) ) , {
100- method : 'POST' ,
101-
102- headers : {
103- 'Content-Length' : body . length ,
104- Host : `localhost:${ chosenPort } ` ,
105- 'Content-Type' : 'multipart/form-data; boundary=--13068458571765726332503797717' ,
106- } ,
107- body
108- } ) . then ( res => {
109- strictEqual ( res . status , 400 ) ;
110- server . close ( ) ;
111- done ( ) ;
112- } ) ;
113-
114+
115+ const res = await fetch ( String ( new URL ( `http:localhost:${ port } /` ) ) , {
116+ method : 'POST' ,
117+ headers : {
118+ 'Content-Length' : body . length ,
119+ Host : `localhost:${ port } ` ,
120+ 'Content-Type' : 'multipart/form-data; boundary=--13068458571765726332503797717' ,
121+ } ,
122+ body
114123 } ) ;
124+
125+ strictEqual ( res . status , 400 ) ;
115126} ) ;
0 commit comments