-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(start-server-core): keep response context headers on non-2xx responses #7936
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
kdelay
wants to merge
4
commits into
TanStack:main
Choose a base branch
from
kdelay:fix/issue-7914-non-2xx-response-headers
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.
+157
−15
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d500af
fix(start-server-core): keep response context headers on non-2xx resp…
kdelay 5936014
test(start-server-core): cover a passed-through fetch response with i…
kdelay 0f46b86
test(start-server-core): fail the fetch response setup when the serve…
kdelay e46c8a9
chore(start-server-core): add changeset for non-2xx response header fix
kdelay 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
114 changes: 114 additions & 0 deletions
114
packages/start-server-core/tests/request-response.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,114 @@ | ||
| // @vitest-environment node | ||
|
|
||
| import { createServer } from 'node:http' | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
| import { | ||
| getResponseHeaders, | ||
| requestHandler, | ||
| setCookie, | ||
| setResponseStatus, | ||
| } from '../src/request-response' | ||
| import type { Server } from 'node:http' | ||
|
|
||
| function run( | ||
| handler: () => Response | Promise<Response>, | ||
| ): Promise<Response> | Response { | ||
| return requestHandler(handler)(new Request('http://localhost/'), {}) | ||
| } | ||
|
|
||
| describe('response context headers', () => { | ||
| it('merges response context headers into a 2xx response', async () => { | ||
| const response = await run(() => { | ||
| getResponseHeaders().set('x-custom-header', 'true') | ||
| return new Response('ok') | ||
| }) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(response.headers.get('x-custom-header')).toBe('true') | ||
| }) | ||
|
|
||
| it('merges response context headers into a non-2xx response', async () => { | ||
| const response = await run(() => { | ||
| getResponseHeaders().set('x-custom-header', 'true') | ||
| setResponseStatus(401) | ||
| return new Response('nope', { status: 401 }) | ||
| }) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(response.headers.get('x-custom-header')).toBe('true') | ||
| }) | ||
|
|
||
| it('keeps the set-cookie headers of both the event and the response', async () => { | ||
| const response = await run(() => { | ||
| setCookie('from-event', 'a') | ||
| return new Response('nope', { | ||
| status: 500, | ||
| headers: { 'set-cookie': 'from-response=b' }, | ||
| }) | ||
| }) | ||
|
|
||
| expect(response.headers.getSetCookie()).toEqual([ | ||
| 'from-response=b', | ||
| 'from-event=a; Path=/', | ||
| ]) | ||
| }) | ||
|
|
||
| it('merges response context headers into an immutable non-2xx response', async () => { | ||
| const response = await run(() => { | ||
| getResponseHeaders().set('x-custom-header', 'true') | ||
| return Response.redirect('http://localhost/next', 302) | ||
| }) | ||
|
|
||
| expect(response.status).toBe(302) | ||
| expect(response.headers.get('location')).toBe('http://localhost/next') | ||
| expect(response.headers.get('x-custom-header')).toBe('true') | ||
| }) | ||
|
|
||
| describe('a fetch response passed through the handler', () => { | ||
| let server: Server | ||
| let origin: string | ||
|
|
||
| beforeAll(async () => { | ||
| server = createServer((_request, response) => { | ||
| response.writeHead(401, { 'set-cookie': ['from-upstream=b'] }) | ||
| response.end('nope') | ||
| }) | ||
| await new Promise<void>((resolve) => | ||
| server.listen(0, '127.0.0.1', resolve), | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| const address = server.address() | ||
| origin = `http://127.0.0.1:${typeof address === 'object' && address ? address.port : 0}` | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await new Promise<void>((resolve, reject) => | ||
| server.close((error) => (error ? reject(error) : resolve())), | ||
| ) | ||
| }) | ||
|
|
||
| it('keeps its own set-cookie header when the rebuild happens', async () => { | ||
| const response = await run(async () => { | ||
| setCookie('from-event', 'a') | ||
| return fetch(origin) | ||
| }) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(response.headers.getSetCookie()).toEqual([ | ||
| 'from-upstream=b', | ||
| 'from-event=a; Path=/', | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| it('lets the response context override a header set on the response', async () => { | ||
| const response = await run(() => { | ||
| getResponseHeaders().set('x-custom-header', 'from-event') | ||
| return new Response('nope', { | ||
| status: 404, | ||
| headers: { 'x-custom-header': 'from-response' }, | ||
| }) | ||
| }) | ||
|
|
||
| expect(response.headers.get('x-custom-header')).toBe('from-event') | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.