Skip to content

Commit 989307d

Browse files
dkundelphilnash
andauthored
feat: add support for request headers & cookies (#373)
* 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]>
1 parent d2321f3 commit 989307d

File tree

18 files changed

+547
-49
lines changed

18 files changed

+547
-49
lines changed

packages/create-twilio-function/CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

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

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

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

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

1212

1313

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

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

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

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

4242

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

4445

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

packages/plugin-serverless/CHANGELOG.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
77

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

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

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

3733

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

39-
36+
**Note:** Version bump only for package @twilio-labs/plugin-serverless
4037

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

packages/plugin-serverless/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ OPTIONS
357357
--typescript Initialize your Serverless project with TypeScript
358358
```
359359

360+
360361
_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)_
361362

362363
## `twilio serverless:list [TYPES]`
@@ -407,6 +408,7 @@ OPTIONS
407408
--to=to [Alias for "environment"]
408409
```
409410

411+
410412
_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)_
411413

412414
## `twilio serverless:list-templates`

packages/runtime-handler/CHANGELOG.md

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
# [1.2.0-rc.3](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-08-03)
7+
8+
**Note:** Version bump only for package @twilio/runtime-handler
9+
10+
11+
12+
13+
14+
# [1.2.0-rc.2](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-07-28)
15+
16+
17+
### Bug Fixes
18+
19+
* **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))
20+
21+
622
# [1.2.0-beta.0](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-09-25)
723

824

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

2238

2339

40+
# [1.2.0-rc.1](https://github.com/twilio-labs/serverless-toolkit/compare/@twilio/[email protected]...@twilio/[email protected]) (2021-07-14)
41+
42+
43+
### Features
44+
45+
* **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))
46+
* **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))
47+
2448

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

packages/runtime-handler/__tests__/dev-runtime/internal/response.test.ts

+148-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ test('has correct defaults', () => {
55
const response = new Response();
66
expect(response['body']).toBeNull();
77
expect(response['statusCode']).toBe(200);
8-
expect(response['headers']).toEqual({});
8+
expect(response['headers']).toEqual({
9+
'Set-Cookie': [],
10+
});
911
});
1012

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

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

