Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check minsize when writing first byte #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func (w *GzipResponseWriter) startGzip() error {
// Initialize the GZIP response.
w.init()
n, err := w.gw.Write(w.buf)

// This should never happen (per io.Writer docs), but if the write didn't
// accept the entire buffer but returned no specific error, we have no clue
// what's going on, so abort just to be safe.
Expand Down
45 changes: 45 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,51 @@ func TestGzipHandlerNoBody(t *testing.T) {
}
}

func TestGzipHandlerStream(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {
t.Fatalf("failed creating listen socket: %v", err)
}
defer ln.Close()
srv := &http.Server{
Handler: nil,
}
go srv.Serve(ln)

handler, ok := GzipHandlerWithOpts(MinSize(0))
assert.Nil(t, ok)
srv.Handler = handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
assert.Equal(t, true, ok)

w.WriteHeader(200)
w.Write([]byte(""))
flusher.Flush()
w.Write([]byte("subsequent write with data"))
flusher.Flush()
}))

req := &http.Request{
Method: "GET",
URL: &url.URL{Path: "/", Scheme: "http", Host: ln.Addr().String()},
Header: make(http.Header),
Close: false,
}

res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Unexpected error making http request %v", err)
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("Unexpected error reading response body %v", err)
}

assert.Equal(t, "subsequent write with data", string(body))

}
func TestGzipHandlerContentLength(t *testing.T) {
testBodyBytes := []byte(testBody)
tests := []struct {
Expand Down