Unable to use the Stripe generateTestHeaderString function in Edge Function unit tests #33011
Replies: 8 comments
-
hey @StefanVDWeide thanks for opening! To confirm, any of the solutions mentioned in the link do not work for you? Does this include using the |
Beta Was this translation helpful? Give feedback.
-
Hey @encima The solutions mentioned in the link indeed don't work. I also don't think the From the link you provided, I am trying to generate test headers in the unit test so that this part in the webhook works:
|
Beta Was this translation helpful? Give feedback.
-
Apologies, I think my comment was not so clear! Instead of using |
Beta Was this translation helpful? Give feedback.
-
Not sure I understand what you mean. We can not call the const payload = {
id: 'evt_test_webhook',
object: 'event',
};
const payloadString = JSON.stringify(payload, null, 2);
const secret = 'whsec_test_secret';
const header = stripe.webhooks.generateTestHeaderString({
payload: payloadString,
secret,
});
const event = stripe.webhooks.constructEvent(payloadString, header, secret);
// Do something with mocked signed event
expect(event.id).to.equal(payload.id); For reference, this is the code of my Edge Function webhook that I'm trying to test. As you can see, we need the header for the Deno.serve(async (request) => {
if (request.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}
const signature = request.headers.get("Stripe-Signature");
const body = await request.text();
try {
const event = (await stripe.webhooks.constructEventAsync(
body,
signature,
STRIPE_WEBHOOK_SIGNING_SECRET,
undefined,
cryptoProvider,
)) as Stripe.DiscriminatedEvent;
const response = await handleStripeEvent(event);
return new Response(JSON.stringify(response), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error(`Error handling request: ${error.message}`);
return new Response(JSON.stringify({ error: error.message }), {
status: error.status || 500,
});
}
}); Hopefully this clears things up! |
Beta Was this translation helpful? Give feedback.
-
This certainly does! Thanks for the extra insights and your patience explaining it. I was clearly in "triage" mode and had not looked deeply enough into it so I appreciate the explanations. I'll let someone more knowledgeable chime in here 😅 |
Beta Was this translation helpful? Give feedback.
-
No worries! And thank you for thinking along! :) |
Beta Was this translation helpful? Give feedback.
-
@StefanVDWeide I had been running into this problem as well and just decided to drop the cryptoProvider entirely, which worked and allowed me to continue with my tests successfully:
This is what's noted on Stripe's documentation on this as well: https://github.com/stripe/stripe-node/blob/master/README.md#testing-webhook-signing (note no cryptoProvider is specified) Not sure if that works for your use case but wanted to drop the line just in case. Good luck! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, due to inactivity on this issue I've moved the issue over to discussions/enhancements. |
Beta Was this translation helpful? Give feedback.
-
Bug report
Describe the bug
When trying to use the
generateTestHeaderString
provide by the Stripe library in one of my edge function unit test, I keep running into the following error:No matter if I wrap the stripe.webhooks.generateTestHeaderString() call in an async function and await it, the error will always be the same. I also tried
const webCrypto = Stripe.createSubtleCryptoProvider();
and use thewebCrypto
object as a custom crypto provider as suggested in the docs for Stripe in edge functions, but this doesn't solve it either.The solutions posed here also don't resolve this issue: stripe/stripe-node#1942
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
supabase/functions/tests
folderdeno test --allow-all tests/stripe-webhook-test.ts
Expected behavior
I expect the headers to be generated using the crypto provider just as when running Stripe functions in edge functions.
System information
Beta Was this translation helpful? Give feedback.
All reactions