Skip to content

Commit 3f40055

Browse files
fix(setupWorker): correctly delete internal accept header on passthrough (#2375)
Co-authored-by: Artem Zakharchenko <[email protected]>
1 parent e61ee12 commit 3f40055

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/mockServiceWorker.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,19 @@ async function getResponse(event, client, requestId) {
199199
// Remove the "accept" header value that marked this request as passthrough.
200200
// This prevents request alteration and also keeps it compliant with the
201201
// user-defined CORS policies.
202-
headers.delete('accept', 'msw/passthrough')
202+
const acceptHeader = headers.get('accept')
203+
if (acceptHeader) {
204+
const values = acceptHeader.split(',').map((value) => value.trim())
205+
const filteredValues = values.filter(
206+
(value) => value !== 'msw/passthrough',
207+
)
208+
209+
if (filteredValues.length > 0) {
210+
headers.set('accept', filteredValues.join(', '))
211+
} else {
212+
headers.delete('accept')
213+
}
214+
}
203215

204216
return fetch(requestClone, { headers })
205217
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { http, passthrough } from 'msw'
2+
import { setupWorker } from 'msw/browser'
3+
4+
const worker = setupWorker(
5+
http.get('*/resource', function originalResolver() {
6+
return passthrough()
7+
}),
8+
)
9+
10+
worker.start()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { HttpServer } from '@open-draft/test-server/lib/http'
2+
import { test, expect } from '../../playwright.extend'
3+
4+
const httpServer = new HttpServer((app) => {
5+
app.get('/resource', (req, res) => {
6+
res.set(req.headers)
7+
res.send('hello world')
8+
})
9+
})
10+
11+
test.beforeAll(async () => {
12+
await httpServer.listen()
13+
})
14+
15+
test.afterAll(async () => {
16+
await httpServer.close()
17+
})
18+
19+
test('removes the internal passthrough request header', async ({
20+
loadExample,
21+
fetch,
22+
}) => {
23+
await loadExample(require.resolve('./worker-passthrough-header.mocks.ts'))
24+
25+
const response = await fetch(httpServer.http.url('/resource'), {
26+
headers: { 'x-custom-header': 'yes' },
27+
})
28+
const headers = await response.allHeaders()
29+
30+
expect(headers).toMatchObject({
31+
// The default header value.
32+
accept: '*/*',
33+
'x-custom-header': 'yes',
34+
})
35+
await expect(response.text()).resolves.toBe('hello world')
36+
})
37+
38+
test('preserves existing "accept" header values when removing the internal passthrough request header', async ({
39+
loadExample,
40+
fetch,
41+
}) => {
42+
await loadExample(require.resolve('./worker-passthrough-header.mocks.ts'))
43+
44+
const response = await fetch(httpServer.http.url('/resource'), {
45+
headers: {
46+
accept: 'text/plain, application/json',
47+
'x-custom-header': 'yes',
48+
},
49+
})
50+
const headers = await response.allHeaders()
51+
52+
expect(headers).toMatchObject({
53+
accept: 'text/plain, application/json',
54+
'x-custom-header': 'yes',
55+
})
56+
await expect(response.text()).resolves.toBe('hello world')
57+
})

0 commit comments

Comments
 (0)