Coverage for tests/test_tutorial/test_header_params/test_tutorial003.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-09-22 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial003", 

14 pytest.param("tutorial003_py310", marks=needs_py310), 

15 "tutorial003_an", 

16 pytest.param("tutorial003_an_py39", marks=needs_py39), 

17 pytest.param("tutorial003_an_py310", marks=needs_py310), 

18 ], 

19) 

20def get_client(request: pytest.FixtureRequest): 1abcdef

21 mod = importlib.import_module(f"docs_src.header_params.{request.param}") 1abcdef

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27@pytest.mark.parametrize( 1abcdef

28 "path,headers,expected_status,expected_response", 

29 [ 

30 ("/items", None, 200, {"X-Token values": None}), 

31 ("/items", {"x-token": "foo"}, 200, {"X-Token values": ["foo"]}), 

32 ( 

33 "/items", 

34 [("x-token", "foo"), ("x-token", "bar")], 

35 200, 

36 {"X-Token values": ["foo", "bar"]}, 

37 ), 

38 ], 

39) 

40def test(path, headers, expected_status, expected_response, client: TestClient): 1abcdef

41 response = client.get(path, headers=headers) 1ghijkl

42 assert response.status_code == expected_status 1ghijkl

43 assert response.json() == expected_response 1ghijkl

44 

45 

46def test_openapi_schema(client: TestClient): 1abcdef

47 response = client.get("/openapi.json") 1mnopqr

48 assert response.status_code == 200 1mnopqr

49 assert response.json() == { 1mnopqr

50 "openapi": "3.1.0", 

51 "info": {"title": "FastAPI", "version": "0.1.0"}, 

52 "paths": { 

53 "/items/": { 

54 "get": { 

55 "summary": "Read Items", 

56 "operationId": "read_items_items__get", 

57 "parameters": [ 

58 { 

59 "required": False, 

60 "schema": IsDict( 

61 { 

62 "title": "X-Token", 

63 "anyOf": [ 

64 {"type": "array", "items": {"type": "string"}}, 

65 {"type": "null"}, 

66 ], 

67 } 

68 ) 

69 | IsDict( 

70 # TODO: remove when deprecating Pydantic v1 

71 { 

72 "title": "X-Token", 

73 "type": "array", 

74 "items": {"type": "string"}, 

75 } 

76 ), 

77 "name": "x-token", 

78 "in": "header", 

79 } 

80 ], 

81 "responses": { 

82 "200": { 

83 "description": "Successful Response", 

84 "content": {"application/json": {"schema": {}}}, 

85 }, 

86 "422": { 

87 "description": "Validation Error", 

88 "content": { 

89 "application/json": { 

90 "schema": { 

91 "$ref": "#/components/schemas/HTTPValidationError" 

92 } 

93 } 

94 }, 

95 }, 

96 }, 

97 } 

98 } 

99 }, 

100 "components": { 

101 "schemas": { 

102 "HTTPValidationError": { 

103 "title": "HTTPValidationError", 

104 "type": "object", 

105 "properties": { 

106 "detail": { 

107 "title": "Detail", 

108 "type": "array", 

109 "items": {"$ref": "#/components/schemas/ValidationError"}, 

110 } 

111 }, 

112 }, 

113 "ValidationError": { 

114 "title": "ValidationError", 

115 "required": ["loc", "msg", "type"], 

116 "type": "object", 

117 "properties": { 

118 "loc": { 

119 "title": "Location", 

120 "type": "array", 

121 "items": { 

122 "anyOf": [{"type": "string"}, {"type": "integer"}] 

123 }, 

124 }, 

125 "msg": {"title": "Message", "type": "string"}, 

126 "type": {"title": "Error Type", "type": "string"}, 

127 }, 

128 }, 

129 } 

130 }, 

131 }