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

Fix expect handler when returning a response #7025

Merged
merged 10 commits into from
Oct 29, 2022
27 changes: 26 additions & 1 deletion tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=cached_value)
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved

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()
Expand Down