gpt4 book ai didi

java - WebTestClient 变异过滤器 java.lang.IllegalStateException : No "WebTestClient-Request-Id" header

转载 作者:行者123 更新时间:2023-12-02 09:13:54 28 4
gpt4 key购买 nike

我正在尝试使用 WebTestClient 为 ExchangeFilterFunctions 编写单元测试,但是在更改并向 webTestClient 添加过滤器时遇到此错误。

java.lang.IllegalStateException: No "WebTestClient-Request-Id" header

at org.springframework.util.Assert.state(Assert.java:94)
at org.springframework.test.web.reactive.server.WiretapConnector.lambda$connect$2(WiretapConnector.java:80)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1510)
at reactor.core.publisher.MonoProcessor.subscribe(MonoProcessor.java:457)
at reactor.core.publisher.MonoMap.subscribe(MonoMap.java:55)
at reactor.core.publisher.MonoPeek.subscribe(MonoPeek.java:71)
at reactor.core.publisher.MonoPeek.subscribe(MonoPeek.java:71)
at reactor.core.publisher.MonoMap.subscribe(MonoMap.java:55)
at reactor.core.publisher.MonoSwitchIfEmpty.subscribe(MonoSwitchIfEmpty.java:44)
at reactor.core.publisher.Mono.block(Mono.java:1517)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultRequestBodyUriSpec.exchange(DefaultWebTestClient.java:283)
at com.ecs.springframework.reactive.web.filter.EcsMdcRequestExchangeFilterTest.whenExchangeFilterFunctionInjectedIntoWebTestClient_thenWebTestClientShouldIncludeHeader

添加以下测试中的注释行并导致上述错误:

  @Test
void whenExchangeFilterFunctionInjectedIntoWebTestClient_thenWebTestClientShouldIncludeHeader() {

RouterFunction function =
RouterFunctions.route(
RequestPredicates.GET("/filter"), request -> ServerResponse.ok().build());

WebTestClient.bindToRouterFunction(function)
.build()
// .mutate()
// .filter(new AddHeaderExchangeFilter())
// .build()
.get()
.uri("/filter")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.valueMatches("FILTER-HEADER-KEY", "FILTER-HEADER-VALUE");
}

添加标题交换过滤器

public class AddHeaderExchangeFilter implements ExchangeFilterFunction {

@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {

ClientRequest newRequest = ClientRequest
.create(request.method(), request.url())
.header("FILTER-HEADER-KEY", "FILTER-HEADER-VALUE")
.build();

return next.exchange(newRequest);
}

}

最佳答案

在您的 AddHeaderExchangeFilter 上,您需要添加当前的请求 header

public class AddHeaderExchangeFilter implements ExchangeFilterFunction {

@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {

ClientRequest newRequest = ClientRequest.create(request.method(), request.url())
.headers(httpHeaders -> httpHeaders.addAll(request.headers()))
.header("FILTER-HEADER-KEY", "FILTER-HEADER-VALUE")
.build();
return next.exchange(newRequest);
}
}

这对于 WebTestClient 正确行为及其在执行 exchange() 时验证它是必要的。
在您的过滤器中,您正在创建一个新请求,并且初始请求中的 header 将被忽略,并且不会传递给 newRequest

<小时/>

旁注:我猜您尝试测试过滤器是否正常工作,但您当前的测试不是验证请求的 header ,而是验证响应的 header (因此您的测试将失败).
我给您的建议是更改路由器以从请求返回 header

RouterFunction function =
RouterFunctions.route(
RequestPredicates.GET("/filter"), request ->
ServerResponse.ok().headers(httpHeaders -> httpHeaders.addAll(request.headers().asHttpHeaders())).build()
);

关于java - WebTestClient 变异过滤器 java.lang.IllegalStateException : No "WebTestClient-Request-Id" header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161186/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com