-
-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ClientRequest): destroy socket when destroying IncomingMessage #597
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,11 @@ export class MockHttpSocket extends MockSocket { | |
// Normally, we shoud listen to the "close" event but it | ||
// can be suppressed by using the "emitClose: false" option. | ||
this.responseParser.free() | ||
|
||
if (error) { | ||
this.emit('error', error) | ||
} | ||
|
||
return super.destroy(error) | ||
} | ||
|
||
|
@@ -153,6 +158,12 @@ export class MockHttpSocket extends MockSocket { | |
} | ||
|
||
const socket = this.createConnection() | ||
|
||
// If the developer destroys the socket, destroy the original connection. | ||
this.once('error', (error) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this: if the socket gets destroyed by the developer, abort the pending actual socket connection. |
||
socket.destroy(error) | ||
}) | ||
|
||
this.address = socket.address.bind(socket) | ||
|
||
// Flush the buffered "socket.write()" calls onto | ||
|
@@ -308,6 +319,11 @@ export class MockHttpSocket extends MockSocket { | |
serverResponse.removeHeader('connection') | ||
serverResponse.removeHeader('date') | ||
|
||
// If the developer destroy the socket, gracefully destroy the response. | ||
this.once('error', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same for the mocked responses: if the socket gets destroyed, abort the pending server response. |
||
serverResponse.destroy() | ||
}) | ||
|
||
// Get the raw headers stored behind the symbol to preserve name casing. | ||
const headers = getRawFetchHeaders(response.headers) || response.headers | ||
for (const [name, value] of headers) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// @vitest-environment node | ||
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import http from 'node:http' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
import { HttpServer } from '@open-draft/test-server/lib/http' | ||
import { waitForClientRequest } from '../../../helpers' | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/', (req, res) => res.sendStatus(200)) | ||
}) | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('emits the "error" event when a bypassed response is destroyed', async () => { | ||
const socketErrorListener = vi.fn() | ||
|
||
const request = http | ||
.get(httpServer.http.url('/')) | ||
.on('socket', (socket) => { | ||
socket.on('error', socketErrorListener) | ||
}) | ||
.on('response', (response) => { | ||
response.destroy(new Error('reason')) | ||
}) | ||
|
||
const { res } = await waitForClientRequest(request) | ||
|
||
expect(res.destroyed).toBe(true) | ||
expect(socketErrorListener).toHaveBeenCalledOnce() | ||
expect(socketErrorListener).toHaveBeenCalledWith(new Error('reason')) | ||
}) | ||
|
||
it('emits the "error" event when a mocked response is destroyed', async () => { | ||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(new Response('hello world')) | ||
}) | ||
|
||
const socketErrorListener = vi.fn() | ||
|
||
const request = http | ||
.get(httpServer.http.url('/')) | ||
.on('socket', (socket) => { | ||
socket.on('error', socketErrorListener) | ||
}) | ||
.on('response', (response) => { | ||
response.destroy(new Error('reason')) | ||
}) | ||
|
||
const { res } = await waitForClientRequest(request) | ||
|
||
expect(res.destroyed).toBe(true) | ||
expect(socketErrorListener).toHaveBeenCalledOnce() | ||
expect(socketErrorListener).toHaveBeenCalledWith(new Error('reason')) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were missing this part. If the destroy was not graceful (contains error), emit the error event on the socket. This is precisely how Node.js behaves.