File tree 3 files changed +39
-1
lines changed 3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ const { baseURL } = useRuntimeConfig().app;
4
4
5
5
const withBase = ( p ) => baseURL + p . replace ( / ^ \/ / , "" ) ;
6
6
7
- const tests = [ "api" , "form-data" ] ;
7
+ const tests = [ "api" , "form-data" , "multipart-form-data" ] ;
8
8
9
9
const manualTests = [ "env" , "node-compat" , "headers" ] ;
10
10
Original file line number Diff line number Diff line change
1
+ // https://github.com/nitrojs/nitro/issues/1721
2
+ export default defineTestHandler (
3
+ "multipart-form-data" ,
4
+ async ( event ) => {
5
+ const formData = await readMultipartFormData ( event ) ;
6
+ const name = formData
7
+ . find ( ( partData ) => partData . name === "name" )
8
+ ?. data . toString ( ) ;
9
+ const rawFile = formData . find ( ( partData ) => partData . name === "file" ) ;
10
+
11
+ return {
12
+ data : {
13
+ name,
14
+ fileName : rawFile . filename ,
15
+ fileType : rawFile . type ,
16
+ fileSize : rawFile . data . byteLength ,
17
+ } ,
18
+ } ;
19
+ } ,
20
+ async ( assert ) => {
21
+ const formData = new FormData ( ) ;
22
+ formData . append ( "name" , "John Doe" ) ;
23
+
24
+ const rawFile = await fetch ( "/data.pdf" ) . then ( ( res ) => res . arrayBuffer ( ) ) ;
25
+
26
+ const file = new Blob ( [ rawFile ] , { type : "application/pdf" } ) ;
27
+ formData . append ( "file" , file , "data.pdf" ) ;
28
+
29
+ const res = await fetch ( "" , { method : "POST" , body : formData } ) . then (
30
+ ( res ) => res . json ( ) ,
31
+ ) ;
32
+ assert ( res . data . name === "John Doe" , `Unexpected response: ${ res . data } ` ) ;
33
+ assert (
34
+ res . data . fileSize === rawFile . byteLength ,
35
+ `Unexpected response: ${ res . data } ` ,
36
+ ) ;
37
+ } ,
38
+ ) ;
You can’t perform that action at this time.
0 commit comments