Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate Lambda Proxy Integration #82

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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