Home > other >  Wiremock stubs for an API client that has hardcoded urls
Wiremock stubs for an API client that has hardcoded urls

Time:09-22

I am trying to test a small service with wiremock tests.

I am fine with stubbing out most of the third party service URLs however I run into problems when using client libraries that have their sanbox URL hardcoded.

The current example is for Braintree.
I want to stub a return for a call to "https://api.sandbox.braintreegateway.com:443" I cannot override that value in the test properties.

(Ref for the hardcoding https://github.com/braintree/braintree_java/blob/9f619bb0dd019921caed1f344046078469fbf1f8/src/main/java/com/braintreegateway/Environment.java)

Is there a way I can configure wiremock to be aware of calls to https://api.sandbox.braintreegateway.com: and return a stubbed response?

CodePudding user response:

I have never used Braintree SDK, but from what I see you use it by providing the Environment to the BraintreeGateway object: https://github.com/braintree/braintree_java/tree/master#quick-start-example

I would create a dedicated test Environment instance containing Wiremock URLs and provide it to the BraintreeGateway in my integration tests.

CodePudding user response:

Assuming the HTTP client in the Braintree lib respects Java's proxy setting system properties, you can configure WireMock to be a forward proxy (or browser proxy as WireMock's docs call it), allow it to intercept calls to any external domain.

This doc explains how to do this using the new proxy configurer utility class: http://wiremock.org/docs/multi-domain-mocking/.

Essentially you'd want to do something like this:

JvmProxyConfigurer.configureFor(wireMockServer);

 wireMockServer.stubFor(get("/stuff")
    .withHost(equalTo("api.sandbox.braintreegateway.com"))
    .willReturn(okJson("{ ... }")));

// Test something that uses the Braintree client lib
  • Related