From ceb53b7a56c6ad79f7833ed85735b648d5913f45 Mon Sep 17 00:00:00 2001 From: Ivan Velasco Date: Thu, 22 Feb 2024 19:26:34 -0600 Subject: [PATCH] http: result info new format and consolidated routine into one function Signed-off-by: Ivan Velasco --- executors/http/http.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/executors/http/http.go b/executors/http/http.go index cea5aafa..afbf02a0 100644 --- a/executors/http/http.go +++ b/executors/http/http.go @@ -228,16 +228,7 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro } } - if result.BodyJSON != nil { - // Convert the map to a JSON string - jsonString, err := json.Marshal(result.BodyJSON) - if err != nil { - return nil, err - } - result.Systemout = fmt.Sprintf("%s-----%s----->%s ", resp.Request.Method, resp.Request.URL, string(jsonString)) - } else { - result.Systemout = fmt.Sprintf("%s-----%s----->%s ", resp.Request.Method, resp.Request.URL, result.Body) - } + result.Systemout = buildResultInfo(&result, resp) } } @@ -488,3 +479,25 @@ func (e Executor) TLSOptions(ctx context.Context) ([]func(*http.Transport) error return opts, nil } + +func buildResultInfo(result *Result, resp *http.Response) string { + var body string + if result.BodyJSON != nil { + jsonString, err := json.Marshal(result.BodyJSON) + if err != nil { + return fmt.Sprintf("Error marshaling JSON: %s", err.Error()) + } + body = string(jsonString) + } else { + body = result.Body + } + + return fmt.Sprintf(`===== Result Info ===== + Method: %s + URL: %s + Body: %s + ======================`, + resp.Request.Method, + resp.Request.URL, + body) +}