middleware: honor Discard when flushing (#1185)
This commit is contained in:
parent
b1c9ab4762
commit
3d1777a1ef
2 changed files with 68 additions and 32 deletions
|
|
@ -63,6 +63,7 @@ type WrapResponseWriter interface {
|
|||
Unwrap() http.ResponseWriter
|
||||
// Discard causes all writes to the original ResponseWriter be discarded,
|
||||
// instead writing only to the tee'd writer if it's set.
|
||||
// Flush calls also do not reach the original ResponseWriter.
|
||||
// The caller is responsible for calling WriteHeader and Write on the
|
||||
// original ResponseWriter once the processing is done.
|
||||
Discard()
|
||||
|
|
@ -119,6 +120,13 @@ func (b *basicWriter) maybeWriteHeader() {
|
|||
}
|
||||
}
|
||||
|
||||
func (b *basicWriter) flush() {
|
||||
b.maybeWriteHeader()
|
||||
if !b.discard {
|
||||
b.ResponseWriter.(http.Flusher).Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *basicWriter) Status() int {
|
||||
return b.code
|
||||
}
|
||||
|
|
@ -145,9 +153,7 @@ type flushWriter struct {
|
|||
}
|
||||
|
||||
func (f *flushWriter) Flush() {
|
||||
f.basicWriter.maybeWriteHeader()
|
||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||
fl.Flush()
|
||||
f.basicWriter.flush()
|
||||
}
|
||||
|
||||
var _ http.Flusher = &flushWriter{}
|
||||
|
|
@ -170,9 +176,7 @@ type flushHijackWriter struct {
|
|||
}
|
||||
|
||||
func (f *flushHijackWriter) Flush() {
|
||||
f.basicWriter.maybeWriteHeader()
|
||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||
fl.Flush()
|
||||
f.basicWriter.flush()
|
||||
}
|
||||
|
||||
func (f *flushHijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
|
|
@ -192,9 +196,7 @@ type httpFancyWriter struct {
|
|||
}
|
||||
|
||||
func (f *httpFancyWriter) Flush() {
|
||||
f.basicWriter.maybeWriteHeader()
|
||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||
fl.Flush()
|
||||
f.basicWriter.flush()
|
||||
}
|
||||
|
||||
func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
|
|
@ -237,9 +239,7 @@ type http2FancyWriter struct {
|
|||
}
|
||||
|
||||
func (f *http2FancyWriter) Flush() {
|
||||
f.basicWriter.maybeWriteHeader()
|
||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||
fl.Flush()
|
||||
f.basicWriter.flush()
|
||||
}
|
||||
|
||||
var _ http.Flusher = &http2FancyWriter{}
|
||||
|
|
|
|||
|
|
@ -50,27 +50,63 @@ func TestWrapResponseWriterStatusWhenFlushed(t *testing.T) {
|
|||
{"explicit", http.StatusCreated, http.StatusCreated},
|
||||
} {
|
||||
t.Run(status.name, func(t *testing.T) {
|
||||
original := httptest.NewRecorder()
|
||||
wrap := writer.wrap(original)
|
||||
if status.code != 0 {
|
||||
wrap.WriteHeader(status.code)
|
||||
for _, mode := range []struct {
|
||||
name string
|
||||
discard bool
|
||||
tee bool
|
||||
}{
|
||||
{"passthrough", false, false},
|
||||
{"tee", false, true},
|
||||
{"discard", true, false},
|
||||
{"discard-with-tee", true, true},
|
||||
} {
|
||||
t.Run(mode.name, func(t *testing.T) {
|
||||
original := &httptest.ResponseRecorder{
|
||||
HeaderMap: make(http.Header),
|
||||
Body: new(bytes.Buffer),
|
||||
}
|
||||
wrap := writer.wrap(original)
|
||||
var tee bytes.Buffer
|
||||
if mode.tee {
|
||||
wrap.Tee(&tee)
|
||||
}
|
||||
if mode.discard {
|
||||
wrap.Discard()
|
||||
}
|
||||
if status.code != 0 {
|
||||
wrap.WriteHeader(status.code)
|
||||
}
|
||||
|
||||
wrap.(http.Flusher).Flush()
|
||||
assertEqual(t, status.want, wrap.Status())
|
||||
assertEqual(t, !mode.discard, original.Flushed)
|
||||
assertEqual(t, 0, wrap.BytesWritten())
|
||||
|
||||
// Flushing commits the status, even before any body is written.
|
||||
wrap.WriteHeader(http.StatusInternalServerError)
|
||||
_, err := wrap.Write([]byte("body"))
|
||||
assertNoError(t, err)
|
||||
assertNoError(t, http.NewResponseController(wrap).Flush())
|
||||
assertEqual(t, status.want, wrap.Status())
|
||||
assertEqual(t, 4, wrap.BytesWritten())
|
||||
if mode.tee {
|
||||
assertEqual(t, "body", tee.String())
|
||||
}
|
||||
if mode.discard {
|
||||
assertEqual(t, false, original.Flushed)
|
||||
assertEqual(t, 0, original.Code)
|
||||
assertEqual(t, "", original.Body.String())
|
||||
original.WriteHeader(http.StatusAccepted)
|
||||
_, err := original.Write([]byte("replacement"))
|
||||
assertNoError(t, err)
|
||||
assertEqual(t, http.StatusAccepted, original.Code)
|
||||
assertEqual(t, "replacement", original.Body.String())
|
||||
} else {
|
||||
assertEqual(t, status.want, original.Code)
|
||||
assertEqual(t, "body", original.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
wrap.(http.Flusher).Flush()
|
||||
assertEqual(t, status.want, wrap.Status())
|
||||
assertEqual(t, status.want, original.Code)
|
||||
assertEqual(t, true, original.Flushed)
|
||||
assertEqual(t, 0, wrap.BytesWritten())
|
||||
|
||||
// Flushing commits the status, even before any body is written.
|
||||
wrap.WriteHeader(http.StatusInternalServerError)
|
||||
_, err := wrap.Write([]byte("body"))
|
||||
assertNoError(t, err)
|
||||
wrap.(http.Flusher).Flush()
|
||||
assertEqual(t, status.want, wrap.Status())
|
||||
assertEqual(t, status.want, original.Code)
|
||||
assertEqual(t, "body", original.Body.String())
|
||||
assertEqual(t, 4, wrap.BytesWritten())
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue