Skip to content

Commit

Permalink
http.go: New BodyUncompressed() method for request and responses
Browse files Browse the repository at this point in the history
The new method returns a body and uncompress if it's gzipped
  • Loading branch information
stokito committed Jun 5, 2022
1 parent 70ad382 commit 5de84c3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions http.go
Expand Up @@ -486,6 +486,48 @@ func inflateData(p []byte) ([]byte, error) {
return bb.B, nil
}

var ErrContentEncodingUnsupported = errors.New("unsupported Content-Encoding")

// BodyUncompressed returns body data and if needed decompress it from gzip, deflate or Brotli.
//
// This method may be used if the response header contains
// 'Content-Encoding' for reading uncompressed request body.
// Use Body for reading the raw request body.
func (req *Request) BodyUncompressed() ([]byte, error) {
switch string(req.Header.ContentEncoding()) {
case "":
return req.Body(), nil
case "deflate":
return req.BodyInflate()
case "gzip":
return req.BodyGunzip()
case "br":
return req.BodyUnbrotli()
default:
return nil, ErrContentEncodingUnsupported
}
}

// BodyUncompressed returns body data and if needed decompress it from gzip, deflate or Brotli.
//
// This method may be used if the response header contains
// 'Content-Encoding' for reading uncompressed response body.
// Use Body for reading the raw response body.
func (resp *Response) BodyUncompressed() ([]byte, error) {
switch string(resp.Header.ContentEncoding()) {
case "":
return resp.Body(), nil
case "deflate":
return resp.BodyInflate()
case "gzip":
return resp.BodyGunzip()
case "br":
return resp.BodyUnbrotli()
default:
return nil, ErrContentEncodingUnsupported
}
}

// BodyWriteTo writes request body to w.
func (req *Request) BodyWriteTo(w io.Writer) error {
if req.bodyStream != nil {
Expand Down
52 changes: 52 additions & 0 deletions http_test.go
Expand Up @@ -347,6 +347,12 @@ func testResponseBodyStreamDeflate(t *testing.T, body []byte, bodySize int) {
if !bytes.Equal(respBody, body) {
t.Fatalf("unexpected body: %q. Expecting %q", respBody, body)
}
// check for invalid
resp.SetBodyRaw([]byte("invalid"))
_, errDeflate := resp.BodyInflate()
if errDeflate == nil || errDeflate.Error() != "zlib: invalid header" {
t.Fatalf("expected error: 'zlib: invalid header' but was %v", errDeflate)
}
}

func testResponseBodyStreamGzip(t *testing.T, body []byte, bodySize int) {
Expand Down Expand Up @@ -375,6 +381,12 @@ func testResponseBodyStreamGzip(t *testing.T, body []byte, bodySize int) {
if !bytes.Equal(respBody, body) {
t.Fatalf("unexpected body: %q. Expecting %q", respBody, body)
}
// check for invalid
resp.SetBodyRaw([]byte("invalid"))
_, errUnzip := resp.BodyGunzip()
if errUnzip == nil || errUnzip.Error() != "unexpected EOF" {
t.Fatalf("expected error: 'unexpected EOF' but was %v", errUnzip)
}
}

func TestResponseWriteGzipNilBody(t *testing.T) {
Expand Down Expand Up @@ -405,6 +417,46 @@ func TestResponseWriteDeflateNilBody(t *testing.T) {
}
}

func TestResponseBodyUncompressed(t *testing.T) {
body := "body"
var r Response
r.SetBodyStream(bytes.NewReader([]byte(body)), len(body))

w := &bytes.Buffer{}
bw := bufio.NewWriter(w)
if err := r.WriteDeflate(bw); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := bw.Flush(); err != nil {
t.Fatalf("unexpected error: %v", err)
}

var resp Response
br := bufio.NewReader(w)
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}

ce := resp.Header.ContentEncoding()
if string(ce) != "deflate" {
t.Fatalf("unexpected Content-Encoding: %s", ce)
}
respBody, err := resp.BodyUncompressed()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(respBody) != body {
t.Fatalf("unexpected body: %q. Expecting %q", respBody, body)
}

// check for invalid encoding
resp.Header.SetContentEncoding("invalid")
_, decodeErr := resp.BodyUncompressed()
if decodeErr != ErrContentEncodingUnsupported {
t.Fatalf("unexpected error: %v", decodeErr)
}
}

func TestResponseSwapBodySerial(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 5de84c3

Please sign in to comment.