Skip to content

Commit

Permalink
test(http): add "connect" and "secureConnect" tests (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Jun 7, 2024
1 parent 664a363 commit 7ab764e
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions test/modules/http/compliance/http-event-connect.test.ts
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)
})

0 comments on commit 7ab764e

Please sign in to comment.