WireMock error while trying to mock an HttpClient with Proxy
Asked Answered
I

1

8

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)
          )
      )
Inattentive answered 16/7, 2019 at 17:5 Comment(1)
Do you have a minimal code repo?Narrate
C
1

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.

Couplet answered 25/7, 2019 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.