mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +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
|
||||
- fix: incorrect data type when extracting data using jmespath
|
||||
- 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)
|
||||
|
||||
|
||||
76
runner.go
76
runner.go
@@ -743,13 +743,8 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
||||
req.Host = u.Host
|
||||
|
||||
// log & print request
|
||||
if r.hrpRunner.debug {
|
||||
reqDump, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return stepResult, errors.Wrap(err, "dump request failed")
|
||||
}
|
||||
fmt.Println("-------------------- request --------------------")
|
||||
fmt.Println(string(reqDump))
|
||||
if err := r.printRequest(req); err != nil {
|
||||
return stepResult, err
|
||||
}
|
||||
|
||||
// do request action
|
||||
@@ -768,14 +763,8 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
||||
}
|
||||
|
||||
// log & print response
|
||||
if r.hrpRunner.debug {
|
||||
fmt.Println("==================== response ===================")
|
||||
respDump, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return stepResult, errors.Wrap(err, "dump response failed")
|
||||
}
|
||||
fmt.Println(string(respDump))
|
||||
fmt.Println("--------------------------------------------------")
|
||||
if err := r.printResponse(resp); err != nil {
|
||||
return stepResult, err
|
||||
}
|
||||
|
||||
// new response object
|
||||
@@ -807,6 +796,63 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
||||
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 {
|
||||
switch resp.Header.Get("Content-Encoding") {
|
||||
case "br":
|
||||
|
||||
Reference in New Issue
Block a user