【发布时间】:2021-03-26 03:16:48
【问题描述】:
我正在尝试为 Stripe webhook 编写单元测试。问题是我也在验证stripe-signature,但它按预期失败了。
有没有办法使用模拟数据将测试中的正确签名传递给 webhook?
这是我正在尝试处理的 webhook 路由的开始
// Retrieve the event by verifying the signature using the raw body and secret.
let event: Stripe.Event;
const signature = headers["stripe-signature"];
try {
event = stripe.webhooks.constructEvent(
raw,
signature,
context.env.stripeWebhookSecret
);
} catch (err) {
throw new ResourceError(RouteErrorCode.STRIPE_WEBHOOK_SIGNATURE_VERIFICATION_FAILD);
}
// Handle event...
而我正在尝试处理的当前测试,我正在使用 Jest:
const postData = { MOCK WEBHOOK EVENT DATA }
const result = await request(app.app)
.post("/webhook/stripe")
.set('stripe-signature', 'HOW TO GET THIS SIGNATURE?')
.send(postData);
【问题讨论】:
标签: node.js unit-testing stripe-payments