Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generateTestHeaderStringAsync function to Webhooks.ts #2048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
receivedAt: number
) => Promise<WebhookEvent>;
generateTestHeaderString: (opts: WebhookTestHeaderOptions) => string;
generateTestHeaderStringAsync: (
opts: WebhookTestHeaderOptions
) => Promise<string>;
};

export function createWebhooks(
Expand Down Expand Up @@ -166,6 +169,35 @@
opts.scheme + '=' + opts.signature,
].join(',');

return generatedHeader;
},
generateTestHeaderStringAsync: async function(
opts: WebhookTestHeaderOptions
): Promise<string> {
if (!opts) {
throw new StripeError({
message: 'Options are required',
});
}

opts.timestamp =
Math.floor(opts.timestamp) || Math.floor(Date.now() / 1000);
opts.scheme = opts.scheme || signature.EXPECTED_SCHEME;

opts.cryptoProvider = opts.cryptoProvider || getCryptoProvider();

opts.signature =
opts.signature ||
(await opts.cryptoProvider.computeHMACSignatureAsync(
opts.timestamp + '.' + opts.payload,
opts.secret
));

const generatedHeader = [
't=' + opts.timestamp,
opts.scheme + '=' + opts.signature,
].join(',');

return generatedHeader;
},
};
Expand Down Expand Up @@ -440,7 +472,7 @@
if (!webhooksCryptoProviderInstance) {
webhooksCryptoProviderInstance = platformFunctions.createDefaultCryptoProvider();
}
return webhooksCryptoProviderInstance!;

Check warning on line 475 in src/Webhooks.ts

View workflow job for this annotation

GitHub Actions / Build

Forbidden non-null assertion
}

Webhook.signature = signature;
Expand Down
24 changes: 24 additions & 0 deletions test/Webhook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ function createWebhooksTestSuite(stripe) {
});
});

describe('.generateTestHeaderStringAsync', () => {
it('should throw when no opts are passed', async () => {
await expect(
stripe.webhooks.generateTestHeaderStringAsync()
).to.be.rejectedWith('Options are required');
});

it('should correctly construct a webhook header', async () => {
const header = await stripe.webhooks.generateTestHeaderStringAsync({
payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
});

expect(header).to.not.be.undefined;
expect(header.split(',')).to.have.lengthOf(2);
expect(header).to.equal(
stripe.webhooks.generateTestHeaderString({
Copy link
Author

Choose a reason for hiding this comment

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

Make sure the generated string is the same as generateTestHeaderString

payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
})
);
});
});

const makeConstructEventTests = (
constructEventFn: typeof stripe.webhooks.construct
) => {
Expand Down