【问题标题】:Enabling Cookies and Redirects in TestRestTemplate在 TestRestTemplate 中启用 Cookie 和重定向
【发布时间】:2017-10-17 05:03:27
【问题描述】:

如何将 TestRestTemplate.HttpClientOption.ENABLE_REDIRECTSTestRestTemplate.HttpClientOption.ENABLE_COOKIES 添加到 spring-boots TestRestTemplate?

我正在使用 spring-boot,所以自动为我配置了 TestRestTemplate。

我可以在创建之前使用RestTemplateBuilder 自定义这个bean。问题是我看不到如何添加这些选项:

    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
        return new RestTemplateBuilder()
                .errorHandler(new ResponseErrorHandler() {
                    ...
                });
    } 

documentation 有一些接受这些选项的构造函数,但问题是已经为我创建了 bean。

【问题讨论】:

    标签: spring spring-boot spring-test


    【解决方案1】:

    您可以创建一个TestRestTemplate 并使用@Bean 注释将其呈现给Spring。

    例如:

    @Bean
    @Primary
    public TestRestTemplate testRestTemplate() {
        RestTemplate restTemplate = new RestTemplateBuilder()
                .errorHandler(new ResponseErrorHandler() {
                    @Override
                    public boolean hasError(ClientHttpResponse response) throws IOException {
                        return false;
                    }
    
                    @Override
                    public void handleError(ClientHttpResponse response) throws IOException {
    
                    }
                }).build();
    
        return new TestRestTemplate(restTemplate, user, password, TestRestTemplate.HttpClientOption.ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES);
    }
    

    或者,如果您不需要自定义 RestTemplate,则使用以下构造函数(在内部为您实例化 RestTemplate):

    @Bean
    @Primary
    public TestRestTemplate testRestTemplate() {
        return new TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES);
    }
    

    更新 1 以解决此评论:

    当我运行测试时,我现在收到以下错误 org.apache.http.ProtocolException: Target host is not specified

    Spring 提供的TestRestTemplate 配置为解析相对于http://localhost:${local.server.port} 的路径。因此,当您将 Spring 提供的实例替换为您自己的实例时,您要么必须提供完整地址(包括主机和端口),要么使用 LocalHostUriTemplateHandler 配置您自己的 TestRestTemplate(您可以在 @987654332 中看到此代码@)。以下是后一种方法的示例:

    @Bean
    @Primary
    public TestRestTemplate testRestTemplate(ApplicationContext applicationContext) {
        RestTemplate restTemplate = new RestTemplateBuilder()
                .errorHandler(new ResponseErrorHandler() {
                    @Override
                    public boolean hasError(ClientHttpResponse response) throws IOException {
                        return false;
                    }
    
                    @Override
                    public void handleError(ClientHttpResponse response) throws IOException {
    
                    }
                }).build();
    
        TestRestTemplate testRestTemplate =
                new TestRestTemplate(restTemplate, user, password, TestRestTemplate.HttpClientOption
                        .ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES);
    
        // let this testRestTemplate resolve paths relative to http://localhost:${local.server.port}
        LocalHostUriTemplateHandler handler =
                new LocalHostUriTemplateHandler(applicationContext.getEnvironment(), "http");
        testRestTemplate.setUriTemplateHandler(handler);
    
        return testRestTemplate;
    }
    

    通过这个 bean 配置,下面的测试用例使用自定义的 TestRestTemplate 并成功地调用了本地主机上的 Spring Boot 应用程序:

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class RestTemplateTest {
    
        @Autowired
        private TestRestTemplate restTemplate;
    
        @Test
        public void test() {
            ResponseEntity<String> forEntity = this.restTemplate.getForEntity("/some/endpoint", String.class);
            System.out.println(forEntity.getBody());
        }
    }
    

    【讨论】:

    • 这会导致更多问题。首先,我需要将@Primary 添加到testRestTemplate(),否则注册了两个bean。其次,当我运行测试时,我现在收到以下错误org.apache.http.ProtocolException: Target host is not specified。我想 spring-boot 在创建 TestRestTemplate 时会发挥更大的作用。
    • @jax 道歉,我已经更新了问题以阐明如何使用自定义的 TestRestTemplate 以及如果您想以与使用 Spring 提供的 @987654340 相同的方式使用它,如何配置它@.
    • 我不完全明白我需要将带有'@Bean'和'@Primary'注释的自定义TestRestTemplate放在哪个文件中?它应该去我的测试课吗?
    猜你喜欢
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多