Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c686f0

Browse files
authoredAug 6, 2018
Merge branch 'master' into bugfix
2 parents 772f671 + 9b7e7bd commit 9c686f0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
 

‎context_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/gin-gonic/gin/binding"
2222
"github.com/stretchr/testify/assert"
2323
"golang.org/x/net/context"
24+
"io"
2425
)
2526

2627
var _ context.Context = &Context{}
@@ -1558,3 +1559,38 @@ func TestContextRenderDataFromReader(t *testing.T) {
15581559
assert.Equal(t, fmt.Sprintf("%d", contentLength), w.HeaderMap.Get("Content-Length"))
15591560
assert.Equal(t, extraHeaders["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))
15601561
}
1562+
1563+
func TestContextStream(t *testing.T) {
1564+
w := CreateTestResponseRecorder()
1565+
c, _ := CreateTestContext(w)
1566+
1567+
stopStream := true
1568+
c.Stream(func(w io.Writer) bool {
1569+
defer func() {
1570+
stopStream = false
1571+
}()
1572+
1573+
w.Write([]byte("test"))
1574+
1575+
return stopStream
1576+
})
1577+
1578+
assert.Equal(t, "testtest", w.Body.String())
1579+
}
1580+
1581+
func TestContextStreamWithClientGone(t *testing.T) {
1582+
w := CreateTestResponseRecorder()
1583+
c, _ := CreateTestContext(w)
1584+
1585+
c.Stream(func(writer io.Writer) bool {
1586+
defer func() {
1587+
w.closeClient()
1588+
}()
1589+
1590+
writer.Write([]byte("test"))
1591+
1592+
return true
1593+
})
1594+
1595+
assert.Equal(t, "test", w.Body.String())
1596+
}

‎test_helpers.go

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package gin
66

77
import (
88
"net/http"
9+
"net/http/httptest"
910
)
1011

1112
// CreateTestContext returns a fresh engine and context for testing purposes
@@ -16,3 +17,23 @@ func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
1617
c.writermem.reset(w)
1718
return
1819
}
20+
21+
type TestResponseRecorder struct {
22+
*httptest.ResponseRecorder
23+
closeChannel chan bool
24+
}
25+
26+
func (r *TestResponseRecorder) CloseNotify() <-chan bool {
27+
return r.closeChannel
28+
}
29+
30+
func (r *TestResponseRecorder) closeClient() {
31+
r.closeChannel <- true
32+
}
33+
34+
func CreateTestResponseRecorder() *TestResponseRecorder {
35+
return &TestResponseRecorder{
36+
httptest.NewRecorder(),
37+
make(chan bool, 1),
38+
}
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.