Skip to content

Commit

Permalink
Add test case for Flush (http.Flusher)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthrgd committed Dec 2, 2017
1 parent 2553672 commit 58d244f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,49 @@ func TestStatusCodes(t *testing.T) {
assert.Equal(t, http.StatusOK, result.StatusCode)
}

type httpFlusherFunc func()

func (fn httpFlusherFunc) Flush() { fn() }

func TestFlush(t *testing.T) {
b := []byte(testBody)
handler := Gzip(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusNotFound)
rw.Write(b)
rw.(http.Flusher).Flush()
rw.Write(b)
}), MinSize(0), CompressionLevel(DefaultCompression))

r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()

var flushed bool
handler.ServeHTTP(struct {
http.ResponseWriter
http.Flusher
}{
w,
httpFlusherFunc(func() { flushed = true }),
}, r)

assert.True(t, flushed, "Flush did not call underlying http.Flusher")

res := w.Result()
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.Equal(t, "gzip", res.Header.Get("Content-Encoding"))

var buf bytes.Buffer
gw, _ := gzip.NewWriterLevel(&buf, DefaultCompression)

gw.Write(b)
gw.Flush() // Flush emits a symbol into the deflate output
gw.Write(b)
gw.Close()

assert.Equal(t, buf.Bytes(), w.Body.Bytes())
}

func TestFlushBeforeWrite(t *testing.T) {
b := []byte(testBody)
handler := Gzip(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 58d244f

Please sign in to comment.