Skip to content

Commit

Permalink
Simulate Lambda Proxy Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ktonon committed Jan 11, 2017
1 parent 482c5bf commit cf32a9f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const webpack = require('webpack');
const express = require('express');
const bodyParser = require('body-parser');

const supportedMethods = 'get post'.split(' ');
const resolveMethods = (method) => (method.toLowerCase() === 'any'
? supportedMethods
: [method.toLowerCase()]);

module.exports = {
serve() {
this.serverless.cli.log('Serving functions...');
Expand Down Expand Up @@ -44,24 +49,24 @@ module.exports = {

for (let funcConf of funcConfs) {
for (let httpEvent of funcConf.events) {
const method = httpEvent.method.toLowerCase();
let endpoint = `/${httpEvent.path}`;
let endpoint = `/${httpEvent.path}`.replace(/^\/+/, '/');
if (this.options.stage) {
endpoint = `/${this.options.stage}${endpoint}`;
}
const path = endpoint.replace(/\{(.+?)\}/g, ':$1');
const path = endpoint
.replace(/\{proxy\+\}/, '*')
.replace(/\{(.+?)\}/g, ':$1');
let handler = this._handlerBase(funcConf, httpEvent);
let optionsHandler = this._optionsHandler;
if (httpEvent.cors) {
handler = this._handlerAddCors(handler);
optionsHandler = this._handlerAddCors(optionsHandler);
}
app.options(path, optionsHandler);
app[method](
path,
handler
);
this.serverless.cli.consoleLog(` ${method.toUpperCase()} - http://localhost:${this._getPort()}${endpoint}`);
for (let method of resolveMethods(httpEvent.method)) {
app[method](path, handler);
}
this.serverless.cli.consoleLog(` ${httpEvent.method.toUpperCase()} - http://localhost:${this._getPort()}${endpoint}`);
}
}

Expand Down
45 changes: 45 additions & 0 deletions tests/serve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,51 @@ describe('serve', () => {
testHandlerOptions
);
});

it('should simulate Lambda Proxy Integration', () => {
const testFuncsConfs = [
{
'events': [
{
'method': 'any',
'path': '/{proxy+}',
'cors': true,
}
],
'handler': 'module1.func1handler',
'handlerFunc': null,
'id': 'func1',
'moduleName': 'module1',
},
];
const testStage = 'test';
module.options.stage = testStage;
const testHandlerBase = 'testHandlerBase';
const testHandlerCors = 'testHandlerCors';
const testHandlerOptions = 'testHandlerOptions';
module._handlerBase = sinon.stub().returns(testHandlerBase);
module._optionsHandler = testHandlerOptions;
module._handlerAddCors = sinon.stub().returns(testHandlerCors);
const app = module._newExpressApp(testFuncsConfs);
expect(app.get).to.have.callCount(1);
expect(app.get).to.have.been.calledWith(
'/test/*',
testHandlerCors
);
expect(app.post).to.have.callCount(1);
expect(app.post).to.have.been.calledWith(
'/test/*',
testHandlerCors
);
expect(app.options).to.have.callCount(1);
expect(app.options).to.have.been.calledWith(
'/test/*',
testHandlerCors
);
expect(module.serverless.cli.consoleLog).to.have.been.calledWith(
' ANY - http://localhost:8000/test/{proxy+}'
);
});
});

describe('serve method', () => {
Expand Down

0 comments on commit cf32a9f

Please sign in to comment.