Skip to content

Commit

Permalink
Add support for other responses assert contains
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Abouzied <ahmed.abouzied@adjust.com>
  • Loading branch information
ahmedaabouzied committed Nov 18, 2024
1 parent 5f3b771 commit 82de43a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 4 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
33 changes: 24 additions & 9 deletions local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,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 @@ -742,22 +743,36 @@ 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
}

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 {
ctx, rv, err := l.VS.Replace(ctx, []byte(bodyDoc))
if err != nil {
return err
}
return l.contains(ctx, received, bodyDoc)
})
})
}

s, substr := string(received), string(rv)
if !strings.Contains(s, substr) {
return augmentBodyErr(ctx, fmt.Errorf("%w %q in %q", errDoesNotContain, substr, s))
}
func (l *LocalClient) iShouldHaveOtherResponsesWithBodyThatContains(ctx context.Context, service, bodyDoc string) (context.Context, error) {
ctx = l.VS.PrepareContext(ctx)

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

0 comments on commit 82de43a

Please # to comment.