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

34 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("tutorial003_py310", marks=needs_py310), 

14 pytest.param("tutorial003_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_no_underscore(client: TestClient): 1abdc

46 response = client.get( 1hij

47 "/items/", 

48 headers=[ 

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

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

51 ("traceparent", "123"), 

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

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

54 ], 

55 ) 

56 assert response.status_code == 422 1hij

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

58 { 

59 "detail": [ 

60 { 

61 "type": "missing", 

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

63 "msg": "Field required", 

64 "input": { 

65 "host": "testserver", 

66 "traceparent": "123", 

67 "x_tag": [], 

68 "accept": "*/*", 

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

70 "connection": "keep-alive", 

71 "user-agent": "testclient", 

72 "save-data": "true", 

73 "if-modified-since": "yesterday", 

74 "x-tag": ["one", "two"], 

75 }, 

76 } 

77 ] 

78 } 

79 ) 

80 

81 

82def test_header_param_model_defaults(client: TestClient): 1abdc

83 response = client.get("/items/", headers=[("save_data", "true")]) 1klm

84 assert response.status_code == 200 1klm

85 assert response.json() == { 1klm

86 "host": "testserver", 

87 "save_data": True, 

88 "if_modified_since": None, 

89 "traceparent": None, 

90 "x_tag": [], 

91 } 

92 

93 

94def test_header_param_model_invalid(client: TestClient): 1abdc

95 response = client.get("/items/") 1nop

96 assert response.status_code == 422 1nop

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

98 { 

99 "detail": [ 

100 { 

101 "type": "missing", 

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

103 "msg": "Field required", 

104 "input": { 

105 "x_tag": [], 

106 "host": "testserver", 

107 "accept": "*/*", 

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

109 "connection": "keep-alive", 

110 "user-agent": "testclient", 

111 }, 

112 } 

113 ] 

114 } 

115 ) 

116 

117 

118def test_header_param_model_extra(client: TestClient): 1abdc

119 response = client.get( 1qrs

120 "/items/", headers=[("save_data", "true"), ("tool", "plumbus")] 

121 ) 

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

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

124 { 

125 "host": "testserver", 

126 "save_data": True, 

127 "if_modified_since": None, 

128 "traceparent": None, 

129 "x_tag": [], 

130 } 

131 ) 

132 

133 

134def test_openapi_schema(client: TestClient): 1abdc

135 response = client.get("/openapi.json") 1tuv

136 assert response.status_code == 200, response.text 1tuv

137 assert response.json() == snapshot( 1tuv

138 { 

139 "openapi": "3.1.0", 

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

141 "paths": { 

142 "/items/": { 

143 "get": { 

144 "summary": "Read Items", 

145 "operationId": "read_items_items__get", 

146 "parameters": [ 

147 { 

148 "name": "host", 

149 "in": "header", 

150 "required": True, 

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

152 }, 

153 { 

154 "name": "save_data", 

155 "in": "header", 

156 "required": True, 

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

158 }, 

159 { 

160 "name": "if_modified_since", 

161 "in": "header", 

162 "required": False, 

163 "schema": { 

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

165 "title": "If Modified Since", 

166 }, 

167 }, 

168 { 

169 "name": "traceparent", 

170 "in": "header", 

171 "required": False, 

172 "schema": { 

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

174 "title": "Traceparent", 

175 }, 

176 }, 

177 { 

178 "name": "x_tag", 

179 "in": "header", 

180 "required": False, 

181 "schema": { 

182 "type": "array", 

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

184 "default": [], 

185 "title": "X Tag", 

186 }, 

187 }, 

188 ], 

189 "responses": { 

190 "200": { 

191 "description": "Successful Response", 

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

193 }, 

194 "422": { 

195 "description": "Validation Error", 

196 "content": { 

197 "application/json": { 

198 "schema": { 

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

200 } 

201 } 

202 }, 

203 }, 

204 }, 

205 } 

206 } 

207 }, 

208 "components": { 

209 "schemas": { 

210 "HTTPValidationError": { 

211 "properties": { 

212 "detail": { 

213 "items": { 

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

215 }, 

216 "type": "array", 

217 "title": "Detail", 

218 } 

219 }, 

220 "type": "object", 

221 "title": "HTTPValidationError", 

222 }, 

223 "ValidationError": { 

224 "properties": { 

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

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

227 "loc": { 

228 "items": { 

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

230 }, 

231 "type": "array", 

232 "title": "Location", 

233 }, 

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

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

236 }, 

237 "type": "object", 

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

239 "title": "ValidationError", 

240 }, 

241 } 

242 }, 

243 } 

244 )