1- import { Multipart , Component } from "../dist/index.js" ;
2- import { expect } from "chai" ;
1+ import { Multipart , Component } from "../dist/index.js" ;
2+ import { expect } from "chai" ;
3+ import { describe } from "mocha" ;
34
45describe ( "Multipart" , function ( ) {
56 describe ( "constructor" , function ( ) {
67 it ( "should initialize with default boundary and mediaType" , function ( ) {
7- const component = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "foo bar" ) ) ;
8+ const component = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "foo bar" ) ) ;
89 const multipart = new Multipart ( [ component ] ) ;
910
1011 expect ( multipart . boundary ) . to . be . an . instanceof ( Uint8Array ) ;
@@ -14,7 +15,7 @@ describe("Multipart", function () {
1415 it ( "should accept a custom boundary and mediaType" , function ( ) {
1516 const boundary = "my-custom-boundary" ;
1617 const mediaType = "Multipart/form-data" ;
17- const component = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "custom content" ) ) ;
18+ const component = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "custom content" ) ) ;
1819 const multipart = new Multipart ( [ component ] , boundary , mediaType ) ;
1920
2021 expect ( new TextDecoder ( ) . decode ( multipart . boundary ) ) . to . equal ( boundary ) ;
@@ -32,8 +33,8 @@ describe("Multipart", function () {
3233 describe ( "parse" , function ( ) {
3334 it ( "should parse Multipart data correctly" , function ( ) {
3435 const boundary = "my-boundary" ;
35- const component1 = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "Component1 content" ) ) ;
36- const component2 = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "Component2 content" ) ) ;
36+ const component1 = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "Component1 content" ) ) ;
37+ const component2 = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "Component2 content" ) ) ;
3738 const multipart = new Multipart ( [ component1 , component2 ] , boundary ) ;
3839
3940 const multipartBytes = multipart . bytes ( ) ;
@@ -90,10 +91,10 @@ describe("Multipart", function () {
9091
9192 it ( "should handle nested multiparts" , function ( ) {
9293 const components = [
93- new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "foo bar" ) ) ,
94+ new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "foo bar" ) ) ,
9495 new Multipart ( [
95- new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "nested Component 1" ) ) ,
96- new Component ( { "content-type" : "application/json" } , new TextEncoder ( ) . encode ( JSON . stringify ( { foo : "bar" } ) ) )
96+ new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "nested Component 1" ) ) ,
97+ new Component ( { "content-type" : "application/json" } , new TextEncoder ( ) . encode ( JSON . stringify ( { foo : "bar" } ) ) )
9798 ] , "inner-boundary" )
9899 ] ;
99100 const multipart = new Multipart ( components , "outer-boundary" ) ;
@@ -112,7 +113,7 @@ describe("Multipart", function () {
112113 expect ( parsedInnerMultipart . parts [ 0 ] . headers . get ( "content-type" ) ) . to . equal ( "text/plain" ) ;
113114 expect ( new TextDecoder ( ) . decode ( parsedInnerMultipart . parts [ 0 ] . body ) ) . to . equal ( "nested Component 1" ) ;
114115 expect ( parsedInnerMultipart . parts [ 1 ] . headers . get ( "content-type" ) ) . to . equal ( "application/json" ) ;
115- expect ( new TextDecoder ( ) . decode ( parsedInnerMultipart . parts [ 1 ] . body ) ) . to . equal ( JSON . stringify ( { foo : "bar" } ) ) ;
116+ expect ( new TextDecoder ( ) . decode ( parsedInnerMultipart . parts [ 1 ] . body ) ) . to . equal ( JSON . stringify ( { foo : "bar" } ) ) ;
116117 } ) ;
117118
118119 it ( "should handle malformed Multipart data" , function ( ) {
@@ -217,6 +218,47 @@ describe("Multipart", function () {
217218 } ) ;
218219 } ) ;
219220
221+ describe ( "part" , function ( ) {
222+ it ( "should create Multipart from Part" , function ( ) {
223+ const multipart = new Multipart ( [
224+ new Component ( { "content-type" : "text/plain" , "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "foo bar" ) ) ,
225+ new Component ( { } , new TextEncoder ( ) . encode ( "test content" ) )
226+ ] ) ;
227+ const part = new Component ( { "Content-Type" : multipart . headers . get ( "content-type" ) } , multipart . bytes ( ) ) ;
228+
229+ const parsedMultipart = Multipart . part ( part ) ;
230+ expect ( parsedMultipart ) . to . be . an . instanceof ( Multipart ) ;
231+ expect ( parsedMultipart . parts . length ) . to . equal ( 2 ) ;
232+ expect ( parsedMultipart . parts [ 0 ] . headers . get ( "content-type" ) ) . to . equal ( "text/plain" ) ;
233+ expect ( parsedMultipart . parts [ 0 ] . headers . get ( "x-foo" ) ) . to . equal ( "bar" ) ;
234+ expect ( new TextDecoder ( ) . decode ( parsedMultipart . parts [ 0 ] . body ) ) . to . equal ( "foo bar" ) ;
235+ expect ( parsedMultipart . parts [ 1 ] . headers . get ( "content-type" ) ) . to . equal ( null ) ;
236+ expect ( new TextDecoder ( ) . decode ( parsedMultipart . parts [ 1 ] . body ) ) . to . equal ( "test content" ) ;
237+ } ) ;
238+ } ) ;
239+
240+ describe ( "blob" , async function ( ) {
241+ it ( "should create Multipart from Blob with type" , async function ( ) {
242+ const boundary = "example-boundary" ;
243+ const component1 = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "Component1 content" ) ) ;
244+ const component2 = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "Component2 content" ) ) ;
245+ const multipart = new Multipart ( [ component1 , component2 ] , boundary ) ;
246+
247+ const blob = multipart . blob ( ) ;
248+ const parsedMultipart = await Multipart . blob ( blob ) ;
249+
250+ expect ( parsedMultipart ) . to . be . an . instanceof ( Multipart ) ;
251+ expect ( new TextDecoder ( ) . decode ( parsedMultipart . boundary ) ) . to . equal ( boundary ) ;
252+ expect ( parsedMultipart . parts . length ) . to . equal ( 2 ) ;
253+ const part1 = parsedMultipart . parts [ 0 ] ;
254+ expect ( part1 . headers . get ( "x-foo" ) ) . to . equal ( "bar" ) ;
255+ expect ( part1 . body ) . to . deep . equal ( component1 . body ) ;
256+ const part2 = parsedMultipart . parts [ 1 ] ;
257+ expect ( part2 . headers . get ( "content-type" ) ) . to . equal ( "text/plain" ) ;
258+ expect ( part2 . body ) . to . deep . equal ( component2 . body ) ;
259+ } ) ;
260+ } ) ;
261+
220262 describe ( "formData" , function ( ) {
221263 it ( "should correctly create Multipart from FormData" , async function ( ) {
222264 const formData = new FormData ( ) ;
@@ -259,7 +301,7 @@ describe("Multipart", function () {
259301 } ) ;
260302
261303 describe ( "#formData" , function ( ) {
262- it ( "should correctly return the FormData of the Multipart" , async function ( ) {
304+ it ( "should correctly return the FormData of the Multipart" , async function ( ) {
263305 const formData = new FormData ( ) ;
264306 formData . append ( "foo" , "bar" ) ;
265307 formData . append ( "bar" , "baz" ) ;
@@ -278,7 +320,7 @@ describe("Multipart", function () {
278320 expect ( new TextDecoder ( ) . decode ( await file . arrayBuffer ( ) ) ) . to . equal ( "console.log('hello world');" ) ;
279321 } ) ;
280322
281- it ( "should handle empty FormData multipart" , async function ( ) {
323+ it ( "should handle empty FormData multipart" , async function ( ) {
282324 const multipart = await Multipart . formData ( new FormData ( ) ) ;
283325 const formData = multipart . formData ( ) ;
284326 expect ( formData ) . to . be . an . instanceof ( FormData ) ;
@@ -289,7 +331,7 @@ describe("Multipart", function () {
289331 describe ( "#body" , function ( ) {
290332 it ( "should correctly return the body of the Multipart" , function ( ) {
291333 const boundary = "test-boundary" ;
292- const component = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "test body" ) ) ;
334+ const component = new Component ( { "content-type" : "text/plain" } , new TextEncoder ( ) . encode ( "test body" ) ) ;
293335 const multipart = new Multipart ( [ component ] , boundary ) ;
294336
295337 const body = multipart . body ;
@@ -327,7 +369,7 @@ describe("Multipart", function () {
327369 describe ( "#bytes" , function ( ) {
328370 it ( "should correctly return the bytes of the Multipart" , function ( ) {
329371 const boundary = "test-boundary" ;
330- const component = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "test content" ) ) ;
372+ const component = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "test content" ) ) ;
331373 const multipart = new Multipart ( [ component ] , boundary ) ;
332374
333375 const bytes = multipart . bytes ( ) ;
@@ -365,6 +407,19 @@ describe("Multipart", function () {
365407 } ) ;
366408 } ) ;
367409
410+ describe ( "#blob" , async function ( ) {
411+ it ( "should correctly return the blob of the Multipart" , async function ( ) {
412+ const boundary = "test-boundary" ;
413+ const component = new Component ( { "x-foo" : "bar" } , new TextEncoder ( ) . encode ( "test content" ) ) ;
414+ const multipart = new Multipart ( [ component ] , boundary ) ;
415+
416+ const blob = multipart . blob ( ) ;
417+
418+ expect ( blob . type ) . to . equal ( multipart . headers . get ( "content-type" ) ) ;
419+ expect ( await blob . bytes ( ) ) . to . deep . equal ( multipart . bytes ( ) ) ;
420+ } ) ;
421+ } ) ;
422+
368423 describe ( "#headers" , function ( ) {
369424 it ( "should have the Content-Type boundary parameters in quotes as per RFC 2616" , function ( ) {
370425 expect ( new Multipart ( [ ] , "foobar" , "multipart/mixed" ) . headers . get ( "content-type" ) ) . to . equal ( "multipart/mixed; boundary=foobar" ) ;
0 commit comments