Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: feature/xml systemout #720

Merged
merged 13 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions executors/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Result struct {
BodyJSON interface{} `json:"bodyjson,omitempty" yaml:"bodyjson,omitempty"`
Headers Headers `json:"headers,omitempty" yaml:"headers,omitempty"`
Err string `json:"err,omitempty" yaml:"err,omitempty"`
Systemout string `json:"systemout,omitempty" yaml:"systemout,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Systemout string `json:"systemout,omitempty" yaml:"systemout,omitempty"`
Systemout string `json:"-" yaml:"-"`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok with that @ivan-velasco ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to test this change out locally today @yesnault

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yesnault tested this finally today and it causes the xml report to exclude this information
When the property is set Systemout string json:"-" yaml:"-"

<system-out><![CDATA[

json report

"systemout": "\n",

When the property is set Systemout string json:"systemout,omitempty" yaml:"systemout,omitempty", then xml report shows up properly

<system-out><![CDATA[
PATCH-----http://

json report

"result.systemout": "PATCH-----http://
"systemout": "PATCH-----http://

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yesnault Any thoughts on this? Has been very helpful in identifying errors quickly.

}

type HTTPRequest struct {
Expand Down Expand Up @@ -100,7 +101,7 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
// dirty: mapstructure doesn't like decoding map[interface{}]interface{}, let's force manually
e.MultipartForm = step["multipart_form"]

r := Result{}
result := Result{}

workdir := venom.StringVarFromCtx(ctx, "venom.testsuite.workdir")

Expand Down Expand Up @@ -179,8 +180,8 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
}

cReq := req.Clone(ctx)
r.Request.Method = cReq.Method
r.Request.URL = req.URL.String()
result.Request.Method = cReq.Method
result.Request.URL = req.URL.String()
if cReq.Body != nil {
body, err := cReq.GetBody()
if err != nil {
Expand All @@ -191,64 +192,76 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
return nil, err
}
defer cReq.Body.Close()
r.Request.Body = string(btes)
result.Request.Body = string(btes)
}
r.Request.Header = cReq.Header
r.Request.Form = cReq.Form
r.Request.PostForm = cReq.PostForm
result.Request.Header = cReq.Header
result.Request.Form = cReq.Form
result.Request.PostForm = cReq.PostForm

start := time.Now()
resp, err := client.Do(req)
if err != nil {
return nil, err
result.Err = err.Error()
return result, err
}
elapsed := time.Since(start)
r.TimeSeconds = elapsed.Seconds()
result.TimeSeconds = elapsed.Seconds()

var bb []byte
if resp.Body != nil {
defer resp.Body.Close()

if !e.SkipBody && isBodySupported(resp) {
var errr error
bb, errr = io.ReadAll(resp.Body)
if errr != nil {
return nil, errr
var err error
bb, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
r.Body = string(bb)
result.Body = string(bb)

if isBodyJSONSupported(resp) {
var m interface{}
decoder := json.NewDecoder(strings.NewReader(string(bb)))
decoder.UseNumber()
if err := decoder.Decode(&m); err == nil {
r.BodyJSON = m
result.BodyJSON = m
}
}

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)
}
}
}

if !e.SkipHeaders {
r.Headers = make(map[string]string)
result.Headers = make(map[string]string)
for k, v := range resp.Header {
if strings.ToLower(k) == "set-cookie" {
r.Headers[k] = strings.Join(v, "; ")
result.Headers[k] = strings.Join(v, "; ")
} else {
r.Headers[k] = v[0]
result.Headers[k] = v[0]
}
}
}

requestContentType := r.Request.Header.Get("Content-Type")
requestContentType := result.Request.Header.Get("Content-Type")
// if PreserveBodyFile == true, the body is not interpolated.
// So, no need to keep it in request here (to re-inject it in vars)
// this will avoid to be interpolated after in vars too.
if e.PreserveBodyFile || !isContentTypeSupported(requestContentType) {
r.Request.Body = ""
result.Request.Body = ""
}

r.StatusCode = resp.StatusCode
return r, nil
result.StatusCode = resp.StatusCode
return result, nil
}

// getRequest returns the request correctly set for the current executor
Expand Down
17 changes: 13 additions & 4 deletions venom_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (v *Venom) OutputResult() error {
return errors.Wrapf(err, "Error: cannot format output yaml (%s)", err)
}
case "xml":
data, err = outputXMLFormat(*testsResult)
data, err = outputXMLFormat(*testsResult, v.Verbose)
if err != nil {
return errors.Wrapf(err, "Error: cannot format output xml (%s)", err)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func outputTapFormat(tests Tests) ([]byte, error) {
return buf.Bytes(), nil
}

func outputXMLFormat(tests Tests) ([]byte, error) {
func outputXMLFormat(tests Tests, verbose int) ([]byte, error) {
testsXML := TestsXML{}

for _, ts := range tests.TestSuites {
Expand Down Expand Up @@ -203,8 +203,12 @@ func outputXMLFormat(tests Tests) ([]byte, error) {
Value: failure.Value,
})
}
systemout.Value += result.Systemout
systemerr.Value += result.Systemerr
if len(result.Errors) > 0 {
appendCleanValue(&systemout.Value, result.Systemout)
} else if verbose > 1 {
appendCleanValue(&systemout.Value, result.Systemout)
}
appendCleanValue(&systemerr.Value, result.Systemerr)
}

tcXML := TestCaseXML{
Expand All @@ -230,3 +234,8 @@ func outputXMLFormat(tests Tests) ([]byte, error) {

return data, nil
}

func appendCleanValue(dest *string, source string) {
cleanedValue := strings.ReplaceAll(source, "\x03", "")
*dest += cleanedValue
}