mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 01:38:12 +08:00
Merge 3c28f41f5b880cdbc33555931a8ce8fe1a5b0500 into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
dc1cc4ea77
@ -1327,7 +1327,7 @@ func (c *Context) SSEvent(name string, message any) {
|
||||
// indicates "Is client disconnected in middle of stream"
|
||||
func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
||||
w := c.Writer
|
||||
clientGone := w.CloseNotify()
|
||||
clientGone := c.Request.Context().Done()
|
||||
for {
|
||||
select {
|
||||
case <-clientGone:
|
||||
|
||||
@ -3023,27 +3023,26 @@ func TestContextRenderDataFromReaderNoHeaders(t *testing.T) {
|
||||
|
||||
type TestResponseRecorder struct {
|
||||
*httptest.ResponseRecorder
|
||||
closeChannel chan bool
|
||||
}
|
||||
|
||||
func (r *TestResponseRecorder) CloseNotify() <-chan bool {
|
||||
return r.closeChannel
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (r *TestResponseRecorder) closeClient() {
|
||||
r.closeChannel <- true
|
||||
r.cancel()
|
||||
}
|
||||
|
||||
func CreateTestResponseRecorder() *TestResponseRecorder {
|
||||
func CreateTestResponseRecorder() (*TestResponseRecorder, *http.Request) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
|
||||
return &TestResponseRecorder{
|
||||
httptest.NewRecorder(),
|
||||
make(chan bool, 1),
|
||||
}
|
||||
cancel,
|
||||
}, req
|
||||
}
|
||||
|
||||
func TestContextStream(t *testing.T) {
|
||||
w := CreateTestResponseRecorder()
|
||||
w, req := CreateTestResponseRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
c.Request = req
|
||||
|
||||
stopStream := true
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
@ -3061,8 +3060,9 @@ func TestContextStream(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextStreamWithClientGone(t *testing.T) {
|
||||
w := CreateTestResponseRecorder()
|
||||
w, req := CreateTestResponseRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
c.Request = req
|
||||
|
||||
c.Stream(func(writer io.Writer) bool {
|
||||
defer func() {
|
||||
@ -3079,7 +3079,7 @@ func TestContextStreamWithClientGone(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextResetInHandler(t *testing.T) {
|
||||
w := CreateTestResponseRecorder()
|
||||
w, _ := CreateTestResponseRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.handlers = []HandlerFunc{
|
||||
|
||||
@ -24,7 +24,6 @@ type ResponseWriter interface {
|
||||
http.ResponseWriter
|
||||
http.Hijacker
|
||||
http.Flusher
|
||||
http.CloseNotifier
|
||||
|
||||
// Status returns the HTTP response status code of the current request.
|
||||
Status() int
|
||||
@ -120,11 +119,6 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return w.ResponseWriter.(http.Hijacker).Hijack()
|
||||
}
|
||||
|
||||
// CloseNotify implements the http.CloseNotifier interface.
|
||||
func (w *responseWriter) CloseNotify() <-chan bool {
|
||||
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
||||
}
|
||||
|
||||
// Flush implements the http.Flusher interface.
|
||||
func (w *responseWriter) Flush() {
|
||||
w.WriteHeaderNow()
|
||||
|
||||
@ -26,7 +26,6 @@ var (
|
||||
_ http.ResponseWriter = ResponseWriter(&responseWriter{})
|
||||
_ http.Hijacker = ResponseWriter(&responseWriter{})
|
||||
_ http.Flusher = ResponseWriter(&responseWriter{})
|
||||
_ http.CloseNotifier = ResponseWriter(&responseWriter{})
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -119,10 +118,6 @@ func TestResponseWriterHijack(t *testing.T) {
|
||||
})
|
||||
assert.True(t, w.Written())
|
||||
|
||||
assert.Panics(t, func() {
|
||||
w.CloseNotify()
|
||||
})
|
||||
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user