我知道,它可以SocketTimeoutException通过使用来模拟withFixedDelay,但是怎么样ConnectionTimeoutException?
我在 Junit 中有这段代码,在那里我清楚地将端口设置为 8888
when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap()))
.thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods");
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON_VALUE)
.withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));
Run Code Online (Sandbox Code Playgroud)
但是当我运行测试时,我在这一行出现了这个错误:
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(..
Run Code Online (Sandbox Code Playgroud)
和错误:
wiremock.org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
Run Code Online (Sandbox Code Playgroud) 在大多数集成测试中,我使用 spring-boot-test(2.1.9.RELEASE) 和 spring-cloud-contract-wiremock(2.0.2.RELEASE)。测试基于 @AutoConfigureWireMock(port = 0) 启动 WireMock 服务器,因此我没有使用任何 WireMockRule 或其他配置设置。
有时验证失败并出现一个非常奇怪的错误:
com.github.tomakehurst.wiremock.client.VerificationException:` com.github.tomakehurst.wiremock.client.VerificationException:com.github.tomakehurst.wiremock.client.VerificationException:没有完全匹配的请求。最相似的请求是:预期:< POST /api/id/delete
但是:< POST /api/id/delete
正如您在上面看到的,预期端点与实际调用完全相同。
你有什么想法 ?或者你以前见过吗?这里有一个未解决的问题: https: //github.com/tomakehurst/wiremock/issues/706,但回复不是很有帮助。
是否可以将wiremock配置为在特定数量的先前响应之后对同一URL和同一请求发送不同的响应?
For example:
1st request -> 503 response
2nd request -> 503 response
3rd request -> 503 response
4th request -> 200 response
Run Code Online (Sandbox Code Playgroud)
这将模拟启动期间不可用的服务。当它启动并运行时,它会响应 200 状态。
我正在尝试为我的登录程序创建模拟.我使用POST方法与几个字段和登录对象(使用登录名,密码等)为此我使用JsonPath.代码如下:
{
"request": {
"method": "POST",
"url": "/login",
"bodyPatterns" : [
{"matchesJsonPath" : "$.method"},
{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
{"matchesJsonPath" : "$.params.login"},
{"matchesJsonPath" : "$.params.password"}
]
},
"response": {
"status": 200,
"bodyFileName": "login.json"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在检查clientVersion,因为它与示例类似.
我的问题是,用给定的POST JSON:
{
"method": "login",
"params": {
"clientVersion": "1",
"login": "test@test.com",
"password": "681819535da188b6ef2"
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了404.但是,当我改变时
{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
Run Code Online (Sandbox Code Playgroud)
正常
{"matchesJsonPath" : "$.params.clientVersion"},
Run Code Online (Sandbox Code Playgroud)
一切正常.
那么 - 如果给定的字段等于某个值,如何使用matchesJsonPath检查内部的wiremock?在我的情况下,如何将它应用于根域,就像方法一样?虽然我们在这里 - 我在检查值是否为空时遇到了类似的问题.我试图应用正则表达式等 - 没有运气.
也在这里分享: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")));
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免第二个StubMapping覆盖第一个 …
Wiremock文档指出,在withBodyFile中指定的文件位置应该在src / test / resources / __ files中。我想在src / test / resources / Testing_ABC / Testcase2 / myfile.xml中拥有文件。
有什么办法可以实现?我尝试了以下操作,但似乎不起作用!
stubFor(get(urlPathEqualTo("/abc")).willReturn
(aResponse().withHeader("Content-Type",
"text/xml; charset=utf-8").withHeader
("Content-Encoding",
"gzip")
.withBodyFile
("src/test/resources/Testing_ABC/Testcase2/myfile.xml)));
Run Code Online (Sandbox Code Playgroud)
但是,当我将文件放入src / test / resources / __ files / myfile.xml并相应地更改路径时,它可以正常工作。
我只是想知道是否可以使Wiremock在__files以外的资源的其他目录中查找,以便在项目中具有良好的资源结构。
我有一个Http Client,它在现实生活中使用代理将请求发送到API。我正在尝试使用WireMock运行我的http客户端测试并模拟API的响应。但是,我无法使Wiremock与代理设置一起使用。我已经尝试了所有相关组合,但仍然无法通过成功的测试。
我尝试了viaProxy配置,proxiedWith但是也不确定我是否正确使用它们。该文档也无济于事。
客户端代码具有以下配置:
private val httpsProxySettings: ConnectionPoolSettings =
ConnectionPoolSettings(actorSystem)
.withConnectionSettings(ClientConnectionSettings(actorSystem))
.withTransport(
ClientTransport.httpsProxy(
InetSocketAddress.createUnresolved(PROXY_HOST, PROXY_PORT)
)
)
Run Code Online (Sandbox Code Playgroud)
测试配置大致如下:
val wireMockServer = new WireMockServer(
wireMockConfig().port(API_PORT).proxyVia(PROXY_HOST, PROXY_PORT)
)
wireMockServer.start()
WireMock.configureFor("localhost", API_PORT)
wireMockServer.stubFor(
put(anyUrl())
.willReturn(
aResponse()
.withStatus(201)
// .proxiedFrom(API_HOST)
)
)
Run Code Online (Sandbox Code Playgroud) 我想了解 MockServerClient( 或 org.mockserver.integration.ClientAndServer) 和 WireMockServer 这两个框架之间的区别是什么?它们可以交换吗?我阅读了文档。但无法弄清楚这两者之间有什么区别?谢谢。
我想为以下 e2e 场景添加一个测试:
我的应用程序通过内部代理服务器向外部服务发出 Web 请求,代理服务器操作请求正文,将请求转发到目标主机并返回返回的响应。
举例来说,我对external.service/an/endpoint(通过my-proxy-server)正文做了一个发布请求
{
"card_number": "<proxy server pls fill the cc details>"
}
Run Code Online (Sandbox Code Playgroud)
代理服务器修改请求填写cc详细信息并转发给external.service/an/endpointwith body
{
"card_number": "372735466563005"
}
Run Code Online (Sandbox Code Playgroud)
external.service 返回状态 OK。代理服务器不加修改地返回响应。
如何使用wiremock 测试此工作流程?我可以WireMock.stubFor()为external.service,但我不知道如何使有线模拟代理与我的 Web 客户端的代理设置一起工作。查看测试,实际上,Rest Template 测试restTemplateWithWireMockAsProxyServer按预期工作,通过代理路由我的请求,但webClientWithWireMockAsProxyServer我的 RCA 出现错误:
20:06:59.165 [qtp105751207-24] DEBUG wiremock.org.eclipse.jetty.server.HttpChannel - REQUEST for //localhost:58978localhost:58978 on HttpChannelOverHttp@4a71ab50{r=1,c=false,c=false/false,a=IDLE,uri=//localhost:58978localhost:58978,age=0}
CONNECT //localhost:58978localhost:58978 HTTP/1.1
Host: localhost:58978
Run Code Online (Sandbox Code Playgroud)
正如这里提到的,这些通过 wiremock 代理的调用是不可能的。但是我所有的网址都像http://localhost:<port>,这意味着我没有进行任何 https 调用。
package com.spotnana.obt.supplier.services;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; …Run Code Online (Sandbox Code Playgroud) wiremock ×10
java ×6
spring-boot ×2
akka-http ×1
api ×1
http-proxy ×1
json ×1
jsonpath ×1
junit ×1
mockito ×1
mockserver ×1
proxy ×1
rest ×1
scala ×1
stubbing ×1
testing ×1
unit-testing ×1