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

Remove/disable stub when it was called one time #2600

Open
7robertodantas opened this issue Feb 2, 2024 · 1 comment
Open

Remove/disable stub when it was called one time #2600

7robertodantas opened this issue Feb 2, 2024 · 1 comment

Comments

@7robertodantas
Copy link

7robertodantas commented Feb 2, 2024

Proposal

One of the features that I miss in wiremock and it is available in mockserver is the ability to set the number of times a given stub mapping should be served, for instance, just once or n times.

This works like a countdown on the stub.

After reaching that number of requests served, it automatically removes the stub and further requests should return 404 Not Found, if there's no other stub matching.

I know there's a .reset() thing, or we can even manually remove the stub, but I believe it would be good to have this per stub.

References

Proposal

I was thinking something like

@Test
public void exactUrlOnly() {
    stubFor(get(urlEqualTo("/some/thing"))
            .times(1)
            .willReturn(aResponse()
                .withHeader("Content-Type", "text/plain")
                .withBody("Hello world!")));

    assertThat(testClient.get("/some/thing").statusCode(), is(200));
    assertThat(testClient.get("/some/thing").statusCode(), is(404)); // second request (no match)
    assertThat(testClient.get("/some/thing/else").statusCode(), is(404));
}

or even something like the below. (this is how mock-server would behave)

@Test
public void exactUrlOnly() {
    stubFor(get(urlEqualTo("/some/thing"))  // first stub
            .times(1)
            .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "text/plain")
                .withBody("Hello world!")));

    stubFor(get(urlEqualTo("/some/thing"))  // second stub
            .times(1)
            .willReturn(aResponse()
            .withStatus(409)));

    assertThat(testClient.get("/some/thing").statusCode(), is(200)); // first request (matches first stub)
    assertThat(testClient.get("/some/thing").statusCode(), is(409)); // second request (matches second stub)
    assertThat(testClient.get("/some/thing").statusCode(), is(404)); // third request (no match)
    assertThat(testClient.get("/some/thing/else").statusCode(), is(404));
}

I know that this is kinda possible to do by the Stateful stuff, however, in this case, we don't need to worry so much about the states, we can simply stub in order that must be served once each.


Example from mock-server that can be used as reference.

https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_path_exactly_twice

new MockServerClient("localhost", 1080)
    .when(
        request()
            .withPath("/some/path"),
        Times.once()
    )
    .respond(
        response()
            .withBody("some_response_body")
    );
@7robertodantas 7robertodantas changed the title Allow wiremock stub when it was called one or more times Remove/disable wiremock stub when it was called one time Feb 2, 2024
@7robertodantas 7robertodantas changed the title Remove/disable wiremock stub when it was called one time Remove/disable stub when it was called one time Feb 2, 2024
@tomakehurst
Copy link
Member

You can already achieve this with Scenarios e.g.

wireMockServer.stubFor(get("/once")
        .inScenario("once")
        .whenScenarioStateIs(Scenario.STARTED)
        .willSetStateTo("next")
        .willReturn(ok("Hello world!"))
);

wireMockServer.stubFor(get("/once")
        .inScenario("once")
        .whenScenarioStateIs("next")
        .willReturn(aResponse().withStatus(409))
);

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

No branches or pull requests

2 participants