Skip to content
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): throw when writing after end for bypass responses #462

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/interceptors/ClientRequest/NodeClientRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { toInteractiveRequest } from '../../utils/toInteractiveRequest'
import { uuidv4 } from '../../utils/uuid'
import { emitAsync } from '../../utils/emitAsync'
import { getRawFetchHeaders } from '../../utils/getRawFetchHeaders'
import { NodeError } from '../../utils/nodeError'
import { isPropertyAccessible } from '../../utils/isPropertyAccessible'

export type Protocol = 'http' | 'https'
Expand Down Expand Up @@ -120,6 +121,13 @@ export class NodeClientRequest extends ClientRequest {
}

write(...args: ClientRequestWriteArgs): boolean {
if (this.state >= HttpClientInternalState.Sent) {
const err = new NodeError('write after end', 'ERR_STREAM_WRITE_AFTER_END')
process.nextTick(() => this.emit('error', err))

return false
}

const [chunk, encoding, callback] = normalizeClientRequestWriteArgs(args)
this.logger.info('write:', { chunk, encoding, callback })
this.chunks.push({ chunk, encoding })
Expand Down
5 changes: 5 additions & 0 deletions src/utils/nodeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class NodeError extends Error {
constructor(readonly message: string, readonly code: string) {
super(message)
}
}
40 changes: 40 additions & 0 deletions test/modules/http/compliance/http-req-end-then-write.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { it, expect, beforeAll, afterAll } from 'vitest'
import http from 'http'
import express from 'express'
import { HttpServer } from '@open-draft/test-server/http'
import { DeferredPromise } from '@open-draft/deferred-promise'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'

const httpServer = new HttpServer((app) => {
app.post('/resource', express.text({ type: '*/*' }), (req, res) => {
res.send(req.body)
})
})

const interceptor = new ClientRequestInterceptor()

beforeAll(async () => {
interceptor.apply()
await httpServer.listen()
})

afterAll(async () => {
interceptor.dispose()
await httpServer.close()
})

it('emits the ERR_STREAM_WRITE_AFTER_END error when write after end given no mocked response', async () => {
const req = http.request(httpServer.http.url('/resource'))

const errorReceived = new DeferredPromise<NodeJS.ErrnoException>()
req.on('error', (error) => {
errorReceived.resolve(error)
})

req.end()
req.write('foo')

const error = await errorReceived

expect(error.code).toBe('ERR_STREAM_WRITE_AFTER_END')
})