Remove Spring MVC suffixPattern and trailingSlash matching
See gh-34036
This commit is contained in:
@@ -79,19 +79,13 @@ class AnnotationDrivenBeanDefinitionParserTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testPathMatchingConfiguration() {
|
||||
loadBeanDefinitions("mvc-config-path-matching.xml");
|
||||
RequestMappingHandlerMapping hm = this.appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertThat(hm).isNotNull();
|
||||
assertThat(hm.useSuffixPatternMatch()).isTrue();
|
||||
assertThat(hm.useTrailingSlashMatch()).isFalse();
|
||||
assertThat(hm.useRegisteredSuffixPatternMatch()).isTrue();
|
||||
assertThat(hm.getUrlPathHelper()).isInstanceOf(TestPathHelper.class);
|
||||
assertThat(hm.getPathMatcher()).isInstanceOf(TestPathMatcher.class);
|
||||
assertThat(hm.getPatternParser()).isNull();
|
||||
List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
|
||||
assertThat(fileExtensions).containsExactly("xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -240,10 +240,7 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void configurePathMatch(PathMatchConfigurer configurer) {
|
||||
configurer.setUseRegisteredSuffixPatternMatch(true)
|
||||
.setUseTrailingSlashMatch(false)
|
||||
.setUrlPathHelper(pathHelper)
|
||||
.setPathMatcher(pathMatcher);
|
||||
configurer.setUrlPathHelper(pathHelper).setPathMatcher(pathMatcher);
|
||||
}
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
@@ -272,9 +269,6 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
webMvcConfig.mvcResourceUrlProvider());
|
||||
|
||||
assertThat(annotationsMapping).isNotNull();
|
||||
assertThat(annotationsMapping.useRegisteredSuffixPatternMatch()).isTrue();
|
||||
assertThat(annotationsMapping.useSuffixPatternMatch()).isTrue();
|
||||
assertThat(annotationsMapping.useTrailingSlashMatch()).isFalse();
|
||||
configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher());
|
||||
|
||||
SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping(
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.condition;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -111,88 +109,6 @@ class PatternsRequestConditionTests {
|
||||
assertThat(match).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void matchSuffixPattern() {
|
||||
MockHttpServletRequest request = initRequest("/foo.html");
|
||||
|
||||
boolean useSuffixPatternMatch = true;
|
||||
PatternsRequestCondition condition =
|
||||
new PatternsRequestCondition(new String[] {"/{foo}"}, null, null, useSuffixPatternMatch, true);
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).containsExactly("/{foo}.*");
|
||||
|
||||
useSuffixPatternMatch = false;
|
||||
condition = new PatternsRequestCondition(
|
||||
new String[] {"/{foo}"}, null, null, useSuffixPatternMatch, false);
|
||||
match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).containsExactly("/{foo}");
|
||||
}
|
||||
|
||||
@Test // SPR-8410
|
||||
@SuppressWarnings("deprecation")
|
||||
void matchSuffixPatternUsingFileExtensions() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition(
|
||||
new String[] {"/jobs/{jobName}"}, null, null, true, false, Collections.singletonList("json"));
|
||||
|
||||
MockHttpServletRequest request = initRequest("/jobs/my.job");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).containsExactly("/jobs/{jobName}");
|
||||
|
||||
request = initRequest("/jobs/my.job.json");
|
||||
match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).containsExactly("/jobs/{jobName}.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void matchSuffixPatternUsingFileExtensions2() {
|
||||
PatternsRequestCondition condition1 = new PatternsRequestCondition(
|
||||
new String[] {"/prefix"}, null, null, true, false, Collections.singletonList("json"));
|
||||
|
||||
PatternsRequestCondition condition2 = new PatternsRequestCondition(
|
||||
new String[] {"/suffix"}, null, null, true, false, null);
|
||||
|
||||
PatternsRequestCondition combined = condition1.combine(condition2);
|
||||
|
||||
MockHttpServletRequest request = initRequest("/prefix/suffix.json");
|
||||
PatternsRequestCondition match = combined.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchTrailingSlash() {
|
||||
MockHttpServletRequest request = initRequest("/foo/");
|
||||
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).containsExactly("/foo/");
|
||||
|
||||
condition = new PatternsRequestCondition(new String[] {"/foo"}, true, null);
|
||||
match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNotNull();
|
||||
assertThat(match.getPatterns()).first(STRING)
|
||||
.as("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)")
|
||||
.isEqualTo("/foo/");
|
||||
|
||||
condition = new PatternsRequestCondition(new String[] {"/foo"}, false, null);
|
||||
match = condition.getMatchingCondition(request);
|
||||
|
||||
assertThat(match).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchPatternContainsExtension() {
|
||||
MockHttpServletRequest request = initRequest("/foo.html");
|
||||
@@ -203,11 +119,7 @@ class PatternsRequestConditionTests {
|
||||
|
||||
@Test // gh-22543
|
||||
void matchWithEmptyPatterns() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition();
|
||||
assertThat(condition.getMatchingCondition(initRequest(""))).isNotNull();
|
||||
assertThat(condition.getMatchingCondition(initRequest("/anything"))).isNull();
|
||||
|
||||
condition = condition.combine(new PatternsRequestCondition());
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition().combine(new PatternsRequestCondition());
|
||||
assertThat(condition.getMatchingCondition(initRequest(""))).isNotNull();
|
||||
assertThat(condition.getMatchingCondition(initRequest("/anything"))).isNull();
|
||||
}
|
||||
|
||||
@@ -616,15 +616,12 @@ class RequestMappingInfoHandlerMappingTests {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private RequestMappingInfo.BuilderConfiguration getBuilderConfig() {
|
||||
RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();
|
||||
if (getPatternParser() != null) {
|
||||
config.setPatternParser(getPatternParser());
|
||||
}
|
||||
else {
|
||||
config.setSuffixPatternMatch(true);
|
||||
config.setRegisteredSuffixPatternMatch(true);
|
||||
config.setPathMatcher(getPathMatcher());
|
||||
}
|
||||
return config;
|
||||
|
||||
@@ -23,8 +23,6 @@ import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.Principal;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -35,7 +33,6 @@ import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
@@ -102,74 +99,6 @@ class RequestMappingHandlerMappingTests {
|
||||
assertThat(mapping.getBuilderConfiguration()).isNotNull().isNotSameAs(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void useRegisteredSuffixPatternMatch() {
|
||||
RequestMappingHandlerMapping mapping = createMapping();
|
||||
|
||||
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||
org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy =
|
||||
new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions);
|
||||
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
|
||||
|
||||
mapping.setContentNegotiationManager(manager);
|
||||
mapping.setUseRegisteredSuffixPatternMatch(true);
|
||||
mapping.afterPropertiesSet();
|
||||
|
||||
assertThat(mapping.useSuffixPatternMatch()).isTrue();
|
||||
assertThat(mapping.useRegisteredSuffixPatternMatch()).isTrue();
|
||||
assertThat(mapping.getFileExtensions()).isEqualTo(Collections.singletonList("json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void useRegisteredSuffixPatternMatchInitialization() {
|
||||
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||
org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy =
|
||||
new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions);
|
||||
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
|
||||
|
||||
final Set<String> extensions = new HashSet<>();
|
||||
|
||||
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping() {
|
||||
@Override
|
||||
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
|
||||
extensions.addAll(getFileExtensions());
|
||||
return super.getMappingForMethod(method, handlerType);
|
||||
}
|
||||
};
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("testController", ComposedAnnotationController.class);
|
||||
wac.refresh();
|
||||
|
||||
mapping.setContentNegotiationManager(manager);
|
||||
mapping.setUseRegisteredSuffixPatternMatch(true);
|
||||
mapping.setApplicationContext(wac);
|
||||
mapping.afterPropertiesSet();
|
||||
|
||||
assertThat(extensions).containsOnly("json");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void suffixPatternMatchSettings() {
|
||||
RequestMappingHandlerMapping mapping = createMapping();
|
||||
|
||||
assertThat(mapping.useSuffixPatternMatch()).isFalse();
|
||||
assertThat(mapping.useRegisteredSuffixPatternMatch()).isFalse();
|
||||
|
||||
mapping.setUseRegisteredSuffixPatternMatch(false);
|
||||
assertThat(mapping.useSuffixPatternMatch())
|
||||
.as("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch")
|
||||
.isFalse();
|
||||
|
||||
mapping.setUseRegisteredSuffixPatternMatch(true);
|
||||
assertThat(mapping.useSuffixPatternMatch())
|
||||
.as("'true' registeredSuffixPatternMatch should enable suffixPatternMatch")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void resolveEmbeddedValuesInPatterns(RequestMappingHandlerMapping mapping) {
|
||||
mapping.setEmbeddedValueResolver(value -> "/${pattern}/bar".equals(value) ? "/foo/bar" : value);
|
||||
|
||||
@@ -178,15 +178,7 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void emptyValueMapping(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
// UrlPathHelper returns "/" for "",
|
||||
// so either the mapping has to be "/" or trailingSlashMatch must be on
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useTrailingSlashMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setContextPath("/foo");
|
||||
@@ -207,15 +199,7 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void errorThrownFromHandlerMethod(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(ControllerWithErrorThrown.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
// UrlPathHelper returns "/" for "",
|
||||
// so either the mapping has to be "/" or trailingSlashMatch must be on
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useTrailingSlashMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(ControllerWithErrorThrown.class, usePathPatterns);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setContextPath("/foo");
|
||||
@@ -514,13 +498,7 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void adaptedHandleMethods(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(MyAdaptedController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(MyAdaptedController.class, usePathPatterns);
|
||||
doTestAdaptedHandleMethods(usePathPatterns);
|
||||
}
|
||||
|
||||
@@ -560,12 +538,6 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
|
||||
if (!usePathPatterns) {
|
||||
// This depends on suffix pattern matching and has different outcomes otherwise,
|
||||
// for example, 404 vs another method matching, depending on the test case.
|
||||
assertThat(response.getContentAsString()).isEqualTo("test-name1-2");
|
||||
}
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/myPath4.do");
|
||||
request.addParameter("param1", "value1");
|
||||
request.addParameter("param2", "2");
|
||||
@@ -787,13 +759,7 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void relativePathDispatchingController(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(MyRelativePathDispatchingController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(MyRelativePathDispatchingController.class, usePathPatterns);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -809,22 +775,11 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
assertThat(response.getContentAsString()).isEqualTo("myLangView");
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/myApp/surprise.do");
|
||||
response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
assertThat(response.getContentAsString()).isEqualTo(!usePathPatterns ? "mySurpriseView" : "myView");
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void relativeMethodPathDispatchingController(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(MyRelativeMethodPathDispatchingController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(MyRelativeMethodPathDispatchingController.class, usePathPatterns);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -845,15 +800,7 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
|
||||
if (!usePathPatterns) {
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
|
||||
}
|
||||
else {
|
||||
assertThat(response.getStatus())
|
||||
.as("Suffixes pattern matching should not work with PathPattern's")
|
||||
.isEqualTo(404);
|
||||
}
|
||||
assertThat(response.getStatus()).as("Suffixes pattern matching is not supported").isEqualTo(404);
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
@@ -1776,108 +1723,6 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
assertThat(response.getContentLength()).as("Expected an empty content").isEqualTo(0);
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
@SuppressWarnings("deprecation")
|
||||
void responseBodyAsHtml(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
// `useSuffixPatternMatch` is not allowed with PathPattern's
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
|
||||
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
|
||||
factoryBean.setFavorPathExtension(true);
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
|
||||
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
|
||||
wac.registerBeanDefinition("handlerAdapter", adapterDef);
|
||||
});
|
||||
|
||||
byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
|
||||
request.setContent(content);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
getServlet().service(request, response);
|
||||
|
||||
if (!usePathPatterns) {
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
|
||||
assertThat(response.getHeader("Content-Disposition")).isEqualTo("inline;filename=f.txt");
|
||||
assertThat(response.getContentAsByteArray()).isEqualTo(content);
|
||||
}
|
||||
else {
|
||||
assertThat(response.getStatus())
|
||||
.as("Suffixes pattern matching should not work with PathPattern's")
|
||||
.isEqualTo(404);
|
||||
}
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
@SuppressWarnings("deprecation")
|
||||
void responseBodyAsHtmlWithSuffixPresent(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
|
||||
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
|
||||
factoryBean.setFavorPathExtension(true);
|
||||
factoryBean.afterPropertiesSet();
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
|
||||
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
|
||||
wac.registerBeanDefinition("handlerAdapter", adapterDef);
|
||||
});
|
||||
|
||||
byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
|
||||
request.setContent(content);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
getServlet().service(request, response);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
|
||||
assertThat(response.getHeader("Content-Disposition")).isNull();
|
||||
assertThat(response.getContentAsByteArray()).isEqualTo(content);
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void responseBodyAsHtmlWithProducesCondition(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
|
||||
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
|
||||
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
|
||||
wac.registerBeanDefinition("handlerAdapter", adapterDef);
|
||||
});
|
||||
|
||||
byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
|
||||
request.setContent(content);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
getServlet().service(request, response);
|
||||
|
||||
if (!usePathPatterns) {
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
|
||||
assertThat(response.getHeader("Content-Disposition")).isNull();
|
||||
assertThat(response.getContentAsByteArray()).isEqualTo(content);
|
||||
}
|
||||
else {
|
||||
assertThat(response.getStatus())
|
||||
.as("Suffixes pattern matching should not work with PathPattern's")
|
||||
.isEqualTo(404);
|
||||
}
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void responseBodyAsTextWithCssExtension(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
|
||||
@@ -3052,7 +2897,6 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
|
||||
static class TestViewResolver implements ViewResolver {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public View resolveViewName(final String viewName, Locale locale) throws Exception {
|
||||
return (model, request, response) -> {
|
||||
@@ -4021,16 +3865,6 @@ class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandl
|
||||
return body;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/a2.html", method = RequestMethod.GET)
|
||||
public String a2(@RequestBody String body) {
|
||||
return body;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/a3", method = RequestMethod.GET, produces = "text/html")
|
||||
public String a3(@RequestBody String body) throws IOException {
|
||||
return body;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/a4", method = RequestMethod.GET)
|
||||
public String a4(@RequestBody String body) {
|
||||
return body;
|
||||
|
||||
@@ -173,7 +173,6 @@ class UriTemplateServletAnnotationControllerHandlerMethodTests extends AbstractS
|
||||
initDispatcherServlet(SimpleUriTemplateController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
mappingDef.getPropertyValues().add("removeSemicolonContent", "false");
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
@@ -182,8 +181,7 @@ class UriTemplateServletAnnotationControllerHandlerMethodTests extends AbstractS
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42;jsessionid=c0o7fszeb1;q=24.xml");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
assertThat(response.getContentAsString())
|
||||
.isEqualTo(!usePathPatterns ? "test-42-24" : "test-42-24.xml");
|
||||
assertThat(response.getContentAsString()).isEqualTo("test-42-24.xml");
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
@@ -320,18 +318,12 @@ class UriTemplateServletAnnotationControllerHandlerMethodTests extends AbstractS
|
||||
|
||||
@PathPatternsParameterizedTest // gh-13187
|
||||
void variableNamesWithUrlExtension(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(VariableNamesController.class, usePathPatterns, wac -> {
|
||||
if (!usePathPatterns) {
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
|
||||
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
|
||||
wac.registerBeanDefinition("handlerMapping", mappingDef);
|
||||
}
|
||||
});
|
||||
initDispatcherServlet(VariableNamesController.class, usePathPatterns);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test/foo.json");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
assertThat(response.getContentAsString()).isEqualTo(!usePathPatterns ? "foo-foo" : "foo-foo.json");
|
||||
assertThat(response.getContentAsString()).isEqualTo("foo-foo.json");
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest // gh-11643
|
||||
@@ -683,13 +675,4 @@ class UriTemplateServletAnnotationControllerHandlerMethodTests extends AbstractS
|
||||
}
|
||||
}
|
||||
|
||||
// @Disabled("ControllerClassNameHandlerMapping")
|
||||
// void controllerClassName() throws Exception {
|
||||
|
||||
// @Disabled("useDefaultSuffixPattern property not supported")
|
||||
// void doubles() throws Exception {
|
||||
|
||||
// @Disabled("useDefaultSuffixPattern property not supported")
|
||||
// void noDefaultSuffixPattern() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@@ -149,10 +149,8 @@ class ResourceHttpRequestHandlerTests {
|
||||
}
|
||||
|
||||
@Test // SPR-14577
|
||||
@SuppressWarnings("deprecation")
|
||||
void getMediaTypeWithFavorPathExtensionOff() throws Exception {
|
||||
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
|
||||
factory.setFavorPathExtension(false);
|
||||
factory.afterPropertiesSet();
|
||||
ContentNegotiationManager manager = factory.getObject();
|
||||
|
||||
|
||||
@@ -6,12 +6,7 @@
|
||||
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
|
||||
|
||||
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
|
||||
<mvc:path-matching
|
||||
suffix-pattern="true"
|
||||
trailing-slash="false"
|
||||
registered-suffixes-only="true"
|
||||
path-helper="pathHelper"
|
||||
path-matcher="pathMatcher" />
|
||||
<mvc:path-matching path-helper="pathHelper" path-matcher="pathMatcher" />
|
||||
</mvc:annotation-driven>
|
||||
|
||||
<bean id="pathMatcher" class="org.springframework.web.servlet.config.TestPathMatcher" />
|
||||
|
||||
Reference in New Issue
Block a user