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

30 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 

7from tests.utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial001_py310", marks=needs_py310), 

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

15 ], 

16) 

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

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

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_header_param_model(client: TestClient): 1abdc

25 response = client.get( 1efg

26 "/items/", 

27 headers=[ 

28 ("save-data", "true"), 

29 ("if-modified-since", "yesterday"), 

30 ("traceparent", "123"), 

31 ("x-tag", "one"), 

32 ("x-tag", "two"), 

33 ], 

34 ) 

35 assert response.status_code == 200 1efg

36 assert response.json() == { 1efg

37 "host": "testserver", 

38 "save_data": True, 

39 "if_modified_since": "yesterday", 

40 "traceparent": "123", 

41 "x_tag": ["one", "two"], 

42 } 

43 

44 

45def test_header_param_model_defaults(client: TestClient): 1abdc

46 response = client.get("/items/", headers=[("save-data", "true")]) 1hij

47 assert response.status_code == 200 1hij

48 assert response.json() == { 1hij

49 "host": "testserver", 

50 "save_data": True, 

51 "if_modified_since": None, 

52 "traceparent": None, 

53 "x_tag": [], 

54 } 

55 

56 

57def test_header_param_model_invalid(client: TestClient): 1abdc

58 response = client.get("/items/") 1klm

59 assert response.status_code == 422 1klm

60 assert response.json() == snapshot( 1klm

61 { 

62 "detail": [ 

63 { 

64 "type": "missing", 

65 "loc": ["header", "save_data"], 

66 "msg": "Field required", 

67 "input": { 

68 "x_tag": [], 

69 "host": "testserver", 

70 "accept": "*/*", 

71 "accept-encoding": "gzip, deflate", 

72 "connection": "keep-alive", 

73 "user-agent": "testclient", 

74 }, 

75 } 

76 ] 

77 } 

78 ) 

79 

80 

81def test_header_param_model_extra(client: TestClient): 1abdc

82 response = client.get( 1nop

83 "/items/", headers=[("save-data", "true"), ("tool", "plumbus")] 

84 ) 

85 assert response.status_code == 200, response.text 1nop

86 assert response.json() == snapshot( 1nop

87 { 

88 "host": "testserver", 

89 "save_data": True, 

90 "if_modified_since": None, 

91 "traceparent": None, 

92 "x_tag": [], 

93 } 

94 ) 

95 

96 

97def test_openapi_schema(client: TestClient): 1abdc

98 response = client.get("/openapi.json") 1qrs

99 assert response.status_code == 200, response.text 1qrs

100 assert response.json() == snapshot( 1qrs

101 { 

102 "openapi": "3.1.0", 

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

104 "paths": { 

105 "/items/": { 

106 "get": { 

107 "summary": "Read Items", 

108 "operationId": "read_items_items__get", 

109 "parameters": [ 

110 { 

111 "name": "host", 

112 "in": "header", 

113 "required": True, 

114 "schema": {"type": "string", "title": "Host"}, 

115 }, 

116 { 

117 "name": "save-data", 

118 "in": "header", 

119 "required": True, 

120 "schema": {"type": "boolean", "title": "Save Data"}, 

121 }, 

122 { 

123 "name": "if-modified-since", 

124 "in": "header", 

125 "required": False, 

126 "schema": { 

127 "anyOf": [{"type": "string"}, {"type": "null"}], 

128 "title": "If Modified Since", 

129 }, 

130 }, 

131 { 

132 "name": "traceparent", 

133 "in": "header", 

134 "required": False, 

135 "schema": { 

136 "anyOf": [{"type": "string"}, {"type": "null"}], 

137 "title": "Traceparent", 

138 }, 

139 }, 

140 { 

141 "name": "x-tag", 

142 "in": "header", 

143 "required": False, 

144 "schema": { 

145 "type": "array", 

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

147 "default": [], 

148 "title": "X Tag", 

149 }, 

150 }, 

151 ], 

152 "responses": { 

153 "200": { 

154 "description": "Successful Response", 

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

156 }, 

157 "422": { 

158 "description": "Validation Error", 

159 "content": { 

160 "application/json": { 

161 "schema": { 

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

163 } 

164 } 

165 }, 

166 }, 

167 }, 

168 } 

169 } 

170 }, 

171 "components": { 

172 "schemas": { 

173 "HTTPValidationError": { 

174 "properties": { 

175 "detail": { 

176 "items": { 

177 "$ref": "#/components/schemas/ValidationError" 

178 }, 

179 "type": "array", 

180 "title": "Detail", 

181 } 

182 }, 

183 "type": "object", 

184 "title": "HTTPValidationError", 

185 }, 

186 "ValidationError": { 

187 "properties": { 

188 "ctx": {"title": "Context", "type": "object"}, 

189 "input": {"title": "Input"}, 

190 "loc": { 

191 "items": { 

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

193 }, 

194 "type": "array", 

195 "title": "Location", 

196 }, 

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

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

199 }, 

200 "type": "object", 

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

202 "title": "ValidationError", 

203 }, 

204 } 

205 }, 

206 } 

207 )