Skip to content

Commit

Permalink
feat: add support for request headers & cookies (#373)
Browse files Browse the repository at this point in the history
* feat(runtime-handler): handle incoming headers and cookies (#293)

* feat(handler): update header and cookie support for Response (#296)

* feat(runtime-types): add cookie/header support for types (#297)

* build: adjust dependency imports for successful build (#314)

* chore(runtime-handler): add artifical rc-version bump

* chore(release): publish %s

 - [email protected]
 - @twilio-labs/[email protected]
 - @twilio/[email protected]
 - @twilio-labs/[email protected]
 - [email protected]

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

* chore(release): publish %s

 - @twilio/[email protected]

* chore(release): publish %s

 - @twilio/[email protected]

* Update package.json

* Update package.json

Co-authored-by: Phil Nash <[email protected]>
  • Loading branch information
dkundel and philnash authored Oct 14, 2021
1 parent d2321f3 commit 989307d
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 49 deletions.
7 changes: 4 additions & 3 deletions packages/create-twilio-function/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [3.2.2-beta.0](https://github.com/twilio-labs/serverless-toolkit/compare/[email protected]@3.2.2-beta.0) (2021-09-25)

**Note:** Version bump only for package create-twilio-function

## [3.2.2-beta.0](https://github.com/twilio-labs/serverless-toolkit/compare/[email protected]@3.2.2-beta.0) (2021-09-25)

**Note:** Version bump only for package create-twilio-function



Expand Down Expand Up @@ -36,10 +36,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

## [3.1.2](https://github.com/twilio-labs/serverless-toolkit/compare/[email protected]@3.1.2) (2021-07-14)

**Note:** Version bump only for package create-twilio-function

**Note:** Version bump only for package create-twilio-function


## [3.1.2-rc.0](https://github.com/twilio-labs/serverless-toolkit/compare/[email protected]@3.1.2-rc.0) (2021-07-14)


## [3.1.1](https://github.com/twilio-labs/serverless-toolkit/compare/[email protected]@3.1.1) (2021-06-30)
Expand Down
7 changes: 2 additions & 5 deletions packages/plugin-serverless/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

**Note:** Version bump only for package @twilio-labs/plugin-serverless





## [2.2.2](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio-labs/[email protected]...@twilio-labs/[email protected]) (2021-07-28)

**Note:** Version bump only for package @twilio-labs/plugin-serverless
Expand All @@ -35,8 +31,9 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
* add new env commands ([#290](https://github.com/twilio-labs/serverless-toolkit/issues/290)) ([7d11a03](https://github.com/twilio-labs/serverless-toolkit/commit/7d11a03aa5f02c6ac06147c2796f7e8c9964396e))


## [2.1.2-rc.0](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio-labs/[email protected]...@twilio-labs/[email protected]) (2021-07-14)


**Note:** Version bump only for package @twilio-labs/plugin-serverless

## [2.1.1](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio-labs/[email protected]...@twilio-labs/[email protected]) (2021-06-30)

Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-serverless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ OPTIONS
--typescript Initialize your Serverless project with TypeScript
```


_See code: [src/commands/serverless/init.js](https://github.com/twilio-labs/serverless-toolkit/blob/v2.2.3-beta.0/src/commands/serverless/init.js)_

## `twilio serverless:list [TYPES]`
Expand Down Expand Up @@ -407,6 +408,7 @@ OPTIONS
--to=to [Alias for "environment"]
```


_See code: [src/commands/serverless/list.js](https://github.com/twilio-labs/serverless-toolkit/blob/v2.2.3-beta.0/src/commands/serverless/list.js)_

## `twilio serverless:list-templates`
Expand Down
24 changes: 24 additions & 0 deletions packages/runtime-handler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.2.0-rc.3](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-08-03)

**Note:** Version bump only for package @twilio/runtime-handler





# [1.2.0-rc.2](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-07-28)


### Bug Fixes

* **runtime-handler:** using set-cookie now sets cookie header ([#332](https://github.com/twilio-labs/serverless-toolkit/issues/332)) ([6f65bc3](https://github.com/twilio-labs/serverless-toolkit/commit/6f65bc3bb692b8bd0b21d932f66ae394000e51a9))


# [1.2.0-beta.0](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-09-25)


Expand All @@ -21,6 +37,14 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline



# [1.2.0-rc.1](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-07-14)


### Features

* **handler:** update header and cookie support for Response ([#296](https://github.com/twilio-labs/serverless-toolkit/issues/296)) ([e9ef02e](https://github.com/twilio-labs/serverless-toolkit/commit/e9ef02ed9e10635623f462db6f53de3669ffaf0b))
* **runtime-handler:** handle incoming headers and cookies ([#293](https://github.com/twilio-labs/serverless-toolkit/issues/293)) ([62ff180](https://github.com/twilio-labs/serverless-toolkit/commit/62ff1801db6a121122fcd944a855ad7f038cafe4))


## [1.1.2](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-07-19)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ test('has correct defaults', () => {
const response = new Response();
expect(response['body']).toBeNull();
expect(response['statusCode']).toBe(200);
expect(response['headers']).toEqual({});
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
});

test('sets status code, body and headers from constructor', () => {
Expand All @@ -24,6 +26,7 @@ test('sets status code, body and headers from constructor', () => {
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
'Set-Cookie': [],
});
});

Expand All @@ -45,7 +48,9 @@ test('sets body correctly', () => {

test('sets headers correctly', () => {
const response = new Response();
expect(response['headers']).toEqual({});
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.setHeaders({
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
Expand All @@ -55,35 +60,152 @@ test('sets headers correctly', () => {
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
'Set-Cookie': [],
};
expect(response['headers']).toEqual(expected);
// @ts-ignore
response.setHeaders(undefined);
expect(response['headers']).toEqual(expected);
});

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

test('sets headers with an array of cookies', () => {
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('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({});
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': 'dkundel.com',
'Set-Cookie': [],
});
response.appendHeader('Content-Type', 'application/json');
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': 'dkundel.com',
'Content-Type': 'application/json',
'Set-Cookie': [],
});
});

test('appends a header correctly with no existing one', () => {
const response = new Response();
expect(response['headers']).toEqual({});
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
// @ts-ignore
response['headers'] = undefined;
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': 'dkundel.com',
'Set-Cookie': [],
});
});

test('appends multi value headers', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
response.appendHeader('Access-Control-Allow-Origin', 'philna.sh');
response.appendHeader('Access-Control-Allow-Methods', 'GET');
response.appendHeader('Access-Control-Allow-Methods', 'DELETE');
response.appendHeader('Access-Control-Allow-Methods', ['PUT', 'POST']);
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': ['dkundel.com', 'philna.sh'],
'Access-Control-Allow-Methods': ['GET', 'DELETE', 'PUT', 'POST'],
'Set-Cookie': [],
});
});

test('sets a single cookie correctly', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.setCookie('name', 'value');
expect(response['headers']).toEqual({
'Set-Cookie': ['name=value'],
});
});

test('sets a cookie with attributes', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.setCookie('Hello', 'World', [
'HttpOnly',
'Secure',
'SameSite=Strict',
'Max-Age=86400',
]);
expect(response['headers']).toEqual({
'Set-Cookie': ['Hello=World;HttpOnly;Secure;SameSite=Strict;Max-Age=86400'],
});
});

test('removes a cookie', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});
response.setCookie('Hello', 'World', [
'HttpOnly',
'Secure',
'SameSite=Strict',
'Max-Age=86400',
]);
response.removeCookie('Hello');
expect(response['headers']).toEqual({
'Set-Cookie': ['Hello=;Max-Age=0'],
});
});

Expand All @@ -107,6 +229,16 @@ test('appendHeader returns the response', () => {
expect(response.appendHeader('X-Test', 'Hello')).toBe(response);
});

test('setCookie returns the response', () => {
const response = new Response();
expect(response.setCookie('name', 'value')).toBe(response);
});

test('removeCookie returns the response', () => {
const response = new Response();
expect(response.removeCookie('name')).toBe(response);
});

test('calls express response correctly', () => {
const mockRes = {
status: jest.fn(),
Expand All @@ -121,7 +253,10 @@ test('calls express response correctly', () => {

expect(mockRes.send).toHaveBeenCalledWith(`I'm a teapot!`);
expect(mockRes.status).toHaveBeenCalledWith(418);
expect(mockRes.set).toHaveBeenCalledWith({ 'Content-Type': 'text/plain' });
expect(mockRes.set).toHaveBeenCalledWith({
'Content-Type': 'text/plain',
'Set-Cookie': [],
});
});

test('serializes a response', () => {
Expand All @@ -134,7 +269,10 @@ test('serializes a response', () => {

expect(serialized.body).toEqual("I'm a teapot!");
expect(serialized.statusCode).toEqual(418);
expect(serialized.headers).toEqual({ 'Content-Type': 'text/plain' });
expect(serialized.headers).toEqual({
'Content-Type': 'text/plain',
'Set-Cookie': [],
});
});

test('serializes a response with content type set to application/json', () => {
Expand All @@ -149,5 +287,8 @@ test('serializes a response with content type set to application/json', () => {
JSON.stringify({ url: 'https://dkundel.com' })
);
expect(serialized.statusCode).toEqual(200);
expect(serialized.headers).toEqual({ 'Content-Type': 'application/json' });
expect(serialized.headers).toEqual({
'Content-Type': 'application/json',
'Set-Cookie': [],
});
});
Loading

0 comments on commit 989307d

Please sign in to comment.