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

feat: add the possibility to check if the body matches many expected strings #21

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func main() {
os.Exit(2)
}

prepareResponseBody(&t)

url := opts.URL
if opts.Port != 0 {
url = fmt.Sprintf("%s:%d", url, opts.Port)
Expand Down Expand Up @@ -81,3 +83,14 @@ func unmarshal(filename string, in []byte, out interface{}) error {

return unmarshalError
}

// In order to keep retro compatibility with ExpectedResponseBody, and at the same time introducing multiple response check with ExpectedResponse
func prepareResponseBody(t *tester.Test) {
if t != nil && t.Contracts != nil && len(t.Contracts) > 0 {
for _, c := range t.Contracts {
if c.ExpectedResponseBody != "" {
c.ExpectedResponses = append(c.ExpectedResponses, c.ExpectedResponseBody)
}
}
}
}
16 changes: 16 additions & 0 deletions smoke_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@
"method": "GET",

"response_headers_is": {"My-Header": "found", "My-Header2": "foundToo"}
},
{
"name": "httpbin_get_body_multiple",
"path": "/get?foo=hello!",
"method": "GET",


"response_contains": ["hello", "foo", "!"]
},
{
"name": "httpbin_get_response_multiple",
"path": "/get?foo=hello!",
"method": "GET",

"response_body_contains": "hello",
"response_contains": ["foo", "!"]
}
]
}
19 changes: 19 additions & 0 deletions smoke_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,22 @@ contracts:
response_headers_is:
My-Header: "found"
My-Header2: "foundToo"

- name: httpbin_get_body_multiple
path: "/get?foo=hello!"
method: GET

response_body_contains: hello
response_contains:
- foo
- !

- name: httpbin_get_response_multiple
path: "/get?foo=hello!"
method: GET

response_contains:
- hello
- foo
- !

32 changes: 17 additions & 15 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ type Contract struct {

ExpectedHTTPCode int `json:"http_code_is" yaml:"http_code_is"`
ExpectedResponseBody string `json:"response_body_contains" yaml:"response_body_contains"`
ExpectedResponses []string `json:"response_contains" yaml:"response_contains"`
ExpectedHeaders map[string]string `json:"response_headers_is" yaml:"response_headers_is"`
}


// Test represents the data for a full test suite
type Test struct {
Globals map[string]string `json:"globals" yaml:"globals"`
Expand Down Expand Up @@ -141,7 +143,7 @@ func (runner *Runner) validateContract(contract Contract) (err error) {
}
}

if contract.ExpectedResponseBody != "" || (contract.Outputs != nil && len(contract.Outputs) > 0) {
if len(contract.ExpectedResponses) > 0 || (contract.Outputs != nil && len(contract.Outputs) > 0) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
Expand Down Expand Up @@ -201,25 +203,25 @@ func validateHTTPCode(contract Contract, resp *http.Response) error {
}

func validateResponseBody(contract Contract, body []byte) error {
if contract.ExpectedResponseBody == "" {
if len(contract.ExpectedResponses) == 0 {
return nil
}

if strings.HasPrefix(contract.ExpectedResponseBody, "r/") {
expectedRegexp := contract.ExpectedResponseBody[2:]
re, err := regexp.Compile(expectedRegexp)
if err == nil {
if !re.Match(body) {
return fmt.Errorf("regular expression did not find any matches in the response body")

for _, r := range contract.ExpectedResponses {
// check if it is a regexp
if strings.HasPrefix(r, "r/") {
expectedRegexp := r[2:]
re, err := regexp.Compile(expectedRegexp)
if err == nil {
if !re.Match(body) {
return fmt.Errorf("regular expression did not find any matches in the response body")
}
}
return nil
} else if !bytes.Contains(body, []byte(r)) {
return fmt.Errorf("expected response not found in the body")
}
}

if !bytes.Contains(body, []byte(contract.ExpectedResponseBody)) {
return fmt.Errorf("expected response not found in the body")
}


return nil
}

Expand Down