HandlerMappingIntrospector handles attribute changes properly

Closes gh-26833
This commit is contained in:
Rossen Stoyanchev
2021-04-22 17:21:03 +01:00
parent 7c3a18490b
commit 86123de883
3 changed files with 114 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,10 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
import org.springframework.web.servlet.function.support.RouterFunctionMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
@@ -99,16 +103,6 @@ public class HandlerMappingIntrospectorTests {
assertThat(actual).isEqualTo(expected);
}
void defaultHandlerMappings() {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.refresh();
List<HandlerMapping> actual = initIntrospector(context).getHandlerMappings();
assertThat(actual.size()).isEqualTo(2);
assertThat(actual.get(0).getClass()).isEqualTo(BeanNameUrlHandlerMapping.class);
assertThat(actual.get(1).getClass()).isEqualTo(RequestMappingHandlerMapping.class);
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void getMatchable(boolean usePathPatterns) throws Exception {
@@ -151,6 +145,22 @@ public class HandlerMappingIntrospectorTests {
assertThatIllegalStateException().isThrownBy(() -> initIntrospector(cxt).getMatchableHandlerMapping(request));
}
@Test // gh-26833
void getMatchablePreservesRequestAttributes() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(TestConfig.class);
context.refresh();
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path");
request.setAttribute("name", "value");
MatchableHandlerMapping matchable = initIntrospector(context).getMatchableHandlerMapping(request);
assertThat(matchable).isNotNull();
// RequestPredicates.restoreAttributes clears and re-adds attributes
assertThat(request.getAttribute("name")).isEqualTo("value");
}
@Test
void getCorsConfigurationPreFlight() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
@@ -204,15 +214,29 @@ public class HandlerMappingIntrospectorTests {
@Configuration
static class TestConfig {
@Bean
public RouterFunctionMapping routerFunctionMapping() {
RouterFunctionMapping mapping = new RouterFunctionMapping();
mapping.setOrder(1);
return mapping;
}
@Bean
public RequestMappingHandlerMapping handlerMapping() {
return new RequestMappingHandlerMapping();
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setOrder(2);
return mapping;
}
@Bean
public TestController testController() {
return new TestController();
}
@Bean
public RouterFunction<?> routerFunction() {
return RouterFunctions.route().GET("/fn-path", request -> ServerResponse.ok().build()).build();
}
}