diff --git a/include/zapierwrapper.js b/include/zapierwrapper.js index 096c46a..ebcee1e 100644 --- a/include/zapierwrapper.js +++ b/include/zapierwrapper.js @@ -2,4 +2,10 @@ const path = require('path'); const zapier = require('zapier-platform-core'); const appPath = path.resolve(__dirname, 'index.js'); -module.exports = {handler: zapier.createAppHandler(appPath)}; +let opts; +try { + opts = require(appPath).flags; +} catch (error) { + // nothing to see here +} +module.exports = { handler: zapier.createAppHandler(appPath, opts) }; diff --git a/index.d.ts b/index.d.ts index c0f0d6a..5b9dbc4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,7 +13,7 @@ export const tools: { env: { inject: (filename?: string) => void } }; // see: https://github.com/zapier/zapier-platform-cli/issues/339#issue-336888249 export const createAppTester: ( appRaw: object, - customStoreKey?: string + options?: { customStoreKey?: string } ) => ( func: (z: ZObject, bundle: B) => Promise, bundle?: Partial // partial so we don't have to make a full bundle in tests diff --git a/src/tools/create-app-tester.js b/src/tools/create-app-tester.js index 034a12f..03a2396 100644 --- a/src/tools/create-app-tester.js +++ b/src/tools/create-app-tester.js @@ -36,8 +36,9 @@ const promisifyHandler = handler => { }; // A shorthand compatible wrapper for testing. -const createAppTester = (appRaw, customStoreKey) => { - const handler = createLambdaHandler(appRaw); +const createAppTester = (appRaw, { customStoreKey } = {}) => { + const opts = appRaw.flags; + const handler = createLambdaHandler(appRaw, opts); const createHandlerPromise = promisifyHandler(handler); const randomSeed = genId(); diff --git a/src/tools/create-lambda-handler.js b/src/tools/create-lambda-handler.js index d1618d7..96ef92c 100644 --- a/src/tools/create-lambda-handler.js +++ b/src/tools/create-lambda-handler.js @@ -130,11 +130,13 @@ const loadApp = (event, rpc, appRawOrPath) => { }); }; -const createLambdaHandler = appRawOrPath => { +const createLambdaHandler = (appRawOrPath, { skipHttpPatch } = {}) => { const handler = (event, context, callback) => { // Adds logging for _all_ kinds of http(s) requests, no matter the library - const httpPatch = createHttpPatch(event); - httpPatch(require('http')); // 'https' uses 'http' under the hood + if (!skipHttpPatch) { + const httpPatch = createHttpPatch(event); + httpPatch(require('http')); // 'https' uses 'http' under the hood + } // Wait for all async events to complete before callback returns. // This is not strictly necessary since this is the default now when diff --git a/test/tools/http-patch.js b/test/tools/http-patch.js new file mode 100644 index 0000000..665a380 --- /dev/null +++ b/test/tools/http-patch.js @@ -0,0 +1,29 @@ +'use strict'; + +const should = require('should'); + +const createAppTester = require('../../src/tools/create-app-tester'); +const appDefinition = require('../userapp'); + +describe.skip('create-lambda-handler', () => { + // this block is skipped because there's no way to un-modify 'http' once we've done it + // I've verified that the bottom test works in isolation, but doesn't when it's part of the larger suite + describe('http patch', () => { + it('should patch by default', async () => { + const appTester = createAppTester(appDefinition); + await appTester(appDefinition.resources.list.list.operation.perform); + const http = require('http'); // core modules are never cached + should(http.patchedByZapier).eql(true); + }); + + it('should be ablet opt out of patch', async () => { + const appTester = createAppTester({ + ...appDefinition, + flags: { skipHttpPatch: true } + }); + await appTester(appDefinition.resources.list.list.operation.perform); + const http = require('http'); // core modules are never cached + should(http.patchedByZapier).eql(undefined); + }); + }); +});