mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 04:22:30 +08:00
fix #100: decode response body in br/gzip/deflate formats
This commit is contained in:
25
runner.go
25
runner.go
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user