Skip to content

fix(runtime-handler): using set-cookie now sets cookie header #332

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

Merged
merged 1 commit into from
Jul 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ test('sets headers with an array of cookies', () => {
expect(response['headers']).toEqual(expected);
});

test('sets cookies with lower case set-cookie', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.setHeaders({
'Access-Control-Allow-Origin': 'example.com',
'set-cookie': ['Hi=Bye', 'Hello=World'],
});
const expected = {
'Access-Control-Allow-Origin': 'example.com',
'Set-Cookie': ['Hi=Bye', 'Hello=World'],
};
expect(response['headers']).toEqual(expected);
});

test('appends a new header correctly', () => {
const response = new Response();
expect(response['headers']).toEqual({
Expand Down
33 changes: 18 additions & 15 deletions packages/runtime-handler/src/dev-runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,35 @@ export class Response implements TwilioResponse {
if (typeof headersObject !== 'object') {
return this;
}
if (!(COOKIE_HEADER in headersObject)) {
headersObject[COOKIE_HEADER] = [];
}

const cookieHeader = headersObject[COOKIE_HEADER];
if (!Array.isArray(cookieHeader)) {
headersObject[COOKIE_HEADER] = [cookieHeader];
this.headers = {};
for (const header in headersObject) {
this.appendHeader(header, headersObject[header]);
}
this.headers = headersObject;

return this;
}

appendHeader(key: string, value: HeaderValue): Response {
log('Appending header for %s', key, value);
this.headers = this.headers || {};
const existingValue = this.headers[key];
let newHeaderValue: HeaderValue = [];
if (existingValue) {
newHeaderValue = [existingValue, value].flat();
if (newHeaderValue) {
this.headers[key] = newHeaderValue;
if (key.toLowerCase() === COOKIE_HEADER.toLowerCase()) {
const existingValue = this.headers[COOKIE_HEADER];
if (existingValue) {
newHeaderValue = [existingValue, value].flat();
if (newHeaderValue) {
this.headers[COOKIE_HEADER] = newHeaderValue;
}
} else {
this.headers[COOKIE_HEADER] = Array.isArray(value) ? value: [value];
}
} else {
if (key === COOKIE_HEADER && !Array.isArray(value)) {
this.headers[key] = [value];
const existingValue = this.headers[key];
if (existingValue) {
newHeaderValue = [existingValue, value].flat();
if (newHeaderValue) {
this.headers[key] = newHeaderValue;
}
} else {
this.headers[key] = value;
}
Expand Down