Coverage for tests / test_param_include_in_schema.py: 100%

45 statements  

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

1import pytest 1abcd

2from fastapi import Cookie, FastAPI, Header, Path, Query 1abcd

3from fastapi.testclient import TestClient 1abcd

4from inline_snapshot import snapshot 1abcd

5 

6app = FastAPI() 1abcd

7 

8 

9@app.get("/hidden_cookie") 1abcd

10async def hidden_cookie( 1abcd

11 hidden_cookie: str | None = Cookie(default=None, include_in_schema=False), 

12): 

13 return {"hidden_cookie": hidden_cookie} 1efghijkl

14 

15 

16@app.get("/hidden_header") 1abcd

17async def hidden_header( 1abcd

18 hidden_header: str | None = Header(default=None, include_in_schema=False), 

19): 

20 return {"hidden_header": hidden_header} 1mnopqrs

21 

22 

23@app.get("/hidden_path/{hidden_path}") 1abcd

24async def hidden_path(hidden_path: str = Path(include_in_schema=False)): 1abcd

25 return {"hidden_path": hidden_path} 1tuvw

26 

27 

28@app.get("/hidden_query") 1abcd

29async def hidden_query( 1abcd

30 hidden_query: str | None = Query(default=None, include_in_schema=False), 

31): 

32 return {"hidden_query": hidden_query} 1xyzABCD

33 

34 

35@pytest.mark.parametrize( 1abcd

36 "path,cookies,expected_status,expected_response", 

37 [ 

38 ( 

39 "/hidden_cookie", 

40 {}, 

41 200, 

42 {"hidden_cookie": None}, 

43 ), 

44 ( 

45 "/hidden_cookie", 

46 {"hidden_cookie": "somevalue"}, 

47 200, 

48 {"hidden_cookie": "somevalue"}, 

49 ), 

50 ], 

51) 

52def test_hidden_cookie(path, cookies, expected_status, expected_response): 1abcd

53 client = TestClient(app, cookies=cookies) 1efghijkl

54 response = client.get(path) 1efghijkl

55 assert response.status_code == expected_status 1efghijkl

56 assert response.json() == expected_response 1efghijkl

57 

58 

59@pytest.mark.parametrize( 1abcd

60 "path,headers,expected_status,expected_response", 

61 [ 

62 ( 

63 "/hidden_header", 

64 {}, 

65 200, 

66 {"hidden_header": None}, 

67 ), 

68 ( 

69 "/hidden_header", 

70 {"Hidden-Header": "somevalue"}, 

71 200, 

72 {"hidden_header": "somevalue"}, 

73 ), 

74 ], 

75) 

76def test_hidden_header(path, headers, expected_status, expected_response): 1abcd

77 client = TestClient(app) 1mnopqrs

78 response = client.get(path, headers=headers) 1mnopqrs

79 assert response.status_code == expected_status 1mnopqrs

80 assert response.json() == expected_response 1mnopqrs

81 

82 

83def test_hidden_path(): 1abcd

84 client = TestClient(app) 1tuvw

85 response = client.get("/hidden_path/hidden_path") 1tuvw

86 assert response.status_code == 200 1tuvw

87 assert response.json() == {"hidden_path": "hidden_path"} 1tuvw

88 

89 

90@pytest.mark.parametrize( 1abcd

91 "path,expected_status,expected_response", 

92 [ 

93 ( 

94 "/hidden_query", 

95 200, 

96 {"hidden_query": None}, 

97 ), 

98 ( 

99 "/hidden_query?hidden_query=somevalue", 

100 200, 

101 {"hidden_query": "somevalue"}, 

102 ), 

103 ], 

104) 

105def test_hidden_query(path, expected_status, expected_response): 1abcd

106 client = TestClient(app) 1xyzABCD

107 response = client.get(path) 1xyzABCD

108 assert response.status_code == expected_status 1xyzABCD

109 assert response.json() == expected_response 1xyzABCD

110 

111 

112def test_openapi_schema(): 1abcd

113 client = TestClient(app) 1EFGH

114 response = client.get("/openapi.json") 1EFGH

115 assert response.status_code == 200 1EFGH

116 assert response.json() == snapshot( 1EFGH

117 { 

118 "openapi": "3.1.0", 

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

120 "paths": { 

121 "/hidden_cookie": { 

122 "get": { 

123 "summary": "Hidden Cookie", 

124 "operationId": "hidden_cookie_hidden_cookie_get", 

125 "responses": { 

126 "200": { 

127 "description": "Successful Response", 

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

129 }, 

130 "422": { 

131 "description": "Validation Error", 

132 "content": { 

133 "application/json": { 

134 "schema": { 

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

136 } 

137 } 

138 }, 

139 }, 

140 }, 

141 } 

142 }, 

143 "/hidden_header": { 

144 "get": { 

145 "summary": "Hidden Header", 

146 "operationId": "hidden_header_hidden_header_get", 

147 "responses": { 

148 "200": { 

149 "description": "Successful Response", 

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

151 }, 

152 "422": { 

153 "description": "Validation Error", 

154 "content": { 

155 "application/json": { 

156 "schema": { 

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

158 } 

159 } 

160 }, 

161 }, 

162 }, 

163 } 

164 }, 

165 "/hidden_path/{hidden_path}": { 

166 "get": { 

167 "summary": "Hidden Path", 

168 "operationId": "hidden_path_hidden_path__hidden_path__get", 

169 "responses": { 

170 "200": { 

171 "description": "Successful Response", 

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

173 }, 

174 "422": { 

175 "description": "Validation Error", 

176 "content": { 

177 "application/json": { 

178 "schema": { 

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

180 } 

181 } 

182 }, 

183 }, 

184 }, 

185 } 

186 }, 

187 "/hidden_query": { 

188 "get": { 

189 "summary": "Hidden Query", 

190 "operationId": "hidden_query_hidden_query_get", 

191 "responses": { 

192 "200": { 

193 "description": "Successful Response", 

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

195 }, 

196 "422": { 

197 "description": "Validation Error", 

198 "content": { 

199 "application/json": { 

200 "schema": { 

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

202 } 

203 } 

204 }, 

205 }, 

206 }, 

207 } 

208 }, 

209 }, 

210 "components": { 

211 "schemas": { 

212 "HTTPValidationError": { 

213 "title": "HTTPValidationError", 

214 "type": "object", 

215 "properties": { 

216 "detail": { 

217 "title": "Detail", 

218 "type": "array", 

219 "items": { 

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

221 }, 

222 } 

223 }, 

224 }, 

225 "ValidationError": { 

226 "title": "ValidationError", 

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

228 "type": "object", 

229 "properties": { 

230 "loc": { 

231 "title": "Location", 

232 "type": "array", 

233 "items": { 

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

235 }, 

236 }, 

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

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

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

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

241 }, 

242 }, 

243 } 

244 }, 

245 } 

246 )