Wiremock: Multiple responses for the same URL and content?

26,395

Solution 1

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/

Solution 2

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>")));
Share:
26,395

Related videos on Youtube

rugden
Author by

rugden

Updated on July 09, 2022

Comments

  • rugden
    rugden almost 2 years

    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?