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

Create Set-Cookie header from the cookie jar (fix #17) #143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

simsor
Copy link

@simsor simsor commented Aug 28, 2020

Hi!

This PR hopefully fixes issue #17 by manually creating the Set-Cookie HTTP header in the create_response function.

I also had to create compat._FakeHTTPResponse in order to make Requests happy: as you mentioned in the issue, extract_cookies_to_jar works by accessing HTTPResponse._original_response, which wasn't populated.

I also added a test which makes sure that cookies are propagated down to the Session object.

Let me know what you think :)

Copy link
Owner

@jamielennox jamielennox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

Thanks for the PR. I'm particularly interested here if this was something you have hit as a limitation, or came across it looking for a bug to fix? The thing that has always concerned me about adding cookies to the session is that the order of your unit tests can become a little unstable.

I've always said that requests-mock should be order independent and that if you were looking to test a big sequence you'd be doing integration testing, and this means that cookies set on the session will now affect subsequent calls.

However, it's been a long time since i've looked at this particular code, and I honestly can't remember why. It's not about requests-mock, it's about how requests handles cookies, and you should expect it to work based on the calls you make, so I can be convinced on the change.

I'm trying to think if i'd consider this a breaking change. I don't think so, but it would definitely need a release note. I can make these changes and merge it if you just want it in, let me know.

Thanks again. Sorry it's taken a while.

"""
:param requests.packages.urllib3.response response: the Response that will
be updated with the Set-Cookie header
:param requests.cookies.RequestsCookieJar jar: a CookieJar containing all
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jar would not appear to be used in function.

I'd say you may as well merge this with the _extract_cookie function? it's basically extending that logic. Otherwise i'd have said please add a _, everything else is explicitly marked private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f69f315 and 5977cc2.

@jamielennox jamielennox added this to the 1.9.0 milestone Oct 4, 2020
@jamielennox jamielennox linked an issue Oct 4, 2020 that may be closed by this pull request
@jamielennox jamielennox modified the milestones: 1.9.0, 1.10 Apr 28, 2021


class SessionCookiesTest(base.TestCase):
def test_cookie(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another test is required that contains two cookies, as the behavior only seems to work for one cookie. See #17 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This answer adds some insight, indicating to me that semicolon separated values aren't appropriate for Set-Cookie. I tried comma-separated values, but that didn't work either... and I've not found a way to indicate more than one Set-Cookie header (because response.headers is a dict).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4ce92fa.

@jaraco
Copy link
Contributor

jaraco commented Jan 6, 2023

Added release notes in 426bf30 and reverted 99 in a96d24f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookies not propagated to requests.Session
3 participants