使用自定义HttpClient测试RestTemplate

时间:2017-06-12 11:32:01

标签: java spring testing spring-boot

我正在开发一个Spring Boot应用程序,它调用一个REST-API,它经常执行303 See Other redirect到正确的位置。

对于给定的资源,我从一个随机的初始URL开始,拦截重定向以存储下一个请求的正确位置,最后执行重定向。

Errors : **fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in fileName** 

 **fsockopen(): Failed to enable crypto in finleName**

**fsockopen(): unable to connect to ssl://domain:443 (Unknown error) in /**

(类import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.ProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultRedirectStrategy; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration class RestTemplateFactory { private static final Logger LOG = LoggerFactory.getLogger(RestTemplateFactory.class); @Autowired KeyMap keyMap; @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { HttpClient httpClient = HttpClientBuilder .create() .setRedirectStrategy(new DefaultRedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { String redirectURL = response.getFirstHeader("Location").getValue(); LOG.debug("Intercepted redirect: original={}, redirect={}", request.getRequestLine(), redirectURL); keyMap.put(redirectURL); return true; } return false; } }) .build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); return builder .requestFactory(requestFactory) .build(); } } 用于存储某些域密钥的位置,该密钥在调用RestTemplate之前存储在ThreadLocal中。)

问题:如何测试这个特殊的RestTemplate?

0 个答案:

没有答案