responses: Cookie handling regression from 0.14.0 to 0.15.0
Environment
Using the responses library under Python 3.9.7
Steps to Reproduce
Run this test against responses 0.14.0 and against responses 0.15.0
#!/usr/bin/env python3
import sys
import requests
import responses
def main():
with responses.RequestsMock() as fakes:
fakes.add(responses.GET, 'https://example.com', status=200,
adding_headers=[
('Set-Cookie', 'mycookie=myvalue; path=/; secure'),
],
body="Returned body",
)
session = requests.session()
response = session.get('https://example.com')
tests = {}
tests['response-cookie'] = 'mycookie' in response.cookies
tests['session-cookie'] = 'mycookie' in session.cookies
failures = 0
for testname, result in tests.items():
if not result:
failures += 1
sys.stdout.write(f"Test {testname} result {result}\n")
if failures:
sys.stdout.write(f"{failures} of {len(tests)} failed.\n")
else:
sys.stdout.write(f"All {len(tests)} tests passed.\n")
return 1 if failures else 0
if __name__ == '__main__':
sys.exit(main())
Expected Result
Expected ‘mycookie’ to appear in both the response.cookies and the session.cookies
Actual Result
‘mycookie’ appears in both the response.cookies and the session.cookies for 0.14.0 ‘mycookie’ appears in the response.cookies but not in the session.cookies for 0.15.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 25 (11 by maintainers)
Commits related to this issue
- Fix regression from stream deprecation Not closing the response results in cookies not being persisted to the requests session. Fixes #421 — committed to getsentry/responses by markstory 3 years ago
- Special handling of the stream flags appears counter-productive Fixes #421 — committed to retracile/responses by retracile 3 years ago
- Fix regression from stream deprecation (#422) Not closing the response results in cookies not being persisted to the requests session. Thanks to @beliaev-maksim for help with the tests. Fixes ... — committed to getsentry/responses by markstory 3 years ago
Release for 0.16 is scheduled. https://github.com/getsentry/responses/actions/runs/1449966071
I suspect the issue may be the removal of the
response.close()call inRequestsMock._on_request, but I’m not sure.