自定义 WebTestClient 以添加默认标头

问题描述 投票:0回答:2

我在 2.7.x 版本中开发了一个 Spring Boot 应用程序。对于集成测试,我正在尝试自定义 WebTestClient 以添加默认标头。

我已经试过了,但是请求中没有添加标题:

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass1{

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void test1() throws Exception {
        webTestClient.get()
                .uri("/foobar/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(JsonArray.class);
   }
}

配置类:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClient client() {
        return WebTestClient.bindToServer()
                .responseTimeout(Duration.ofMinutes(2))
                .defaultHeader(API_KEY_HEADER, API_KEY_VALUE)
                .exchangeStrategies(ExchangeStrategies.withDefaults())
                .build();
    }
}

更新:

我还尝试在“WebTestClientConfig”类中使用WebTestClientBuilderCustomizer,并删除了“client()”方法,但问题仍然存在。看这个例子:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClientBuilderCustomizer webTestClientBuilderCustomizer() {
        return (builder) -> builder.defaultHeader(API_KEY_HEADER, API_KEY_VALUE);
    }

}

关于如何实现我的目标有什么想法吗?谢谢

spring-boot spring-boot-test
2个回答
0
投票

可以按如下方式进行。本质上,您必须为已构建的 * 删除 @Autowired WebTestClient。为了方便起见,我使用 Authorization Bearer header 进行了测试。

import org.json.JSONArray;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass1 {

    WebTestClient.Builder builder = WebTestClient.bindToWebHandler(exchange -> exchange.getResponse().setComplete())
            .configureClient().defaultHeader("Authorization", "Bearer something");
    WebTestClient webTestClient = builder.build();

    @Test
    public void test1() throws Exception {
        webTestClient.get().uri("/foobar").exchange().expectStatus().isOk().expectBody(JSONArray.class);
    }
}

如果您喜欢@Autowired,请创建一个@Bean,其名称返回WebTestClient,并使用Builder 设置的默认标头,并使用@Qualifier 来获取它。否则你会得到 dupl bean defn 错误。


0
投票

在@samabcde 的帮助下,我将代码更改为此解决方案:

测试班:

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(WebTestClientConfig.class) // <---- !added!
public class TestClass1{

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void test1() throws Exception {
        webTestClient.get()
                .uri("/foobar/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(JsonArray.class);
   }
}

配置类:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClientBuilderCustomizer webTestClientBuilderCustomizer() {
        return (builder) -> builder.defaultHeader(API_KEY_HEADER, API_KEY_VALUE);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.