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

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5 

6 

7@pytest.fixture( 1abcd

8 name="client", 

9 params=[ 

10 "tutorial001_py310", 

11 ], 

12) 

13def get_client(request: pytest.FixtureRequest): 1abcd

14 mod = importlib.import_module(f"docs_src.strict_content_type.{request.param}") 1efghijklmno

15 client = TestClient(mod.app) 1efghijklmno

16 return client 1efghijklmno

17 

18 

19def test_lax_post_without_content_type_is_parsed_as_json(client: TestClient): 1abcd

20 response = client.post( 1pqrs

21 "/items/", 

22 content='{"name": "Foo", "price": 50.5}', 

23 ) 

24 assert response.status_code == 200, response.text 1pqrs

25 assert response.json() == {"name": "Foo", "price": 50.5} 1pqrs

26 

27 

28def test_lax_post_with_json_content_type(client: TestClient): 1abcd

29 response = client.post( 1tuvw

30 "/items/", 

31 json={"name": "Foo", "price": 50.5}, 

32 ) 

33 assert response.status_code == 200, response.text 1tuvw

34 assert response.json() == {"name": "Foo", "price": 50.5} 1tuvw

35 

36 

37def test_lax_post_with_text_plain_is_still_rejected(client: TestClient): 1abcd

38 response = client.post( 1xyzA

39 "/items/", 

40 content='{"name": "Foo", "price": 50.5}', 

41 headers={"Content-Type": "text/plain"}, 

42 ) 

43 assert response.status_code == 422, response.text 1xyzA