getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.14k stars 354 forks source link

_recorder - matching http headers as well as URL #639

Open stevelittlefish opened 1 year ago

stevelittlefish commented 1 year ago

For my project, I need to mock responses from an API where some of the parameters are included in a HTTP header, so for example I would make a request to http://api.com/projects with a http header set to user-id: joebloggs

Currently I am recording the responses by doing:

@_recorder.record(filepath="out.yaml")
def test_something():
    ...

and then I'm loading the yaml file like this:

@responses.activate
def test_something():
    responses.add_from_file("out.yaml")
    ...

Is it possible to set up the registry to match on this HTTP header, and also save this header in the .yaml files that it generates?

beliaev-maksim commented 1 year ago

fully legit request

I am working on the framework to record matchers as well

just to be sure, you are looking for matching request header as per https://github.com/getsentry/responses#request-headers-validation ?

stevelittlefish commented 1 year ago

Yes I think so (although I haven't actually done it like that using this library as I have just been using the recorded files up until now).

I'm pretty sure I would want to do something like:

    responses.get(
        url="http://loginsystem.com/projects",
        match=[matchers.header_matcher({"Impersonate-User": "Mr. Bean"})],
    )
beliaev-maksim commented 1 year ago

Gotcha

Unfortunately, I cannot commit to any timeline, since it is pretty hard to accommodate all the matchers for the recorder. Need to think about some nice API that could be extended and that provides all the functionality for recording into file

stevelittlefish commented 1 year ago

I know you can't commit to a timeline, (and I'm not trying to rush you), but would you be able to give some indication of whether it's likely to be weeks or months or longer?

I need to decide whether I am going to wait or try some other workaround / look for another solution in the meantime.

beliaev-maksim commented 1 year ago

in the best case by end of June but likely to take more since we will need to iterate through the process

PS consider applying manually responses with matchers in meantime