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

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

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();
    }
}

(类 KeyMap 用于存储某些域密钥的位置,该域密钥在调用RestTemplate之前存储在ThreadLocal中 . )

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