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 2024-08-08 03:53 +0000

1import gzip 1abcde

2import json 1abcde

3 

4import pytest 1abcde

5from fastapi import Request 1abcde

6from fastapi.testclient import TestClient 1abcde

7 

8from docs_src.custom_request_and_route.tutorial001 import app 1abcde

9 

10 

11@app.get("/check-class") 1abcde

12async def check_gzip_request(request: Request): 1abcde

13 return {"request_class": type(request).__name__} 1abcde

14 

15 

16client = TestClient(app) 1abcde

17 

18 

19@pytest.mark.parametrize("compress", [True, False]) 1abcde

20def test_gzip_request(compress): 1abcde

21 n = 1000 1abcde

22 headers = {} 1abcde

23 body = [1] * n 1abcde

24 data = json.dumps(body).encode() 1abcde

25 if compress: 1abcde

26 data = gzip.compress(data) 1abcde

27 headers["Content-Encoding"] = "gzip" 1abcde

28 headers["Content-Type"] = "application/json" 1abcde

29 response = client.post("/sum", content=data, headers=headers) 1abcde

30 assert response.json() == {"sum": n} 1abcde

31 

32 

33def test_request_class(): 1abcde

34 response = client.get("/check-class") 1abcde

35 assert response.json() == {"request_class": "GzipRequest"} 1abcde