Skip to content

Commit c5ce62f

Browse files
committed
feat: use multiValueHeaders for requests and responses
1 parent 3a5c7ed commit c5ce62f

File tree

9 files changed

+196
-154
lines changed

9 files changed

+196
-154
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ npm install aws-serverless-express
1313

1414
```js
1515
// lambda.js
16-
'use strict'
1716
const awsServerlessExpress = require('aws-serverless-express')
1817
const app = require('./app')
1918
const servererlessExpress = awsServerlessExpress.configure({

__tests__/integration.js

Lines changed: 97 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ function clone (json) {
1313

1414
function makeEvent (eventOverrides) {
1515
const baseEvent = clone(apiGatewayEvent)
16-
const multiValueHeaders = Object.assign({}, baseEvent.multiValueHeaders, eventOverrides.multiValueHeaders)
17-
const root = Object.assign({}, baseEvent, eventOverrides)
16+
const multiValueHeaders = {
17+
...baseEvent.multiValueHeaders,
18+
...eventOverrides.multiValueHeaders
19+
}
20+
const root = {
21+
...baseEvent,
22+
...eventOverrides
23+
}
1824
root.multiValueHeaders = multiValueHeaders
1925
root.pathParameters.proxy = eventOverrides.path && eventOverrides.path.replace(/^\//, '')
2026
return root
@@ -23,9 +29,9 @@ function makeEvent (eventOverrides) {
2329
function expectedRootResponse () {
2430
return makeResponse({
2531
'multiValueHeaders': {
26-
'content-length': '3747',
27-
'content-type': 'text/html; charset=utf-8',
28-
'etag': 'W/"ea3-WawLnWdlaCO/ODv9DBVcX0ZTchw"'
32+
'content-length': ['3748'],
33+
'content-type': ['text/html; charset=utf-8'],
34+
'etag': ['W/"ea4-ct1r5H/kBAj6dO7y//hr9wiNLls"']
2935
}
3036
})
3137
}
@@ -37,13 +43,19 @@ function makeResponse (response) {
3743
'statusCode': 200
3844
}
3945
const baseHeaders = {
40-
'access-control-allow-origin': '*',
41-
'connection': 'close',
42-
'content-type': 'application/json; charset=utf-8',
43-
'x-powered-by': 'Express'
46+
'access-control-allow-origin': ['*'],
47+
'connection': ['close'],
48+
'content-type': ['application/json; charset=utf-8'],
49+
'x-powered-by': ['Express']
50+
}
51+
const multiValueHeaders = {
52+
...baseHeaders,
53+
...response.multiValueHeaders
54+
}
55+
const finalResponse = {
56+
...baseResponse,
57+
...response
4458
}
45-
const multiValueHeaders = Object.assign({}, baseHeaders, response.multiValueHeaders)
46-
const finalResponse = Object.assign({}, baseResponse, response)
4759
finalResponse.multiValueHeaders = multiValueHeaders
4860
return finalResponse
4961
}
@@ -60,7 +72,7 @@ describe('integration tests', () => {
6072
}), {
6173
succeed
6274
})
63-
expect(response.promise.then).toBeTruthy()
75+
expect(response.then).toBeTruthy()
6476
})
6577

6678
test('GET HTML (initial request)', (done) => {
@@ -102,8 +114,8 @@ describe('integration tests', () => {
102114
expect(response).toEqual(makeResponse({
103115
'body': '[{"id":1,"name":"Joe"},{"id":2,"name":"Jane"}]',
104116
'multiValueHeaders': {
105-
'content-length': '46',
106-
'etag': 'W/"2e-Lu6qxFOQSPFulDAGUFiiK6QgREo"'
117+
'content-length': ['46'],
118+
'etag': ['W/"2e-Lu6qxFOQSPFulDAGUFiiK6QgREo"']
107119
}
108120
}))
109121
done()
@@ -122,10 +134,10 @@ describe('integration tests', () => {
122134
expect(response.body.startsWith('<!DOCTYPE html>')).toBe(true)
123135
const expectedResponse = makeResponse({
124136
'multiValueHeaders': {
125-
'content-length': '151',
126-
'content-security-policy': "default-src 'self'",
127-
'content-type': 'text/html; charset=utf-8',
128-
'x-content-type-options': 'nosniff'
137+
'content-length': ['151'],
138+
'content-security-policy': ["default-src 'self'"],
139+
'content-type': ['text/html; charset=utf-8'],
140+
'x-content-type-options': ['nosniff']
129141
},
130142
statusCode: 404
131143
})
@@ -148,8 +160,8 @@ describe('integration tests', () => {
148160
expect(response).toEqual(makeResponse({
149161
'body': '{"id":1,"name":"Joe"}',
150162
'multiValueHeaders': {
151-
'content-length': '21',
152-
'etag': 'W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"'
163+
'content-length': ['21'],
164+
'etag': ['W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"']
153165
}
154166
}))
155167
done()
@@ -168,8 +180,8 @@ describe('integration tests', () => {
168180
expect(response).toEqual(makeResponse({
169181
'body': '{"id":1,"name":"Joe"}',
170182
'multiValueHeaders': {
171-
'content-length': '21',
172-
'etag': 'W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"'
183+
'content-length': ['21'],
184+
'etag': ['W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"']
173185
}
174186
}))
175187
done()
@@ -190,8 +202,8 @@ describe('integration tests', () => {
190202
expect(response).toEqual(makeResponse({
191203
'body': '{"id":1,"name":"Joe"}',
192204
'multiValueHeaders': {
193-
'content-length': '21',
194-
'etag': 'W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"'
205+
'content-length': ['21'],
206+
'etag': ['W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"']
195207
}
196208
}))
197209
done()
@@ -203,7 +215,7 @@ describe('integration tests', () => {
203215
}),
204216
resolutionMode: 'PROMISE'
205217
})
206-
.promise.then(succeed)
218+
.then(succeed)
207219
})
208220

209221
test('GET JSON single (resolutionMode = PROMISE; new server)', (done) => {
@@ -212,8 +224,8 @@ describe('integration tests', () => {
212224
expect(response).toEqual(makeResponse({
213225
'body': '{"id":1,"name":"Joe"}',
214226
'multiValueHeaders': {
215-
'content-length': '21',
216-
'etag': 'W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"'
227+
'content-length': ['21'],
228+
'etag': ['W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"']
217229
}
218230
}))
219231
newServererlessExpress.server.close()
@@ -224,7 +236,7 @@ describe('integration tests', () => {
224236
path: '/users/1',
225237
httpMethod: 'GET'
226238
})
227-
newServererlessExpress.handler(event).promise.then(succeed)
239+
newServererlessExpress.handler(event).then(succeed)
228240
})
229241

230242
test('GET JSON single 404', (done) => {
@@ -233,8 +245,8 @@ describe('integration tests', () => {
233245
expect(response).toEqual(makeResponse({
234246
'body': '{}',
235247
'multiValueHeaders': {
236-
'content-length': '2',
237-
'etag': 'W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"'
248+
'content-length': ['2'],
249+
'etag': ['W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"']
238250
},
239251
statusCode: 404
240252
}))
@@ -261,10 +273,10 @@ describe('integration tests', () => {
261273
expect(response).toEqual(makeResponse({
262274
'body': samLogoBase64,
263275
'multiValueHeaders': {
264-
'accept-ranges': 'bytes',
265-
'cache-control': 'public, max-age=0',
266-
'content-length': '15933',
267-
'content-type': 'image/png'
276+
'accept-ranges': ['bytes'],
277+
'cache-control': ['public, max-age=0'],
278+
'content-length': ['15933'],
279+
'content-type': ['image/png']
268280
},
269281
'isBase64Encoded': true
270282
}))
@@ -295,18 +307,19 @@ describe('integration tests', () => {
295307
expect(response).toEqual(makeResponse({
296308
'body': `{"id":3,"name":"${newName}"}`,
297309
'multiValueHeaders': {
298-
'content-length': '43',
299-
'etag': 'W/"2b-ksYHypm1DmDdjEzhtyiv73Bluqk"'
310+
'content-length': ['43'],
311+
'etag': ['W/"2b-ksYHypm1DmDdjEzhtyiv73Bluqk"']
300312
},
301313
statusCode: 201
302314
}))
303315
done()
304316
}
305-
serverlessExpress.handler(makeEvent({
317+
const event = makeEvent({
306318
path: '/users',
307319
httpMethod: 'POST',
308320
body: `{"name": "${newName}"}`
309-
}), {
321+
})
322+
serverlessExpress.handler(event, {
310323
succeed
311324
})
312325
})
@@ -317,8 +330,8 @@ describe('integration tests', () => {
317330
expect(response).toEqual(makeResponse({
318331
'body': `{"id":3,"name":"${newName}"}`,
319332
'multiValueHeaders': {
320-
'content-length': '43',
321-
'etag': 'W/"2b-ksYHypm1DmDdjEzhtyiv73Bluqk"'
333+
'content-length': ['43'],
334+
'etag': ['W/"2b-ksYHypm1DmDdjEzhtyiv73Bluqk"']
322335
},
323336
statusCode: 200
324337
}))
@@ -338,8 +351,8 @@ describe('integration tests', () => {
338351
expect(response).toEqual(makeResponse({
339352
'body': `[{"id":2,"name":"Jane"},{"id":3,"name":"${newName}"}]`,
340353
'multiValueHeaders': {
341-
'content-length': '68',
342-
'etag': 'W/"44-AtuxlvrIBL8NXP4gvEQTI77suNg"'
354+
'content-length': ['68'],
355+
'etag': ['W/"44-AtuxlvrIBL8NXP4gvEQTI77suNg"']
343356
},
344357
statusCode: 200
345358
}))
@@ -359,8 +372,8 @@ describe('integration tests', () => {
359372
expect(response).toEqual(makeResponse({
360373
'body': '{"id":2,"name":"Samuel"}',
361374
'multiValueHeaders': {
362-
'content-length': '24',
363-
'etag': 'W/"18-uGyzhJdtXqacOe9WRxtXSNjIk5Q"'
375+
'content-length': ['24'],
376+
'etag': ['W/"18-uGyzhJdtXqacOe9WRxtXSNjIk5Q"']
364377
},
365378
statusCode: 200
366379
}))
@@ -381,8 +394,24 @@ describe('integration tests', () => {
381394
expect(response).toEqual(makeResponse({
382395
'body': '{"id":2,"name":"Samuel"}',
383396
'multiValueHeaders': {
384-
'content-length': '24',
385-
'etag': 'W/"18-uGyzhJdtXqacOe9WRxtXSNjIk5Q"'
397+
'access-control-allow-origin': [
398+
'*'
399+
],
400+
'connection': [
401+
'close'
402+
],
403+
'content-length': [
404+
'24'
405+
],
406+
'content-type': [
407+
'application/json; charset=utf-8'
408+
],
409+
'etag': [
410+
'W/"18-uGyzhJdtXqacOe9WRxtXSNjIk5Q"'
411+
],
412+
'x-powered-by': [
413+
'Express'
414+
]
386415
},
387416
statusCode: 200
388417
}))
@@ -416,7 +445,7 @@ describe('integration tests', () => {
416445
httpMethod: 'GET',
417446
body: '{"name": "Sam502"}',
418447
multiValueHeaders: {
419-
'Content-Length': '-1'
448+
'Content-Length': ['-1']
420449
}
421450
}), {
422451
succeed
@@ -476,7 +505,27 @@ describe('integration tests', () => {
476505
})
477506
})
478507

479-
test.todo('set-cookie')
508+
test('Multiple headers of the same name (set-cookie)', (done) => {
509+
const succeed = response => {
510+
delete response.multiValueHeaders.date
511+
expect(response).toEqual(makeResponse({
512+
body: '{}',
513+
'multiValueHeaders': {
514+
'set-cookie': ['Foo=bar; Path=/,Fizz=buzz; Path=/'],
515+
'content-length': ['2'],
516+
'etag': ['W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"']
517+
},
518+
statusCode: 200
519+
}))
520+
done()
521+
}
522+
serverlessExpress.handler(makeEvent({
523+
path: '/cookie',
524+
httpMethod: 'GET'
525+
}), {
526+
succeed
527+
})
528+
})
480529

481530
test('server.onClose', (done) => {
482531
// NOTE: this must remain as the final test as it closes `server`

0 commit comments

Comments
 (0)