Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing all cookies in the Flask test response

After I make a request with the Flask test client, I want to access the cookies that the server set. If I iterate over response.headers, I see multiple Set-Cookie headers, but if I do response.headers["Set-Cookie"], I only get one value. Additionally, the headers are unparsed strings that are hard to test.

response = client.get("/")
print(response.headers['Set-Cookie'])
'mycookie=value; Expires=Thu, 27-Jun-2019 13:42:19 GMT; Max-Age=1800; Path=/'

for item in response.headers:
    print(item)

('Content-Type', 'application/javascript')
('Content-Length', '215')
('Set-Cookie', 'mycookie=value; Expires=Thu, 27-Jun-2019 13:42:19 GMT; Max-Age=1800; Path=/')
('Set-Cookie', 'mycookie2=another; Domain=.client.com; Expires=Sun, 04-Apr-2021 13:42:19 GMT; Max-Age=62208000; Path=/')
('Set-Cookie', 'mycookie3=something; Domain=.client.com; Expires=Thu, 04-Apr-2019 14:12:19 GMT; Max-Age=1800; Path=/')

Why does accessing the Set-Cookie header only give me one header? How can I access the cookies and their properties for testing?

like image 238
datacubed Avatar asked Apr 04 '19 13:04

datacubed


People also ask

How do I get all the cookies in my flask?

Flask cookies In Flask, set the cookie on the response object. Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.

How do I get cookies from response headers?

Just set the Set-Cookie header in the response from the server side code. The browser should save it automatically. As a developer, you may be able to inspect the value of the cookies using "Developer Tools". And the same cookie will be sent in subsequent requests to the same domain, until the cookie expires.

How does flask help with cookies?

Flask – Cookies Its purpose is to remember and track data pertaining to a client's usage for better visitor experience and site statistics. A Request object contains a cookie's attribute. It is a dictionary object of all the cookie variables and their corresponding values, a client has transmitted.


2 Answers

The previous answer guided me to a slightly alternate version depending on what you want to do with the cookie.

I tried using client.cookie_jar, but I was testing for a few "non-standard" attributes like HttpOnly and SameSite. The cookie returned from client.cookie_jar does not return them, so I instead inspect the Set-Cookie header:

from werkzeug.http import parse_cookie

cookies = response.headers.getlist('Set-Cookie')
cookie = next(
    (cookie for cookie in cookies if expected_cookie_name in cookie),
    None
)

assert cookie is not None
cookie_attrs = parse_cookie(cookie)

assert cookie_attrs[expected_cookie_name] == expected_cookie_value
assert 'Secure' in cookie_attrs
assert 'HttpOnly' in cookie_attrs
assert cookie_attrs['SameSite'] == 'Lax'
like image 90
Jason Capriotti Avatar answered Nov 10 '22 21:11

Jason Capriotti


response.headers is a MultiDict, which provides the getlist method to get all the values for a given key.

response.headers.getlist('Set-Cookie')

It might be more useful to examine the cookies the client has, rather than the specific raw Set-Cookie headers returned by a response. client.cookie_jar is a CookieJar instance, iterating over it yields Cookie instances. For example, to get the value of the cookie with the name "user_id":

client.post("/login")
cookie = next(
    (cookie for cookie in client.cookie_jar if cookie.name == "user_id"),
    None
)
assert cookie is not None
assert cookie.value == "4"
like image 44
davidism Avatar answered Nov 10 '22 20:11

davidism