Java 使用wiremock,我可以返回依赖于发布请求的正文吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18581520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using wiremock, can I return a body that is dependent on the post request
提问by Alexandre H. Tremblay
I am trying to test an openid provider class. The openid consumer class is making an http request. I am mocking the response to this request using wiremock. I am trying to mock a valid openid response. However, the valid response depends on the request parameters. Using wiremock, can I set up a mock request where the body of the response is dependent on the request parameters?
我正在尝试测试 openid 提供程序类。openid 消费者类正在发出 http 请求。我正在使用 wiremock 嘲笑对这个请求的响应。我正在尝试模拟有效的 openid 响应。但是,有效响应取决于请求参数。使用wiremock,我可以设置一个模拟请求,其中响应的主体取决于请求参数吗?
回答by dkatzel
I've never used wiremock. But according to their online documentationyou can write a mock that matches URL and Request body parameters. So you should be able to return different mocks depending on the parameters in either the URL itself or embedded in the request body.
我从未使用过wiremock。但是根据他们的在线文档,您可以编写一个匹配 URL 和请求正文参数的模拟。因此,您应该能够根据 URL 本身或嵌入在请求正文中的参数返回不同的模拟。
回答by user2944810
As far as I know and my experience with WireMock, no.
据我所知和我使用 WireMock 的经验,没有。
You can't parameterize a response with arguments passed through request. The best you can do is use matchers to make your mocked server respond accordingly.
您不能使用通过请求传递的参数来参数化响应。你能做的最好的事情就是使用匹配器让你的模拟服务器做出相应的响应。
I would recommend you making some unit or integration tests with plain jUnit in order to test requests/responses in such cases. They should be quicker if you want to test that receipt requests are responding correctly. I see WireMock as an alternative to do acceptance test, to ensure that your interface with other REST services are not getting broken.
我建议您使用普通 jUnit 进行一些单元或集成测试,以便在这种情况下测试请求/响应。如果您想测试收据请求是否正确响应,它们应该更快。我将 WireMock 视为进行验收测试的替代方案,以确保您与其他 REST 服务的接口不会被破坏。
回答by Cyberwiz
I've investigated this a fair bit and the answer is no (as of today, anyway).
我已经对此进行了相当多的调查,答案是否定的(截至今天,无论如何)。
You need to set up a specific (static) response for each request that requires a unique response.
您需要为每个需要唯一响应的请求设置一个特定的(静态)响应。
回答by PiersyP
This is possible, you just have to make use of a ResponseTansformer. In the below example code the responseDefinition is determined by the stubbing given below. Here I mock an encoding service by simply returning the body bytes back to the caller. Although in the transformer I am free to return whatever I like based on the contents of the request.
这是可能的,您只需要使用 ResponseTansformer。在下面的示例代码中, responseDefinition 由下面给出的存根决定。在这里,我通过简单地将正文字节返回给调用者来模拟编码服务。尽管在转换器中,我可以根据请求的内容自由返回我喜欢的任何内容。
int port = 8080;
WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().port(port).extensions(new ResponseTransformer() {
@Override
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files) {
return new ResponseDefinitionBuilder().like(responseDefinition)
.withBody(request.getBodyAsString().getBytes())
.build();
}
@Override
public String name() {
return "request body returning request transformer";
}
}));
wireMockServer.start();
WireMock.configureFor("localhost", port);
stubFor(post(urlEqualTo("/encode"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/octet-stream")
.withStatus(200)));
stubFor(post(urlEqualTo("/decode"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/octet-stream")
.withStatus(200)));
回答by Hung Tran
Wiremock supports extensions that you can write yourself that act as a middleware used to intercept the request and response bodies so you can format it however you like. It's very flexible and allows you to make up new response bodies dynamically or even no response at all.
Wiremock 支持您可以自己编写的扩展,这些扩展充当用于拦截请求和响应主体的中间件,因此您可以根据需要对其进行格式化。它非常灵活,允许您动态地组成新的响应主体,甚至根本没有响应。
As an example, we wrote an extension for this at Opentable and open sourced it on Maven Central. It allows you treat the json attributes as variables and interpolate them into your response body. Check it out. Let us know how it goes or if you have any questions. https://github.com/opentable/wiremock-body-transformer
例如,我们在 Opentable 上为此编写了一个扩展,并在 Maven Central 上将其开源。它允许您将 json 属性视为变量并将它们插入到您的响应正文中。一探究竟。如果您有任何疑问,请告诉我们进展如何。 https://github.com/opentable/wiremock-body-transformer