Debian Codegraph @

tests/test_tutorial/test_custom_request_and_route/test_tutorial001.py

raw
1 import gzip
2 import importlib
3 import json
4
5 import pytest
6 from fastapi import Request
7 from fastapi.testclient import TestClient
8
9 from tests.utils import needs_py310
10
11
12 @pytest.fixture(
13 name="client",
14 params=[
15 pytest.param("tutorial001_py310", marks=needs_py310),
16 pytest.param("tutorial001_an_py310", marks=needs_py310),
17 ],
18 )
19 def get_client(request: pytest.FixtureRequest):
20 mod = importlib.import_module(f"docs_src.custom_request_and_route.{request.param}")
21
22 @mod.app.get("/check-class")
23 async def check_gzip_request(request: Request):
24 return {"request_class": type(request).__name__}
25
26 client = TestClient(mod.app)
27 return client
28
29
30 @pytest.mark.parametrize("compress", [True, False])
31 def test_gzip_request(client: TestClient, compress):
32 n = 1000
33 headers = {}
34 body = [1] * n
35 data = json.dumps(body).encode()
36 if compress:
37 data = gzip.compress(data)
38 headers["Content-Encoding"] = "gzip"
39 headers["Content-Type"] = "application/json"
40 response = client.post("/sum", content=data, headers=headers)
41 assert response.json() == {"sum": n}
42
43
44 def test_request_class(client: TestClient):
45 response = client.get("/check-class")
46 assert response.json() == {"request_class": "GzipRequest"}