Skip to content

Commit 3efb8a4

Browse files
Merge pull request #8 from dragonsinth/readfrom
responseRecorders should implement io.ReaderFrom
2 parents 7404cbf + c6b5a39 commit 3efb8a4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

responsewriter.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sigsci
33
import (
44
"bufio"
55
"fmt"
6+
"io"
67
"net"
78
"net/http"
89
)
@@ -75,6 +76,13 @@ func (w *responseRecorder) Write(b []byte) (int, error) {
7576
return w.base.Write(b)
7677
}
7778

79+
func (w *responseRecorder) ReadFrom(r io.Reader) (n int64, err error) {
80+
if rf, ok := w.base.(io.ReaderFrom); ok {
81+
return rf.ReadFrom(r)
82+
}
83+
return io.Copy(w.base, r)
84+
}
85+
7886
// Hijack hijacks the connection from the HTTP handler so that it can be used directly (websockets, etc.)
7987
// NOTE: This will fail if the wrapped http.responseRecorder is not a http.Hijacker.
8088
func (w *responseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
@@ -106,3 +114,8 @@ func (w *responseRecorderFlusher) Flush() {
106114
f.Flush()
107115
}
108116
}
117+
118+
// ensure our writers satisfy the intended interfaces
119+
var _ http.Hijacker = (*responseRecorder)(nil)
120+
var _ io.ReaderFrom = (*responseRecorder)(nil)
121+
var _ http.Flusher = (*responseRecorderFlusher)(nil)

responsewriter_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sigsci
22

33
import (
44
"fmt"
5+
"io"
56
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
@@ -25,6 +26,10 @@ func (w *testResponseRecorder) Write(b []byte) (int, error) {
2526
return w.Recorder.Write(b)
2627
}
2728

29+
func (w *testResponseRecorder) ReadFrom(r io.Reader) (n int64, err error) {
30+
return io.Copy(w.Recorder, r)
31+
}
32+
2833
// testResponseRecorderFlusher is a httptest.ResponseRecorder with the Flusher interface
2934
type testResponseRecorderFlusher struct {
3035
Recorder *httptest.ResponseRecorder
@@ -42,6 +47,10 @@ func (w *testResponseRecorderFlusher) Write(b []byte) (int, error) {
4247
return w.Recorder.Write(b)
4348
}
4449

50+
func (w *testResponseRecorderFlusher) ReadFrom(r io.Reader) (n int64, err error) {
51+
return io.Copy(w.Recorder, r)
52+
}
53+
4554
func (w *testResponseRecorderFlusher) Flush() {
4655
w.Recorder.Flush()
4756
}

0 commit comments

Comments
 (0)