6
6
import os
7
7
import re
8
8
import threading
9
+ import warnings
9
10
from unittest import mock
10
11
11
12
import pytest
@@ -1133,7 +1134,22 @@ def test_span_origin(sentry_init, capture_events):
1133
1134
assert span ["origin" ] == "auto.http.starlette"
1134
1135
1135
1136
1136
- parametrize_test_configurable_status_codes = pytest .mark .parametrize (
1137
+ class NonIterableContainer :
1138
+ """Wraps any container and makes it non-iterable.
1139
+
1140
+ Used to test backwards compatibility with our old way of defining failed_request_status_codes, which allowed
1141
+ passing in a list of (possibly non-iterable) containers. The Python standard library does not provide any built-in
1142
+ non-iterable containers, so we have to define our own.
1143
+ """
1144
+
1145
+ def __init__ (self , inner ):
1146
+ self .inner = inner
1147
+
1148
+ def __contains__ (self , item ):
1149
+ return item in self .inner
1150
+
1151
+
1152
+ parametrize_test_configurable_status_codes_deprecated = pytest .mark .parametrize (
1137
1153
"failed_request_status_codes,status_code,expected_error" ,
1138
1154
[
1139
1155
(None , 500 , True ),
@@ -1150,28 +1166,29 @@ def test_span_origin(sentry_init, capture_events):
1150
1166
([range (400 , 403 ), 500 , 501 ], 501 , True ),
1151
1167
([range (400 , 403 ), 500 , 501 ], 503 , False ),
1152
1168
([], 500 , False ),
1169
+ ([NonIterableContainer (range (500 , 600 ))], 500 , True ),
1170
+ ([NonIterableContainer (range (500 , 600 ))], 404 , False ),
1153
1171
],
1154
1172
)
1155
- """Test cases for configurable status codes.
1173
+ """Test cases for configurable status codes (deprecated API) .
1156
1174
Also used by the FastAPI tests.
1157
1175
"""
1158
1176
1159
1177
1160
- @parametrize_test_configurable_status_codes
1161
- def test_configurable_status_codes (
1178
+ @parametrize_test_configurable_status_codes_deprecated
1179
+ def test_configurable_status_codes_deprecated (
1162
1180
sentry_init ,
1163
1181
capture_events ,
1164
1182
failed_request_status_codes ,
1165
1183
status_code ,
1166
1184
expected_error ,
1167
1185
):
1168
- sentry_init (
1169
- integrations = [
1170
- StarletteIntegration (
1171
- failed_request_status_codes = failed_request_status_codes
1172
- )
1173
- ]
1174
- )
1186
+ with pytest .warns (DeprecationWarning ):
1187
+ starlette_integration = StarletteIntegration (
1188
+ failed_request_status_codes = failed_request_status_codes
1189
+ )
1190
+
1191
+ sentry_init (integrations = [starlette_integration ])
1175
1192
1176
1193
events = capture_events ()
1177
1194
@@ -1191,3 +1208,59 @@ async def _error(request):
1191
1208
assert len (events ) == 1
1192
1209
else :
1193
1210
assert not events
1211
+
1212
+
1213
+ parametrize_test_configurable_status_codes = pytest .mark .parametrize (
1214
+ ("failed_request_status_codes" , "status_code" , "expected_error" ),
1215
+ (
1216
+ (None , 500 , True ),
1217
+ (None , 400 , False ),
1218
+ ({500 , 501 }, 500 , True ),
1219
+ ({500 , 501 }, 401 , False ),
1220
+ ({* range (400 , 500 )}, 401 , True ),
1221
+ ({* range (400 , 500 )}, 500 , False ),
1222
+ ({* range (400 , 600 )}, 300 , False ),
1223
+ ({* range (400 , 600 )}, 403 , True ),
1224
+ ({* range (400 , 600 )}, 503 , True ),
1225
+ ({* range (400 , 403 ), 500 , 501 }, 401 , True ),
1226
+ ({* range (400 , 403 ), 500 , 501 }, 405 , False ),
1227
+ ({* range (400 , 403 ), 500 , 501 }, 501 , True ),
1228
+ ({* range (400 , 403 ), 500 , 501 }, 503 , False ),
1229
+ (set (), 500 , False ),
1230
+ ),
1231
+ )
1232
+
1233
+
1234
+ @parametrize_test_configurable_status_codes
1235
+ def test_configurable_status_codes (
1236
+ sentry_init ,
1237
+ capture_events ,
1238
+ failed_request_status_codes ,
1239
+ status_code ,
1240
+ expected_error ,
1241
+ ):
1242
+ integration_kwargs = {}
1243
+ if failed_request_status_codes is not None :
1244
+ integration_kwargs ["failed_request_status_codes" ] = failed_request_status_codes
1245
+
1246
+ with warnings .catch_warnings ():
1247
+ warnings .simplefilter ("error" , DeprecationWarning )
1248
+ starlette_integration = StarletteIntegration (** integration_kwargs )
1249
+
1250
+ sentry_init (integrations = [starlette_integration ])
1251
+
1252
+ events = capture_events ()
1253
+
1254
+ async def _error (_ ):
1255
+ raise HTTPException (status_code )
1256
+
1257
+ app = starlette .applications .Starlette (
1258
+ routes = [
1259
+ starlette .routing .Route ("/error" , _error , methods = ["GET" ]),
1260
+ ],
1261
+ )
1262
+
1263
+ client = TestClient (app )
1264
+ client .get ("/error" )
1265
+
1266
+ assert len (events ) == int (expected_error )
0 commit comments