Skip to content

Commit

Permalink
feat: add request in HTTP executor result (#314)
Browse files Browse the repository at this point in the history
* feat: add request in HTTP executor result

Signed-off-by: francois  samin <francois.samin@corp.ovh.com>

* Update executors/http/http.go

Co-authored-by: Yvonnick Esnault <yvonnick.esnault@corp.ovh.com>
  • Loading branch information
fsamin and yesnault authored Nov 19, 2020
1 parent 5b5d65a commit cc86621
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
68 changes: 43 additions & 25 deletions executors/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ In your yaml file, you can use:

```yaml

name: Title of TestSuite
name: HTTP testsuite
testcases:

- name: GET http testcase
- name: get http testcase
steps:
- type: http
method: GET
Expand All @@ -37,56 +36,75 @@ testcases:
- result.body ShouldContainSubstring /dedicated/server
- result.body ShouldContainSubstring /ipLoadbalancing
- result.statuscode ShouldEqual 200
- result.bodyjson.api ShouldBeNil
- result.bodyjson.apis ShouldNotBeEmpty
- result.bodyjson.apis.apis0 ShouldNotBeNil
- result.bodyjson.apis.apis0.path ShouldEqual /allDom


- name: POST http with bodyFile
- name: post http multipart
steps:
- type: http
method: POST
url: https://eu.api.ovh.com/1.0/
bodyFile: /tmp/myfile.tmp
url: https://eu.api.ovh.com/1.0/auth/logout
multipart_form:
file: '@./venom.gif'
assertions:
- result.statuscode ShouldNotEqual 200

- result.statuscode ShouldEqual 401

- name: POST http with multipart
- name: post http enhanced assertions
steps:
- type: http
method: POST
url: https://eu.api.ovh.com/1.0/
multipart_form:
file: '@/tmp/myfile.tmp'
url: https://eu.api.ovh.com/1.0/newAccount/rules
assertions:
- result.statuscode ShouldNotEqual 200

- name: GET API health over Unix Socket
- result.statuscode ShouldEqual 200
- result.bodyjson.__type__ ShouldEqual Array
# Ensure a minimum of fields are present.
- result.bodyjson.__len__ ShouldBeGreaterThanOrEqualTo 8
# Ensure fields have the right keys.
- result.bodyjson.bodyjson0 ShouldContainKey fieldName
- result.bodyjson.bodyjson0 ShouldContainKey mandatory
- result.bodyjson.bodyjson0 ShouldContainKey regularExpression
- result.bodyjson.bodyjson0 ShouldContainKey prefix
- result.bodyjson.bodyjson0 ShouldContainKey examples
- result.bodyjson.bodyjson0 ShouldNotContainKey lol
- result.statuscode ShouldNotEqual {{.post-http-multipart.result.statuscode}}

- name: get http (with options)
steps:
- type: http
method: GET
unix_sock: /run/myapp/health.sock
url: http://unix/health
method: POST
url: https://eu.api.ovh.com/1.0
skip_body: true
skip_headers: true
info: request is {{.result.request.method}} {{.result.request.url}} {{.result.request.body}}
assertions:
- result.bodyjson.success ShouldBeTrue
- result.statuscode ShouldEqual 405
- result.body ShouldBeEmpty
- result.headers ShouldBeEmpty


```
*NB: to post a file with multipart_form, prefix the path to the file with '@'*

## Output

```
result.executor
result.request
result.timeseconds
result.timehuman
result.statuscode
result.body
result.bodyjson
result.headers
result.error
result.err
```
- result.timeseconds & result.timehuman: time of execution
- result.executor.executor.method: HTTP method used, example: GET
- result.executor.executor.url: url called
- result.executor.executor.multipartform: multipartform if exists
- result.request.method: HTTP method of the request
- result.request.url: HTTP URL of the request
- result.request.body: body content as string
- result.request.form: HTTP form map
- result.request.post_form: HTTP post form map
- result.err: if exists, this field contains error
- result.body: body of HTTP response
- result.bodyjson: body of HTTP response if it's a JSON. You can access json data as result.bodyjson.yourkey for example.
Expand Down
29 changes: 29 additions & 0 deletions executors/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,22 @@ type Result struct {
TimeSeconds float64 `json:"timeseconds,omitempty" yaml:"timeseconds,omitempty"`
TimeHuman string `json:"timehuman,omitempty" yaml:"timehuman,omitempty"`
StatusCode int `json:"statuscode,omitempty" yaml:"statuscode,omitempty"`
Request HTTPRequest `json:"request,omitempty" yaml:"request,omitempty"`
Body string `json:"body,omitempty" yaml:"body,omitempty"`
BodyJSON interface{} `json:"bodyjson,omitempty" yaml:"bodyjson,omitempty"`
Headers Headers `json:"headers,omitempty" yaml:"headers,omitempty"`
Err string `json:"err,omitempty" yaml:"err,omitempty"`
}

type HTTPRequest struct {
Method string `json:"method,omitempty"`
URL string `json:"url,omitempty"`
Header http.Header `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
Form url.Values `json:"form,omitempty"`
PostForm url.Values `json:"post_form,omitempty"`
}

// ZeroValueResult return an empty implemtation of this executor result
func (Executor) ZeroValueResult() interface{} {
return Result{}
Expand Down Expand Up @@ -125,6 +135,25 @@ func (Executor) Run(ctx context.Context, step venom.TestStep, workdir string) (i
}
}

cReq := req.Clone(ctx)
r.Request.Method = cReq.Method
r.Request.URL = req.URL.String()
if cReq.Body != nil {
body, err := cReq.GetBody()
if err != nil {
return nil, err
}
btes, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
defer cReq.Body.Close()
r.Request.Body = string(btes)
}
r.Request.Header = cReq.Header
r.Request.Form = cReq.Form
r.Request.PostForm = cReq.PostForm

start := time.Now()
resp, err := client.Do(req)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion process_teststep.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func (v *Venom) RunTestStep(ctx context.Context, e ExecutorRunner, ts *TestSuite
TestStep: step,
Variables: ts.Vars,
}
output, _ := json.MarshalIndent(fdump, "", " ")
output, err := json.MarshalIndent(fdump, "", " ")
if err != nil {
Error(ctx, "unable to marshal result: %v", err)
}

oDir := v.OutputDir
if oDir == "" {
Expand Down
5 changes: 3 additions & 2 deletions tests/http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ testcases:
assertions:
- result.statuscode ShouldEqual 401

- name: post http (enhanced assertions)
- name: post http enhanced assertions
steps:
- type: http
method: POST
Expand All @@ -54,7 +54,7 @@ testcases:
- result.bodyjson.bodyjson0 ShouldNotContainKey lol
- result.statuscode ShouldNotEqual {{.post-http-multipart.result.statuscode}}

- name: get http (with templated stuff)
- name: get http with templated variables
steps:
- type: http
method: POST
Expand All @@ -70,6 +70,7 @@ testcases:
url: https://eu.api.ovh.com/1.0
skip_body: true
skip_headers: true
info: request is {{.result.request.method}} {{.result.request.url}} {{.result.request.body}}
assertions:
- result.statuscode ShouldEqual 405
- result.body ShouldBeEmpty
Expand Down

0 comments on commit cc86621

Please sign in to comment.