Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[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
def test_get_cookies_for_url():
jar = CookieJar()
jar.add(URL(b'https://foo.org'), Cookie(b'hello', b'world'))
cookies = list(jar.get_cookies_for_url(URL(b'https://foo.org/hello-world')))
assert len(cookies) == 1
assert cookies[0].name == b'hello'
assert cookies[0].value == b'world'
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
[Cookie(b'session', b'123'), Cookie(b'aaa', b'bbb', domain=b'bezkitu.org')],
b'HTTP/1.1 400 Bad Request\r\n'
b'Server: BlackSheep\r\n'
b'set-cookie: session=123\r\n'
b'set-cookie: aaa=bbb; Domain=bezkitu.org\r\n'
b'content-type: text/plain\r\n'
b'content-length: 12\r\n\r\nHello, World'
)
])
async def test_write_http_response(response, cookies, expected_result):
response.set_cookies(cookies)
data = b''
async for chunk in scribe.write_response(response):
data += chunk
assert data == expected_result
async def test_remove_cookie_with_expiration():
expire_cookie = Cookie(b'X-Foo', b'Foo')
expire_cookie.expiration = datetime.utcnow() + timedelta(days=-2)
fake_pools = FakePools([Response(200, [(b'Set-Cookie', write_response_cookie(Cookie(b'X-Foo', b'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
async def test_cookies_jar_single_cookie():
fake_pools = FakePools([Response(200,
[(b'Set-Cookie', write_response_cookie(Cookie(b'X-Foo', b'Foo')))])
.with_content(TextContent('Hello, World!')),
Response(200, None, TextContent('Hello!'))])
check_cookie = False
async def middleware_for_assertions(request, next_handler):
if check_cookie:
cookie = request.cookies.get('X-Foo')
assert cookie is not None, 'X-Foo cookie must be configured for following requests'
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'/') # the first request doesn't have any cookie because the response will set;
check_cookie = True
def test_stored_cookie_max_age_precedence():
cookie = Cookie(b'X-Foo', b'Foo')
cookie.set_max_age(0)
cookie.expiration = datetime.utcnow() + timedelta(days=2)
stored_cookie = StoredCookie(cookie)
assert stored_cookie.is_expired()
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)
async def set_cookies(name: FromQuery(str), value: FromQuery(str)):
response = text('Setting cookie')
response.set_cookie(Cookie(name.encode(), value.encode()))
return response
def test_cookiejar_get_domain(request_url, cookie_domain, expected_domain):
jar = CookieJar()
cookie = Cookie(b'Name', b'Value', domain=cookie_domain)
domain = jar.get_domain(request_url, cookie)
assert domain == expected_domain