Switch to ContainerPath variants in HandlerMapping's

This commit is contained in:
Rossen Stoyanchev
2017-07-01 20:05:17 -04:00
parent 145eab9c2f
commit 15cf9c1d78
10 changed files with 53 additions and 51 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.CacheControl;
import org.springframework.http.server.reactive.PathContainer;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
import org.springframework.web.reactive.HandlerMapping;
@@ -81,7 +82,8 @@ public class ResourceHandlerRegistryTests {
@Test
public void mapPathToLocation() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");
exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE,
PathContainer.parse("/testStylesheet.css", StandardCharsets.UTF_8));
ResourceWebHandler handler = getHandler("/resources/**");
handler.handle(exchange).block(Duration.ofSeconds(5));

View File

@@ -24,6 +24,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.PathContainer;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
@@ -103,7 +104,9 @@ public class SimpleUrlHandlerMappingTests {
assertNotNull(actual);
assertSame(bean, actual);
//noinspection OptionalGetWithoutIsPresent
assertEquals(pathWithinMapping, exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
PathContainer path = exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
assertNotNull(path);
assertEquals(pathWithinMapping, path.value());
}
else {
assertNull(actual);

View File

@@ -44,6 +44,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.PathContainer;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
@@ -276,14 +277,6 @@ public class ResourceWebHandlerTests {
assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
}
@Test
public void ignoreInvalidEscapeSequence() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
setPathWithinHandlerMapping(exchange, "/%foo%/bar.txt");
this.handler.handle(exchange).block(TIMEOUT);
assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
}
@Test
public void processPath() throws Exception {
assertSame("/foo/bar", this.handler.processPath("/foo/bar"));
@@ -559,7 +552,8 @@ public class ResourceWebHandlerTests {
private void setPathWithinHandlerMapping(ServerWebExchange exchange, String path) {
exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, path);
exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE,
PathContainer.parse(path, StandardCharsets.UTF_8));
}
private long resourceLastModified(String resourceName) throws IOException {

View File

@@ -213,10 +213,9 @@ public class RequestMappingInfoHandlerMappingTests {
@SuppressWarnings("unchecked")
public void handleMatchUriTemplateVariables() throws Exception {
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
RequestMappingInfo key = paths("/{path1}/{path2}").build();
this.handlerMapping.handleMatch(key, handlerMethod, lookupPath, exchange);
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map<String, String> uriVariables = (Map<String, String>) exchange.getAttributes().get(name);
@@ -232,8 +231,7 @@ public class RequestMappingInfoHandlerMappingTests {
URI url = URI.create("/group/a%2Fb");
ServerWebExchange exchange = method(HttpMethod.GET, url).toExchange();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, handlerMethod, lookupPath, exchange);
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
@SuppressWarnings("unchecked")
@@ -248,8 +246,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void handleMatchBestMatchingPatternAttribute() throws Exception {
RequestMappingInfo key = paths("/{path1}/2", "/**").build();
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, handlerMethod, lookupPath, exchange);
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE);
assertEquals("/{path1}/2", bestMatch.getPatternString());
@@ -262,8 +259,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Exception {
RequestMappingInfo key = paths().build();
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, handlerMethod, lookupPath, exchange);
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE);
assertEquals("/1/2", bestMatch.getPatternString());
@@ -349,8 +345,7 @@ public class RequestMappingInfoHandlerMappingTests {
private void handleMatch(ServerWebExchange exchange, String pattern) {
RequestMappingInfo info = paths(pattern).build();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(info, handlerMethod, lookupPath, exchange);
this.handlerMapping.handleMatch(info, handlerMethod, exchange);
}
@SuppressWarnings("unchecked")