-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ignore irrelevant request listeners during response patching (#253)
* fix(ClientRequest): await idle of relevant requests * fix(fetch): await idle of relevant requests * fix(xhr): await idle of relevant requests * chore(helpers): reject on missing "resolver" event dispatch * chore: set browser tests timeout to 15s * fix(RemoteHttpInterceptor): await idle of relevant requests * chore: remove logs from the FetchInterceptor
- Loading branch information
1 parent
b676f0e
commit 17d9794
Showing
16 changed files
with
462 additions
and
124 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
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
43 changes: 43 additions & 0 deletions
43
test/modules/XMLHttpRequest/response/xhr-response-patching.browser.runtime.js
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,43 @@ | ||
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest' | ||
|
||
const interceptor = new XMLHttpRequestInterceptor() | ||
|
||
interceptor.on('request', async (request) => { | ||
window.dispatchEvent( | ||
new CustomEvent('resolver', { | ||
detail: JSON.stringify({ | ||
id: request.id, | ||
method: request.method, | ||
url: request.url.href, | ||
headers: request.headers.all(), | ||
credentials: request.credentials, | ||
body: request.body, | ||
}), | ||
}) | ||
) | ||
|
||
if (request.url.pathname === '/mocked') { | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
|
||
const req = new XMLHttpRequest() | ||
req.open('GET', window.originalUrl, true) | ||
req.send() | ||
await new Promise((resolve, reject) => { | ||
req.addEventListener('loadend', resolve) | ||
req.addEventListener('error', reject) | ||
}) | ||
|
||
request.respondWith({ | ||
status: req.status, | ||
statusText: req.statusText, | ||
headers: { | ||
'X-Custom-Header': req.getResponseHeader('X-Custom-Header'), | ||
}, | ||
body: `${req.responseText} world`, | ||
}) | ||
} | ||
}) | ||
|
||
interceptor.apply() | ||
|
||
window.interceptor = interceptor |
58 changes: 58 additions & 0 deletions
58
test/modules/XMLHttpRequest/response/xhr-response-patching.browser.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,58 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { createBrowserXMLHttpRequest } from '../../../helpers' | ||
|
||
declare namespace window { | ||
export let originalUrl: string | ||
} | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/original', (req, res) => { | ||
res | ||
.set('access-control-expose-headers', 'x-custom-header') | ||
.set('x-custom-header', 'yes') | ||
.send('hello') | ||
}) | ||
}) | ||
|
||
async function prepareRuntime() { | ||
const runtime = await pageWith({ | ||
example: path.resolve( | ||
__dirname, | ||
'xhr-response-patching.browser.runtime.js' | ||
), | ||
}) | ||
|
||
await runtime.page.evaluate((url) => { | ||
window.originalUrl = url | ||
}, httpServer.http.url('/original')) | ||
|
||
return runtime | ||
} | ||
|
||
beforeAll(async () => { | ||
await httpServer.listen() | ||
}) | ||
|
||
afterAll(async () => { | ||
await httpServer.close() | ||
}) | ||
|
||
test('responds to an HTTP request handled in the resolver', async () => { | ||
const runtime = await prepareRuntime() | ||
const callXMLHttpRequest = createBrowserXMLHttpRequest(runtime) | ||
|
||
const [, response] = await callXMLHttpRequest({ | ||
method: 'GET', | ||
url: 'http://localhost/mocked', | ||
}) | ||
|
||
expect(response.status).toBe(200) | ||
expect(response.statusText).toBe('OK') | ||
expect(response.headers).toBe('x-custom-header: yes') | ||
expect(response.body).toBe('hello world') | ||
}) |
Oops, something went wrong.