mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-09 12:12:37 +08:00
test(response_writer): add tests for Flush() with and without http.Flusher
This commit is contained in:
parent
d75fcd4c9a
commit
b190d07a7a
@ -15,10 +15,33 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
// TestResponseWriterFlushWithFlusher verifies Flush() calls the underlying Flusher.
|
||||||
// func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
func TestResponseWriterFlushWithFlusher(t *testing.T) {
|
||||||
// func (w *responseWriter) CloseNotify() <-chan bool {
|
testWriter := httptest.NewRecorder()
|
||||||
// func (w *responseWriter) Flush() {
|
writer := &responseWriter{ResponseWriter: testWriter}
|
||||||
|
writer.Flush()
|
||||||
|
assert.True(t, testWriter.Flushed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestResponseWriterFlushWithNonFlusher verifies Flush() is a no-op
|
||||||
|
// when the underlying ResponseWriter does not implement http.Flusher.
|
||||||
|
// Reproduces the panic reported in https://github.com/gin-gonic/gin/issues/4460
|
||||||
|
func TestResponseWriterFlushWithNonFlusher(t *testing.T) {
|
||||||
|
nonFlusher := &nonFlusherWriter{header: http.Header{}}
|
||||||
|
writer := &responseWriter{ResponseWriter: nonFlusher}
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
writer.Flush()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// nonFlusherWriter is a minimal http.ResponseWriter that does NOT implement http.Flusher.
|
||||||
|
type nonFlusherWriter struct {
|
||||||
|
header http.Header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *nonFlusherWriter) Header() http.Header { return w.header }
|
||||||
|
func (w *nonFlusherWriter) Write(b []byte) (int, error) { return len(b), nil }
|
||||||
|
func (w *nonFlusherWriter) WriteHeader(code int) {}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ ResponseWriter = &responseWriter{}
|
_ ResponseWriter = &responseWriter{}
|
||||||
@ -318,3 +341,6 @@ func TestPusherWithoutPusher(t *testing.T) {
|
|||||||
pusher := w.Pusher()
|
pusher := w.Pusher()
|
||||||
assert.Nil(t, pusher, "Expected pusher to be nil")
|
assert.Nil(t, pusher, "Expected pusher to be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestResponseWriterFlushWithFlusher verifies that Flush() delegates to
|
||||||
|
// the underlying writer when it implements http.Flusher.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user