Skip to content

Commit

Permalink
fix(ClientRequest): prevent rawHeaders from being shared across ins…
Browse files Browse the repository at this point in the history
…tances (#694)

Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
Michael Solomon and kettanaito authored Jan 2, 2025
1 parent 6400beb commit a5ef7ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/interceptors/ClientRequest/utils/recordRawHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,18 @@ it('does not throw on using Headers before recording', () => {
request.headers.set('X-My-Header', '1')
expect(getRawFetchHeaders(request.headers)).toEqual([['X-My-Header', '1']])
})

/**
* @see https://github.com/mswjs/interceptors/issues/681
*/
it('isolates headers between different headers instances', async () => {
recordRawFetchHeaders()
const original = new Headers()
const firstClone = new Headers(original)
firstClone.set('Content-Type', 'application/json')
const secondClone = new Headers(original)

expect(original.get('Content-Type')).toBeNull()
expect(firstClone.get('Content-Type')).toBe('application/json')
expect(secondClone.get('Content-Type')).toBeNull()
})
9 changes: 8 additions & 1 deletion src/interceptors/ClientRequest/utils/recordRawHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ export function recordRawFetchHeaders() {
[Reflect.get(headersInit, kRawHeaders)],
newTarget
)
ensureRawHeadersSymbol(headers, Reflect.get(headersInit, kRawHeaders))
ensureRawHeadersSymbol(headers, [
/**
* @note Spread the retrieved headers to clone them.
* This prevents multiple Headers instances from pointing
* at the same internal "rawHeaders" array.
*/
...Reflect.get(headersInit, kRawHeaders),
])
return headers
}

Expand Down

0 comments on commit a5ef7ea

Please sign in to comment.