Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WireMock error while trying to mock an HttpClient with Proxy

I have an Http Client which uses a proxy in real life to send a request to an API. I am trying to use WireMock to run my http client tests and mock the responses for the API. However, I could not manage to make Wiremock work with a proxy setup. I have tried all the relevant combinations and still couldn't manage to get a successful test.

I have tried viaProxy configuration and also proxiedWith but not sure if I am using them correctly. The documentation is not helping much either.

The client code has the following config:

private val httpsProxySettings: ConnectionPoolSettings =
    ConnectionPoolSettings(actorSystem)
      .withConnectionSettings(ClientConnectionSettings(actorSystem))
      .withTransport(
        ClientTransport.httpsProxy(
          InetSocketAddress.createUnresolved(PROXY_HOST, PROXY_PORT)
        )
      )

And the test configuration is along the lines of:

      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)
          )
      )
like image 968
igalbenardete Avatar asked Jul 16 '19 17:07

igalbenardete


People also ask

Does wiremock support HTTP CONNECT method?

Wiremock doesn't support HTTP CONNECT method. You can try Hoverfly as an alternative to Wiremock. There is a github issue if you're interested in details.

Why can't wiremock decrypt a HTTPS request?

Sorry, something went wrong. Unfortunately, the decrypt + re-encrypt approach is the only viable option for HTTPS, the reason being that WireMock needs to see the contents of the request in order to decide what to do with it.

What is proxying in wiremock?

Proxying WireMock has the ability to selectively proxy requests through to other hosts. This supports a proxy/intercept setup where requests are by default proxied to another (possibly real, live) service, but where specific stubs are configured these are returned in place of the remote service’s response.

Why does wiremock verify the target certificates when browser proxying?

When browser proxying, however, it is normal to proxy all traffic, often for the entire operating system. This would present a substantial security risk, so by default WireMock will verify the target certificates when browser proxying. You can trust specific hosts as follows:


1 Answers

You may try to changing to
val wireMockServer = new WireMockServer(options().proxyVia(PROXY_HOST,PROXY_PORT))
as stated here http://wiremock.org/docs/proxying/.

Are you testing with a put request? you may try to change
wireMockServer.stubFor(put(anyUrl())...)
to a
wireMockServer.stubFor(post(anyUrl())...)
or
wireMockServer.stubFor(get(anyUrl())...)

Are you using HTTPS? it may need aditional configuration.
Also, try adding a header with at least the content and a body compatible with content setting to the request and response both.

like image 121
Leonardo Goes Avatar answered Sep 20 '22 03:09

Leonardo Goes