From 5cce320dd9d6a3a24c234a39a75b6c1e06112630 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 17:55:26 +0100 Subject: [PATCH 01/10] Add test for custom response from expect handler --- tests/test_web_functional.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 6a2b882a638..c1115399e99 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -5,7 +5,7 @@ import pathlib import socket import zlib -from typing import Any +from typing import Any, Optional from unittest import mock import brotli @@ -576,6 +576,31 @@ async def expect_handler(request): await resp.release() +async def test_expect_handler_custom_response(aiohttp_client: Any) -> None: + cache = {"foo": "bar"} + + async def handler(request: web.Request) -> web.Response: + return web.Response(text="handler") + + async def expect_handler(request: web.Request) -> Optional[web.Response]: + k = request.headers.get("X-Key") + cached_value = cache.get(k) + if cached_value: + return web.Response(text=cache_value) + + app = web.Application() + app.router.add_post("/", handler, expect_handler=expect_handler) + client = await aiohttp_client(app) + + async with client.post("/", expect100=True, headers={"X-Key": "foo"}) as resp: + assert resp.status == 200 + assert await resp.text() == "bar" + + async with client.post("/", expect100=True, headers={"X-Key": "spam"}) as resp: + assert resp.status == 200 + assert await resp.text() == "handler" + + async def test_100_continue_for_not_found(aiohttp_client: Any) -> None: app = web.Application() From 651251d588460a283bdcf65816aef5d20bbc54e5 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 17:57:04 +0100 Subject: [PATCH 02/10] Update test_web_functional.py --- tests/test_web_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index c1115399e99..34c09006679 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -586,7 +586,7 @@ async def expect_handler(request: web.Request) -> Optional[web.Response]: k = request.headers.get("X-Key") cached_value = cache.get(k) if cached_value: - return web.Response(text=cache_value) + return web.Response(text=cached_value) app = web.Application() app.router.add_post("/", handler, expect_handler=expect_handler) From 27a3eee22dc5c2907bfc036408bbb14cb404e36a Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 18:01:45 +0100 Subject: [PATCH 03/10] Update test_web_functional.py --- tests/test_web_functional.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 34c09006679..cbeb015e7e8 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -589,7 +589,8 @@ async def expect_handler(request: web.Request) -> Optional[web.Response]: return web.Response(text=cached_value) app = web.Application() - app.router.add_post("/", handler, expect_handler=expect_handler) + # expect_handler is only typed on add_route(). + app.router.add_route("POST", "/", handler, expect_handler=expect_handler) client = await aiohttp_client(app) async with client.post("/", expect100=True, headers={"X-Key": "foo"}) as resp: From 4a04875329fec0f47242b801c7c01f96129d0521 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 18:13:54 +0100 Subject: [PATCH 04/10] Update web_urldispatcher.py --- aiohttp/web_urldispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index c5efdcbfe7e..10feb54ec09 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -82,7 +82,7 @@ PATH_SEP: Final[str] = re.escape("/") -_ExpectHandler = Callable[[Request], Awaitable[None]] +_ExpectHandler = Callable[[Request], Awaitable[Optional[StreamResponse]]] _Resolve = Tuple[Optional["UrlMappingMatchInfo"], Set[str]] From f361adf9bc6e02c921bd33206abaffd182d78147 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 18:17:33 +0100 Subject: [PATCH 05/10] Update abc.py --- aiohttp/abc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/abc.py b/aiohttp/abc.py index 58ebb1c1de1..59fb68fd000 100644 --- a/aiohttp/abc.py +++ b/aiohttp/abc.py @@ -63,7 +63,7 @@ def handler(self) -> Callable[[Request], Awaitable[StreamResponse]]: @property @abstractmethod - def expect_handler(self) -> Callable[[Request], Awaitable[None]]: + def expect_handler(self) -> Callable[[Request], Awaitable[Optional[StreamResponse]]]: """Expect handler for 100-continue processing""" @property # pragma: no branch From be9033926f895db1cbcfed9901f4d2a708c4873c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 17:18:15 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- aiohttp/abc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aiohttp/abc.py b/aiohttp/abc.py index 59fb68fd000..41200b99457 100644 --- a/aiohttp/abc.py +++ b/aiohttp/abc.py @@ -63,7 +63,9 @@ def handler(self) -> Callable[[Request], Awaitable[StreamResponse]]: @property @abstractmethod - def expect_handler(self) -> Callable[[Request], Awaitable[Optional[StreamResponse]]]: + def expect_handler( + self, + ) -> Callable[[Request], Awaitable[Optional[StreamResponse]]]: """Expect handler for 100-continue processing""" @property # pragma: no branch From 6e007d32ebe6c69ce3eb9a3e0a216458f0ac19d4 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 20:33:09 +0100 Subject: [PATCH 07/10] Update test_web_functional.py --- tests/test_web_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index cbeb015e7e8..f4a39d4f350 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -577,7 +577,7 @@ async def expect_handler(request): async def test_expect_handler_custom_response(aiohttp_client: Any) -> None: - cache = {"foo": "bar"} + cache = {b"foo": "bar"} async def handler(request: web.Request) -> web.Response: return web.Response(text="handler") From 800f53ec2a61ebf6babddce241f75d77c2278ca9 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 20:48:55 +0100 Subject: [PATCH 08/10] Update test_web_functional.py --- tests/test_web_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index f4a39d4f350..cbeb015e7e8 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -577,7 +577,7 @@ async def expect_handler(request): async def test_expect_handler_custom_response(aiohttp_client: Any) -> None: - cache = {b"foo": "bar"} + cache = {"foo": "bar"} async def handler(request: web.Request) -> web.Response: return web.Response(text="handler") From ee8afc3e48d939f3edb713eac795e9246c6f68b7 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 20:52:33 +0100 Subject: [PATCH 09/10] Update web_urldispatcher.py --- aiohttp/web_urldispatcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 10feb54ec09..153f5ba5de3 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -213,8 +213,8 @@ def get_info(self) -> _InfoDict: def url_for(self, *args: str, **kwargs: str) -> URL: """Construct url for route with additional params.""" - async def handle_expect_header(self, request: Request) -> None: - await self._expect_handler(request) + async def handle_expect_header(self, request: Request) -> Optional[StreamResponse]: + return await self._expect_handler(request) class UrlMappingMatchInfo(BaseDict, AbstractMatchInfo): From 984cf2e395d31c8489fea8a5dbc5848931821549 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 16 Oct 2022 21:01:30 +0100 Subject: [PATCH 10/10] Create 7025.bugfix --- CHANGES/7025.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/7025.bugfix diff --git a/CHANGES/7025.bugfix b/CHANGES/7025.bugfix new file mode 100644 index 00000000000..34e07dda9ed --- /dev/null +++ b/CHANGES/7025.bugfix @@ -0,0 +1 @@ +Fix response returned from expect handler being thrown away. -- by :user:`Dreamsorcerer`