MCPcopy
hub / github.com/go-chi/chi / TestHttpFancyWriterReadFromByteCountWithTee

Function TestHttpFancyWriterReadFromByteCountWithTee

middleware/wrap_writer_test.go:94–111  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

92// write went through basicWriter.Write (which already increments the counter),
93// resulting in double-counting the bytes when a Tee writer was set.
94func 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}

Callers

nothing calls this directly

Calls 5

ReadFromMethod · 0.95
assertNoErrorFunction · 0.85
assertEqualFunction · 0.85
TeeMethod · 0.65
BytesWrittenMethod · 0.65

Tested by

no test coverage detected