Skip to content

Commit

Permalink
Replace the shouldGzip impl w/ httputils.Negotiate
Browse files Browse the repository at this point in the history
Previously the ParseAccept method from
github.com/golang/gddo/httputil/header was used in (*handler).shouldGzip.
It's not clear this method was meant to be exported and widely used,
and it's use in shouldGzip did not account for case-insensitivity.

Instead it's replaced by Negotiate added to github.com/tmthrgd/httputils
in tmthrgd/httputils@2983d2a.
  • Loading branch information
tmthrgd committed Feb 17, 2018
1 parent fab370b commit 14e0d16
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 483 deletions.
10 changes: 2 additions & 8 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"sync"

"github.com/golang/gddo/httputil/header"
"github.com/tmthrgd/httputils"
)

Expand Down Expand Up @@ -332,13 +331,8 @@ func (h *handler) shouldGzip(r *http.Request) bool {
return true
}

for _, spec := range header.ParseAccept(r.Header, "Accept-Encoding") {
if spec.Value == "gzip" && spec.Q > 0 {
return true
}
}

return false
match := httputils.Negotiate(r.Header, "Accept-Encoding", "gzip")
return match == "gzip"
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
16 changes: 16 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ func TestGzipHandler(t *testing.T) {
assert.Equal(t, gzipStrLevel(testBody, DefaultCompression), resp2.Body.Bytes())
}

func TestGzipHandlerAcceptEncodingCaseInsensitive(t *testing.T) {
// This just exists to provide something for GzipHandler to wrap.
handler := newTestHandler(testBody)

req := httptest.NewRequest(http.MethodGet, "/whatever", nil)
req.Header.Set("Accept-Encoding", "GZIP")
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
res := resp.Result()

assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, "gzip", res.Header.Get("Content-Encoding"))
assert.Equal(t, "Accept-Encoding", res.Header.Get("Vary"))
assert.Equal(t, gzipStrLevel(testBody, DefaultCompression), resp.Body.Bytes())
}

func TestGzipLevelHandler(t *testing.T) {
for lvl := BestSpeed; lvl <= BestCompression; lvl++ {
req := httptest.NewRequest(http.MethodGet, "/whatever", nil)
Expand Down
27 changes: 0 additions & 27 deletions vendor/github.com/golang/gddo/LICENSE

This file was deleted.

297 changes: 0 additions & 297 deletions vendor/github.com/golang/gddo/httputil/header/header.go

This file was deleted.

0 comments on commit 14e0d16

Please sign in to comment.