Skip to content

Commit

Permalink
Decompress more bodies (#140)
Browse files Browse the repository at this point in the history
* Decompress more bodies

* Fix linting

* Bump version
  • Loading branch information
raviqqe authored Dec 19, 2020
1 parent 2be1d44 commit 23585f4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "time"

const (
version = "2.3.1"
version = "2.3.2"
agentName = "muffet"
concurrency = 1024
tcpTimeout = 5 * time.Second
Expand Down
4 changes: 4 additions & 0 deletions fasthttp_http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (r fasthttpHTTPResponse) Body() ([]byte, error) {
switch string(r.response.Header.Peek("Content-Encoding")) {
case "gzip":
return r.response.BodyGunzip()
case "deflate":
return r.response.BodyInflate()
case "br":
return r.response.BodyUnbrotli()
}

return r.response.Body(), nil
Expand Down
66 changes: 66 additions & 0 deletions fasthttp_http_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"bytes"
"compress/gzip"
"compress/zlib"
"testing"

"github.com/andybalholm/brotli"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)

func TestFastHTTPResponseDecodeGzipBody(t *testing.T) {
b := bytes.Buffer{}
w := gzip.NewWriter(&b)
_, err := w.Write([]byte("foo"))
assert.Nil(t, err)
err = w.Close()
assert.Nil(t, err)

r := fasthttp.Response{}
r.Header.Add("Content-Encoding", "gzip")
r.SetBody(b.Bytes())

bs, err := newFasthttpHTTPResponse(nil, &r).Body()

assert.Nil(t, err)
assert.Equal(t, "foo", string(bs))
}

func TestFastHTTPResponseDecodeDeflateBody(t *testing.T) {
b := bytes.Buffer{}
w := zlib.NewWriter(&b)
_, err := w.Write([]byte("foo"))
assert.Nil(t, err)
err = w.Close()
assert.Nil(t, err)

r := fasthttp.Response{}
r.Header.Add("Content-Encoding", "deflate")
r.SetBody(b.Bytes())

bs, err := newFasthttpHTTPResponse(nil, &r).Body()

assert.Nil(t, err)
assert.Equal(t, "foo", string(bs))
}

func TestFastHTTPResponseDecodeBrotliBody(t *testing.T) {
b := bytes.Buffer{}
w := brotli.NewWriter(&b)
_, err := w.Write([]byte("foo"))
assert.Nil(t, err)
err = w.Close()
assert.Nil(t, err)

r := fasthttp.Response{}
r.Header.Add("Content-Encoding", "br")
r.SetBody(b.Bytes())

bs, err := newFasthttpHTTPResponse(nil, &r).Body()

assert.Nil(t, err)
assert.Equal(t, "foo", string(bs))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/raviqqe/muffet/v2
go 1.12

require (
github.com/andybalholm/brotli v1.0.0
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jessevdk/go-flags v1.4.0
Expand Down

0 comments on commit 23585f4

Please sign in to comment.