Skip to content

Commit

Permalink
Add support for body contains assertion (#24)
Browse files Browse the repository at this point in the history
* Add support for body contains assertion

Signed-off-by: Ahmed Abouzied <ahmed.abouzied@adjust.com>

* Add support for other responses assert contains

Signed-off-by: Ahmed Abouzied <ahmed.abouzied@adjust.com>

---------

Signed-off-by: Ahmed Abouzied <ahmed.abouzied@adjust.com>
  • Loading branch information
ahmedaabouzied authored Nov 18, 2024
1 parent 1d87b3d commit 1a6bc21
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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 @@ func (l *LocalClient) RegisterSteps(s *godog.ScenarioContext) {

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 @@ func (l *LocalClient) RegisterSteps(s *godog.ScenarioContext) {
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 @@ const (
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) iShouldHaveResponseWithBody(ctx context.Context, service,
})
}

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
}

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

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

0 comments on commit 1a6bc21

Please # to comment.