javaspring-bootwiremockbraintree-sandbox

Wiremock stubs for an API client that has hardcoded urls


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?


Solution

  • 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