Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testclient test for domain restricted cookies #2154

Merged
merged 4 commits into from
May 31, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/test_testclient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import sys
from asyncio import current_task as asyncio_current_task
from contextlib import asynccontextmanager

Expand Down Expand Up @@ -273,3 +274,48 @@ def homepage(request):
client = test_client_factory(app)
response = client.get("/", params={"param": param})
assert response.text == param


@pytest.mark.parametrize(
"domain, ok",
[
pytest.param(
"testserver",
True,
marks=[
pytest.mark.xfail(
sys.version_info < (3, 11),
reason="Fails due to domain handling in http.cookiejar module (see "
"#2152)",
),
],
),
("testserver.local", True),
("localhost", False),
("example.com", False),
],
)
def test_domain_restricted_cookies(test_client_factory, monkeypatch, domain, ok):
"""
Test that test client discards domain restricted cookies which do not match the
base_url of the testclient (`http://testserver` by default).

The domain `testserver.local` works because the Python http.cookiejar module derives
the "effective domain" by appending `.local` to non-dotted request domains
in accordance with RFC 2965.
"""

async def app(scope, receive, send):
response = Response("Hello, world!", media_type="text/plain")
response.set_cookie(
"mycookie",
"myvalue",
path="/",
domain=domain,
)
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/")
cookie_set = len(response.cookies) == 1
assert cookie_set == ok