MCPcopy
hub / github.com/labstack/echo / TestBodyDump_ReadError

Function TestBodyDump_ReadError

middleware/body_dump_test.go:165–198  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

163}
164
165func TestBodyDump_ReadError(t *testing.T) {
166 e := echo.New()
167
168 // Create a reader that fails during read
169 failingReader := &failingReadCloser{
170 data: []byte("partial data"),
171 failAt: 7, // Fail after 7 bytes
172 failWith: errors.New("connection reset"),
173 }
174
175 req := httptest.NewRequest(http.MethodPost, "/", failingReader)
176 rec := httptest.NewRecorder()
177 c := e.NewContext(req, rec)
178
179 h := func(c *echo.Context) error {
180 // This handler should not be reached if body read fails
181 body, _ := io.ReadAll(c.Request().Body)
182 return c.String(http.StatusOK, string(body))
183 }
184
185 requestBodyReceived := ""
186 mw := BodyDump(func(c *echo.Context, reqBody, resBody []byte, err error) {
187 requestBodyReceived = string(reqBody)
188 })
189
190 err := mw(h)(c)
191
192 // Verify error is propagated
193 assert.Error(t, err)
194 assert.Contains(t, err.Error(), "connection reset")
195
196 // Verify handler was not executed (callback wouldn't have received data)
197 assert.Empty(t, requestBodyReceived)
198}
199
200// failingReadCloser is a helper type for testing read errors
201type failingReadCloser struct {

Callers

nothing calls this directly

Calls 5

RequestMethod · 0.95
StringMethod · 0.95
BodyDumpFunction · 0.85
NewContextMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…