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)
)
)
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.
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.
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.
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:
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 changewireMockServer.stubFor(put(anyUrl())...)
to a wireMockServer.stubFor(post(anyUrl())...)
orwireMockServer.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With