First Check
Commit to Help
Example Code
import uuid
from fastapi import FastAPI, APIRouter, Header, HTTPException
from fastapi.testclient import TestClient
from pydantic import BaseModel
# Models and CRUD
class Item(BaseModel):
id: int
content: str
def crud_get_items():
# This function lies in a separate module
return [
Item(id=1, content="First item"),
Item(id=2, content="Second item"),
]
# App
app = APIRouter()
@app.get("/")
async def get_items(user_id: str = Header()):
data = crud_get_items()
if not len(data): # This is redundant in the example, but exist in "normal" codebase
raise HTTPException(status_code=404, detail="No items found")
return data
# Tests
client = TestClient(app)
def test_get_items():
headers = {"user-id": str(uuid.uuid4())}
res = client.get("/", headers=headers)
assert res.status_code == 200
def test_get_items_without_userid():
res = client.get("/")
assert res.status_code == 422
Description
When testing my app I noticed, that sending request to router without header parameter set, I get RequestValidationError instead of 422 status code. This doesn't happen, when using FastAPI instead of APIRouter.
One could say, that an easy fix to this is to wrap the second test code in pytest.raises(), but this will only execute the client.get while skipping all asserts the test has.
from fastapi.exceptions import RequestValidationError
def test_get_items_without_userid():
with pytest.raises(RequestValidationError):
res = client.get("/") # This line will execute
assert res.status_code == 422 # This line won't (and any below)
Operating System
Windows
Operating System Details
Windows 10, 21H2
FastAPI Version
0.85.0
Python Version
Python 3.10.4
Additional Context
No response
First Check
Commit to Help
Example Code
Description
When testing my app I noticed, that sending request to router without header parameter set, I get
RequestValidationErrorinstead of422status code. This doesn't happen, when usingFastAPIinstead ofAPIRouter.One could say, that an easy fix to this is to wrap the second test code in
pytest.raises(), but this will only execute theclient.getwhile skipping all asserts the test has.Operating System
Windows
Operating System Details
Windows 10, 21H2
FastAPI Version
0.85.0
Python Version
Python 3.10.4
Additional Context
No response