Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #150 from zapier/skip-http-patch
Browse files Browse the repository at this point in the history
skip http patching
  • Loading branch information
xavdid authored Apr 25, 2019
2 parents 6754bd0 + 1401d57 commit 0a7cff0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
8 changes: 7 additions & 1 deletion include/zapierwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
) => <T extends any, B extends Bundle>(
func: (z: ZObject, bundle: B) => Promise<T>,
bundle?: Partial<B> // partial so we don't have to make a full bundle in tests
Expand Down
5 changes: 3 additions & 2 deletions src/tools/create-app-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions src/tools/create-lambda-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/tools/http-patch.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 0a7cff0

Please sign in to comment.