Coverage for tests / test_tutorial / test_custom_request_and_route / test_tutorial001.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import gzip 1abdc

2import importlib 1abdc

3import json 1abdc

4 

5import pytest 1abdc

6from fastapi import Request 1abdc

7from fastapi.testclient import TestClient 1abdc

8 

9from tests.utils import needs_py310 1abdc

10 

11 

12@pytest.fixture( 1abdc

13 name="client", 

14 params=[ 

15 pytest.param("tutorial001_py310", marks=needs_py310), 

16 pytest.param("tutorial001_an_py310", marks=needs_py310), 

17 ], 

18) 

19def get_client(request: pytest.FixtureRequest): 1abdc

20 mod = importlib.import_module(f"docs_src.custom_request_and_route.{request.param}") 1abc

21 

22 @mod.app.get("/check-class") 1abc

23 async def check_gzip_request(request: Request): 1abc

24 return {"request_class": type(request).__name__} 1hij

25 

26 client = TestClient(mod.app) 1abc

27 return client 1abc

28 

29 

30@pytest.mark.parametrize("compress", [True, False]) 1abdc

31def test_gzip_request(client: TestClient, compress): 1abdc

32 n = 1000 1efg

33 headers = {} 1efg

34 body = [1] * n 1efg

35 data = json.dumps(body).encode() 1efg

36 if compress: 1efg

37 data = gzip.compress(data) 1efg

38 headers["Content-Encoding"] = "gzip" 1efg

39 headers["Content-Type"] = "application/json" 1efg

40 response = client.post("/sum", content=data, headers=headers) 1efg

41 assert response.json() == {"sum": n} 1efg

42 

43 

44def test_request_class(client: TestClient): 1abdc

45 response = client.get("/check-class") 1hij

46 assert response.json() == {"request_class": "GzipRequest"} 1hij