mirror of https://github.com/gin-gonic/gin.git
fix call Stream() on copy of *Context (#1763)
This commit is contained in:
parent
5acf660117
commit
ca5e7090c7
|
@ -78,7 +78,6 @@ func (c *Context) reset() {
|
||||||
// This has to be used when the context has to be passed to a goroutine.
|
// This has to be used when the context has to be passed to a goroutine.
|
||||||
func (c *Context) Copy() *Context {
|
func (c *Context) Copy() *Context {
|
||||||
var cp = *c
|
var cp = *c
|
||||||
cp.writermem.ResponseWriter = nil
|
|
||||||
cp.Writer = &cp.writermem
|
cp.Writer = &cp.writermem
|
||||||
cp.index = abortIndex
|
cp.index = abortIndex
|
||||||
cp.handlers = nil
|
cp.handlers = nil
|
||||||
|
|
|
@ -324,7 +324,6 @@ func TestContextCopy(t *testing.T) {
|
||||||
|
|
||||||
cp := c.Copy()
|
cp := c.Copy()
|
||||||
assert.Nil(t, cp.handlers)
|
assert.Nil(t, cp.handlers)
|
||||||
assert.Nil(t, cp.writermem.ResponseWriter)
|
|
||||||
assert.Equal(t, &cp.writermem, cp.Writer.(*responseWriter))
|
assert.Equal(t, &cp.writermem, cp.Writer.(*responseWriter))
|
||||||
assert.Equal(t, cp.Request, c.Request)
|
assert.Equal(t, cp.Request, c.Request)
|
||||||
assert.Equal(t, cp.index, abortIndex)
|
assert.Equal(t, cp.index, abortIndex)
|
||||||
|
@ -1755,3 +1754,29 @@ func TestContextResetInHandler(t *testing.T) {
|
||||||
c.Next()
|
c.Next()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextStreamToCopyOfContext(t *testing.T) {
|
||||||
|
w := CreateTestResponseRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
|
h := func(c *Context) {
|
||||||
|
nc := c.Copy()
|
||||||
|
nc.Stream(func(w io.Writer) bool {
|
||||||
|
w.Write([]byte("1"))
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
c.String(http.StatusOK, "%s", "2")
|
||||||
|
w.closeClient()
|
||||||
|
|
||||||
|
nc.Stream(func(w io.Writer) bool {
|
||||||
|
w.Write([]byte("3"))
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
h(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, "12", w.Body.String())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue