Skip to content

Commit

Permalink
fix(respose): fix multiplying response body
Browse files Browse the repository at this point in the history
  • Loading branch information
jindrichskupa committed Apr 11, 2024
1 parent d8d0c1d commit ce2db8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 0 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce2db8e

Please sign in to comment.