|
| 1 | +# TEST008 - Error Handling Middleware Testing |
| 2 | + |
| 3 | +This document records the testing evidence and result for ticket **TEST008**. |
| 4 | + |
| 5 | +## Ticket intent |
| 6 | + |
| 7 | +- Trigger API errors. |
| 8 | +- Check response format. |
| 9 | +- Confirm consistent error responses. |
| 10 | +- Confirm proper status codes. |
| 11 | + |
| 12 | +## Scope |
| 13 | + |
| 14 | +Validated backend error handling behavior implemented in: |
| 15 | + |
| 16 | +- `database/logging_system/request_middleware.py` |
| 17 | +- `database/logging_system/exception_handler.py` |
| 18 | + |
| 19 | +Automated tests added in: |
| 20 | + |
| 21 | +- `test/test_t1008_error_handling_middleware.py` |
| 22 | + |
| 23 | +## Test cases implemented |
| 24 | + |
| 25 | +1. **Success path includes request traceability** |
| 26 | + - Call `GET /ok`. |
| 27 | + - Expect HTTP `200`. |
| 28 | + - Expect body: `{"ok": true}`. |
| 29 | + - Expect `X-Request-ID` response header is present. |
| 30 | + |
| 31 | +2. **Error path returns consistent format and status** |
| 32 | + - Call `GET /explode` (forced runtime exception). |
| 33 | + - Expect HTTP `500`. |
| 34 | + - Expect body includes: |
| 35 | + - `message` = `"Internal server error"` |
| 36 | + - `request_id` (non-empty) |
| 37 | + - Expect `X-Request-ID` header equals body `request_id`. |
| 38 | + |
| 39 | +## Fix applied during testing |
| 40 | + |
| 41 | +While executing TEST008, one assertion failed because error responses did not include the `X-Request-ID` header. |
| 42 | +To align success and error behavior, `global_exception_handler` was updated to set: |
| 43 | + |
| 44 | +- `headers={"X-Request-ID": request_id}` |
| 45 | + |
| 46 | +in the returned `JSONResponse`. |
| 47 | + |
| 48 | +## Execution evidence |
| 49 | + |
| 50 | +Command run from repository root: |
| 51 | + |
| 52 | +```bash |
| 53 | +python -m pytest test/test_t1008_error_handling_middleware.py -q |
| 54 | +``` |
| 55 | + |
| 56 | +Observed result: |
| 57 | + |
| 58 | +```text |
| 59 | +.. [100%] |
| 60 | +2 passed in 0.30s |
| 61 | +``` |
| 62 | + |
| 63 | +## Final result |
| 64 | + |
| 65 | +TEST008 acceptance criteria are satisfied by automated tests: |
| 66 | + |
| 67 | +- API errors are triggered and validated. |
| 68 | +- Error response format is consistent. |
| 69 | +- Proper status codes are returned (`200` success, `500` unhandled error). |
| 70 | +- `request_id` tracing is consistent in both payload and response header. |
0 commit comments