mardi 7 mars 2017

Wiremock: Multiple responses for the same URL and Content?

Also shared here: http://ift.tt/2mVgvkQ

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 2nd StubMapping from overriding the first -- to make ensure that the first time doSomethings() makes a request, a response with status code 500 is returned, and the second time, a different response with status code 200 is returned?

Aucun commentaire:

Enregistrer un commentaire