getsentry / responses

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

Cannot record header to file #715

Closed dwt closed 5 months ago

dwt commented 6 months ago

Describe the bug

When recording a request that contains a location header, that header is not there anymore, when replaying that request.

Additional context

It seems the code in _add_from_file() doesn't seem to restore headers, while the code in _dump() does seem to save them.

Version of responses

0.25.0

Steps to Reproduce

>>> import responses
>>> responses.post(url="fnord", status=201, headers=dict(Location='fnord'))
<Response(url='fnord' status=201 content_type='text/plain' headers='{"Location": "fnord"}')>
>>> responses.registered()
[<Response(url='fnord' status=201 content_type='text/plain' headers='{"Location": "fnord"}')>, <Response(url='fnord' status=201 content_type='text/plain' headers='{"Location": "fnord"}')>]
>>> import responses._recorder
>>> import yaml
>>> responses._recorder._dump(responses.registered(), open('/dev/stdout', 'w'), yaml.dump)
responses:
- response:
    auto_calculate_content_length: false
    body: ''
    content_type: text/plain
    headers:
      Location: fnord
    method: POST
    status: 201
    url: fnord
>>> responses._recorder._dump(responses.registered(), open('/tmp/dump', 'w'), yaml.dump)
>>> responses._add_from_file(file_path="/tmp/dump")
>>> responses.registered()
[<Response(url='fnord' status=201 content_type='text/plain' headers='{"Location": "fnord"}')>, <Response(url='fnord' status=201 content_type='text/plain' headers='null')>]

Expected Result

Interestingly this does save the location headers, while they are not saved in my application where I discovered this. Still when loading the dump, the headers are ignored.

I'd expect the location header to survive the save / restore.

Actual Result

Location header gets stripped by the replay from file.

TheJacobWalters commented 6 months ago

I was able to reproduce this. If the maintainers determine this to be a bug that should be fixed. Could I try and fix it?

markstory commented 6 months ago

If you have time and interest a pull request would be greatly appreciated @TheJacobWalters