Skip to content

Commit cf32a9f

Browse files
committedJan 11, 2017
Simulate Lambda Proxy Integration
1 parent 482c5bf commit cf32a9f

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed
 

‎lib/serve.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const webpack = require('webpack');
55
const express = require('express');
66
const bodyParser = require('body-parser');
77

8+
const supportedMethods = 'get post'.split(' ');
9+
const resolveMethods = (method) => (method.toLowerCase() === 'any'
10+
? supportedMethods
11+
: [method.toLowerCase()]);
12+
813
module.exports = {
914
serve() {
1015
this.serverless.cli.log('Serving functions...');
@@ -44,24 +49,24 @@ module.exports = {
4449

4550
for (let funcConf of funcConfs) {
4651
for (let httpEvent of funcConf.events) {
47-
const method = httpEvent.method.toLowerCase();
48-
let endpoint = `/${httpEvent.path}`;
52+
let endpoint = `/${httpEvent.path}`.replace(/^\/+/, '/');
4953
if (this.options.stage) {
5054
endpoint = `/${this.options.stage}${endpoint}`;
5155
}
52-
const path = endpoint.replace(/\{(.+?)\}/g, ':$1');
56+
const path = endpoint
57+
.replace(/\{proxy\+\}/, '*')
58+
.replace(/\{(.+?)\}/g, ':$1');
5359
let handler = this._handlerBase(funcConf, httpEvent);
5460
let optionsHandler = this._optionsHandler;
5561
if (httpEvent.cors) {
5662
handler = this._handlerAddCors(handler);
5763
optionsHandler = this._handlerAddCors(optionsHandler);
5864
}
5965
app.options(path, optionsHandler);
60-
app[method](
61-
path,
62-
handler
63-
);
64-
this.serverless.cli.consoleLog(` ${method.toUpperCase()} - http://localhost:${this._getPort()}${endpoint}`);
66+
for (let method of resolveMethods(httpEvent.method)) {
67+
app[method](path, handler);
68+
}
69+
this.serverless.cli.consoleLog(` ${httpEvent.method.toUpperCase()} - http://localhost:${this._getPort()}${endpoint}`);
6570
}
6671
}
6772

‎tests/serve.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,51 @@ describe('serve', () => {
349349
testHandlerOptions
350350
);
351351
});
352+
353+
it('should simulate Lambda Proxy Integration', () => {
354+
const testFuncsConfs = [
355+
{
356+
'events': [
357+
{
358+
'method': 'any',
359+
'path': '/{proxy+}',
360+
'cors': true,
361+
}
362+
],
363+
'handler': 'module1.func1handler',
364+
'handlerFunc': null,
365+
'id': 'func1',
366+
'moduleName': 'module1',
367+
},
368+
];
369+
const testStage = 'test';
370+
module.options.stage = testStage;
371+
const testHandlerBase = 'testHandlerBase';
372+
const testHandlerCors = 'testHandlerCors';
373+
const testHandlerOptions = 'testHandlerOptions';
374+
module._handlerBase = sinon.stub().returns(testHandlerBase);
375+
module._optionsHandler = testHandlerOptions;
376+
module._handlerAddCors = sinon.stub().returns(testHandlerCors);
377+
const app = module._newExpressApp(testFuncsConfs);
378+
expect(app.get).to.have.callCount(1);
379+
expect(app.get).to.have.been.calledWith(
380+
'/test/*',
381+
testHandlerCors
382+
);
383+
expect(app.post).to.have.callCount(1);
384+
expect(app.post).to.have.been.calledWith(
385+
'/test/*',
386+
testHandlerCors
387+
);
388+
expect(app.options).to.have.callCount(1);
389+
expect(app.options).to.have.been.calledWith(
390+
'/test/*',
391+
testHandlerCors
392+
);
393+
expect(module.serverless.cli.consoleLog).to.have.been.calledWith(
394+
' ANY - http://localhost:8000/test/{proxy+}'
395+
);
396+
});
352397
});
353398

354399
describe('serve method', () => {

0 commit comments

Comments
 (0)