mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 22:44:05 +08:00
fix: omit print request/response body for non-text content
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
- change: json unmarshal to json.Number when parsing data
|
- change: json unmarshal to json.Number when parsing data
|
||||||
- fix: incorrect data type when extracting data using jmespath
|
- fix: incorrect data type when extracting data using jmespath
|
||||||
- fix: decode response body in br/gzip/deflate formats
|
- fix: decode response body in br/gzip/deflate formats
|
||||||
|
- fix: omit print request/response body for non-text content
|
||||||
|
|
||||||
## v0.6.1 (2022-02-17)
|
## v0.6.1 (2022-02-17)
|
||||||
|
|
||||||
|
|||||||
76
runner.go
76
runner.go
@@ -743,13 +743,8 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
|||||||
req.Host = u.Host
|
req.Host = u.Host
|
||||||
|
|
||||||
// log & print request
|
// log & print request
|
||||||
if r.hrpRunner.debug {
|
if err := r.printRequest(req); err != nil {
|
||||||
reqDump, err := httputil.DumpRequest(req, true)
|
return stepResult, err
|
||||||
if err != nil {
|
|
||||||
return stepResult, errors.Wrap(err, "dump request failed")
|
|
||||||
}
|
|
||||||
fmt.Println("-------------------- request --------------------")
|
|
||||||
fmt.Println(string(reqDump))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// do request action
|
// do request action
|
||||||
@@ -768,14 +763,8 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// log & print response
|
// log & print response
|
||||||
if r.hrpRunner.debug {
|
if err := r.printResponse(resp); err != nil {
|
||||||
fmt.Println("==================== response ===================")
|
return stepResult, err
|
||||||
respDump, err := httputil.DumpResponse(resp, true)
|
|
||||||
if err != nil {
|
|
||||||
return stepResult, errors.Wrap(err, "dump response failed")
|
|
||||||
}
|
|
||||||
fmt.Println(string(respDump))
|
|
||||||
fmt.Println("--------------------------------------------------")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// new response object
|
// new response object
|
||||||
@@ -807,6 +796,63 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
|||||||
return stepResult, err
|
return stepResult, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *caseRunner) printRequest(req *http.Request) error {
|
||||||
|
if !r.hrpRunner.debug {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
reqContentType := req.Header.Get("Content-Type")
|
||||||
|
printBody := shouldPrintBody(reqContentType)
|
||||||
|
reqDump, err := httputil.DumpRequest(req, printBody)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "dump request failed")
|
||||||
|
}
|
||||||
|
fmt.Println("-------------------- request --------------------")
|
||||||
|
reqContent := string(reqDump)
|
||||||
|
if req.Body != nil && !printBody {
|
||||||
|
reqContent += fmt.Sprintf("(request body omitted for Content-Type: %v)", reqContentType)
|
||||||
|
}
|
||||||
|
fmt.Println(reqContent)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *caseRunner) printResponse(resp *http.Response) error {
|
||||||
|
if !r.hrpRunner.debug {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Println("==================== response ===================")
|
||||||
|
respContentType := resp.Header.Get("Content-Type")
|
||||||
|
printBody := shouldPrintBody(respContentType)
|
||||||
|
respDump, err := httputil.DumpResponse(resp, printBody)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "dump response failed")
|
||||||
|
}
|
||||||
|
respContent := string(respDump)
|
||||||
|
if !printBody {
|
||||||
|
respContent += fmt.Sprintf("(response body omitted for Content-Type: %v)", respContentType)
|
||||||
|
}
|
||||||
|
fmt.Println(respContent)
|
||||||
|
fmt.Println("--------------------------------------------------")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// shouldPrintBody return true if the Content-Type is printable
|
||||||
|
// including text/*, application/json, application/xml, application/www-form-urlencoded
|
||||||
|
func shouldPrintBody(contentType string) bool {
|
||||||
|
if strings.HasPrefix(contentType, "text/") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(contentType, "application/json") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(contentType, "application/xml") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(contentType, "application/www-form-urlencoded") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func decodeResponseBody(resp *http.Response) error {
|
func decodeResponseBody(resp *http.Response) error {
|
||||||
switch resp.Header.Get("Content-Encoding") {
|
switch resp.Header.Get("Content-Encoding") {
|
||||||
case "br":
|
case "br":
|
||||||
|
|||||||
Reference in New Issue
Block a user