Skip to content

Commit 711c7e3

Browse files
committed
fix issue with missing/undefined event
1 parent b5d66c4 commit 711c7e3

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class API {
147147
async run(event,context,cb) {
148148

149149
// Set the event, context and callback
150-
this._event = event
150+
this._event = event || {}
151151
this._context = this.context = typeof context === 'object' ? context : {}
152152
this._cb = cb ? cb : undefined
153153

lib/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class REQUEST {
5454
async parseRequest() {
5555

5656
// Set the method
57-
this.method = this.app._event.httpMethod.toUpperCase()
57+
this.method = this.app._event.httpMethod ? this.app._event.httpMethod.toUpperCase() : 'GET'
5858

5959
// Set the path
6060
this.path = this.app._event.path
@@ -63,7 +63,7 @@ class REQUEST {
6363
this.query = this.app._event.queryStringParameters ? this.app._event.queryStringParameters : {}
6464

6565
// Set the raw headers
66-
this.rawHeaders = this.app._event.headers
66+
this.rawHeaders = this.app._event.headers || {}
6767

6868
// Set the headers to lowercase
6969
this.headers = Object.keys(this.rawHeaders).reduce((acc,header) =>

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const encodeBody = body =>
3737
exports.encodeBody = encodeBody
3838

3939
exports.parsePath = path => {
40-
return path.trim().split('?')[0].replace(/^\/(.*?)(\/)*$/,'$1').split('/')
40+
return path ? path.trim().split('?')[0].replace(/^\/(.*?)(\/)*$/,'$1').split('/') : []
4141
}
4242

4343

test/run.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ let event = {
2424
/******************************************************************************/
2525
/*** DEFINE TEST ROUTE ***/
2626
/******************************************************************************/
27+
28+
api.get('/', function(req,res) {
29+
res.status(200).json({
30+
method: 'get',
31+
status: 'ok'
32+
})
33+
})
34+
2735
api.get('/test', function(req,res) {
2836
res.status(200).json({
2937
method: 'get',
@@ -82,5 +90,23 @@ describe('Main handler Async/Await:', function() {
8290
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false })
8391
}) // end it
8492

93+
it('Without event', async function() {
94+
let _event = {}
95+
let result = await api.run(_event,{})
96+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
97+
}) // end it
98+
99+
it('With undefined event', async function() {
100+
let _event = undefined
101+
let result = await api.run(_event,{})
102+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
103+
}) // end it
104+
105+
it('With null event', async function() {
106+
let _event = null
107+
let result = await api.run(_event,{})
108+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
109+
}) // end it
110+
85111

86112
}) // end tests

0 commit comments

Comments
 (0)