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): do not unshift write buffer when flushing request headers #674

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"@types/jest": "^27.0.3",
"@types/node": "^18.19.31",
"@types/node-fetch": "2.5.12",
"@types/superagent": "^8.1.9",
"@types/supertest": "^2.0.11",
"@types/ws": "^8.5.10",
"axios": "^1.6.0",
Expand All @@ -158,7 +159,7 @@
"socket.io": "^4.7.4",
"socket.io-client": "^4.7.4",
"socket.io-parser": "^4.2.4",
"superagent": "^6.1.0",
"superagent": "^10.1.1",
"supertest": "^6.1.6",
"ts-jest": "^27.1.1",
"tsup": "^6.5.0",
Expand Down
93 changes: 38 additions & 55 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/interceptors/ClientRequest/MockHttpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,13 @@ export class MockHttpSocket extends MockSocket {
}

private flushWriteBuffer(): void {
let args: NormalizedSocketWriteArgs | undefined
while ((args = this.writeBuffer.shift())) {
args?.[2]?.()
for (const [, , callback] of this.writeBuffer) {
/**
* @note If the write callbacks are ever called twice,
* we need to mark them with a symbol so they aren't called
* again in the `passthrough` method.
*/
callback?.()
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/modules/http/compliance/http-req-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,29 @@ it('calls all write callbacks before the mocked response', async () => {
expect(requestBodyCallback).toHaveBeenCalledWith('one')
expect(await text()).toBe('hello world')
})

it('does not call write callbacks ...', async () => {
const requestBodyCallback = vi.fn()
const requestWriteCallback = vi.fn()

interceptor.once('request', async ({ request }) => {
requestBodyCallback(await request.text())
})

const request = http.request(httpServer.http.url('/resource'), {
method: 'POST',
headers: { 'content-type': 'text/plain' },
})
request.write('one', requestWriteCallback)
request.write('two', requestWriteCallback)
request.end('three', requestWriteCallback)

const { text } = await waitForClientRequest(request)

// Must call each write callback once.
expect(requestWriteCallback).toHaveBeenCalledTimes(3)
// Must be able to read the request stream in the interceptor.
expect(requestBodyCallback).toHaveBeenCalledWith('onetwothree')
// Must send the correct request body to the server.
expect(await text()).toBe('onetwothree')
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @vitest-environment node
/**
* @see https://github.com/mswjs/msw/issues/2309
*/
import http from 'node:http'
import path from 'node:path'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { vi, afterAll, beforeAll, afterEach, it, expect } from 'vitest'
import { HttpServer } from '@open-draft/test-server/http'
import superagent from 'superagent'

const interceptor = new ClientRequestInterceptor()

const httpServer = new HttpServer((app) => {
app.post('/upload', (req, res) => {
res.status(200).json({
contentType: req.header('content-type'),
contentLength: req.header('content-length'),
})
})
})

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

afterEach(() => {
interceptor.removeAllListeners()
})

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

it('does not skip first request bytes on passthrough POST request', async () => {
const socketDataCallback = vi.fn()

const underlyingServer = httpServer['_http'] as http.Server
underlyingServer.on('connection', (socket) => {
socket.on('data', (chunk) => socketDataCallback(chunk.toString('utf8')))
})

const response = await superagent
.post(httpServer.http.url('/upload'))
.attach(
'file',
/**
* @note The issue is only reproducible when providing a path
* to the uploaded file. Providing buffer works fine.
*/
path.resolve(__dirname, 'http-post-missing-first-bytes-file.png')
)
.timeout(1000)
.catch((error) => {
console.error(error)
expect.fail('Request must not error')
})

expect(response.status).toBe(200)
// Must send the uploaded file to the server.
expect(response.body).toEqual({
contentType: expect.stringMatching('multipart/form-data; boundary='),
contentLength: '3723',
})

// Must send correct request headers.
expect(socketDataCallback).toHaveBeenNthCalledWith(
1,
expect.stringContaining('POST /upload HTTP/1.1\r\n')
)
})
Loading