diff --git a/options.go b/options.go index cf92163..9e8f40f 100644 --- a/options.go +++ b/options.go @@ -216,6 +216,9 @@ func WithSlog(logger *slog.Logger) Option { // runs without a deadline) — this is safe and will not panic. func WithTimeout(d time.Duration) Option { return func(e *Engine) { + if d <= 0 { + return + } e.middlewares = append(e.middlewares, timeout.New( timeout.WithTimeout(d), timeout.WithResponse(timeoutResponse), diff --git a/timeout_test.go b/timeout_test.go index c0e99a8..7630712 100644 --- a/timeout_test.go +++ b/timeout_test.go @@ -148,15 +148,8 @@ func TestWithTimeout_Good_CombinesWithOtherMiddleware(t *testing.T) { } func TestWithTimeout_Ugly_ZeroDurationDoesNotPanic(t *testing.T) { - skipIfRaceDetector(t) gin.SetMode(gin.TestMode) - defer func() { - if r := recover(); r != nil { - t.Fatalf("WithTimeout(0) panicked: %v", r) - } - }() - e, err := api.New(api.WithTimeout(0)) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -168,5 +161,15 @@ func TestWithTimeout_Ugly_ZeroDurationDoesNotPanic(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil) h.ServeHTTP(w, req) - // We only care that it did not panic. Status may vary with zero timeout. + if w.Code != http.StatusOK { + t.Fatalf("expected 200 with zero timeout disabled, got %d", w.Code) + } + + var resp api.Response[string] + if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { + t.Fatalf("unmarshal error: %v", err) + } + if resp.Data != "pong" { + t.Fatalf("expected Data=%q, got %q", "pong", resp.Data) + } }