Skip to content

Commit

Permalink
Add tests for Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Jan 11, 2023
1 parent d77aae6 commit 9d7b051
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/Webhooks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ const Webhook: WebhookObject = {
cryptoProvider
);

// @ts-ignore
const jsonPayload = JSON.parse(payload);
const jsonPayload =
payload instanceof Uint8Array
? JSON.parse(new TextDecoder().decode(payload))
: JSON.parse(payload);
return jsonPayload;
},

Expand All @@ -99,7 +101,10 @@ const Webhook: WebhookObject = {
);

// @ts-ignore
const jsonPayload = JSON.parse(payload);
const jsonPayload =
payload instanceof Uint8Array
? JSON.parse(new TextDecoder().decode(payload))
: JSON.parse(payload);
return jsonPayload;
},

Expand Down
16 changes: 16 additions & 0 deletions test/Webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ describe('Webhooks', () => {
expect(event.id).to.equal(EVENT_PAYLOAD.id);
});

it('should return an Event instance from a payload and header with type Uint8Array', async () => {
const header = stripe.webhooks.generateTestHeaderString({
payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
});

const textEncoder = new TextEncoder();
const event = await constructEventFn(
new Uint8Array(textEncoder.encode(EVENT_PAYLOAD_STRING)),
new Uint8Array(textEncoder.encode(header)),
SECRET
);

expect(event.id).to.equal(EVENT_PAYLOAD.id);
});

it('should raise a JSON error from invalid JSON payload', async () => {
const header = stripe.webhooks.generateTestHeaderString({
payload: '} I am not valid JSON; 123][',
Expand Down

0 comments on commit 9d7b051

Please sign in to comment.