-
-
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: prevent infinite loop when bypassing
sendBeacon()
requests (#2353
- Loading branch information
1 parent
8cb7b01
commit 2fa98c3
Showing
11 changed files
with
224 additions
and
38 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
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('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) | ||
}) |
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,80 @@ | ||
// @vitest-environment node | ||
import { http, bypass } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import express from 'express' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.use('/resource', (_req, res, next) => { | ||
res.setHeader('access-control-allow-headers', '*') | ||
next() | ||
}) | ||
app.post('/resource', express.text(), (req, res) => { | ||
res.json({ | ||
text: req.body, | ||
requestHeaders: req.headers, | ||
}) | ||
}) | ||
}) | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(async () => { | ||
server.listen() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
server.close() | ||
await httpServer.close() | ||
}) | ||
|
||
it('supports patching an original HTTP response', async () => { | ||
server.use( | ||
http.post(httpServer.http.url('/resource'), async ({ request }) => { | ||
const originalResponse = await fetch(bypass(request)) | ||
const { text, requestHeaders } = await originalResponse.json() | ||
return new Response(text.toUpperCase(), { headers: requestHeaders }) | ||
}), | ||
) | ||
|
||
const response = await fetch(httpServer.http.url('/resource'), { | ||
method: 'POST', | ||
body: 'world', | ||
}) | ||
|
||
await expect(response.text()).resolves.toBe('WORLD') | ||
|
||
// Must not contain the internal bypass request header. | ||
expect(Object.fromEntries(response.headers)).toHaveProperty('accept', '*/*') | ||
}) | ||
|
||
it('preserves request "accept" header when patching a response', async () => { | ||
server.use( | ||
http.post(httpServer.http.url('/resource'), async ({ request }) => { | ||
const originalResponse = await fetch(bypass(request)) | ||
const { text, requestHeaders } = await originalResponse.json() | ||
return new Response(text.toUpperCase(), { headers: requestHeaders }) | ||
}), | ||
) | ||
|
||
const response = await fetch(httpServer.http.url('/resource'), { | ||
method: 'POST', | ||
headers: { | ||
accept: 'application/json', | ||
}, | ||
body: 'world', | ||
}) | ||
|
||
await expect(response.text()).resolves.toBe('WORLD') | ||
|
||
// Must not contain the internal bypass request header. | ||
expect(Object.fromEntries(response.headers)).toHaveProperty( | ||
'accept', | ||
'application/json', | ||
) | ||
}) |