-
-
Notifications
You must be signed in to change notification settings - Fork 163
Fix: Correctly pass http version to the ServerResponse in node #756
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
Open
RobinVdBroeck
wants to merge
3
commits into
mswjs:main
Choose a base branch
from
RobinVdBroeck:fix/pass-http-version-node
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -60,6 +60,8 @@ export class MockHttpSocket extends MockSocket { | |
| private request?: Request | ||
| private requestParser: HTTPParser<0> | ||
| private requestStream?: Readable | ||
| private httpVersionMajor?: number | ||
| private httpVersionMinor?: number | ||
| private shouldKeepAlive?: boolean | ||
|
|
||
| private socketState: 'unknown' | 'mock' | 'passthrough' = 'unknown' | ||
|
|
@@ -340,7 +342,20 @@ export class MockHttpSocket extends MockSocket { | |
|
|
||
| // Create a `ServerResponse` instance to delegate HTTP message parsing, | ||
| // Transfer-Encoding, and other things to Node.js internals. | ||
| const serverResponse = new ServerResponse(new IncomingMessage(this)) | ||
| const incomingMessage = new IncomingMessage(this) | ||
|
|
||
| // Set the HTTP version on the IncomingMessage. | ||
| // This is critical for Node.js to correctly allow keep alive. | ||
| // By default, nodejs sets keepalive to false if the HttpVersion is not at least | ||
| // 1.1: https://github.com/nodejs/node/blob/70f6b58ac655234435a99d72b857dd7b316d34bf/lib/_http_server.js#L211C1-L211C34 | ||
| // The version should normally already have been set by the onRequestStart method | ||
| if(this.httpVersionMajor != null && this.httpVersionMinor != null) { | ||
| incomingMessage.httpVersion = `${this.httpVersionMajor}.${this.httpVersionMinor}` | ||
| incomingMessage.httpVersionMajor = this.httpVersionMajor | ||
| incomingMessage.httpVersionMinor = this.httpVersionMinor | ||
| } | ||
|
|
||
| const serverResponse = new ServerResponse(incomingMessage) | ||
|
|
||
| /** | ||
| * Assign a mock socket instance to the server response to | ||
|
|
@@ -521,6 +536,8 @@ export class MockHttpSocket extends MockSocket { | |
| ____, | ||
| shouldKeepAlive | ||
| ) => { | ||
| this.httpVersionMajor = versionMajor | ||
|
Member
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. A single socket instance might be reused across different requests (decided by the agent). If we decide to go with this approach, we need to reset these properties at some point (e.g. at new request start). |
||
| this.httpVersionMinor = versionMinor | ||
| this.shouldKeepAlive = shouldKeepAlive | ||
|
|
||
| const url = new URL(path || '', this.baseUrl) | ||
|
|
@@ -641,6 +658,7 @@ export class MockHttpSocket extends MockSocket { | |
| status, | ||
| statusText | ||
| ) => { | ||
|
|
||
| const headers = FetchResponse.parseRawHeaders([ | ||
| ...this.responseRawHeadersBuffer, | ||
| ...(rawHeaders || []), | ||
|
|
@@ -708,4 +726,4 @@ export class MockHttpSocket extends MockSocket { | |
| this.responseStream.push(null) | ||
| } | ||
| } | ||
| } | ||
| } | ||
214 changes: 214 additions & 0 deletions
214
test/modules/http/response/http-response-connection-headers.test.ts
This file contains hidden or 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,214 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
| import http from 'http' | ||
| import https from 'https' | ||
| import { HttpServer } from '@open-draft/test-server/http' | ||
| import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
| import { waitForClientRequest } from '../../../helpers' | ||
|
|
||
| const httpServer = new HttpServer((app) => { | ||
| app.get('/', (_req, res) => { | ||
| res.status(200).send('response with keep-alive') | ||
| }) | ||
|
|
||
| app.get('/stream', (_req, res) => { | ||
| res.write('chunk1') | ||
| setTimeout(() => { | ||
| res.write('chunk2') | ||
| res.end() | ||
| }, 10) | ||
| }) | ||
| }) | ||
|
|
||
| const interceptor = new ClientRequestInterceptor() | ||
|
|
||
| beforeAll(async () => { | ||
| interceptor.apply() | ||
| await httpServer.listen() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| interceptor.removeAllListeners() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| interceptor.dispose() | ||
| await httpServer.close() | ||
| }) | ||
|
|
||
| it('responds to an HTTP request with connection: keep-alive header', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| controller.respondWith( | ||
| new Response('mock response', { | ||
| status: 200, | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| const request = http.get('http://localhost/', { | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| }, | ||
| }) | ||
|
|
||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.headers.connection).toBe('keep-alive') | ||
| expect(await text()).toBe('mock response') | ||
| }) | ||
|
|
||
| it('responds to an HTTP request with connection: close header', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| controller.respondWith( | ||
| new Response('mock response', { | ||
| status: 200, | ||
| headers: { | ||
| Connection: 'close', | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| const request = http.get('http://localhost/', { | ||
| headers: { | ||
| Connection: 'close', | ||
| }, | ||
| }) | ||
|
|
||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.headers.connection).toBe('close') | ||
| expect(await text()).toBe('mock response') | ||
| }) | ||
|
|
||
| it('responds to an HTTP request without connection header', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| controller.respondWith( | ||
| new Response('mock response', { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| const request = http.get('http://localhost/') | ||
|
|
||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(await text()).toBe('mock response') | ||
| }) | ||
|
|
||
| it('responds to an HTTPS request with connection: keep-alive header', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| controller.respondWith( | ||
| new Response('mock response https', { | ||
| status: 200, | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| const request = https.get('https://localhost/', { | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| }, | ||
| }) | ||
|
|
||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.headers.connection).toBe('keep-alive') | ||
| expect(await text()).toBe('mock response https') | ||
| }) | ||
|
|
||
| it('responds to a streaming request with connection: keep-alive header', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| const encoder = new TextEncoder() | ||
| const stream = new ReadableStream({ | ||
| start(streamController) { | ||
| streamController.enqueue(encoder.encode('chunk1')) | ||
| setTimeout(() => { | ||
| streamController.enqueue(encoder.encode('chunk2')) | ||
| streamController.close() | ||
| }, 10) | ||
| }, | ||
| }) | ||
|
|
||
| controller.respondWith( | ||
| new Response(stream, { | ||
| status: 200, | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| const request = http.get('http://localhost/', { | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| }, | ||
| }) | ||
|
|
||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.headers.connection).toBe('keep-alive') | ||
| expect(await text()).toBe('chunk1chunk2') | ||
| }) | ||
|
|
||
| it('handles multiple sequential requests with keep-alive', async () => { | ||
| interceptor.on('request', ({ controller }) => { | ||
| controller.respondWith( | ||
| new Response('mock response sequential', { | ||
| status: 200, | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| 'Content-Type': 'text/plain', | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| // First request | ||
| const request1 = http.get('http://localhost/', { | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| }, | ||
| }) | ||
|
|
||
| const { res: res1, text: text1 } = await waitForClientRequest(request1) | ||
|
|
||
| expect(res1.statusCode).toBe(200) | ||
| expect(res1.headers.connection).toBe('keep-alive') | ||
| expect(await text1()).toBe('mock response sequential') | ||
|
|
||
| // Second request | ||
| const request2 = http.get('http://localhost/', { | ||
| headers: { | ||
| Connection: 'keep-alive', | ||
| }, | ||
| }) | ||
|
|
||
| const { res: res2, text: text2 } = await waitForClientRequest(request2) | ||
|
|
||
| expect(res2.statusCode).toBe(200) | ||
| expect(res2.headers.connection).toBe('keep-alive') | ||
| expect(await text2()).toBe('mock response sequential') | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hm, this is interesting! From what I gathered, this
IncomingMessagewas purely internal to help us handle the mock response and shouldn't influence the user-facing behaviors in any way. So you say it does?This might be an indicator that we should consider migrating from this.
All
ServerResponsedoes here is help us parse out the response. User-facingClientRequestconstructs its own instances, includingIncomingMessage, and ours never leaks or affects the user-facing data in any way. At least, that is the intention.