Bug report
This issue affects Spring Framework 6.2.0, which is included in Spring Boot 3.4.0.
Description
After upgrading to Spring Framework 6.2.0, unit tests that verify request header modifications in Gateway filters fail. The headers appear to be modified correctly at runtime, but the modifications are not visible in test assertions using MockServerWebExchange
.
Here's a StackOverflow post for this issue: StackOverflow post
Steps to reproduce
I created a minimal test case repository for this: https://github.com/Foteno/gateway-bug
Expected behavior
The test should pass as it did in Spring Framework 6.1.x, with the modified header being visible in the MockServerWebExchange
.
Actual behavior
The assertion fails because the header modification is not visible in the MockServerWebExchange
during the test.
Environment
- Spring Framework 6.2.0
- Spring Boot 3.4.0
- Spring Cloud Gateway 4.2.0
- Java 17
Comment From: bclozel
I think the test was green by accident in the past, as the assertion is not performed on the resulting exchange but the original one. You should write instead:
@Test
void shouldRunFilter() {
MockServerHttpRequest mockRequest = MockServerHttpRequest
.get("/testfilter")
.build();
MockServerWebExchange exchange = MockServerWebExchange.from(mockRequest);
Mono<Void> result = testGatewayFilter.filter(exchange, e -> {
assertEquals("test", e.getRequest().getHeaders().getFirst("test-header"));
return Mono.empty();
});
StepVerifier.create(result).verifyComplete();
}
I'm not sure why this behavior change, but I suspect that it's related to Spring Cloud Gateway and not Spring Framework.
Comment From: Foteno
Understood, thank you very much. I was not able to track down the specific change that could've caused this, all I noticed was the spring-web version. But your suggested way solves my issues that appeared after version change.