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