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
This commit is contained in:
parent
86f50b20f2
commit
3ce7613195
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.springframework.web.reactive.function.server.support;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
@ -23,6 +25,7 @@ import reactor.test.StepVerifier;
|
|||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.reactive.accept.StandardApiVersionDeprecationHandler;
|
||||
import org.springframework.web.reactive.config.ApiVersionConfigurer;
|
||||
import org.springframework.web.reactive.config.EnableWebFlux;
|
||||
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
||||
|
@ -65,9 +68,7 @@ public class RouterFunctionMappingVersionTests {
|
|||
testGetHandler("1.5", "1.5");
|
||||
}
|
||||
|
||||
|
||||
private void testGetHandler(String version, String expectedBody) {
|
||||
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(
|
||||
MockServerHttpRequest.get("/").header("X-API-Version", version));
|
||||
|
||||
|
@ -78,13 +79,35 @@ public class RouterFunctionMappingVersionTests {
|
|||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void deprecation() {
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(
|
||||
MockServerHttpRequest.get("/").header("X-API-Version", "1"));
|
||||
|
||||
Mono<?> result = this.mapping.getHandler(exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(handler -> {
|
||||
assertThat(((TestHandler) handler).body()).isEqualTo("none");
|
||||
assertThat(exchange.getResponse().getHeaders().getFirst("Link"))
|
||||
.isEqualTo("<https://example.org/deprecation>; rel=\"deprecation\"; type=\"text/html\"");
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
@EnableWebFlux
|
||||
private static class WebConfig implements WebFluxConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureApiVersioning(ApiVersionConfigurer configurer) {
|
||||
configurer.useRequestHeader("X-API-Version").addSupportedVersions("1", "1.1", "1.3");
|
||||
|
||||
StandardApiVersionDeprecationHandler handler = new StandardApiVersionDeprecationHandler();
|
||||
handler.configureVersion("1").setDeprecationLink(URI.create("https://example.org/deprecation"));
|
||||
|
||||
configurer.useRequestHeader("X-API-Version")
|
||||
.addSupportedVersions("1", "1.1", "1.3")
|
||||
.setDeprecationHandler(handler);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -16,11 +16,16 @@
|
|||
|
||||
package org.springframework.web.servlet.function.support;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.accept.StandardApiVersionDeprecationHandler;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
@ -30,6 +35,7 @@ import org.springframework.web.servlet.function.RouterFunctions;
|
|||
import org.springframework.web.servlet.function.ServerRequest;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -66,7 +72,6 @@ public class RouterFunctionMappingVersionTests {
|
|||
testGetHandler("1.5", "1.5");
|
||||
}
|
||||
|
||||
|
||||
private void testGetHandler(String version, String expectedBody) throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.addHeader("X-API-Version", version);
|
||||
|
@ -74,13 +79,37 @@ public class RouterFunctionMappingVersionTests {
|
|||
assertThat(((TestHandler) handler).body()).isEqualTo(expectedBody);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deprecation() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.addHeader("X-API-Version", "1");
|
||||
|
||||
HandlerExecutionChain chain = this.mapping.getHandler(request);
|
||||
assertThat(chain).isNotNull();
|
||||
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
|
||||
interceptor.preHandle(request, response, chain.getHandler());
|
||||
}
|
||||
|
||||
assertThat(((TestHandler) chain.getHandler()).body()).isEqualTo("none");
|
||||
assertThat(response.getHeader("Link"))
|
||||
.isEqualTo("<https://example.org/deprecation>; rel=\"deprecation\"; type=\"text/html\"");
|
||||
}
|
||||
|
||||
|
||||
@EnableWebMvc
|
||||
private static class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureApiVersioning(ApiVersionConfigurer configurer) {
|
||||
configurer.useRequestHeader("X-API-Version").addSupportedVersions("1", "1.1", "1.3");
|
||||
|
||||
StandardApiVersionDeprecationHandler handler = new StandardApiVersionDeprecationHandler();
|
||||
handler.configureVersion("1").setDeprecationLink(URI.create("https://example.org/deprecation"));
|
||||
|
||||
configurer.useRequestHeader("X-API-Version")
|
||||
.addSupportedVersions("1", "1.1", "1.3")
|
||||
.setDeprecationHandler(handler);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
Loading…
Reference in New Issue