Coverage for tests/test_tutorial/test_custom_request_and_route/test_tutorial001.py: 100%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import gzip 1abcdef
2import json 1abcdef
4import pytest 1abcdef
5from fastapi import Request 1abcdef
6from fastapi.testclient import TestClient 1abcdef
8from docs_src.custom_request_and_route.tutorial001 import app 1abcdef
11@app.get("/check-class") 1abcdef
12async def check_gzip_request(request: Request): 1abcdef
13 return {"request_class": type(request).__name__} 1mnopqr
16client = TestClient(app) 1abcdef
19@pytest.mark.parametrize("compress", [True, False]) 1abcdef
20def test_gzip_request(compress): 1abcdef
21 n = 1000 1ghijkl
22 headers = {} 1ghijkl
23 body = [1] * n 1ghijkl
24 data = json.dumps(body).encode() 1ghijkl
25 if compress: 1ghijkl
26 data = gzip.compress(data) 1ghijkl
27 headers["Content-Encoding"] = "gzip" 1ghijkl
28 headers["Content-Type"] = "application/json" 1ghijkl
29 response = client.post("/sum", content=data, headers=headers) 1ghijkl
30 assert response.json() == {"sum": n} 1ghijkl
33def test_request_class(): 1abcdef
34 response = client.get("/check-class") 1mnopqr
35 assert response.json() == {"request_class": "GzipRequest"} 1mnopqr