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

Multiple mapping with anyBody depends on orders declaration #298

Closed
silk-bahamut opened this issue Feb 26, 2024 · 5 comments
Closed

Multiple mapping with anyBody depends on orders declaration #298

silk-bahamut opened this issue Feb 26, 2024 · 5 comments
Labels

Comments

@silk-bahamut
Copy link

Description of the bug

The order of declaration of simulation depends on order even when using anyBody() method

Steps to reproduce the issue

Here are two tests


  @Test
  void shouldNotFail(Hoverfly hoverfly) {
    hoverfly.simulate(SimulationSource.dsl(
        HoverflyDsl.service("https://test.com")
            .post("/sameUrl")
            .body(HoverflyMatchers.matchesPartialJson(HttpBodyConverter.jsonWithSingleQuotes("{'attr':'value'}")))
            .willReturn(ResponseCreators.success())
            .post("/sameUrl")
            .anyBody()
            .willReturn(ResponseCreators.badRequest())
    ));
    Assertions.assertThat(new RestTemplate().exchange("https://test.com/sameUrl", HttpMethod.POST, new HttpEntity<>("""
            {
              "test": 1,
              "attr": "value",
              "list": [ "A", "B" ]
            }
            """), String.class))
        .isNotNull();
  }

  @Test
  void orderShouldNotMatter(Hoverfly hoverfly) {
    hoverfly.simulate(SimulationSource.dsl(
        HoverflyDsl.service("https://test.com")
            .post("/sameUrl")
            .anyBody()
            .willReturn(ResponseCreators.badRequest())
            .post("/sameUrl")
            .body(HoverflyMatchers.matchesPartialJson(HttpBodyConverter.jsonWithSingleQuotes("{'attr':'value'}")))
            .willReturn(ResponseCreators.success())
    ));
    Assertions.assertThat(new RestTemplate().exchange("https://test.com/sameUrl", HttpMethod.POST, new HttpEntity<>("""
            {
              "test": 1,
              "attr": "value",
              "list": [ "A", "B" ]
            }
            """), String.class))
        .isNotNull();
  }

Observed result

The order is critical to the good execution of tests when it should not.

Expected result

Both test should work the same way.

Additional relevant information

If not indicated above:

  1. Hoverfly Java version: 0.16.1
@tommysitu
Copy link
Member

It looks like a bug in the matching logic - the absent of body matcher and a non-exact matcher resolves to the same score, so hoverfly will just take the first one, giving you the illusion that order plays a part here.

@tommysitu tommysitu added the bug label Feb 27, 2024
@silk-bahamut
Copy link
Author

It was my though too
In case of similar score, the order plays a role and the last one wins it all?

@tommysitu
Copy link
Member

It was my though too

In case of similar score, the order plays a role and the last one wins it all?

That's right.

@tommysitu
Copy link
Member

I've raised a PR to fix this, thanks for your report!

@tommysitu
Copy link
Member

fixed in v0.17.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants