-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ClientRequest): fix a custom agent context in
createConnection
(#…
…636) Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
- Loading branch information
1 parent
236f552
commit e3035f7
Showing
2 changed files
with
74 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// @vitest-environment node | ||
import { vi, it, expect, beforeAll, afterAll, afterEach } from 'vitest' | ||
import http from 'node:http' | ||
import https from 'node:https' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest/index' | ||
import { waitForClientRequest } from '../../../../test/helpers' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/resource', (req, res) => res.send('hello world')) | ||
}) | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('preserves the context of the "createConnection" function in a custom http agent', async () => { | ||
const createConnectionContextSpy = vi.fn() | ||
class CustomHttpAgent extends http.Agent { | ||
createConnection(options: any, callback: any) { | ||
createConnectionContextSpy(this) | ||
return super.createConnection(options, callback) | ||
} | ||
} | ||
const agent = new CustomHttpAgent() | ||
|
||
const request = http.get(httpServer.http.url('/resource'), { agent }) | ||
await waitForClientRequest(request) | ||
|
||
const [context] = createConnectionContextSpy.mock.calls[0] | ||
expect(context.constructor.name).toBe('CustomHttpAgent') | ||
}) | ||
|
||
it('preserves the context of the "createConnection" function in a custom https agent', async () => { | ||
const createConnectionContextSpy = vi.fn() | ||
class CustomHttpsAgent extends https.Agent { | ||
createConnection(options: any, callback: any) { | ||
createConnectionContextSpy(this) | ||
return super.createConnection(options, callback) | ||
} | ||
} | ||
const agent = new CustomHttpsAgent() | ||
|
||
const request = https.get(httpServer.https.url('/resource'), { | ||
agent, | ||
rejectUnauthorized: false, | ||
}) | ||
await waitForClientRequest(request) | ||
|
||
const [context] = createConnectionContextSpy.mock.calls[0] | ||
expect(context.constructor.name).toBe('CustomHttpsAgent') | ||
}) |