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
Unable to use the Stripe generateTestHeaderString function in Edge Function unit tests #26126
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 |
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:
|
Apologies, I think my comment was not so clear! Instead of using |
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! |
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 😅 |
No worries! And thank you for thinking along! :) |
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
The text was updated successfully, but these errors were encountered: