Wiremock:对相同 URL 和内容的多个响应?
Wiremock: Multiple responses for the same URL and content?
也在这里分享:https://github.com/tomakehurst/wiremock/issues/625
我正在编写一个集成测试来验证我与 REST API 交互的应用程序是否正确处理了不成功的请求。为此,我想模拟一个 GET 请求两次发送到 HTTP 端点的场景。第一次请求不成功,响应状态码为500;第二次,请求成功,响应状态码为 200。
考虑以下示例:
@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")));
}
有没有办法避免第二个 StubMapping 覆盖第一个 - 以确保第一次 doSomething()
发出请求时, 具有状态的响应返回代码 500,第二次 返回状态代码为 200 的不同响应?
这就是场景功能的用途。
您需要将两个存根放入场景中(即相同的场景名称),使第一个存根触发到新状态的转换,然后使第二个存根取决于处于第二状态的场景,并且第一个存根取决于场景处于 STARTED
状态。
使用场景功能,这样的事情很有帮助:
// 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>")));
也在这里分享:https://github.com/tomakehurst/wiremock/issues/625
我正在编写一个集成测试来验证我与 REST API 交互的应用程序是否正确处理了不成功的请求。为此,我想模拟一个 GET 请求两次发送到 HTTP 端点的场景。第一次请求不成功,响应状态码为500;第二次,请求成功,响应状态码为 200。
考虑以下示例:
@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")));
}
有没有办法避免第二个 StubMapping 覆盖第一个 - 以确保第一次 doSomething()
发出请求时, 具有状态的响应返回代码 500,第二次 返回状态代码为 200 的不同响应?
这就是场景功能的用途。
您需要将两个存根放入场景中(即相同的场景名称),使第一个存根触发到新状态的转换,然后使第二个存根取决于处于第二状态的场景,并且第一个存根取决于场景处于 STARTED
状态。
使用场景功能,这样的事情很有帮助:
// 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>")));