Wiremock代理存根问题

时间:2018-08-10 06:14:15

标签: wiremock

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();

3 个答案:

答案 0 :(得分:1)

如果您想使用WireMock充当http://example.com的代理,那么您会误解proxiedFrom的含义。它用于将本地Wiremock服务器收到的请求转发到另一个网站。例如,您设置了在localhost:8888上运行的wiremock。然后,您设置类似proxiedFrom("example.com")的东西。结果,发送到localhost:8888/rooms/{id}的所有内容都会转发到example.com/rooms/{id}

WireMock proxy Documentation

答案 1 :(得分:0)

WireMockServer wireMockServer =新的WireMockServer(wireMockConfig()。proxyVia(“ http://example.com”,79));

尝试proxyVia()... 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();

答案 2 :(得分:0)

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

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