| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from dirty_equals import IsOneOf |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
|
| 7 |
from tests.utils import needs_py310 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture( |
| 11 |
name="client", |
| 12 |
params=[ |
| 13 |
pytest.param("tutorial002_py310", marks=needs_py310), |
| 14 |
pytest.param("tutorial002_an_py310", marks=needs_py310), |
| 15 |
], |
| 16 |
) |
| 17 |
def get_client(request: pytest.FixtureRequest): |
| 18 |
mod = importlib.import_module(f"docs_src.custom_request_and_route.{request.param}") |
| 19 |
|
| 20 |
client = TestClient(mod.app) |
| 21 |
return client |
| 22 |
|
| 23 |
|
| 24 |
def test_endpoint_works(client: TestClient): |
| 25 |
response = client.post("/", json=[1, 2, 3]) |
| 26 |
assert response.json() == 6 |
| 27 |
|
| 28 |
|
| 29 |
def test_exception_handler_body_access(client: TestClient): |
| 30 |
response = client.post("/", json={"numbers": [1, 2, 3]}) |
| 31 |
assert response.json() == { |
| 32 |
"detail": { |
| 33 |
"errors": [ |
| 34 |
{ |
| 35 |
"type": "list_type", |
| 36 |
"loc": ["body"], |
| 37 |
"msg": "Input should be a valid list", |
| 38 |
"input": {"numbers": [1, 2, 3]}, |
| 39 |
} |
| 40 |
], |
| 41 |
# httpx 0.28.0 switches to compact JSON https://github.com/encode/httpx/issues/3363 |
| 42 |
"body": IsOneOf('{"numbers": [1, 2, 3]}', '{"numbers":[1,2,3]}'), |
| 43 |
} |
| 44 |
} |