def test_stored_cookie_max_age_precedence(): cookie = Cookie("X-Foo", "Foo") cookie.max_age = 0 cookie.expires = datetime.utcnow() + timedelta(days=2) stored_cookie = StoredCookie(cookie) assert stored_cookie.is_expired()
async def test_remove_cookie_with_expiration(): expire_cookie = Cookie("X-Foo", "Foo") expire_cookie.expires = datetime.utcnow() + timedelta(days=-2) fake_pools = FakePools( [ Response( 200, [(b"Set-Cookie", write_response_cookie(Cookie("X-Foo", "Foo")))] ).with_content(TextContent("Hello, World!")), Response(200, None, TextContent("Hello!")), Response( 200, [(b"Set-Cookie", write_response_cookie(expire_cookie))] ).with_content(TextContent("Hello, World!")), Response(200, None, TextContent("Hello!")), ] ) expect_cookie = False async def middleware_for_assertions(request, next_handler): cookie = request.cookies.get("X-Foo") if expect_cookie: assert cookie is not None, "X-Foo cookie must be configured" else: assert cookie is None return await next_handler(request) async with ClientSession( base_url=b"https://bezkitu.org", pools=fake_pools, middlewares=[middleware_for_assertions], ) as client: await client.get(b"/") # <-- cookie set here expect_cookie = True await client.get(b"/") # <-- expect cookie in request expect_cookie = True await client.get(b"/") # <-- expect cookie in request; it gets removed here expect_cookie = False await client.get( b"/" ) # <-- expect missing cookie; was deleted by previous response