From 0423fc96e8dcbf55af78b982c380313670064d47 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 21 Feb 2022 21:18:31 +0800 Subject: [PATCH] fix: omit print request/response body for non-text content --- docs/CHANGELOG.md | 1 + runner.go | 76 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9ceb24fc..d4759487 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) diff --git a/runner.go b/runner.go index 1e826734..6676c48f 100644 --- a/runner.go +++ b/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":