-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ClientRequest): ignore request destroyed state when responding wi…
…th a mocked response (#432)
- Loading branch information
1 parent
e73e809
commit 217c6e9
Showing
5 changed files
with
128 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { it, expect, beforeAll, afterAll } from 'vitest' | ||
import http from 'http' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { sleep, waitForClientRequest } from '../../../helpers' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/resource', (req, res) => { | ||
res.send('original response') | ||
}) | ||
}) | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('supports custom delay before responding with a mock', async () => { | ||
interceptor.once('request', async ({ request }) => { | ||
await sleep(750) | ||
request.respondWith(new Response('mocked response')) | ||
}) | ||
|
||
const requestStart = Date.now() | ||
const request = http.get('https://non-existing-host.com') | ||
const { res, text } = await waitForClientRequest(request) | ||
const requestEnd = Date.now() | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(await text()).toBe('mocked response') | ||
expect(requestEnd - requestStart).toBeGreaterThanOrEqual(700) | ||
}) | ||
|
||
it('supports custom delay before receiving the original response', async () => { | ||
interceptor.once('request', async () => { | ||
// This will simply delay the request execution before | ||
// it receives the original response. | ||
await sleep(750) | ||
}) | ||
|
||
const requestStart = Date.now() | ||
const request = http.get(httpServer.http.url('/resource')) | ||
const { res, text } = await waitForClientRequest(request) | ||
const requestEnd = Date.now() | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(await text()).toBe('original response') | ||
expect(requestEnd - requestStart).toBeGreaterThanOrEqual(700) | ||
}) |
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