fix #100: decode response body in br/gzip/deflate formats

This commit is contained in:
debugtalk
2022-02-21 16:58:36 +08:00
parent 1f0996d269
commit c6535f0f78
4 changed files with 31 additions and 1 deletions

View File

@@ -1,10 +1,12 @@
# Release History
## v0.6.2 (2022-02-20)
## v0.6.2 (2022-02-21)
- fix: omit pseudo header names for HTTP/1, e.g. :authority
- fix: generate `headers.\"Content-Type\"` in har2case
- change: json unmarshal to json.Number when parsing data
- fix: incorrect data type when extracting data using jmespath
- fix: decode response body in br/gzip/deflate formats
## v0.6.1 (2022-02-17)

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.16
require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/google/brotli/go/cbrotli v0.0.0-20220110100810-f4153a09f87c
github.com/google/uuid v1.3.0
github.com/httprunner/hrp/plugin v0.0.0
github.com/jinzhu/copier v0.3.2

2
go.sum
View File

@@ -127,6 +127,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/brotli/go/cbrotli v0.0.0-20220110100810-f4153a09f87c h1:r47YgJ24CPvKxwxxHYPuE+FX1GgNtV93E7uaknKW0HU=
github.com/google/brotli/go/cbrotli v0.0.0-20220110100810-f4153a09f87c/go.mod h1:nOPhAkwVliJdNTkj3gXpljmWhjc4wCaVqbMJcPKWP4s=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=

View File

@@ -3,6 +3,8 @@ package hrp
import (
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
"crypto/tls"
_ "embed"
"encoding/json"
@@ -23,6 +25,7 @@ import (
"testing"
"time"
"github.com/google/brotli/go/cbrotli"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -758,6 +761,12 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
}
defer resp.Body.Close()
// decode response body in br/gzip/deflate formats
err = decodeResponseBody(resp)
if err != nil {
return stepResult, errors.Wrap(err, "decode response body failed")
}
// log & print response
if r.hrpRunner.debug {
fmt.Println("==================== response ===================")
@@ -798,6 +807,22 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
return stepResult, err
}
func decodeResponseBody(resp *http.Response) error {
switch resp.Header.Get("Content-Encoding") {
case "br":
resp.Body = cbrotli.NewReader(resp.Body)
case "gzip":
gr, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
resp.Body = gr
case "deflate":
resp.Body = flate.NewReader(resp.Body)
}
return nil
}
func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err error) {
stepResult = &stepData{
Name: step.Name,