@@ -253,18 +253,89 @@ describe('HttpResponse.html()', () => {
253
253
} )
254
254
} )
255
255
256
- it ( 'creates an array buffer response' , async ( ) => {
257
- const buffer = new TextEncoder ( ) . encode ( 'hello world' )
258
- const response = HttpResponse . arrayBuffer ( buffer )
256
+ describe ( 'HttpResponse.arrayBuffer()' , ( ) => {
257
+ it ( 'creates an array buffer response' , async ( ) => {
258
+ const buffer = new TextEncoder ( ) . encode ( 'hello world' )
259
+ const response = HttpResponse . arrayBuffer ( buffer )
259
260
260
- expect ( response . status ) . toBe ( 200 )
261
- expect ( response . statusText ) . toBe ( 'OK' )
262
- expect ( response . body ) . toBeInstanceOf ( ReadableStream )
261
+ expect ( response . status ) . toBe ( 200 )
262
+ expect ( response . statusText ) . toBe ( 'OK' )
263
+ expect ( response . body ) . toBeInstanceOf ( ReadableStream )
263
264
264
- const responseData = await response . arrayBuffer ( )
265
- expect ( responseData ) . toEqual ( buffer . buffer )
266
- expect ( Object . fromEntries ( response . headers . entries ( ) ) ) . toEqual ( {
267
- 'content-length' : '11' ,
265
+ const responseData = await response . arrayBuffer ( )
266
+ expect ( responseData ) . toEqual ( buffer . buffer )
267
+ expect ( Object . fromEntries ( response . headers . entries ( ) ) ) . toEqual ( {
268
+ 'content-length' : '11' ,
269
+ 'content-type' : 'application/octet-stream' ,
270
+ } )
271
+ } )
272
+
273
+ it ( 'allows overriding the "Content-Type" response header' , async ( ) => {
274
+ const buffer = new TextEncoder ( ) . encode ( 'hello world' )
275
+ const response = HttpResponse . arrayBuffer ( buffer , {
276
+ headers : {
277
+ 'Content-Type' : 'text/plain; charset=utf-8' ,
278
+ } ,
279
+ } )
280
+
281
+ expect ( response . status ) . toBe ( 200 )
282
+ expect ( response . statusText ) . toBe ( 'OK' )
283
+ expect ( response . body ) . toBeInstanceOf ( ReadableStream )
284
+
285
+ const responseData = await response . arrayBuffer ( )
286
+ expect ( responseData ) . toEqual ( buffer . buffer )
287
+ expect ( Object . fromEntries ( response . headers . entries ( ) ) ) . toEqual ( {
288
+ 'content-length' : '11' ,
289
+ 'content-type' : 'text/plain; charset=utf-8' ,
290
+ } )
291
+ } )
292
+
293
+ it ( 'creates an array buffer response from a shared array buffer' , async ( ) => {
294
+ const arrayBuffer = new TextEncoder ( ) . encode ( 'hello world' )
295
+
296
+ // Copy the data from the array buffer to a shared array buffer
297
+ const sharedBuffer = new SharedArrayBuffer ( 11 )
298
+ const sharedView = new Uint8Array ( sharedBuffer )
299
+ sharedView . set ( arrayBuffer )
300
+
301
+ const response = HttpResponse . arrayBuffer ( sharedBuffer )
302
+
303
+ expect ( response . status ) . toBe ( 200 )
304
+ expect ( response . statusText ) . toBe ( 'OK' )
305
+ expect ( response . body ) . toBeInstanceOf ( ReadableStream )
306
+
307
+ const responseData = await response . arrayBuffer ( )
308
+ expect ( responseData ) . toEqual ( arrayBuffer . buffer )
309
+ expect ( Object . fromEntries ( response . headers . entries ( ) ) ) . toEqual ( {
310
+ 'content-length' : '11' ,
311
+ 'content-type' : 'application/octet-stream' ,
312
+ } )
313
+ } )
314
+
315
+ it ( 'allows overriding the "Content-Type" response header for shared array buffers' , async ( ) => {
316
+ const arrayBuffer = new TextEncoder ( ) . encode ( 'hello world' )
317
+
318
+ // Copy the data from the array buffer to a shared array buffer
319
+ const sharedBuffer = new SharedArrayBuffer ( 11 )
320
+ const sharedView = new Uint8Array ( sharedBuffer )
321
+ sharedView . set ( arrayBuffer )
322
+
323
+ const response = HttpResponse . arrayBuffer ( sharedBuffer , {
324
+ headers : {
325
+ 'Content-Type' : 'text/plain; charset=utf-8' ,
326
+ } ,
327
+ } )
328
+
329
+ expect ( response . status ) . toBe ( 200 )
330
+ expect ( response . statusText ) . toBe ( 'OK' )
331
+ expect ( response . body ) . toBeInstanceOf ( ReadableStream )
332
+
333
+ const responseData = await response . arrayBuffer ( )
334
+ expect ( responseData ) . toEqual ( arrayBuffer . buffer )
335
+ expect ( Object . fromEntries ( response . headers . entries ( ) ) ) . toEqual ( {
336
+ 'content-length' : '11' ,
337
+ 'content-type' : 'text/plain; charset=utf-8' ,
338
+ } )
268
339
} )
269
340
} )
270
341
0 commit comments