Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 52 additions & 11 deletions tests/test_benchmarks_client_request.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
"""codspeed benchmarks for client requests."""

import asyncio
from http.cookies import Morsel
from http.cookies import BaseCookie
from typing import Union

from multidict import CIMultiDict
from pytest_codspeed import BenchmarkFixture
from yarl import URL

from aiohttp.client_reqrep import ClientRequest
from aiohttp.client_reqrep import ClientRequest, ClientResponse
from aiohttp.cookiejar import CookieJar
from aiohttp.helpers import TimerNoop
from aiohttp.http_writer import HttpVersion11
from aiohttp.tracing import Trace


def test_client_request_update_cookies(
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
) -> None:
req = ClientRequest("get", URL("http://python.org"), loop=loop)
morsel: "Morsel[str]" = Morsel()
morsel.set(key="string", val="Another string", coded_val="really")
morsel_cookie = {"str": morsel}
url = URL("http://python.org")
req = ClientRequest("get", url, loop=loop)
cookie_jar = CookieJar()
cookie_jar.update_cookies({"string": "Another string"})
cookies = cookie_jar.filter_cookies(url)
assert cookies["string"].value == "Another string"

@benchmark
def _run() -> None:
req.update_cookies(cookies=morsel_cookie)
req.update_cookies(cookies=cookies)


def test_create_client_request_with_cookies(
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
) -> None:
url = URL("http://python.org")
cookie_jar = CookieJar()
cookie_jar.update_cookies({"cookie": "value"})
cookies = cookie_jar.filter_cookies(url)
assert cookies["cookie"].value == "value"
timer = TimerNoop()
traces: list[Trace] = []
headers = CIMultiDict[str]()

