Wiremock-通过转发代理进行记录/播放

时间:2019-02-06 13:15:06

标签: wiremock

我正在尝试使用Wiremock(目前为独立)来记录/播放对外部SaaS系统的所有请求。但是,我正在尝试通过公司网络来代理所有请求。

真正的请求是:

curl -v -k --proxy "mycompany.proxy:81111" "https://api.thirdparty.com/api/v1/soap?wsdl"

返回结果

从Wiremock文档中,我尝试了以下配置:

java -jar wiremock-standalone-2.21.0.jar --https-port 443 --record-mappings --proxy-all="https://api.thirdparty.com" --preserve-host-header --proxy-via="http://mycompany.proxy:81111" --verbose --print-all-network-traffic

但是,使用Wiremock时似乎无法获得相同的响应。

如果我尝试在HTTPS上打Wiremock ...

curl -v -k "https://localhost:443/api/v1/soap?wsdl"

* timeout on name lookup is not supported
*   Trying 127.0.0.1...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--   0 
* Connected to localhost (127.0.0.1) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: 
ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:/path/to/gitbash/ssl/certs/ca-bundle.crt  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
 } [5 bytes data]
 * TLSv1.2 (OUT), TLS handshake, Client hello (1):
 } [512 bytes data]
   0     0    0     0    0     0      0      0 --:--:--  0:00:30 --:--:--     
 0* Unknown SSL protocol error in connection to localhost:443
   0     0    0     0    0     0      0      0 --:--:--  0:00:30 --:--:--     0
 * Closing connection 0
 curl: (35) Unknown SSL protocol error in connection to localhost:443

和Wiremock控制台上。...

    2019-02-06 13:00:08.017 Problem decoding network traffic
java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(Unknown Source)
        at java.nio.charset.CharsetDecoder.decode(Unknown Source)
        at com.github.tomakehurst.wiremock.http.trafficlistener.ConsoleNotifyingWiremockNetworkTrafficListener.incoming(ConsoleNotifyingWiremockNetworkTrafficListener.java:40)
        at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer$NetworkTrafficListenerAdapter.incoming(JettyHttpServer.java:435)
        at wiremock.org.eclipse.jetty.io.NetworkTrafficSelectChannelEndPoint.notifyIncoming(NetworkTrafficSelectChannelEndPoint.java:125)
        at wiremock.org.eclipse.jetty.io.NetworkTrafficSelectChannelEndPoint.fill(NetworkTrafficSelectChannelEndPoint.java:48)
        at wiremock.org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.fill(SslConnection.java:516)
        at wiremock.org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:240)
        at wiremock.org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
        at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
        at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
        at java.lang.Thread.run(Unknown Source)

如果我尝试用http ...打打Wiremock ...

$ curl -v -k "http://localhost:8080/api/v1/soap?wsdl"
* timeout on name lookup is not supported
*   Trying 127.0.0.1...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0    * Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /api/v1/soap?wsdl HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.46.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Server: Jetty(9.2.z-SNAPSHOT)
<
{ [95 bytes data]
 100    84    0    84    0     0   2709      0 --:--:-- --:--:-- --:--:--  5600
No response could be served as there are no stub mappings in this WireMock instance.
* Connection #0 to host localhost left intact

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

根据Wiremock documentation,您可以通过编程方式设置为通过代理(而不是通过JSON)定位最终API。

  

通过另一个代理服务器进行代理

     

如果您位于仅允许HTTP流量发送到   通过不透明的代理连接到互联网,您可能希望设置代理映射   通过该服务器的那条路线。可以通过以下方式编程配置   将配置对象传递给WireMockServer的构造函数,或者   这样的JUnit规则:

WireMockServer wireMockServer = new WireMockServer(options()
    .proxyVia("proxy.mycorp.com", 8080)
);