TestHttpFancyWriterReadFromByteCountWithTee is a regression test for https://github.com/go-chi/chi/issues/1067. httpFancyWriter.ReadFrom was adding n to basicWriter.bytes even when the write went through basicWriter.Write (which already increments the counter), resulting in double-counting the bytes
(t *testing.T)
| 92 | // write went through basicWriter.Write (which already increments the counter), |
| 93 | // resulting in double-counting the bytes when a Tee writer was set. |
| 94 | func TestHttpFancyWriterReadFromByteCountWithTee(t *testing.T) { |
| 95 | original := &httptest.ResponseRecorder{ |
| 96 | HeaderMap: make(http.Header), |
| 97 | Body: new(bytes.Buffer), |
| 98 | } |
| 99 | f := &httpFancyWriter{basicWriter: basicWriter{ResponseWriter: original}} |
| 100 | |
| 101 | var teeBuf bytes.Buffer |
| 102 | f.Tee(&teeBuf) |
| 103 | |
| 104 | const input = "hello world" |
| 105 | n, err := f.ReadFrom(strings.NewReader(input)) |
| 106 | assertNoError(t, err) |
| 107 | assertEqual(t, int64(len(input)), n) |
| 108 | // Before the fix, BytesWritten() returned 22 (double-counted). |
| 109 | assertEqual(t, len(input), f.BytesWritten()) |
| 110 | assertEqual(t, []byte(input), teeBuf.Bytes()) |
| 111 | } |
nothing calls this directly
no test coverage detected