Skip to content
This repository was archived by the owner on Sep 6, 2020. It is now read-only.

Commit 4baab69

Browse files
committed
[sync] add more test and flush overwrite header
1 parent 05b366f commit 4baab69

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

context_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"errors"
1010
"fmt"
11+
"io"
1112
"os"
1213
"reflect"
1314
"strings"
@@ -28,6 +29,26 @@ import (
2829

2930
var _ context.Context = &Context{}
3031

32+
type TestResponseRecorder struct {
33+
*httptest.ResponseRecorder
34+
closeChannel chan bool
35+
}
36+
37+
func (r *TestResponseRecorder) CloseNotify() <-chan bool {
38+
return r.closeChannel
39+
}
40+
41+
func (r *TestResponseRecorder) closeClient() {
42+
r.closeChannel <- true
43+
}
44+
45+
func CreateTestResponseRecorder() *TestResponseRecorder {
46+
return &TestResponseRecorder{
47+
httptest.NewRecorder(),
48+
make(chan bool, 1),
49+
}
50+
}
51+
3152
// Unit tests TODO
3253
// func (c *Context) File(filepath string) {
3354
// func (c *Context) Negotiate(code int, config Negotiate) {
@@ -1581,3 +1602,38 @@ func TestContextRenderDataFromReader(t *testing.T) {
15811602
assert.Equal(t, fmt.Sprintf("%d", contentLength), w.HeaderMap.Get("Content-Length"))
15821603
assert.Equal(t, extraHeaders["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))
15831604
}
1605+
1606+
func TestContextStream(t *testing.T) {
1607+
w := CreateTestResponseRecorder()
1608+
c, _ := CreateTestContext(w)
1609+
1610+
stopStream := true
1611+
c.Stream(func(w io.Writer) bool {
1612+
defer func() {
1613+
stopStream = false
1614+
}()
1615+
1616+
w.Write([]byte("test"))
1617+
1618+
return stopStream
1619+
})
1620+
1621+
assert.Equal(t, "testtest", w.Body.String())
1622+
}
1623+
1624+
func TestContextStreamWithClientGone(t *testing.T) {
1625+
w := CreateTestResponseRecorder()
1626+
c, _ := CreateTestContext(w)
1627+
1628+
c.Stream(func(writer io.Writer) bool {
1629+
defer func() {
1630+
w.closeClient()
1631+
}()
1632+
1633+
writer.Write([]byte("test"))
1634+
1635+
return true
1636+
})
1637+
1638+
assert.Equal(t, "test", w.Body.String())
1639+
}

response_writer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,6 @@ func (w *responseWriter) CloseNotify() <-chan bool {
126126

127127
// Flush implements the http.Flush interface.
128128
func (w *responseWriter) Flush() {
129+
w.WriteHeaderNow()
129130
w.ResponseWriter.(http.Flusher).Flush()
130131
}

response_writer_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,20 @@ func TestResponseWriterHijack(t *testing.T) {
115115

116116
w.Flush()
117117
}
118+
119+
func TestResponseWriterFlush(t *testing.T) {
120+
testServer := httptest.NewServer(http.HandlerFunc(
121+
func(w http.ResponseWriter, r *http.Request) {
122+
writer := &responseWriter{}
123+
writer.reset(w)
124+
125+
writer.WriteHeader(http.StatusInternalServerError)
126+
writer.Flush()
127+
}))
128+
defer testServer.Close()
129+
130+
// should return 500
131+
resp, err := http.Get(testServer.URL)
132+
assert.NoError(t, err)
133+
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
134+
}

0 commit comments

Comments
 (0)