question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sessions lose cookies

See original GitHub issue

Hi,

It seems that there’s a problem with the mocked HTTP requests returned when using a session - the session cookie is returned in the response object, but does not get set in the session.

I am pretty sure it’s down to the extract_cookies_to_jar function in cookielib/cookies.py:

def extract_cookies_to_jar(jar, request, response):
    """Extract the cookies from the response into a CookieJar.

    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object
    """
    if not (hasattr(response, '_original_response') and
            response._original_response):
        return
    # the _original_response field is the wrapped httplib.HTTPResponse object,
    req = MockRequest(request)
    # pull out the HTTPMessage with the headers and put it in the mock:
    res = MockResponse(response._original_response.msg)
    jar.extract_cookies(res, req)

The _original_response attribute is missing from the mocked HTTPResponse, so no cookies get pulled out into the jar. I’m trying to come up with a solution and if I do I’ll post here, but I should thought I’d let the community at large know. Perhaps I’m doing something silly 😃

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mathewcohlecommented, Jul 24, 2018

Hi,

I want to report that the issue still persists. Following example reproduces it:

import requests
import responses

session = requests.Session()
headers = {'Set-Cookie': 'foo=bar'}

with responses.RequestsMock() as rsps:
    rsps.add(
        responses.GET, 'https://google.com', headers=headers)
    r = session.get('https://google.com')


print(r.cookies)
>>> <RequestsCookieJar[<Cookie foo=bar for />]>

print(session.cookies)
>>> <RequestsCookieJar[]>
$ python --version
Python 3.7.0

$ pip freeze 
responses==0.9.0
requests==2.19.1
0reactions
markstorycommented, Dec 12, 2019

Closing as #285 was merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are some things that can cause session cookies to fail ...
Their session tokens are still valid, and they haven't logged out. All of a sudden, they apparently don't have the session cookie anymore....
Read more >
Session variable and Cookies losing value - MSDN - Microsoft
When users log into my application, i set some values to a cookie (UserID, emailaddress, UserName etc) these are used often throughout the...
Read more >
Lost all my cookies (browser sessions) after Brave crash #4492
This just happened to me (lost all cookies after crash). This is terrible loose of time. I'm considering switching to another browser. Version ......
Read more >
HTTP session problems - IBM
HTTP sessions are not getting created, or are lost between requests. By default, the session manager uses cookies to store the session ID...
Read more >
Session Restore remembers logins from session cookies
Our focus is on end users and consumers, not on shared computer labs, and the value of this feature is pretty huge to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found