@benchmark
def _run() -> None:
ClientRequest(
method="get",
url=url,
loop=loop,
headers=None,
params=None,
skip_auto_headers=None,
response_class=ClientResponse,
proxy=None,
proxy_auth=None,
proxy_headers=None,
timer=timer,
session=None,
ssl=True,
traces=traces,
trust_env=False,
server_hostname=None,
headers=headers,
data=None,
cookies={"cookie": "value"},
cookies=cookies,
auth=None,
version=HttpVersion11,
compress=False,
Expand All @@ -50,16 +75,32 @@ def test_create_client_request_with_headers(
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
) -> None:
url = URL("http://python.org")
timer = TimerNoop()
traces: list[Trace] = []
headers = CIMultiDict({"header": "value", "another": "header"})
cookies = BaseCookie[str]()

@benchmark
def _run() -> None:
ClientRequest(
method="get",
url=url,
loop=loop,
headers={"header": "value", "another": "header"},
params=None,
skip_auto_headers=None,
response_class=ClientResponse,
proxy=None,
proxy_auth=None,
proxy_headers=None,
timer=timer,
session=None,
ssl=True,
traces=traces,
trust_env=False,
server_hostname=None,
headers=headers,
data=None,
cookies=None,
cookies=cookies,
auth=None,
version=HttpVersion11,
compress=False,
Expand Down
127 changes: 56 additions & 71 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ async def test_access_non_existing_resource(
client = await aiohttp_client(app)

# Request the root of the static directory.
r = await client.get("/non_existing_resource")
assert r.status == 404
async with client.get("/non_existing_resource") as r:
assert r.status == 404


@pytest.mark.parametrize(
Expand All @@ -369,8 +369,8 @@ async def handler(request: web.Request) -> web.Response:
app.router.add_get(registered_path, handler)
client = await aiohttp_client(app)

r = await client.get(request_url)
assert r.status == 200
async with client.get(request_url) as r:
assert r.status == 200


async def test_handler_metadata_persistence() -> None:
Expand Down Expand Up @@ -405,8 +405,8 @@ async def test_static_directory_without_read_permission(
app.router.add_static("/", str(tmp_path), show_index=True)
client = await aiohttp_client(app)

r = await client.get(f"/{my_dir.name}/{file_request}")
assert r.status == 403
async with client.get(f"/{my_dir.name}/{file_request}") as r:
assert r.status == 403


@pytest.mark.parametrize("file_request", ["", "my_file.txt"])
Expand Down Expand Up @@ -440,10 +440,10 @@ def mock_is_dir(self: pathlib.Path, **kwargs: Any) -> bool:
app.router.add_static("/", str(tmp_path), show_index=True)
client = await aiohttp_client(app)

r = await client.get("/")
assert r.status == 200
r = await client.get(f"/{my_dir.name}/{file_request}")
assert r.status == 403
async with client.get("/") as r:
assert r.status == 200
async with client.get(f"/{my_dir.name}/{file_request}") as r:
assert r.status == 403


@pytest.mark.skipif(
Expand All @@ -461,8 +461,8 @@ async def test_static_file_without_read_permission(
app.router.add_static("/", str(tmp_path))
client = await aiohttp_client(app)

r = await client.get(f"/{my_file.name}")
assert r.status == 403
async with client.get(f"/{my_file.name}") as r:
assert r.status == 403


async def test_static_file_with_mock_permission_error(
Expand Down Expand Up @@ -490,10 +490,10 @@ def mock_open(self: pathlib.Path, *args: Any, **kwargs: Any) -> Any:
client = await aiohttp_client(app)

# Test the mock only applies to my_file, then test the permission error.
r = await client.get(f"/{my_readable.name}")
assert r.status == 200
r = await client.get(f"/{my_file.name}")
assert r.status == 403
async with client.get(f"/{my_readable.name}") as r:
assert r.status == 200
async with client.get(f"/{my_file.name}") as r:
assert r.status == 403


async def test_access_symlink_loop(
Expand All @@ -510,8 +510,8 @@ async def test_access_symlink_loop(
client = await aiohttp_client(app)

# Request the root of the static directory.
r = await client.get("/" + my_dir_path.name)
assert r.status == 404
async with client.get("/" + my_dir_path.name) as r:
assert r.status == 404


async def test_access_compressed_file_as_symlink(
Expand All @@ -530,9 +530,8 @@ async def test_access_compressed_file_as_symlink(
client = await aiohttp_client(app)

# Symlink should be ignored; response reflects missing uncompressed file.
resp = await client.get(f"/{gz_link.stem}", auto_decompress=False)
assert resp.status == 404
resp.release()
async with client.get(f"/{gz_link.stem}", auto_decompress=False) as resp:
assert resp.status == 404

# Again symlin is ignored, and then uncompressed is served.
txt_file = gz_link.with_suffix("")
Expand Down Expand Up @@ -564,8 +563,8 @@ async def test_access_special_resource(
app.router.add_static("/", str(tmp_path))

client = await aiohttp_client(app)
r = await client.get(f"/{my_special.name}")
assert r.status == 403
async with client.get(f"/{my_special.name}") as r:
assert r.status == 403
my_socket.close()


Expand Down Expand Up @@ -595,8 +594,8 @@ def mock_stat(path: Any, **kwargs: Any) -> os.stat_result:
app.router.add_static("/", str(tmp_path))
client = await aiohttp_client(app)

r = await client.get(f"/{my_special.name}")
assert r.status == 403
async with client.get(f"/{my_special.name}") as r:
assert r.status == 403


async def test_partially_applied_handler(aiohttp_client: AiohttpClient) -> None:
Expand Down Expand Up @@ -626,8 +625,8 @@ async def test_static_head(
app.router.add_static("/", str(tmp_path))
client = await aiohttp_client(app)

r = await client.head("/test.txt")
assert r.status == 200
async with client.head("/test.txt") as r:
assert r.status == 200

# Check that there is no content sent (see #4809). This can't easily be
# done with aiohttp_client because the buffering can consume the content.
Expand Down Expand Up @@ -665,21 +664,17 @@ async def handler(request: web.Request) -> web.Response:
app.router.add_get("/b", handler, allow_head=False, name="b")
client = await aiohttp_client(app)

r = await client.get("/a")
assert r.status == 200
r.release()
async with client.get("/a") as r:
assert r.status == 200

r = await client.head("/a")
assert r.status == 200
r.release()
async with client.head("/a") as r:
assert r.status == 200

r = await client.get("/b")
assert r.status == 200
r.release()
async with client.get("/b") as r:
assert r.status == 200

r = await client.head("/b")
assert r.status == 405
r.release()
async with client.head("/b") as r:
assert r.status == 405


@pytest.mark.parametrize(
Expand Down Expand Up @@ -736,17 +731,14 @@ async def post(self) -> web.Response:

client = await aiohttp_client(app)

r = await client.get("/a")
assert r.status == 200
r.release()
async with client.get("/a") as r:
assert r.status == 200

r = await client.post("/a")
assert r.status == 200
r.release()
async with client.post("/a") as r:
assert r.status == 200

r = await client.put("/a")
assert r.status == 405
r.release()
async with client.put("/a") as r:
assert r.status == 405


async def test_decorate_view(aiohttp_client: AiohttpClient) -> None:
Expand All @@ -765,17 +757,14 @@ async def post(self) -> web.Response:

client = await aiohttp_client(app)

r = await client.get("/a")
assert r.status == 200
r.release()
async with client.get("/a") as r:
assert r.status == 200

r = await client.post("/a")
assert r.status == 200
r.release()
async with client.post("/a") as r:
assert r.status == 200

r = await client.put("/a")
assert r.status == 405
r.release()
async with client.put("/a") as r:
assert r.status == 405


async def test_web_view(aiohttp_client: AiohttpClient) -> None:
Expand All @@ -792,17 +781,14 @@ async def post(self) -> web.Response:

client = await aiohttp_client(app)

r = await client.get("/a")
assert r.status == 200
r.release()
async with client.get("/a") as r:
assert r.status == 200

r = await client.post("/a")
assert r.status == 200
r.release()
async with client.post("/a") as r:
assert r.status == 200

r = await client.put("/a")
assert r.status == 405
r.release()
async with client.put("/a") as r:
assert r.status == 405


async def test_static_absolute_url(
Expand All @@ -817,8 +803,8 @@ async def test_static_absolute_url(
here = pathlib.Path(__file__).parent
app.router.add_static("/static", here)
client = await aiohttp_client(app)
resp = await client.get("/static/" + str(file_path.resolve()))
assert resp.status == 403
async with client.get("/static/" + str(file_path.resolve())) as resp:
assert resp.status == 403


async def test_for_issue_5250(
Expand Down Expand Up @@ -960,9 +946,8 @@ async def get(self) -> web.Response:

client = await aiohttp_client(app)

r = await client.get("///a")
assert r.status == 200
r.release()
async with client.get("///a") as r:
assert r.status == 200


async def test_route_with_regex(aiohttp_client: AiohttpClient) -> None:
Expand Down
Loading