From 3e13ebec93ab078865b383bed36c8ec6b24dedf9 Mon Sep 17 00:00:00 2001 From: SALLEYRON Julien Date: Tue, 2 Jan 2018 16:02:03 +0100 Subject: [PATCH] We need to flush the end of the body when retry is streamed --- middlewares/retry.go | 1 + middlewares/retry_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/middlewares/retry.go b/middlewares/retry.go index 94ee99686..eee243a29 100644 --- a/middlewares/retry.go +++ b/middlewares/retry.go @@ -56,6 +56,7 @@ func (retry *Retry) ServeHTTP(rw http.ResponseWriter, r *http.Request) { // It's a stream request and the body gets already sent to the client. // Therefore we should not send the response a second time. if recorder.streamingResponseStarted { + recorder.Flush() break } diff --git a/middlewares/retry_test.go b/middlewares/retry_test.go index bb92754a6..4f74efba7 100644 --- a/middlewares/retry_test.go +++ b/middlewares/retry_test.go @@ -134,3 +134,21 @@ type countingRetryListener struct { func (l *countingRetryListener) Retried(req *http.Request, attempt int) { l.timesCalled++ } + +func TestRetryWithFlush(t *testing.T) { + next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(200) + rw.Write([]byte("FULL ")) + rw.(http.Flusher).Flush() + rw.Write([]byte("DATA")) + }) + + retry := NewRetry(1, next, &countingRetryListener{}) + responseRecorder := httptest.NewRecorder() + + retry.ServeHTTP(responseRecorder, &http.Request{}) + + if responseRecorder.Body.String() != "FULL DATA" { + t.Errorf("Wrong body %q want %q", responseRecorder.Body.String(), "FULL DATA") + } +}