@@ -237,6 +237,28 @@ describe("Multipart", function () {
237237 } ) ;
238238 } ) ;
239239
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+
240262 describe ( "formData" , function ( ) {
241263 it ( "should correctly create Multipart from FormData" , async function ( ) {
242264 const formData = new FormData ( ) ;
@@ -385,6 +407,19 @@ describe("Multipart", function () {
385407 } ) ;
386408 } ) ;
387409
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+
388423 describe ( "#headers" , function ( ) {
389424 it ( "should have the Content-Type boundary parameters in quotes as per RFC 2616" , function ( ) {
390425 expect ( new Multipart ( [ ] , "foobar" , "multipart/mixed" ) . headers . get ( "content-type" ) ) . to . equal ( "multipart/mixed; boundary=foobar" ) ;
0 commit comments