Skip to content
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

Feature request: Allow regex-matching for header values #651

Closed
spreeni opened this issue Jul 13, 2023 · 8 comments
Closed

Feature request: Allow regex-matching for header values #651

spreeni opened this issue Jul 13, 2023 · 8 comments

Comments

@spreeni
Copy link

spreeni commented Jul 13, 2023

I would like to match the expected format of a request-signature, without necessarily knowing the exact signature hash. To enable this or similar use cases, it would be nice to be able to supply compiled regex for the matchers.header_matcher similar as it is possible for the url.

In general, this could be a nice feature for all other matchers as well.

Example:
If I want to for example test that an outgoing request to an external API is supplied with a valid signature and a Bearer token. Currently, I'd implement a custom matcher for this. But it would be nice to test header fields optionally with regex directly in the header_matcher.

responses.get(
    url,
    status=200,
    json=mock_data,
    match=[
        matchers.header_matcher(
            {
                "Message-Signature": re.compile(r'signature="\S+",created=\d+'),
                "Authorization": "Bearer API_TOKEN",
            },
            strict_match=False
        ),
        signature_matcher,
    ],
)

Ps: I love responses, thanks for the great work :)

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Jul 13, 2023
@beliaev-maksim
Copy link
Collaborator

please provide an example of the expected syntax and use case

@spreeni
Copy link
Author

spreeni commented Jul 13, 2023

I added an example, please see if it helps clarify what I meant.

@beliaev-maksim
Copy link
Collaborator

@spreeni have you considered creating a custom matcher?

@spreeni
Copy link
Author

spreeni commented Jul 18, 2023

@beliaev-maksim Hey sorry, I missed the notification somehow. Yes, I found custom matchers and that is how I implemented it myself now. But maybe as a convenience feature it would be nice to have this functionality directly within the matchers. It could make a fairly common approach more readable.

@beliaev-maksim
Copy link
Collaborator

@spreeni can you fire a PR with implementation proposal based on your custom matcher?

@geetptl
Copy link
Contributor

geetptl commented Aug 9, 2023

@beliaev-maksim can I take this?

@beliaev-maksim
Copy link
Collaborator

@geetptl your contribution will be warmly welcomed!

Please check Contribution section https://github.com/getsentry/responses#contributing

@geetptl
Copy link
Contributor

geetptl commented Aug 9, 2023

Think I'll need to add one more test like test_request_matches_headers_regex_strict. Might need a little more work, I'll get back soon.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Aug 9, 2023
@getsantry getsantry bot removed the status in GitHub Issues with 👀 Aug 9, 2023
markstory pushed a commit that referenced this issue Aug 22, 2023
* [#651] regex compatability for headers matching

* [#651] Adding test for strict with regex

* [#651] Simplifying logic + tests for various sub cases

* [#651] Refactoring test cases in to TestHeaderWithRegex class

* [#651] Making url a property in test class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

5 participants