diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 1343a023b1454c83270fb66746d2039ab278af7b..5e95a9e94c0932348ef0456e0a00e51067f9f551 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -502,6 +502,7 @@ func (h *HTTPBin) Delay(w http.ResponseWriter, r *http.Request) { select { case <-r.Context().Done(): + w.WriteHeader(499) // "Client Closed Request" https://httpstatuses.com/499 return case <-time.After(delay): } diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index e93f4f033300e736edbed642a1589df24d84692a..4371a3b0d256daaeab6920630461216a09988a25 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -5,6 +5,7 @@ import ( "bytes" "compress/gzip" "compress/zlib" + "context" "encoding/json" "errors" "fmt" @@ -1469,6 +1470,19 @@ func TestDelay(t *testing.T) { } }) + t.Run("cancelation causes 499", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) + defer cancel() + + r, _ := http.NewRequestWithContext(ctx, "GET", "/delay/1s", nil) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + + if w.Code != 499 { + t.Errorf("expected 499 response, got %d", w.Code) + } + }) + var badTests = []struct { url string code int