|
16 | 16 |
|
17 | 17 | package org.springframework.web.servlet.function.support;
|
18 | 18 |
|
| 19 | +import java.net.URI; |
| 20 | + |
19 | 21 | import org.junit.jupiter.api.BeforeEach;
|
20 | 22 | import org.junit.jupiter.api.Test;
|
21 | 23 |
|
22 | 24 | import org.springframework.context.annotation.Bean;
|
| 25 | +import org.springframework.web.accept.StandardApiVersionDeprecationHandler; |
23 | 26 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
| 27 | +import org.springframework.web.servlet.HandlerExecutionChain; |
| 28 | +import org.springframework.web.servlet.HandlerInterceptor; |
24 | 29 | import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
|
25 | 30 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
26 | 31 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
30 | 35 | import org.springframework.web.servlet.function.ServerRequest;
|
31 | 36 | import org.springframework.web.servlet.function.ServerResponse;
|
32 | 37 | import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
| 38 | +import org.springframework.web.testfixture.servlet.MockHttpServletResponse; |
33 | 39 | import org.springframework.web.testfixture.servlet.MockServletContext;
|
34 | 40 |
|
35 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -66,21 +72,44 @@ void mapVersion() throws Exception {
|
66 | 72 | testGetHandler("1.5", "1.5");
|
67 | 73 | }
|
68 | 74 |
|
69 |
| - |
70 | 75 | private void testGetHandler(String version, String expectedBody) throws Exception {
|
71 | 76 | MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
72 | 77 | request.addHeader("X-API-Version", version);
|
73 | 78 | HandlerFunction<?> handler = (HandlerFunction<?>) this.mapping.getHandler(request).getHandler();
|
74 | 79 | assertThat(((TestHandler) handler).body()).isEqualTo(expectedBody);
|
75 | 80 | }
|
76 | 81 |
|
| 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 | + |
77 | 100 |
|
78 | 101 | @EnableWebMvc
|
79 | 102 | private static class WebConfig implements WebMvcConfigurer {
|
80 | 103 |
|
81 | 104 | @Override
|
82 | 105 | 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); |
84 | 113 | }
|
85 | 114 |
|
86 | 115 | @Bean
|
|
0 commit comments