Skip to content

Commit

Permalink
[3.8] Backport fix for setting cookies (#5233)
Browse files Browse the repository at this point in the history
Co-authored-by: aio-libs-github-bot[bot] <72856194+aio-libs-github-bot[bot]@users.noreply.github.com>
Co-authored-by: Gary Wilson Jr <[email protected]>
Co-authored-by: Sam Bull <[email protected]>
  • Loading branch information
4 people authored Nov 29, 2020
1 parent 1e6ec85 commit bb04402
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
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"

0 comments on commit bb04402

Please sign in to comment.