Skip to content

Commit

Permalink
fix(setupWorker): correctly delete internal accept header on passth…
Browse files Browse the repository at this point in the history
…rough (#2375)

Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
smouillour and kettanaito authored Dec 5, 2024
1 parent e61ee12 commit 3f40055
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/mockServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,19 @@ async function getResponse(event, client, requestId) {
// Remove the "accept" header value that marked this request as passthrough.
// This prevents request alteration and also keeps it compliant with the
// user-defined CORS policies.
headers.delete('accept', 'msw/passthrough')
const acceptHeader = headers.get('accept')
if (acceptHeader) {
const values = acceptHeader.split(',').map((value) => value.trim())
const filteredValues = values.filter(
(value) => value !== 'msw/passthrough',
)

if (filteredValues.length > 0) {
headers.set('accept', filteredValues.join(', '))
} else {
headers.delete('accept')
}
}

return fetch(requestClone, { headers })
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { http, passthrough } from 'msw'
import { setupWorker } from 'msw/browser'

const worker = setupWorker(
http.get('*/resource', function originalResolver() {
return passthrough()
}),
)

worker.start()
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { HttpServer } from '@open-draft/test-server/lib/http'
import { test, expect } from '../../playwright.extend'

const httpServer = new HttpServer((app) => {
app.get('/resource', (req, res) => {
res.set(req.headers)
res.send('hello world')
})
})

test.beforeAll(async () => {
await httpServer.listen()
})

test.afterAll(async () => {
await httpServer.close()
})

test('removes the internal passthrough request header', async ({
loadExample,
fetch,
}) => {
await loadExample(require.resolve('./worker-passthrough-header.mocks.ts'))

const response = await fetch(httpServer.http.url('/resource'), {
headers: { 'x-custom-header': 'yes' },
})
const headers = await response.allHeaders()

expect(headers).toMatchObject({
// The default header value.
accept: '*/*',
'x-custom-header': 'yes',
})
await expect(response.text()).resolves.toBe('hello world')
})

test('preserves existing "accept" header values when removing the internal passthrough request header', async ({
loadExample,
fetch,
}) => {
await loadExample(require.resolve('./worker-passthrough-header.mocks.ts'))

const response = await fetch(httpServer.http.url('/resource'), {
headers: {
accept: 'text/plain, application/json',
'x-custom-header': 'yes',
},
})
const headers = await response.allHeaders()

expect(headers).toMatchObject({
accept: 'text/plain, application/json',
'x-custom-header': 'yes',
})
await expect(response.text()).resolves.toBe('hello world')
})

0 comments on commit 3f40055

Please sign in to comment.