Skip to content

Commit 23c16e6

Browse files
ushiboyushiboy
and
ushiboy
authoredMar 15, 2024··
fix: parse "Set-Cookie" response header with commas correctly (#2075)
Co-authored-by: ushiboy <ushiboy.dev@gmail.com>
1 parent ec76862 commit 23c16e6

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed
 

‎src/core/utils/HttpResponse/decorators.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import statuses from '@bundled-es-modules/statuses'
22
import type { HttpResponseInit } from '../../HttpResponse'
3+
import { Headers as HeadersPolyfill } from 'headers-polyfill'
34

45
const { message } = statuses
56

@@ -40,10 +41,13 @@ export function decorateResponse(
4041
// Cookie forwarding is only relevant in the browser.
4142
if (typeof document !== 'undefined') {
4243
// Write the mocked response cookies to the document.
43-
// Note that Fetch API Headers will concatenate multiple "Set-Cookie"
44-
// headers into a single comma-separated string, just as it does
45-
// with any other multi-value headers.
46-
const responseCookies = init.headers.get('Set-Cookie')?.split(',') || []
44+
// Use `headers-polyfill` to get the Set-Cookie header value correctly.
45+
// This is an alternative until TypeScript 5.2
46+
// and Node.js v20 become the minimum supported version
47+
// and getSetCookie in Headers can be used directly.
48+
const responseCookies = HeadersPolyfill.prototype.getSetCookie.call(
49+
init.headers,
50+
)
4751

4852
for (const cookieString of responseCookies) {
4953
// No need to parse the cookie headers because it's defined

‎test/browser/rest-api/cookies.mocks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const worker = setupWorker(
2323
headers: [
2424
['Set-Cookie', 'firstCookie=yes'],
2525
['Set-Cookie', 'secondCookie=no; Max-Age=1000'],
26+
['Set-Cookie', 'thirdCookie=1,2,3'],
2627
],
2728
},
2829
)

‎test/browser/rest-api/cookies.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ test('allows setting multiple response cookies', async ({
4848
expect(allCookies).toEqual({
4949
firstCookie: 'yes',
5050
secondCookie: 'no',
51+
thirdCookie: '1,2,3',
5152
})
5253
})

0 commit comments

Comments
 (0)
Please sign in to comment.