|
22 | 22 | def fastapi_app_factory():
|
23 | 23 | app = FastAPI()
|
24 | 24 |
|
| 25 | + @app.get("/error") |
| 26 | + async def _error(): |
| 27 | + capture_message("Hi") |
| 28 | + 1 / 0 |
| 29 | + return {"message": "Hi"} |
| 30 | + |
25 | 31 | @app.get("/message")
|
26 | 32 | async def _message():
|
27 | 33 | capture_message("Hi")
|
@@ -218,3 +224,101 @@ async def _error(request: Request):
|
218 | 224 | event = events[0]
|
219 | 225 | assert event["request"]["data"] == {"password": "[Filtered]"}
|
220 | 226 | assert event["request"]["headers"]["authorization"] == "[Filtered]"
|
| 227 | + |
| 228 | + |
| 229 | +@pytest.mark.asyncio |
| 230 | +def test_response_status_code_ok_in_transaction_context(sentry_init, capture_envelopes): |
| 231 | + """ |
| 232 | + Tests that the response status code is added to the transaction "response" context. |
| 233 | + """ |
| 234 | + sentry_init( |
| 235 | + integrations=[StarletteIntegration(), FastApiIntegration()], |
| 236 | + traces_sample_rate=1.0, |
| 237 | + release="demo-release", |
| 238 | + ) |
| 239 | + |
| 240 | + envelopes = capture_envelopes() |
| 241 | + |
| 242 | + app = fastapi_app_factory() |
| 243 | + |
| 244 | + client = TestClient(app) |
| 245 | + client.get("/message") |
| 246 | + |
| 247 | + (_, transaction_envelope) = envelopes |
| 248 | + transaction = transaction_envelope.get_transaction_event() |
| 249 | + |
| 250 | + assert transaction["type"] == "transaction" |
| 251 | + assert len(transaction["contexts"]) > 0 |
| 252 | + assert ( |
| 253 | + "response" in transaction["contexts"].keys() |
| 254 | + ), "Response context not found in transaction" |
| 255 | + assert transaction["contexts"]["response"]["status_code"] == 200 |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.asyncio |
| 259 | +def test_response_status_code_error_in_transaction_context( |
| 260 | + sentry_init, |
| 261 | + capture_envelopes, |
| 262 | +): |
| 263 | + """ |
| 264 | + Tests that the response status code is added to the transaction "response" context. |
| 265 | + """ |
| 266 | + sentry_init( |
| 267 | + integrations=[StarletteIntegration(), FastApiIntegration()], |
| 268 | + traces_sample_rate=1.0, |
| 269 | + release="demo-release", |
| 270 | + ) |
| 271 | + |
| 272 | + envelopes = capture_envelopes() |
| 273 | + |
| 274 | + app = fastapi_app_factory() |
| 275 | + |
| 276 | + client = TestClient(app) |
| 277 | + with pytest.raises(ZeroDivisionError): |
| 278 | + client.get("/error") |
| 279 | + |
| 280 | + ( |
| 281 | + _, |
| 282 | + _, |
| 283 | + transaction_envelope, |
| 284 | + ) = envelopes |
| 285 | + transaction = transaction_envelope.get_transaction_event() |
| 286 | + |
| 287 | + assert transaction["type"] == "transaction" |
| 288 | + assert len(transaction["contexts"]) > 0 |
| 289 | + assert ( |
| 290 | + "response" in transaction["contexts"].keys() |
| 291 | + ), "Response context not found in transaction" |
| 292 | + assert transaction["contexts"]["response"]["status_code"] == 500 |
| 293 | + |
| 294 | + |
| 295 | +@pytest.mark.asyncio |
| 296 | +def test_response_status_code_not_found_in_transaction_context( |
| 297 | + sentry_init, |
| 298 | + capture_envelopes, |
| 299 | +): |
| 300 | + """ |
| 301 | + Tests that the response status code is added to the transaction "response" context. |
| 302 | + """ |
| 303 | + sentry_init( |
| 304 | + integrations=[StarletteIntegration(), FastApiIntegration()], |
| 305 | + traces_sample_rate=1.0, |
| 306 | + release="demo-release", |
| 307 | + ) |
| 308 | + |
| 309 | + envelopes = capture_envelopes() |
| 310 | + |
| 311 | + app = fastapi_app_factory() |
| 312 | + |
| 313 | + client = TestClient(app) |
| 314 | + client.get("/non-existing-route-123") |
| 315 | + |
| 316 | + (transaction_envelope,) = envelopes |
| 317 | + transaction = transaction_envelope.get_transaction_event() |
| 318 | + |
| 319 | + assert transaction["type"] == "transaction" |
| 320 | + assert len(transaction["contexts"]) > 0 |
| 321 | + assert ( |
| 322 | + "response" in transaction["contexts"].keys() |
| 323 | + ), "Response context not found in transaction" |
| 324 | + assert transaction["contexts"]["response"]["status_code"] == 404 |
0 commit comments