Skip to content

Commit 1cef8b8

Browse files
committed
pysnippetGH-42: testscases middleware overhandling excepts
- Unintended errors in code should be 500 - Non Http custom exceptions should be 500 - Handled exceptions should use handler (and be 418 in this case) But all of them are reported as 401 Unauthorized even when the entrypoint does not requires authorization.
1 parent 1e6d30a commit 1cef8b8

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/test_middleware.py

+42
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,45 @@ async def test_middleware_on_logout(get_app):
2626

2727
response = await client.get("/user")
2828
assert response.status_code == 403 # Forbidden
29+
30+
@pytest.mark.anyio
31+
async def test_middleware_do_not_interfer_user_errors(get_app):
32+
app=get_app()
33+
@app.get('/unexpected_error')
34+
def unexpected():
35+
undefined_id # Intended code error
36+
37+
async with AsyncClient(app=app, base_url="http://test") as client:
38+
response = await client.get("/unexpected_error")
39+
assert response.status_code == 500 # Internal server error
40+
41+
@pytest.mark.anyio
42+
async def test_middleware_ignores_custom_exceptions(get_app):
43+
class MyCustomException(Exception): pass
44+
app=get_app()
45+
@app.get('/custom_exception')
46+
def custom_exception():
47+
raise MyCustomException()
48+
49+
async with AsyncClient(app=app, base_url="http://test") as client:
50+
response = await client.get("/custom_exception")
51+
assert response.status_code == 500 # Internal server error
52+
53+
@pytest.mark.anyio
54+
async def test_middleware_ignores_handled_custom_exceptions(get_app):
55+
class MyCustomException(Exception): pass
56+
app=get_app()
57+
@app.exception_handler(MyCustomException)
58+
async def unicorn_exception_handler(request, exc):
59+
return JSONResponse(
60+
status_code=418,
61+
content={"message": f"I am a Teapot!"},
62+
)
63+
64+
@app.get('/custom_exception')
65+
def custom_exception():
66+
raise MyCustomException()
67+
68+
async with AsyncClient(app=app, base_url="http://test") as client:
69+
response = await client.get("/custom_exception")
70+
assert response.status_code == 418 # I am a teapot!

0 commit comments

Comments
 (0)