-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(MockHttpSocket): forward tls socket properties (#556)
- Loading branch information
1 parent
085d1ec
commit 430c65e
Showing
2 changed files
with
102 additions
and
1 deletion.
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,66 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import https from 'node:https' | ||
import type { TLSSocket } from 'node:tls' | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('emits a correct TLS Socket instance for a handled HTTPS request', async () => { | ||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(new Response('hello world')) | ||
}) | ||
|
||
const request = https.get('https://example.com') | ||
const socketPromise = new DeferredPromise<TLSSocket>() | ||
request.on('socket', (socket) => { | ||
socket.on('connect', () => socketPromise.resolve(socket as TLSSocket)) | ||
}) | ||
|
||
const socket = await socketPromise | ||
|
||
// Must be a TLS socket. | ||
expect(socket.encrypted).toBe(true) | ||
// The server certificate wasn't signed by one of the CA | ||
// specified in the Socket constructor. | ||
expect(socket.authorized).toBe(false) | ||
|
||
expect(socket.getSession()).toBeUndefined() | ||
expect(socket.getProtocol()).toBe('TLSv1.3') | ||
expect(socket.isSessionReused()).toBe(false) | ||
}) | ||
|
||
it('emits a correct TLS Socket instance for a bypassed HTTPS request', async () => { | ||
const request = https.get('https://example.com') | ||
const socketPromise = new DeferredPromise<TLSSocket>() | ||
request.on('socket', (socket) => { | ||
socket.on('connect', () => socketPromise.resolve(socket as TLSSocket)) | ||
}) | ||
|
||
const socket = await socketPromise | ||
|
||
// Must be a TLS socket. | ||
expect(socket.encrypted).toBe(true) | ||
// The server certificate wasn't signed by one of the CA | ||
// specified in the Socket constructor. | ||
expect(socket.authorized).toBe(false) | ||
|
||
expect(socket.getSession()).toBeUndefined() | ||
expect(socket.getProtocol()).toBe('TLSv1.3') | ||
expect(socket.isSessionReused()).toBe(false) | ||
}) |