diff options
author | 2023-06-06 11:09:16 -0400 | |
---|---|---|
committer | 2023-06-06 10:09:16 -0500 | |
commit | 4929332c3210d1634b8607c7736d9049860a2079 (patch) | |
tree | f232675fb2d6820e6262a0f50aaa49efa42e9e0e /packages/integrations/node/test/createOutgoingHttpHeaders.test.js | |
parent | 409c60028aaab09b8f2383ef5730531cd23db4ba (diff) | |
download | astro-4929332c3210d1634b8607c7736d9049860a2079.tar.gz astro-4929332c3210d1634b8607c7736d9049860a2079.tar.zst astro-4929332c3210d1634b8607c7736d9049860a2079.zip |
#7226 - fixes NodeJS adapter for multiple set-cookie headers (and other header issues) (#7227)
* Utilizes the new standard WebAPI Fetch Headers.getSetCookie() function
to safely handle multiple set-cookie headers when converting from a
WebAPI Response to a NodeJS ServerResponse
Modifies the existing nodeMiddleware logic which first set AstroCookies
on ServerResponse.setHeader(...) and then called
ServerResponse.writeHead(status, Response.headers) which means any that
if the WebAPI Response had any set-cookie headers on it, they would
replace anything from AstroCookies.
The new logic delegates appending AstroCookie values onto the WebAPI
Response Headers object, so that a single unified function safely
converts the WebAPI Response Headers into a NodeJS compatible
OutgoingHttpHeaders object utilizing the new standard
Headers.getSetCookie() function provided by the undici WebAPI polyfills.
Plus extensive test coverage.
* #7226 - changeset for NodeJS adapter set-cookie fix
* fixing all double quotes to single quotes
---------
Co-authored-by: Alex Sherwin <alex.sherwin@acadia.inc>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/node/test/createOutgoingHttpHeaders.test.js')
-rw-r--r-- | packages/integrations/node/test/createOutgoingHttpHeaders.test.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/packages/integrations/node/test/createOutgoingHttpHeaders.test.js b/packages/integrations/node/test/createOutgoingHttpHeaders.test.js new file mode 100644 index 000000000..1876c9a00 --- /dev/null +++ b/packages/integrations/node/test/createOutgoingHttpHeaders.test.js @@ -0,0 +1,78 @@ +import { expect } from 'chai'; + +import { createOutgoingHttpHeaders } from '../dist/createOutgoingHttpHeaders.js'; + +describe('createOutgoingHttpHeaders', () => { + + it('undefined input headers', async () => { + const result = createOutgoingHttpHeaders(undefined); + expect(result).to.equal(undefined); + }); + + it('null input headers', async () => { + const result = createOutgoingHttpHeaders(undefined); + expect(result).to.equal(undefined); + }); + + it('Empty Headers', async () => { + const headers = new Headers(); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.equal(undefined); + }); + + it('Headers with single key', async () => { + const headers = new Headers(); + headers.append('x-test', 'hello world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ 'x-test': 'hello world' }); + }); + + it('Headers with multiple keys', async () => { + const headers = new Headers(); + headers.append('x-test1', 'hello'); + headers.append('x-test2', 'world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ 'x-test1': 'hello', 'x-test2': 'world' }); + }); + + it('Headers with multiple values (not set-cookie)', async () => { + const headers = new Headers(); + headers.append('x-test', 'hello'); + headers.append('x-test', 'world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ 'x-test': 'hello, world' }); + }); + + it('Headers with multiple values (set-cookie special case)', async () => { + const headers = new Headers(); + headers.append('set-cookie', 'hello'); + headers.append('set-cookie', 'world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] }); + }); + + it('Headers with multiple values (set-cookie case handling)', async () => { + const headers = new Headers(); + headers.append('Set-cookie', 'hello'); + headers.append('Set-Cookie', 'world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] }); + }); + + it('Headers with all use cases', async () => { + const headers = new Headers(); + headers.append('x-single', 'single'); + headers.append('x-triple', 'one'); + headers.append('x-triple', 'two'); + headers.append('x-triple', 'three'); + headers.append('Set-cookie', 'hello'); + headers.append('Set-Cookie', 'world'); + const result = createOutgoingHttpHeaders(headers); + expect(result).to.deep.equal({ + 'x-single': 'single', + 'x-triple': 'one, two, three', + 'set-cookie': ['hello', 'world'], + }); + }); + +});
\ No newline at end of file |