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

Simple test case to check if header is available in failed request response, if handled #79

Merged
merged 2 commits into from
Nov 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions tests/test_context/test_context_on_unhandled_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
from starlette.responses import Response
from starlette.testclient import TestClient

from starlette_context import middleware, plugins
from starlette_context import middleware, plugins, context
from starlette_context.header_keys import HeaderKeys


class CustomExc(Exception):
pass


def custom_exc_handler(request: Request, exc):
return Response(status_code=400, headers=context.data)


app = Starlette(
middleware=[
Middleware(
middleware.ContextMiddleware, plugins=(plugins.RequestIdPlugin(),)
middleware.RawContextMiddleware,
plugins=(plugins.RequestIdPlugin(),),
)
]
],
exception_handlers={CustomExc: custom_exc_handler},
)


Expand All @@ -22,6 +33,11 @@ async def exc(request: Request):
raise Exception


@app.route("/handled")
async def handled(request: Request):
raise CustomExc


@app.route("/")
async def index(request: Request):
return Response(status_code=status.HTTP_200_OK)
Expand All @@ -38,3 +54,7 @@ def test_context_persistence():
resp = client.get("/exc")
assert resp.status_code == 500
assert HeaderKeys.request_id.lower() not in resp.headers

resp = client.get("/handled")
assert resp.status_code == 400
assert HeaderKeys.request_id.lower() in resp.headers