diff --git a/README.md b/README.md index fef0722..b6a1527 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Executes a lambda given the `options` object, which is a dictionary where the ke | `callback`|optional, lambda third parameter [callback][1]. When left out a Promise is returned| | `onInvocationEnd`|optional. called once the invocation ended. useful when awslambda.streamifyResponse is used to distinguish between end of response stream and end of invocation. | | `clientContext`|optional, used to populated clientContext property of lambda second parameter (context) +| `contextOverwrite`| optional, a function that overwrites the context object. It can get and overwrite the values of the context (such as awsRequestId). | #### `lambdaLocal.setLogger(logger)` #### `lambdaLocal.getLogger()` diff --git a/src/lambdalocal.ts b/src/lambdalocal.ts index 109ed67..149b0c1 100644 --- a/src/lambdalocal.ts +++ b/src/lambdalocal.ts @@ -187,6 +187,7 @@ function _executeSync(opts) { timeoutMs = opts.timeoutMs || 3000, verboseLevel = opts.verboseLevel, callback = opts.callback, + contextOverwrite = opts.contextOverwrite, onInvocationEnd = opts.onInvocationEnd, clientContext = null; @@ -304,6 +305,8 @@ function _executeSync(opts) { var ctx = context.generate_context(); + if(contextOverwrite) opts.contextOverwrite(ctx); + const executeLambdaFunc = lambdaFunc => { try { //load event diff --git a/test/test.js b/test/test.js index ca20250..9206766 100644 --- a/test/test.js +++ b/test/test.js @@ -301,6 +301,26 @@ describe("- Testing lambdalocal.js", function () { }); }); }); + describe('* Context overwrite', function () { + it("Should overwrite context", function(cb){ + var lambdalocal = require(lambdalocal_path); + lambdalocal.execute({ + event: require(path.join(__dirname, "./events/test-event.js")), + lambdaPath: path.join(__dirname, "./functs/test-func.js"), + lambdaHandler: functionName, + contextOverwrite: function (ctx){ + ctx.awsRequestId = "test id"; + ctx.functionName = "test function"; + }, + callback: function (err, done) { + assert.equal(done.result, "testvar"); + assert.equal(done.context.awsRequestId, "test id"); + assert.equal(done.context.functionName, "test function"); + cb(); + } + }); + }); + }); describe('* Nested Run', function () { it("Should handle nested calls properly (context singleton)", function (cb) { var lambdalocal = require(lambdalocal_path);