diff --git a/humatest/humatest.go b/humatest/humatest.go index fde3ba27..85debeb0 100644 --- a/humatest/humatest.go +++ b/humatest/humatest.go @@ -345,7 +345,10 @@ func dumpBody(body io.ReadCloser, buf *bytes.Buffer) (io.ReadCloser, error) { } body.Close() if strings.Contains(buf.String(), "json") { - json.Indent(buf, b, "", " ") + if err := json.Indent(buf, b, "", " "); err != nil { + // Indent failed, so just write the buffer. + buf.Write(b) + } } else { buf.Write(b) } diff --git a/humatest/humatest_test.go b/humatest/humatest_test.go index 27b57b23..07caef08 100644 --- a/humatest/humatest_test.go +++ b/humatest/humatest_test.go @@ -151,7 +151,7 @@ func TestNewAPI(t *testing.T) { } func TestDumpBodyError(t *testing.T) { - req, _ := http.NewRequest(http.MethodGet, "http://example.com/foo?bar=baz", nil) + req := httptest.NewRequest(http.MethodGet, "http://example.com/foo?bar=baz", nil) req.Header.Set("Foo", "bar") req.Host = "example.com" req.Body = io.NopCloser(iotest.ErrReader(io.ErrUnexpectedEOF)) @@ -165,6 +165,17 @@ func TestDumpBodyError(t *testing.T) { require.Error(t, err) } +func TestDumpBodyInvalidJSON(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "http://example.com/foo?bar=baz", nil) + req.Header.Set("Content-Type", "application/json") + req.Host = "example.com" + req.Body = io.NopCloser(strings.NewReader("invalid json")) + + b, err := DumpRequest(req) + require.NoError(t, err) + assert.Contains(t, string(b), "invalid json") +} + func TestOpenAPIRequired(t *testing.T) { assert.PanicsWithValue(t, "custom huma.Config structs must specify a value for OpenAPI", func() { New(t, huma.Config{})