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

[3.8] Backport fix for setting cookies #5233

Merged
merged 9 commits into from
Nov 29, 2020
1 change: 1 addition & 0 deletions CHANGES/5233.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix cookies disappearing from HTTPExceptions.
21 changes: 10 additions & 11 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,7 @@ async def _handle_request(
finally:
self._current_request = None
except HTTPException as exc:
resp = Response(
status=exc.status, reason=exc.reason, text=exc.text, headers=exc.headers
)
resp = exc
reset = await self.finish_response(request, resp, start_time)
except asyncio.CancelledError:
raise
Expand All @@ -434,6 +432,15 @@ async def _handle_request(
resp = self.handle_error(request, 500, exc)
reset = await self.finish_response(request, resp, start_time)
else:
# Deprecation warning (See #2415)
if getattr(resp, "__http_exception__", False):
warnings.warn(
"returning HTTPException object is deprecated "
"(#2415) and will be removed, "
"please raise the exception instead",
DeprecationWarning,
)

reset = await self.finish_response(request, resp, start_time)

return resp, reset
Expand Down Expand Up @@ -492,14 +499,6 @@ async def start(self) -> None:
except (asyncio.CancelledError, ConnectionError):
self.log_debug("Ignored premature client disconnection")
break
# Deprecation warning (See #2415)
if getattr(resp, "__http_exception__", False):
warnings.warn(
"returning HTTPException object is deprecated "
"(#2415) and will be removed, "
"please raise the exception instead",
DeprecationWarning,
)

# Drop the processed task from asyncio.Task.all_tasks() early
del task
Expand Down
28 changes: 28 additions & 0 deletions tests/test_web_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,31 @@ def test_HTTPException_retains_cause() -> None:
tb = "".join(format_exception(ei.type, ei.value, ei.tb))
assert "CustomException" in tb
assert "direct cause" in tb


async def test_HTTPException_retains_cookie(aiohttp_client):
@web.middleware
async def middleware(request, handler):
try:
return await handler(request)
except web.HTTPException as exc:
exc.set_cookie("foo", request["foo"])
raise exc

async def save(request):
request["foo"] = "works"
raise web.HTTPFound("/show")

async def show(request):
return web.Response(text=request.cookies["foo"])

app = web.Application(middlewares=[middleware])
app.router.add_route("GET", "/save", save)
app.router.add_route("GET", "/show", show)
client = await aiohttp_client(app)

resp = await client.get("/save")
assert resp.status == 200
assert str(resp.url)[-5:] == "/show"
text = await resp.text()
assert text == "works"