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

Ensure multiple cookies set in dev result in multiple set-cookie headers #6973

Merged
merged 6 commits into from
May 3, 2023
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
5 changes: 5 additions & 0 deletions .changeset/twelve-feet-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Ensure multiple cookies set in dev result in multiple set-cookie headers
7 changes: 4 additions & 3 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ export async function writeWebResponse(res: http.ServerResponse, webResponse: Re

// Attach any set-cookie headers added via Astro.cookies.set()
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
setCookieHeaders.forEach((cookie) => {
headers.append('set-cookie', cookie);
});
if(setCookieHeaders.length) {
// Always use `res.setHeader` because headers.append causes them to be concatenated.
res.setHeader('set-cookie', setCookieHeaders);
}

const _headers = Object.fromEntries(headers.entries());

Expand Down
15 changes: 8 additions & 7 deletions packages/astro/test/fixtures/ssr-api-route/src/pages/login.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

export function post() {
const headers = new Headers();
headers.append('Set-Cookie', `foo=foo; HttpOnly`);
headers.append('Set-Cookie', `bar=bar; HttpOnly`);

/** @type {import('astro').APIRoute} */
export function post({ cookies }) {
cookies.set('foo', 'foo', {
httpOnly: true
});
cookies.set('bar', 'bar', {
httpOnly: true
});
return new Response('', {
status: 201,
headers,
});
}
32 changes: 28 additions & 4 deletions packages/astro/test/ssr-api-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import { File, FormData } from 'undici';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
import net from 'net';

describe('API routes in SSR', () => {
/** @type {import('./test-utils').Fixture} */
Expand Down Expand Up @@ -95,11 +96,34 @@ describe('API routes in SSR', () => {
});

it('Can set multiple headers of the same type', async () => {
const response = await fixture.fetch('/login', {
method: 'POST',
const response = await new Promise(resolve => {
let { port } = devServer.address;
let host = 'localhost';
let socket = new net.Socket();
socket.connect(port, host);
socket.on('connect', () => {
let rawRequest = `POST /login HTTP/1.1\r\nHost: ${host}\r\n\r\n`;
socket.write(rawRequest);
});

let rawResponse = '';
socket.setEncoding('utf-8')
socket.on('data', chunk => {
rawResponse += chunk.toString();
socket.destroy();
});
socket.on('close', () => {
resolve(rawResponse);
});
});
const setCookie = response.headers.get('set-cookie');
expect(setCookie).to.equal('foo=foo; HttpOnly, bar=bar; HttpOnly');

let count = 0;
let exp = /set-cookie\:/g;
while(exp.exec(response)) {
count++;
}

expect(count).to.equal(2, 'Found two seperate set-cookie response headers')
});
});
});