Coverage for tests / test_tutorial / test_response_directly / test_tutorial002.py: 100%

19 statements  

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

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 pytest.param("tutorial002_py310"), 

12 ], 

13) 

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

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

16 

17 client = TestClient(mod.app) 1abc

18 return client 1abc

19 

20 

21def test_path_operation(client: TestClient): 1abdc

22 expected_content = """<?xml version="1.0"?> 1efg

23 <shampoo> 

24 <Header> 

25 Apply shampoo here. 

26 </Header> 

27 <Body> 

28 You'll have to use soap here. 

29 </Body> 

30 </shampoo> 

31 """ 

32 

33 response = client.get("/legacy/") 1efg

34 assert response.status_code == 200, response.text 1efg

35 assert response.headers["content-type"] == "application/xml" 1efg

36 assert response.text == expected_content 1efg

37 

38 

39def test_openapi_schema(client: TestClient): 1abdc

40 response = client.get("/openapi.json") 1hij

41 assert response.status_code == 200, response.text 1hij

42 assert response.json() == snapshot( 1hij

43 { 

44 "info": { 

45 "title": "FastAPI", 

46 "version": "0.1.0", 

47 }, 

48 "openapi": "3.1.0", 

49 "paths": { 

50 "/legacy/": { 

51 "get": { 

52 "operationId": "get_legacy_data_legacy__get", 

53 "responses": { 

54 "200": { 

55 "content": { 

56 "application/json": { 

57 "schema": {}, 

58 }, 

59 }, 

60 "description": "Successful Response", 

61 }, 

62 }, 

63 "summary": "Get Legacy Data", 

64 }, 

65 }, 

66 }, 

67 } 

68 )