Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions middleware/body_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package middleware
import (
"bufio"
"bytes"
"fmt"
"errors"
"io"
"net"
Expand Down Expand Up @@ -67,7 +68,11 @@ func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
// Request
reqBody := []byte{}
if c.Request().Body != nil { // Read
reqBody, _ = io.ReadAll(c.Request().Body)
var errRead error
reqBody, errRead = io.ReadAll(c.Request().Body)
if errRead != nil {
return errRead
}
}
c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset

Expand Down Expand Up @@ -100,7 +105,7 @@ func (w *bodyDumpResponseWriter) Write(b []byte) (int, error) {
func (w *bodyDumpResponseWriter) Flush() {
err := http.NewResponseController(w.ResponseWriter).Flush()
if err != nil && errors.Is(err, http.ErrNotSupported) {
panic(errors.New("response writer flushing is not supported"))
panic(fmt.Errorf("echo: response writer %T does not support flushing (http.Flusher interface)", w.ResponseWriter))
}
}

Expand Down
6 changes: 4 additions & 2 deletions middleware/body_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package middleware

import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -92,11 +93,12 @@ func TestBodyDumpFails(t *testing.T) {
}

func TestBodyDumpResponseWriter_CanNotFlush(t *testing.T) {
rw := new(testResponseWriterNoFlushHijack)
bdrw := bodyDumpResponseWriter{
ResponseWriter: new(testResponseWriterNoFlushHijack), // this RW does not support flush
ResponseWriter: rw, // this RW does not support flush
}

assert.PanicsWithError(t, "response writer flushing is not supported", func() {
assert.PanicsWithError(t, fmt.Sprintf("echo: response writer %T does not support flushing (http.Flusher interface)", rw), func() {
bdrw.Flush()
})
}
Expand Down