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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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'

export type Protocol = 'http' | 'https'

Expand Down Expand Up @@ -104,6 +105,13 @@ export class NodeClientRequest extends ClientRequest {
}

write(...args: ClientRequestWriteArgs): boolean {
if (this.isRequestSent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update this after #453 is merged to rely on the new this.state here:

if (this.state >= HttpRequestInternalState.Sent) {
  // Throw an error if attempting to write
  // after the request has been 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(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')
})