Skip to content

Commit

Permalink
fix: Don't fail if there is no request header or query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcorlu committed Dec 11, 2019
1 parent a2706a5 commit adb8512
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Request extends ReadableStream {
constructor(event) {
super();

event.multiValueHeaders = event.multiValueHeaders || {};

this.headers = Object.keys(event.multiValueHeaders).reduce((headers, key) => {
const value = event.multiValueHeaders[key];
headers[key.toLowerCase()] = value.length > 1 ? value : value[0];
Expand All @@ -17,6 +19,8 @@ class Request extends ReadableStream {

this.hostname = this.headers.host
this.method = event.httpMethod;

event.multiValueQueryStringParameters = event.multiValueQueryStringParameters || {};
this.query = Object.keys(event.multiValueQueryStringParameters).reduce((queryParams, key) => {
const value = event.multiValueQueryStringParameters[key];
queryParams[key] = value.length > 1 ? value : value[0];
Expand Down
73 changes: 45 additions & 28 deletions src/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ const { Request } = require('./request');

describe('Request object', () => {
const requestObject = {a:1};
const event = {
body: JSON.stringify(requestObject),
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(requestObject).length,
'X-Header': 'value2'
},
multiValueHeaders: {
'Content-Type': [ 'application/json' ],
'Content-Length': [ JSON.stringify(requestObject).length ],
'X-Header': [ 'value1', 'value2' ]
},
httpMethod: 'POST',
isBase64Encoded: false,
path: '/path',
pathParameters: { },
queryStringParameters: {
a: '1',
b: '2'
},
multiValueQueryStringParameters: {
a: [ '1' ],
b: [ '1', '2']
},
stageVariables: { },
requestContext: {},
resource: ''
};
let event;
beforeEach(() => {
event = {
body: JSON.stringify(requestObject),
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(requestObject).length,
'X-Header': 'value2'
},
multiValueHeaders: {
'Content-Type': [ 'application/json' ],
'Content-Length': [ JSON.stringify(requestObject).length ],
'X-Header': [ 'value1', 'value2' ]
},
httpMethod: 'POST',
isBase64Encoded: false,
path: '/path',
pathParameters: { },
queryStringParameters: {
a: '1',
b: '2'
},
multiValueQueryStringParameters: {
a: [ '1' ],
b: [ '1', '2']
},
stageVariables: { },
requestContext: {},
resource: ''
};
});

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

it('should read query as empty object if there is no queryparamters', () => {
delete event.multiValueQueryStringParameters;
const request = new Request(event);

expect(request.query).toEqual({});
});

it('should read headers as empty object if there is no headers', () => {
delete event.multiValueHeaders;
const request = new Request(event);

expect(request.headers).toEqual({});
});

it('should handle weird header asks', () => {
const request = new Request(event);

Expand Down

0 comments on commit adb8512

Please sign in to comment.