Skip to content

Commit 3ce7613

Browse files
committed
Add version deprecation tests for router functions
Given the move of ApiVersionStrategy support to AbstractHandlerMapping, deprecation should already work. We only need tests to show it. See gh-35113
1 parent 86f50b2 commit 3ce7613

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/support/RouterFunctionMappingVersionTests.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package org.springframework.web.reactive.function.server.support;
1818

19+
import java.net.URI;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123
import reactor.core.publisher.Mono;
2224
import reactor.test.StepVerifier;
2325

2426
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2527
import org.springframework.context.annotation.Bean;
28+
import org.springframework.web.reactive.accept.StandardApiVersionDeprecationHandler;
2629
import org.springframework.web.reactive.config.ApiVersionConfigurer;
2730
import org.springframework.web.reactive.config.EnableWebFlux;
2831
import org.springframework.web.reactive.config.WebFluxConfigurer;
@@ -65,9 +68,7 @@ void mapVersion() {
6568
testGetHandler("1.5", "1.5");
6669
}
6770

68-
6971
private void testGetHandler(String version, String expectedBody) {
70-
7172
MockServerWebExchange exchange = MockServerWebExchange.from(
7273
MockServerHttpRequest.get("/").header("X-API-Version", version));
7374

@@ -78,13 +79,35 @@ private void testGetHandler(String version, String expectedBody) {
7879
.verifyComplete();
7980
}
8081

82+
@Test
83+
void deprecation() {
84+
MockServerWebExchange exchange = MockServerWebExchange.from(
85+
MockServerHttpRequest.get("/").header("X-API-Version", "1"));
86+
87+
Mono<?> result = this.mapping.getHandler(exchange);
88+
89+
StepVerifier.create(result)
90+
.consumeNextWith(handler -> {
91+
assertThat(((TestHandler) handler).body()).isEqualTo("none");
92+
assertThat(exchange.getResponse().getHeaders().getFirst("Link"))
93+
.isEqualTo("<https://example.org/deprecation>; rel=\"deprecation\"; type=\"text/html\"");
94+
})
95+
.verifyComplete();
96+
}
97+
8198

8299
@EnableWebFlux
83100
private static class WebConfig implements WebFluxConfigurer {
84101

85102
@Override
86103
public void configureApiVersioning(ApiVersionConfigurer configurer) {
87-
configurer.useRequestHeader("X-API-Version").addSupportedVersions("1", "1.1", "1.3");
104+
105+
StandardApiVersionDeprecationHandler handler = new StandardApiVersionDeprecationHandler();
106+
handler.configureVersion("1").setDeprecationLink(URI.create("https://example.org/deprecation"));
107+
108+
configurer.useRequestHeader("X-API-Version")
109+
.addSupportedVersions("1", "1.1", "1.3")
110+
.setDeprecationHandler(handler);
88111
}
89112

90113
@Bean

spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingVersionTests.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package org.springframework.web.servlet.function.support;
1818

19+
import java.net.URI;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123

2224
import org.springframework.context.annotation.Bean;
25+
import org.springframework.web.accept.StandardApiVersionDeprecationHandler;
2326
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
27+
import org.springframework.web.servlet.HandlerExecutionChain;
28+
import org.springframework.web.servlet.HandlerInterceptor;
2429
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
2530
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
2631
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -30,6 +35,7 @@
3035
import org.springframework.web.servlet.function.ServerRequest;
3136
import org.springframework.web.servlet.function.ServerResponse;
3237
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
38+
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
3339
import org.springframework.web.testfixture.servlet.MockServletContext;
3440

3541
import static org.assertj.core.api.Assertions.assertThat;
@@ -66,21 +72,44 @@ void mapVersion() throws Exception {
6672
testGetHandler("1.5", "1.5");
6773
}
6874

69-
7075
private void testGetHandler(String version, String expectedBody) throws Exception {
7176
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
7277
request.addHeader("X-API-Version", version);
7378
HandlerFunction<?> handler = (HandlerFunction<?>) this.mapping.getHandler(request).getHandler();
7479
assertThat(((TestHandler) handler).body()).isEqualTo(expectedBody);
7580
}
7681

82+
@Test
83+
void deprecation() throws Exception {
84+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
85+
request.addHeader("X-API-Version", "1");
86+
87+
HandlerExecutionChain chain = this.mapping.getHandler(request);
88+
assertThat(chain).isNotNull();
89+
90+
MockHttpServletResponse response = new MockHttpServletResponse();
91+
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
92+
interceptor.preHandle(request, response, chain.getHandler());
93+
}
94+
95+
assertThat(((TestHandler) chain.getHandler()).body()).isEqualTo("none");
96+
assertThat(response.getHeader("Link"))
97+
.isEqualTo("<https://example.org/deprecation>; rel=\"deprecation\"; type=\"text/html\"");
98+
}
99+
77100

78101
@EnableWebMvc
79102
private static class WebConfig implements WebMvcConfigurer {
80103

81104
@Override
82105
public void configureApiVersioning(ApiVersionConfigurer configurer) {
83-
configurer.useRequestHeader("X-API-Version").addSupportedVersions("1", "1.1", "1.3");
106+
107+
StandardApiVersionDeprecationHandler handler = new StandardApiVersionDeprecationHandler();
108+
handler.configureVersion("1").setDeprecationLink(URI.create("https://example.org/deprecation"));
109+
110+
configurer.useRequestHeader("X-API-Version")
111+
.addSupportedVersions("1", "1.1", "1.3")
112+
.setDeprecationHandler(handler);
84113
}
85114

86115
@Bean

0 commit comments

Comments
 (0)