-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(http): add "connect" and "secureConnect" tests (#576)
- Loading branch information
1 parent
664a363
commit 7ab764e
Showing
1 changed file
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @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 httpServer = new HttpServer((app) => { | ||
app.get('/', (req, res) => { | ||
res.send('original') | ||
}) | ||
}) | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('emits the "connect" event for a mocked request', async () => { | ||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(new Response('hello world')) | ||
}) | ||
|
||
const connectListener = vi.fn() | ||
const request = http.get(httpServer.http.url('/')) | ||
request.on('socket', (socket) => { | ||
socket.on('connect', connectListener) | ||
}) | ||
|
||
await waitForClientRequest(request) | ||
|
||
expect(connectListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('emits the "connect" event for a bypassed request', async () => { | ||
const connectListener = vi.fn() | ||
const request = http.get(httpServer.http.url('/')) | ||
request.on('socket', (socket) => { | ||
socket.on('connect', connectListener) | ||
}) | ||
|
||
await waitForClientRequest(request) | ||
|
||
expect(connectListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('emits the "secureConnect" event for a mocked HTTPS request', async () => { | ||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(new Response('hello world')) | ||
}) | ||
|
||
const connectListener = vi.fn<[string]>() | ||
const request = https.get(httpServer.https.url('/')) | ||
request.on('socket', (socket) => { | ||
socket.on('connect', () => connectListener('connect')) | ||
socket.on('secureConnect', () => connectListener('secureConnect')) | ||
}) | ||
|
||
await waitForClientRequest(request) | ||
|
||
expect(connectListener).toHaveBeenNthCalledWith(1, 'connect') | ||
expect(connectListener).toHaveBeenNthCalledWith(2, 'secureConnect') | ||
expect(connectListener).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('emits the "secureConnect" event for a mocked HTTPS request', async () => { | ||
const connectListener = vi.fn<[string]>() | ||
const request = https.get(httpServer.https.url('/'), { | ||
rejectUnauthorized: false, | ||
}) | ||
request.on('socket', (socket) => { | ||
socket.on('connect', () => connectListener('connect')) | ||
socket.on('secureConnect', () => connectListener('secureConnect')) | ||
}) | ||
|
||
await waitForClientRequest(request) | ||
|
||
expect(connectListener).toHaveBeenNthCalledWith(1, 'connect') | ||
expect(connectListener).toHaveBeenNthCalledWith(2, 'secureConnect') | ||
expect(connectListener).toHaveBeenCalledTimes(2) | ||
}) |