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

Or() and And() don't work correctly #145

Closed
Moon1706 opened this issue Jul 19, 2023 · 2 comments · Fixed by #146
Closed

Or() and And() don't work correctly #145

Moon1706 opened this issue Jul 19, 2023 · 2 comments · Fixed by #146

Comments

@Moon1706
Copy link

Hey! Today I've tried to make Matcher using Or:

httpmock.BodyContainsString(`"subject":"incorrect"`).Or(httpmock.BodyContainsString(`"scenario":"incorrect"`))

and figured out that in 100% of cases, the first matcher was working correctly, but the second one, which was called in Or() function, was ignored. I suppose there is an issue in this line: https://github.com/jarcoal/httpmock/blob/v1/match.go#L246 . Could you check, please?

maxatome added a commit that referenced this issue Jul 20, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
@maxatome
Copy link
Collaborator

Hi, good catch !

#146 should fix it.

Could you test and tell us, please?

maxatome added a commit that referenced this issue Jul 20, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
@maxatome
Copy link
Collaborator

Just as a note, do you know tdhttpmock? it allows you to match JSON content very easily, as it seems it is your case here:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.JSONBody(td.Any(
    td.SuperJSONOf(`{"subject":"incorrect"}`),
    td.SuperJSONOf(`{"scenario":"incorrect"}`),
  ))

See Any and SuperJSONOf doc.

Or if you don't care the JSON is correct, but using regexp:

tdhttpmock.Body(td.Re(`"(subject|scenario)":"incorrect"`))

See Re doc.

maxatome added a commit that referenced this issue Jul 24, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

At the same time, simplify the body copy and make the "rearming" more
performant.

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
maxatome added a commit that referenced this issue Jul 24, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

At the same time, simplify the body copy and make the "rearming" more
performant.

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
maxatome added a commit that referenced this issue Aug 16, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

At the same time, simplify the body copy and make the "rearming" more
performant.

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
maxatome added a commit that referenced this issue Aug 16, 2023
Both "rearm" the body before (re-)reading it, so they can be used
several times in a same matcher (combined with Or & And).

At the same time, simplify the body copy and make the "rearming" more
performant.

Closes #145.

Signed-off-by: Maxime Soulé <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants