A panicking handler must not crash the process; it should produce a 500 response with an error on the body read, mirroring net/http.Server behavior.
(t *testing.T)
| 282 | // A panicking handler must not crash the process; it should produce a 500 |
| 283 | // response with an error on the body read, mirroring net/http.Server behavior. |
| 284 | func TestInMemoryRoundTripper_HandlerPanic(t *testing.T) { |
| 285 | t.Parallel() |
| 286 | |
| 287 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 288 | panic("unexpected nil pointer") |
| 289 | }) |
| 290 | |
| 291 | rt, err := aibridged.NewTransportFactory(handler).TransportFor("openai", aibridge.SourceAgents) |
| 292 | require.NoError(t, err) |
| 293 | |
| 294 | ctx := aibridge.WithDelegatedAPIKeyID(testutil.Context(t, testutil.WaitShort), "test-key-id") |
| 295 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://aibridge/panic", nil) |
| 296 | require.NoError(t, err) |
| 297 | |
| 298 | resp, err := rt.RoundTrip(req) |
| 299 | require.NoError(t, err) |
| 300 | defer resp.Body.Close() |
| 301 | |
| 302 | require.Equal(t, http.StatusInternalServerError, resp.StatusCode) |
| 303 | _, err = io.ReadAll(resp.Body) |
| 304 | require.Error(t, err) |
| 305 | require.Contains(t, err.Error(), "handler panicked") |
| 306 | } |
| 307 | |
| 308 | // The in-memory transport must reject any RoundTrip whose context does not |
| 309 | // carry a delegated API key ID. The handler relies on this invariant to know |
nothing calls this directly
no test coverage detected