Browse Source

Merge pull request #57 from stevenyap/api-gateway-lambda-proxy

Catering to Lambda proxy enabled API request/response
master
Nicola Peduzzi 8 years ago
committed by GitHub
parent
commit
2650649c6f
  1. 14
      lib/serve.js
  2. 43
      tests/serve.test.js

14
lib/serve.js

@ -50,7 +50,7 @@ module.exports = {
endpoint = `/${this.options.stage}${endpoint}`;
}
const path = endpoint.replace(/\{(.+?)\}/g, ':$1');
let handler = this._handlerBase(funcConf);
let handler = this._handlerBase(funcConf, httpEvent);
let optionsHandler = this._optionsHandler;
if (httpEvent.cors) {
handler = this._handlerAddCors(handler);
@ -101,7 +101,9 @@ module.exports = {
};
},
_handlerBase(funcConf) {
_handlerBase(funcConf, httpEvent) {
const isLambdaProxyIntegration = httpEvent && httpEvent.integration !== 'lambda'
return (req, res) => {
const func = funcConf.handlerFunc;
const event = {
@ -109,14 +111,18 @@ module.exports = {
headers: req.headers,
body: req.body,
path: req.params,
query: req.query,
[isLambdaProxyIntegration ? 'queryStringParameters' : 'query']: req.query
// principalId,
// stageVariables,
};
const context = this.getContext(funcConf.id);
func(event, context, (err, resp) => {
if (err) {
res.status(500).send(err);
return res.status(500).send(err);
}
if (isLambdaProxyIntegration) {
res.status(resp.statusCode).send(resp.body);
} else {
res.status(200).send(resp);
}

43
tests/serve.test.js

@ -64,6 +64,9 @@ describe('serve', () => {
id: 'testFuncId',
handlerFunc: testHandlerFunc,
};
const testHttpEvent = {
integration: 'lambda'
}
const testReq = {
method: 'testmethod',
headers: 'testheaders',
@ -76,7 +79,7 @@ describe('serve', () => {
};
testRes.status = sinon.stub().returns(testRes);
module.getContext = sinon.stub().returns('testContext');
const handler = module._handlerBase(testFuncConf);
const handler = module._handlerBase(testFuncConf, testHttpEvent);
handler(testReq, testRes);
expect(testRes.status).to.have.been.calledWith(200);
expect(testRes.send).to.have.been.calledWith(testHandlerResp);
@ -111,6 +114,44 @@ describe('serve', () => {
expect(testRes.status).to.have.been.calledWith(500);
expect(testRes.send).to.have.been.calledWith(testHandlerErr);
});
it('handles lambda-proxy integration for request and response', () => {
const testHandlerResp = { statusCode: 200, body: 'testHandlerResp' };
const testHandlerFunc = sinon.spy((ev, ct, cb) => {
cb(null, testHandlerResp);
});
const testFuncConf = {
id: 'testFuncId',
handlerFunc: testHandlerFunc,
};
const testHttpEvent = {}
const testReq = {
method: 'testmethod',
headers: 'testheaders',
body: 'testbody',
params: 'testparams',
query: 'testquery',
};
const testRes = {
send: sinon.spy(),
};
testRes.status = sinon.stub().returns(testRes);
module.getContext = sinon.stub().returns('testContext');
const handler = module._handlerBase(testFuncConf, testHttpEvent);
handler(testReq, testRes);
expect(testRes.status).to.have.been.calledWith(testHandlerResp.statusCode);
expect(testRes.send).to.have.been.calledWith(testHandlerResp.body);
expect(testHandlerFunc).to.have.been.calledWith(
{
body: 'testbody',
headers: 'testheaders',
method: 'testmethod',
path: 'testparams',
queryStringParameters: 'testquery',
},
'testContext'
);
});
});
describe('_optionsHandler', () => {

Loading…
Cancel
Save