def test_write_cookie( name, value, expires, domain, path, http_only, secure, max_age, same_site, expected_result, ): cookie = Cookie( name, value, datetime_to_cookie_format(expires) if expires else None, domain, path, http_only, secure, datetime_to_cookie_format(max_age) if max_age else None, same_site, ) value = scribe.write_response_cookie(cookie) assert value == expected_result
def test_cookie_jar_does_not_override_http_only_cookie_with_non_http_only_cookie( ): jar = CookieJar() jar.add( URL(b"https://foo.org"), Cookie( b"hello", b"world", expires=datetime_to_cookie_format(datetime.utcnow() + timedelta(days=2)), http_only=True, ), ) jar.add( URL(b"https://foo.org"), Cookie( b"hello", b"world2", expires=datetime_to_cookie_format(datetime.utcnow() + timedelta(days=2)), http_only=True, ), ) cookie = jar.get(b"foo.org", b"/", b"hello") assert cookie is not None assert cookie.cookie.value == b"world2" jar.add( URL(b"https://foo.org"), Cookie( b"hello", b"world modified", expires=datetime_to_cookie_format(datetime.utcnow() + timedelta(days=2)), http_only=False, ), ) cookie = jar.get(b"foo.org", b"/", b"hello") assert cookie is not None assert cookie.cookie.value == b"world2"
[URL(b'https://foo.bezkitu.org'), b'foo.org']]) def test_cookiejar_invalid_domain(request_url, cookie_domain): jar = CookieJar() cookie = Cookie(b'Name', b'Value', domain=cookie_domain) with pytest.raises(InvalidCookieDomain): jar.add(request_url, cookie) @pytest.mark.parametrize( 'cookie,expected_value', [[Cookie(b'name', b'value'), False], [ Cookie(b'name', b'value', expires=datetime_to_cookie_format(datetime.utcnow() + timedelta(days=-20))), True ]]) def test_stored_cookie_is_expired(cookie, expected_value): stored = StoredCookie(cookie) expired = stored.is_expired() assert expected_value == expired @pytest.mark.asyncio async def test_cookies_jar_single_cookie(): fake_pools = FakePools([ Response( 200, Headers([ Header(b'Set-Cookie', write_response_cookie(Cookie(b'X-Foo', b'Foo')))
def test_datetime_from_cookie_format_2(expected_result, value): bytes_value = datetime_to_cookie_format(value) assert bytes_value == expected_result