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)
          )
      )
igalbenardete
  • 207
  • 2
  • 12

1 Answers1

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.

Leonardo Goes
  • 199
  • 1
  • 10