From ce2db8e71113cfa049aa9a7b389aca2e133ee947 Mon Sep 17 00:00:00 2001 From: Jindrich Skupa Date: Thu, 11 Apr 2024 11:18:54 +0200 Subject: [PATCH] fix(respose): fix multiplying response body --- logger.go | 1 - logger_test.go | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/logger.go b/logger.go index 8ecfa09..ca947ad 100644 --- a/logger.go +++ b/logger.go @@ -118,7 +118,6 @@ func (p *logRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer resp.Flush() respBodyBytes := resp.buf.Bytes() - _, _ = w.Write(respBodyBytes) headers := make(map[string]string) for name, values := range r.Header { diff --git a/logger_test.go b/logger_test.go index 7f7ac24..4052e9e 100644 --- a/logger_test.go +++ b/logger_test.go @@ -39,6 +39,7 @@ func TestGetPlaintext(t *testing.T) { handler.ServeHTTP(recorder, req) assertHeaderExists(t, req) + assertBody(t, recorder, "helloWorld") } func TestGetJson(t *testing.T) { @@ -241,6 +242,14 @@ func assertHeaderExists(t *testing.T, req *http.Request) { } } +func assertBody(t *testing.T, recorder *httptest.ResponseRecorder, expected string) { + t.Helper() + + if recorder.Body.String() != expected { + t.Errorf("invalid body value: %s, expected %s", recorder.Body.String(), expected) + } +} + func prepareConfig(contentTypes []string, maxBodySize int, statusCodes []int) *traefikmiddlewarerequestlogger.Config { cfg := traefikmiddlewarerequestlogger.CreateConfig() cfg.ContentTypes = contentTypes