4649
test('sets headers correctly', () => {
4750
const response = new Response();
48-
expect(response['headers']).toEqual({});
51+
expect(response['headers']).toEqual({
52+
'Set-Cookie': [],
53+
});
4954
response.setHeaders({
5055
'Access-Control-Allow-Origin': 'example.com',
5156
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
@@ -55,35 +60,152 @@ test('sets headers correctly', () => {
5560
'Access-Control-Allow-Origin': 'example.com',
5661
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
5762
'Access-Control-Allow-Headers': 'Content-Type',
63+
'Set-Cookie': [],
5864
};
5965
expect(response['headers']).toEqual(expected);
6066
// @ts-ignore
6167
response.setHeaders(undefined);
6268
expect(response['headers']).toEqual(expected);
6369
});
6470

71+
test('sets headers with string cookies', () => {
72+
const response = new Response();
73+
expect(response['headers']).toEqual({
74+
'Set-Cookie': [],
75+
});
76+
response.setHeaders({
77+
'Access-Control-Allow-Origin': 'example.com',
78+
'Set-Cookie': 'Hi=Bye',
79+
});
80+
const expected = {
81+
'Access-Control-Allow-Origin': 'example.com',
82+
'Set-Cookie': ['Hi=Bye'],
83+
};
84+
expect(response['headers']).toEqual(expected);
85+
});
86+
87+
test('sets headers with an array of cookies', () => {
88+
const response = new Response();
89+
expect(response['headers']).toEqual({
90+
'Set-Cookie': [],
91+
});
92+
response.setHeaders({
93+
'Access-Control-Allow-Origin': 'example.com',
94+
'Set-Cookie': ['Hi=Bye', 'Hello=World'],
95+
});
96+
const expected = {
97+
'Access-Control-Allow-Origin': 'example.com',
98+
'Set-Cookie': ['Hi=Bye', 'Hello=World'],
99+
};
100+
expect(response['headers']).toEqual(expected);
101+
});
102+
103+
test('sets cookies with lower case set-cookie', () => {
104+
const response = new Response();
105+
expect(response['headers']).toEqual({
106+
'Set-Cookie': [],
107+
});
108+
response.setHeaders({
109+
'Access-Control-Allow-Origin': 'example.com',
110+
'set-cookie': ['Hi=Bye', 'Hello=World'],
111+
});
112+
const expected = {
113+
'Access-Control-Allow-Origin': 'example.com',
114+
'Set-Cookie': ['Hi=Bye', 'Hello=World'],
115+
};
116+
expect(response['headers']).toEqual(expected);
117+
});
118+
65119
test('appends a new header correctly', () => {
66120
const response = new Response();
67-
expect(response['headers']).toEqual({});
121+
expect(response['headers']).toEqual({
122+
'Set-Cookie': [],
123+
});
68124
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
69125
expect(response['headers']).toEqual({
70126
'Access-Control-Allow-Origin': 'dkundel.com',
127+
'Set-Cookie': [],
71128
});
72129
response.appendHeader('Content-Type', 'application/json');
73130
expect(response['headers']).toEqual({
74131
'Access-Control-Allow-Origin': 'dkundel.com',
75132
'Content-Type': 'application/json',
133+
'Set-Cookie': [],
76134
});
77135
});
78136

79137
test('appends a header correctly with no existing one', () => {
80138
const response = new Response();
81-
expect(response['headers']).toEqual({});
139+
expect(response['headers']).toEqual({
140+
'Set-Cookie': [],
141+
});
82142
// @ts-ignore
83143
response['headers'] = undefined;
84144
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
85145
expect(response['headers']).toEqual({
86146
'Access-Control-Allow-Origin': 'dkundel.com',
147+
'Set-Cookie': [],
148+
});
149+
});
150+
151+
test('appends multi value headers', () => {
152+
const response = new Response();
153+
expect(response['headers']).toEqual({
154+
'Set-Cookie': [],
155+
});
156+
response.appendHeader('Access-Control-Allow-Origin', 'dkundel.com');
157+
response.appendHeader('Access-Control-Allow-Origin', 'philna.sh');
158+
response.appendHeader('Access-Control-Allow-Methods', 'GET');
159+
response.appendHeader('Access-Control-Allow-Methods', 'DELETE');
160+
response.appendHeader('Access-Control-Allow-Methods', ['PUT', 'POST']);
161+
expect(response['headers']).toEqual({
162+
'Access-Control-Allow-Origin': ['dkundel.com', 'philna.sh'],
163+
'Access-Control-Allow-Methods': ['GET', 'DELETE', 'PUT', 'POST'],
164+
'Set-Cookie': [],
165+
});
166+
});
167+
168+
test('sets a single cookie correctly', () => {
169+
const response = new Response();
170+
expect(response['headers']).toEqual({
171+
'Set-Cookie': [],
172+
});
173+
response.setCookie('name', 'value');
174+
expect(response['headers']).toEqual({
175+
'Set-Cookie': ['name=value'],
176+
});
177+
});
178+
179+
test('sets a cookie with attributes', () => {
180+
const response = new Response();
181+
expect(response['headers']).toEqual({
182+
'Set-Cookie': [],
183+
});
184+
response.setCookie('Hello', 'World', [
185+
'HttpOnly',
186+
'Secure',
187+
'SameSite=Strict',
188+
'Max-Age=86400',
189+
]);
190+
expect(response['headers']).toEqual({
191+
'Set-Cookie': ['Hello=World;HttpOnly;Secure;SameSite=Strict;Max-Age=86400'],
192+
});
193+
});
194+
195+
test('removes a cookie', () => {
196+
const response = new Response();
197+
expect(response['headers']).toEqual({
198+
'Set-Cookie': [],
199+
});
200+
response.setCookie('Hello', 'World', [
201+
'HttpOnly',
202+
'Secure',
203+
'SameSite=Strict',
204+
'Max-Age=86400',
205+
]);
206+
response.removeCookie('Hello');
207+
expect(response['headers']).toEqual({
208+
'Set-Cookie': ['Hello=;Max-Age=0'],
87209
});
88210
});
89211

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

232+
test('setCookie returns the response', () => {
233+
const response = new Response();
234+
expect(response.setCookie('name', 'value')).toBe(response);
235+
});
236+
237+
test('removeCookie returns the response', () => {
238+
const response = new Response();
239+
expect(response.removeCookie('name')).toBe(response);
240+
});
241+
110242
test('calls express response correctly', () => {
111243
const mockRes = {
112244
status: jest.fn(),
@@ -121,7 +253,10 @@ test('calls express response correctly', () => {
121253

122254
expect(mockRes.send).toHaveBeenCalledWith(`I'm a teapot!`);
123255
expect(mockRes.status).toHaveBeenCalledWith(418);
124-
expect(mockRes.set).toHaveBeenCalledWith({ 'Content-Type': 'text/plain' });
256+
expect(mockRes.set).toHaveBeenCalledWith({
257+
'Content-Type': 'text/plain',
258+
'Set-Cookie': [],
259+
});
125260
});
126261

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

135270
expect(serialized.body).toEqual("I'm a teapot!");
136271
expect(serialized.statusCode).toEqual(418);
137-
expect(serialized.headers).toEqual({ 'Content-Type': 'text/plain' });
272+
expect(serialized.headers).toEqual({
273+
'Content-Type': 'text/plain',
274+
'Set-Cookie': [],
275+
});
138276
});
139277

140278
test('serializes a response with content type set to application/json', () => {
@@ -149,5 +287,8 @@ test('serializes a response with content type set to application/json', () => {
149287
JSON.stringify({ url: 'https://dkundel.com' })
150288
);
151289
expect(serialized.statusCode).toEqual(200);
152-
expect(serialized.headers).toEqual({ 'Content-Type': 'application/json' });
290+
expect(serialized.headers).toEqual({
291+
'Content-Type': 'application/json',
292+
'Set-Cookie': [],
293+
});
153294
});

0 commit comments

Comments
 (0)