-
-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bypass): support
sendBeacon()
requests
- Loading branch information
1 parent
8cb7b01
commit 8be9ad9
Showing
7 changed files
with
98 additions
and
13 deletions.
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
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
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
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
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
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,14 @@ | ||
import { http, bypass } from 'msw' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
const worker = setupWorker( | ||
http.post('/analytics', ({ request }) => { | ||
return new Response(request.body) | ||
}), | ||
http.post('*/analytics-bypass', ({ request }) => { | ||
const nextRequest = bypass(request) | ||
return fetch(nextRequest) | ||
}), | ||
) | ||
|
||
worker.start() |
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,56 @@ | ||
import { test, expect } from '../playwright.extend' | ||
|
||
test('supports mocking a response to a "sendBeacon" request', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
await loadExample(require.resolve('./send-beacon.mocks.ts')) | ||
|
||
const isQueuedPromise = page.evaluate(() => { | ||
return navigator.sendBeacon( | ||
'/analytics', | ||
JSON.stringify({ event: 'pageview' }), | ||
) | ||
}) | ||
|
||
const response = await page.waitForResponse((response) => { | ||
return response.url().endsWith('/analytics') | ||
}) | ||
|
||
expect(response.status()).toBe(200) | ||
// Technically, "sendBeacon" responses don't send any body back. | ||
// We use this body only to verify that the request body was accessible | ||
// in the request handlers. | ||
await expect(response.text()).resolves.toBe('{"event":"pageview"}') | ||
|
||
// Must return true, indicating that the server has queued the sent data. | ||
await expect(isQueuedPromise).resolves.toBe(true) | ||
}) | ||
|
||
test.only('supports bypassing "sendBeacon" requests', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
const { compilation } = await loadExample( | ||
require.resolve('./send-beacon.mocks.ts'), | ||
{ | ||
beforeNavigation(compilation) { | ||
compilation.use((router) => { | ||
router.post('/analytics-bypass', (_req, res) => { | ||
res.status(200).end() | ||
}) | ||
}) | ||
}, | ||
}, | ||
) | ||
|
||
const url = new URL('./analytics-bypass', compilation.previewUrl).href | ||
const isQueuedPromise = page.evaluate((url) => { | ||
return navigator.sendBeacon(url, JSON.stringify({ event: 'pageview' })) | ||
}, url) | ||
|
||
const response = await page.waitForResponse(url) | ||
expect(response.status()).toBe(200) | ||
|
||
await expect(isQueuedPromise).resolves.toBe(true) | ||
}) |