@@ -51,15 +51,20 @@ function getHeaderByName (headers, key) {
5151 }
5252}
5353
54+ /** @param {string[] } headers */
55+ function buildHeadersFromArray ( headers ) { // fetch HeadersList
56+ const clone = headers . slice ( )
57+ const entries = [ ]
58+ for ( let index = 0 ; index < clone . length ; index += 2 ) {
59+ entries . push ( [ clone [ index ] , clone [ index + 1 ] ] )
60+ }
61+ return Object . fromEntries ( entries )
62+ }
63+
5464function matchHeaders ( mockDispatch , headers ) {
5565 if ( typeof mockDispatch . headers === 'function' ) {
5666 if ( Array . isArray ( headers ) ) { // fetch HeadersList
57- const clone = headers . slice ( )
58- const entries = [ ]
59- for ( let index = 0 ; index < clone . length ; index += 2 ) {
60- entries . push ( [ clone [ index ] , clone [ index + 1 ] ] )
61- }
62- headers = Object . fromEntries ( entries )
67+ headers = buildHeadersFromArray ( headers )
6368 }
6469 return mockDispatch . headers ( headers ? lowerCaseEntries ( headers ) : { } )
6570 }
@@ -284,7 +289,13 @@ function mockDispatch (opts, handler) {
284289 }
285290
286291 function handleReply ( mockDispatches ) {
287- const responseData = getResponseData ( typeof data === 'function' ? data ( opts ) : data )
292+ // fetch's HeadersList is a 1D string array
293+ const optsHeaders = Array . isArray ( opts . headers )
294+ ? buildHeadersFromArray ( opts . headers )
295+ : opts . headers
296+ const responseData = getResponseData (
297+ typeof data === 'function' ? data ( { ...opts , headers : optsHeaders } ) : data
298+ )
288299 const responseHeaders = generateKeyValues ( headers )
289300 const responseTrailers = generateKeyValues ( trailers )
290301
0 commit comments