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

skip http patching #150

Merged
merged 3 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
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).appFlags;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call it just flags or options? Having app in the name feels redundant.

} 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.appFlags;
const handler = createLambdaHandler(appRaw, opts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will opt out of logging HTTP requests (even z.request), right? Though, it would be strange to skipHttpPatch and still use z.request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z.request logs separately, so this'll opt out of logging made by anything besides z.request. namely, 3rd party SDKs

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,
appFlags: { skipHttpPatch: true }
});
await appTester(appDefinition.resources.list.list.operation.perform);
const http = require('http'); // core modules are never cached
should(http.patchedByZapier).eql(undefined);
});
});
});