-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(setupWorker): correctly delete internal
accept
header on passth…
…rough (#2375) Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
e61ee12
commit 3f40055
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/browser/msw-api/setup-worker/worker-passthrough-header.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
57 changes: 57 additions & 0 deletions
57
test/browser/msw-api/setup-worker/worker-passthrough-header.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |