Skip to content

Add WebhookSignature.generate_test_header_string helper#1810

Open
vishwakt wants to merge 1 commit into
stripe:masterfrom
vishwakt:add-generate-test-header-string
Open

Add WebhookSignature.generate_test_header_string helper#1810
vishwakt wants to merge 1 commit into
stripe:masterfrom
vishwakt:add-generate-test-header-string

Conversation

@vishwakt
Copy link
Copy Markdown

Summary

Adds a public WebhookSignature.generate_test_header_string() helper that mirrors stripe-node's generateTestHeaderString(), so users can sign test payloads without reaching for the underscore-prefixed _compute_signature internal.

Fixes #1697.

Implementation notes

  • New method lives on WebhookSignature (next to EXPECTED_SCHEME, _compute_signature, and verify_header), accessed as stripe.WebhookSignature.generate_test_header_string(...).
  • Python Optional keyword arguments are used instead of the Node SDK's options object, matching Python conventions while preserving the full Node parameter surface (payload, secret, timestamp, scheme, signature).
  • Defaults: timestampint(time.time()), schemeEXPECTED_SCHEME, signature → computed from payload + secret. Passing signature explicitly inserts it verbatim, mirroring Node.
  • The internal generate_header helper in tests/test_webhook.py was refactored to delegate to the new public method. This proves the new API covers every existing test scenario and removes the duplicated signing logic from tests.
  • New TestGenerateTestHeaderString class covers: defaults, round-trip through verify_header, custom timestamp/scheme, signature pass-through, and bad-secret failure.

Example usage

import stripe

header = stripe.WebhookSignature.generate_test_header_string(
    payload=payload_json,
    secret="whsec_test_secret",
)
event = stripe.Webhook.construct_event(payload_json, header, "whsec_test_secret")

Test plan

  • just test — 4245 passed, 6 skipped
  • just lint — clean
  • just format-check — clean
  • just typecheck — only pre-existing error in stripe/_http_client.py:681 (present on master, unrelated to this change)

Exposes a public utility for generating signed Stripe-Signature header
values for use in tests, mirroring stripe-node's generateTestHeaderString.
Closes the need to depend on the underscore-prefixed _compute_signature
internal when writing tests against webhook handling code.

Fixes stripe#1697
@vishwakt vishwakt requested a review from a team as a code owner May 14, 2026 03:23
@vishwakt vishwakt requested review from prathmesh-stripe and removed request for a team May 14, 2026 03:23
@cla-assistant
Copy link
Copy Markdown

cla-assistant Bot commented May 14, 2026

CLA assistant check
All committers have signed the CLA.

Comment thread stripe/_webhook.py
signature: Optional[str] = None,
) -> str:
"""
Generates a value for the `Stripe-Signature` header that can be used
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring uses a mix of markdown and RST syntax. I would suggest sticking to markdown to be consistent with the rest of the code base. You can specify parameters as:

Args:
    payload: ...
    secret: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a public helper to sign webhook data

2 participants