I'm testing a Java REST client using Wiremock, specifically how it handles an empty response.
Code under test:
public Content makeRequest(...) {
try {
ResponseEntity response = restTemplate.exchange(...);
if(response.getStatusCode() != 200) {
throw new BadStatusCodeException(response.getStatusCode());
}
return extractContent(response);
} catch (IOException e) {
throw new ProtocolException(e);
}
}
Test:
Wiremock.stubFor(put(urlPathEqualTo("/foo"))
.willReturn(aResponse().withFault(EMPTY_RESPONSE)));
thrown.expect(ProtocolException.class);
client.makeRequest();
This test passes on my local machine, but on our build machine it fails, throwing a BadStatusException
with status code 500.
My best guess is that on my local machine, RestTemplate is making a direct connection to the Wiremock server, but that on the build machine the connection is going via a proxy -- perhaps because of an environment variable etc. -- and that the proxy is returning a 500 error when it encounters the fault simulated by WireMock.
Forcing my RestTemplate
to use Apache HttpClient, like this:
ClientHttpRequestFactory httpRequestFactory =
new HttpComponentsAsyncClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
... seems to fix the problem, but I don't really want to bring extra dependencies into this library (the consumer of this library will inject their own RestTemplate, configured however they like).
Is my theory plausible? How can I verify this is what's happening? How can I force RestTemplate to connect directly? What other explanations could there be?