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

Add support for body contains assertion #24

Merged
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
6 changes: 5 additions & 1 deletion _testdata/Dynamic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Feature: Dynamic data is used in steps
"created_at":"<ignore-diff>","updated_at": "<ignore-diff>"
}
"""
And I should have response with body, that contains
"""
"id":"$user_id"
"""

# Creating an order for that user with $user_id.
When I request HTTP endpoint with method "POST" and URI "/order/$user_id/?user_id=$user_id"
Expand All @@ -43,4 +47,4 @@ Feature: Dynamic data is used in steps
"user_id":"$user_id",
"prefixed_user": "static_prefix::$user"
}
"""
"""
9 changes: 9 additions & 0 deletions _testdata/LocalClient.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Feature: HTTP Service
And I should have other responses with body, that matches JSON paths
| $.status | "failed" |
| $.error | "foo" |
And I should have other responses with body, that contains
"""
"status":"failed"
"""

And I should have other responses with header "Content-Type: application/json"

Expand Down Expand Up @@ -128,4 +132,9 @@ Feature: HTTP Service
"""
And I should have "some-service" response with header "Content-Type: application/json"

And I should have "some-service" response with body, that contains
"""
some
"""


37 changes: 37 additions & 0 deletions local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@

s.Step(`^I should have(.*) response with body from file$`, l.iShouldHaveResponseWithBodyFromFile)
s.Step(`^I should have(.*) response with body$`, l.iShouldHaveResponseWithBody)
s.Step(`^I should have(.*) response with body, that contains$`, l.iShouldHaveResponseWithBodyThatContains)
s.Step(`^I should have(.*) response with body, that matches JSON from file$`, l.iShouldHaveResponseWithBodyThatMatchesJSONFromFile)
s.Step(`^I should have(.*) response with body, that matches JSON$`, l.iShouldHaveResponseWithBodyThatMatchesJSON)
s.Step(`^I should have(.*) response with body, that matches JSON paths$`, l.iShouldHaveResponseWithBodyThatMatchesJSONPaths)
Expand All @@ -220,6 +221,7 @@
s.Step(`^I should have(.*) other responses with header "([^"]*): ([^"]*)"$`, l.iShouldHaveOtherResponsesWithHeader)
s.Step(`^I should have(.*) other responses with headers$`, l.iShouldHaveOtherResponsesWithHeaders)
s.Step(`^I should have(.*) other responses with body$`, l.iShouldHaveOtherResponsesWithBody)
s.Step(`^I should have(.*) other responses with body, that contains$`, l.iShouldHaveOtherResponsesWithBodyThatContains)
s.Step(`^I should have(.*) other responses with body from file$`, l.iShouldHaveOtherResponsesWithBodyFromFile)
s.Step(`^I should have(.*) other responses with body, that matches JSON$`, l.iShouldHaveOtherResponsesWithBodyThatMatchesJSON)
s.Step(`^I should have(.*) other responses with body, that matches JSON from file$`, l.iShouldHaveOtherResponsesWithBodyThatMatchesJSONFromFile)
Expand Down Expand Up @@ -553,6 +555,7 @@
errUnexpectedExpectations = sentinelError("unexpected existing expectations")
errInvalidNumberOfColumns = sentinelError("invalid number of columns")
errUnexpectedBody = sentinelError("unexpected body")
errDoesNotContain = sentinelError("does not contain")
)

func statusCode(statusOrCode string) (int, error) {
Expand Down Expand Up @@ -740,6 +743,40 @@
})
}

func (l *LocalClient) contains(ctx context.Context, received []byte, bodyDoc string) error {
ctx, rv, err := l.VS.Replace(ctx, []byte(bodyDoc))
if err != nil {
return err
}

Check notice on line 750 in local_client.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 748:750 are not covered by tests.

s, substr := string(received), string(rv)
if !strings.Contains(s, substr) {
return augmentBodyErr(ctx, fmt.Errorf("%w %q in %q", errDoesNotContain, substr, s))
}

Check notice on line 755 in local_client.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 753:755 are not covered by tests.

return nil
}

func (l *LocalClient) iShouldHaveResponseWithBodyThatContains(ctx context.Context, service, bodyDoc string) (context.Context, error) {
ctx = l.VS.PrepareContext(ctx)

return l.expectResponse(ctx, service, func(c *httpmock.Client) error {
return c.ExpectResponseBodyCallback(func(received []byte) error {
return l.contains(ctx, received, bodyDoc)
})
})
}

func (l *LocalClient) iShouldHaveOtherResponsesWithBodyThatContains(ctx context.Context, service, bodyDoc string) (context.Context, error) {
ctx = l.VS.PrepareContext(ctx)

return l.expectResponse(ctx, service, func(c *httpmock.Client) error {
return c.ExpectOtherResponsesBodyCallback(func(received []byte) error {
return l.contains(ctx, received, bodyDoc)
})
})
}

func (l *LocalClient) iShouldHaveResponseWithBodyFromFile(ctx context.Context, service, filePath string) (context.Context, error) {
ctx = l.VS.PrepareContext(ctx)

Expand Down
Loading