Add WebhookSignature.generate_test_header_string helper#1810
Open
vishwakt wants to merge 1 commit into
Open
Conversation
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
Viicos
reviewed
May 15, 2026
| signature: Optional[str] = None, | ||
| ) -> str: | ||
| """ | ||
| Generates a value for the `Stripe-Signature` header that can be used |
There was a problem hiding this comment.
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: ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a public
WebhookSignature.generate_test_header_string()helper that mirrors stripe-node'sgenerateTestHeaderString(), so users can sign test payloads without reaching for the underscore-prefixed_compute_signatureinternal.Fixes #1697.
Implementation notes
WebhookSignature(next toEXPECTED_SCHEME,_compute_signature, andverify_header), accessed asstripe.WebhookSignature.generate_test_header_string(...).Optionalkeyword 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).timestamp→int(time.time()),scheme→EXPECTED_SCHEME,signature→ computed frompayload+secret. Passingsignatureexplicitly inserts it verbatim, mirroring Node.generate_headerhelper intests/test_webhook.pywas 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.TestGenerateTestHeaderStringclass covers: defaults, round-trip throughverify_header, custom timestamp/scheme, signature pass-through, and bad-secret failure.Example usage
Test plan
just test— 4245 passed, 6 skippedjust lint— cleanjust format-check— cleanjust typecheck— only pre-existing error instripe/_http_client.py:681(present onmaster, unrelated to this change)