Skip to content

Commit adb8512

Browse files
committed
fix: Don't fail if there is no request header or query parameters
1 parent a2706a5 commit adb8512

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

src/request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Request extends ReadableStream {
99
constructor(event) {
1010
super();
1111

12+
event.multiValueHeaders = event.multiValueHeaders || {};
13+
1214
this.headers = Object.keys(event.multiValueHeaders).reduce((headers, key) => {
1315
const value = event.multiValueHeaders[key];
1416
headers[key.toLowerCase()] = value.length > 1 ? value : value[0];
@@ -17,6 +19,8 @@ class Request extends ReadableStream {
1719

1820
this.hostname = this.headers.host
1921
this.method = event.httpMethod;
22+
23+
event.multiValueQueryStringParameters = event.multiValueQueryStringParameters || {};
2024
this.query = Object.keys(event.multiValueQueryStringParameters).reduce((queryParams, key) => {
2125
const value = event.multiValueQueryStringParameters[key];
2226
queryParams[key] = value.length > 1 ? value : value[0];

src/request.spec.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@ const { Request } = require('./request');
22

33
describe('Request object', () => {
44
const requestObject = {a:1};
5-
const event = {
6-
body: JSON.stringify(requestObject),
7-
headers: {
8-
'Content-Type': 'application/json',
9-
'Content-Length': JSON.stringify(requestObject).length,
10-
'X-Header': 'value2'
11-
},
12-
multiValueHeaders: {
13-
'Content-Type': [ 'application/json' ],
14-
'Content-Length': [ JSON.stringify(requestObject).length ],
15-
'X-Header': [ 'value1', 'value2' ]
16-
},
17-
httpMethod: 'POST',
18-
isBase64Encoded: false,
19-
path: '/path',
20-
pathParameters: { },
21-
queryStringParameters: {
22-
a: '1',
23-
b: '2'
24-
},
25-
multiValueQueryStringParameters: {
26-
a: [ '1' ],
27-
b: [ '1', '2']
28-
},
29-
stageVariables: { },
30-
requestContext: {},
31-
resource: ''
32-
};
5+
let event;
6+
beforeEach(() => {
7+
event = {
8+
body: JSON.stringify(requestObject),
9+
headers: {
10+
'Content-Type': 'application/json',
11+
'Content-Length': JSON.stringify(requestObject).length,
12+
'X-Header': 'value2'
13+
},
14+
multiValueHeaders: {
15+
'Content-Type': [ 'application/json' ],
16+
'Content-Length': [ JSON.stringify(requestObject).length ],
17+
'X-Header': [ 'value1', 'value2' ]
18+
},
19+
httpMethod: 'POST',
20+
isBase64Encoded: false,
21+
path: '/path',
22+
pathParameters: { },
23+
queryStringParameters: {
24+
a: '1',
25+
b: '2'
26+
},
27+
multiValueQueryStringParameters: {
28+
a: [ '1' ],
29+
b: [ '1', '2']
30+
},
31+
stageVariables: { },
32+
requestContext: {},
33+
resource: ''
34+
};
35+
});
3336

3437
it('should read query parameter', () => {
3538
const request = new Request(event);
@@ -57,6 +60,20 @@ describe('Request object', () => {
5760
expect(request.get('X-Header')).toEqual(['value1', 'value2']);
5861
});
5962

63+
it('should read query as empty object if there is no queryparamters', () => {
64+
delete event.multiValueQueryStringParameters;
65+
const request = new Request(event);
66+
67+
expect(request.query).toEqual({});
68+
});
69+
70+
it('should read headers as empty object if there is no headers', () => {
71+
delete event.multiValueHeaders;
72+
const request = new Request(event);
73+
74+
expect(request.headers).toEqual({});
75+
});
76+
6077
it('should handle weird header asks', () => {
6178
const request = new Request(event);
6279

0 commit comments

Comments
 (0)