Wiremock: Multiple responses for the same URL and content?
Asked Answered
C

3

55

Also shared here: https://github.com/tomakehurst/wiremock/issues/625

I'm writing an integration test to verify that my application that interacts with a REST API handles unsuccessful requests appropriately. To do this, I'm wanting to simulate a scenario where a GET requests is made twice to a HTTP endpoint. First time, request is not successful with a response status code of 500; second time, request is successful with a response status code of 200.

Consider the example below:

@Rule
public WireMockRule wireMockRule 
        = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());

@Test
public void testRetryScenario(){

    // First StubMapping
    stubFor(get(urlEqualTo("/my/resource"))
            .withHeader("Accept", equalTo("text/xml"))
            .willReturn(aResponse()
                .withStatus(500) // request unsuccessful with status code 500
                .withHeader("Content-Type", "text/xml")
                .withBody("<response>Some content</response>")));

    // Second StubMapping
    stubFor(get(urlEqualTo("/my/resource"))
            .withHeader("Accept", equalTo("text/xml"))
            .willReturn(aResponse()
                .withStatus(200)  // request successful with status code 200
                .withHeader("Content-Type", "text/xml")
                .withBody("<response>Some content</response>")));

    //Method under test that makes calls to endpoint
    doSomething();

    Thread.sleep(5000);

    //Verify GET request was made again after first attempt
    verify(exactly(2), getRequestedFor(urlEqualTo("/my/resource")));

}

Is there a way to avoid the second StubMapping from overriding the first -- to make sure that the first time doSomething() makes a request, a response with status code 500 is returned, and the second time, a different response with status code 200 is returned?

Casie answered 8/3, 2017 at 0:49 Comment(0)
D
61

This is what the Scenarios feature is for.

You'll need to put both stubs into a scenario (i.e. same scenario name), make the first stub trigger a transition to a new state, then make the second stub contingent on the scenario being in the second state and the first stub contingent on the scenario being in the STARTED state.

See: http://wiremock.org/docs/stateful-behaviour/

Disario answered 8/3, 2017 at 13:25 Comment(0)
C
50

Something like this helped, using the Scenarios feature:

// First StubMapping
stubFor(get(urlEqualTo("/my/resource"))
        .withHeader("Accept", equalTo("text/xml"))
        .inScenario("Retry Scenario")
        .whenScenarioStateIs(STARTED)
        .willReturn(aResponse()
            .withStatus(500) // request unsuccessful with status code 500
            .withHeader("Content-Type", "text/xml")
            .withBody("<response>Some content</response>"))
        .willSetStateTo("Cause Success")));

// Second StubMapping
stubFor(get(urlEqualTo("/my/resource"))
        .withHeader("Accept", equalTo("text/xml"))
        .inScenario("Retry Scenario")
        .whenScenarioStateIs("Cause Success")
        .willReturn(aResponse()
            .withStatus(200)  // request successful with status code 200
            .withHeader("Content-Type", "text/xml")
            .withBody("<response>Some content</response>")));
Casie answered 8/3, 2017 at 16:28 Comment(0)
W
0

Problem is scenarios does not work in wiremock-jetty12 in 3.6.0 (junit 5) it is no longer definable on stubFor(sth).withHeader but now is stubFor(post("someUrl").withHeader) and scenarios are not fired. Only last stub is taken.

Wanonah answered 13/6 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.