()
| 374 | } |
| 375 | |
| 376 | func (ts *MiddlewareTestSuite) TestTimeoutMiddleware() { |
| 377 | ts.Config.API.MaxRequestDuration = 5 * time.Microsecond |
| 378 | req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) |
| 379 | w := httptest.NewRecorder() |
| 380 | |
| 381 | timeoutHandler := timeoutMiddleware(ts.Config.API.MaxRequestDuration) |
| 382 | |
| 383 | slowHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 384 | // Simulate a slow handler which should trigger the timeout. |
| 385 | // Use context-aware wait so the goroutine exits when the timeout fires, |
| 386 | // avoiding a data race with test cleanup. |
| 387 | select { |
| 388 | case <-time.After(1 * time.Second): |
| 389 | ts.API.handler.ServeHTTP(w, r) |
| 390 | case <-r.Context().Done(): |
| 391 | return |
| 392 | } |
| 393 | }) |
| 394 | timeoutHandler(slowHandler).ServeHTTP(w, req) |
| 395 | assert.Equal(ts.T(), http.StatusGatewayTimeout, w.Code) |
| 396 | |
| 397 | var data map[string]interface{} |
| 398 | require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) |
| 399 | require.Equal(ts.T(), apierrors.ErrorCodeRequestTimeout, data["error_code"]) |
| 400 | require.Equal(ts.T(), float64(504), data["code"]) |
| 401 | require.NotNil(ts.T(), data["msg"]) |
| 402 | } |
| 403 | |
| 404 | func TestTimeoutResponseWriter(t *testing.T) { |
| 405 | // timeoutResponseWriter should exhitbit a similar behavior as http.ResponseWriter |
nothing calls this directly
no test coverage detected