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 4a6cfe7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 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
102 changes: 59 additions & 43 deletions test/Webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,59 +63,75 @@ describe('Webhooks', () => {
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][',
secret: SECRET,
});
await expect(
constructEventFn('} I am not valid JSON; 123][', header, SECRET)
).to.be.rejectedWith(/Unexpected token/);
await expect(
constructEventFn('} I am not valid JSON; 123][', header, SECRET)
).to.be.rejectedWith(/Unexpected token/);
});

it('should raise a SignatureVerificationError from a valid JSON payload and an invalid signature header', async () => {
const header = 'bad_header';

await expect(
constructEventFn(EVENT_PAYLOAD_STRING, header, SECRET)
).to.be.rejectedWith(
/Unable to extract timestamp and signatures from header/
);
});

it('should error if you pass a signature which is an array, even though our types say you can', async () => {
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,
});

await expect(
constructEventFn(EVENT_PAYLOAD_STRING, [header], SECRET)
).to.be.rejectedWith(
'Unexpected: An array was passed as a header, which should not be possible for the stripe-signature header.'
);
});

it('should invoke a custom CryptoProvider', async () => {
const header = stripe.webhooks.generateTestHeaderString({
payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
signature: 'fake signature',
});

const textEncoder = new TextEncoder();
const event = await constructEventFn(
EVENT_PAYLOAD_STRING,
header,
SECRET,
undefined,
new FakeCryptoProvider()
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][',
// secret: SECRET,
// });
// await expect(
// constructEventFn('} I am not valid JSON; 123][', header, SECRET)
// ).to.be.rejectedWith(/Unexpected token/);
// await expect(
// constructEventFn('} I am not valid JSON; 123][', header, SECRET)
// ).to.be.rejectedWith(/Unexpected token/);
// });

// it('should raise a SignatureVerificationError from a valid JSON payload and an invalid signature header', async () => {
// const header = 'bad_header';

// await expect(
// constructEventFn(EVENT_PAYLOAD_STRING, header, SECRET)
// ).to.be.rejectedWith(
// /Unable to extract timestamp and signatures from header/
// );
// });

// it('should error if you pass a signature which is an array, even though our types say you can', async () => {
// const header = stripe.webhooks.generateTestHeaderString({
// payload: EVENT_PAYLOAD_STRING,
// secret: SECRET,
// });

// await expect(
// constructEventFn(EVENT_PAYLOAD_STRING, [header], SECRET)
// ).to.be.rejectedWith(
// 'Unexpected: An array was passed as a header, which should not be possible for the stripe-signature header.'
// );
// });

// it('should invoke a custom CryptoProvider', async () => {
// const header = stripe.webhooks.generateTestHeaderString({
// payload: EVENT_PAYLOAD_STRING,
// secret: SECRET,
// signature: 'fake signature',
// });

// const event = await constructEventFn(
// EVENT_PAYLOAD_STRING,
// header,
// SECRET,
// undefined,
// new FakeCryptoProvider()
// );

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

Expand Down

0 comments on commit 4a6cfe7

Please sign in to comment.