线模拟代理存根问题



Wiremock 代理存根在主机 http://example.com 的端口 1234 上运行,但 httpURLConnection.getResponseCode(( 给出连接被拒绝错误。

wireMockServer.stubFor(get(urlMatching(".*"))              
.willReturn(aResponse().proxiedFrom("http://example.com:1234")));


ConnectionFactory conFac = new ConnectionFactory("http://example.com", 1234);
HttpURLConnection httpURLConnection = conFac.getHttpURLConnection(new URL(new URI("http", null, "http://example.com", 1234, null, null, null).toString()));
httpURLConnection.getResponseCode();

如果你想使用WireMock作为http://example.com的代理,那么你就错过了proxiedFrom代表什么。它用于将本地 wiremock 服务器收到的请求转发到其他网站。例如,您设置在localhost:8888上运行的 wiremock。然后,您设置类似proxiedFrom("example.com").因此,发送到localhost:8888/rooms/{id}的内容将转发给example.com/rooms/{id}

WireMock 代理文档

WireMockServer wireMockServer= new WireMockServer(wireMockConfig((.proxyVia("http://example.com", 79((;

已尝试代理通过((... httpURLConnection.getResponseCode((;给出 java.net.ConnectException: 连接被拒绝

ConnectionFactory conFac = new ConnectionFactory("http://example.com", 1234);
HttpURLConnection httpURLConnection = conFac.getHttpURLConnection(new URL(new URI("http", null, "http://example.com", 1234, null, null, null).toString()));
httpURLConnection.getResponseCode();

它是 1234 而不是 79。仍然抛出ConectException。

WireMockServer wireMockServer = new WireMockServer(wireMockConfig().proxyVia("http://example.com", 1234));

最新更新