Expose RequestPath in ServerHttpRequest

The new structured getPath() method replaces the existing
getContextPath() + getPathWithinApplication().

Issue: SPR-15648
This commit is contained in:
Rossen Stoyanchev
2017-06-11 21:57:54 -04:00
parent 2d17411ec4
commit 38a12ed4ba
26 changed files with 127 additions and 115 deletions

View File

@@ -213,7 +213,7 @@ public class RequestMappingInfoHandlerMappingTests {
@SuppressWarnings("unchecked")
public void handleMatchUriTemplateVariables() throws Exception {
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
RequestMappingInfo key = paths("/{path1}/{path2}").build();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
@@ -232,7 +232,7 @@ public class RequestMappingInfoHandlerMappingTests {
URI url = URI.create("/group/a%2Fb");
ServerWebExchange exchange = method(HttpMethod.GET, url).toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
@@ -248,7 +248,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().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
assertEquals("/{path1}/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
@@ -258,7 +258,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Exception {
RequestMappingInfo key = paths().build();
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
assertEquals("/1/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
@@ -367,7 +367,7 @@ public class RequestMappingInfoHandlerMappingTests {
private void handleMatch(ServerWebExchange exchange, String pattern) {
RequestMappingInfo info = paths(pattern).build();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(info, lookupPath, exchange);
}

View File

@@ -117,7 +117,7 @@ public class ContextPathIntegrationTests {
@GetMapping("/test")
public String handle(ServerHttpRequest request) {
return "Tested in " + request.getContextPath();
return "Tested in " + request.getPath().contextPath().value();
}
}

View File

@@ -47,7 +47,7 @@ public class RedirectViewTests {
@Before
public void setup() {
this.exchange = MockServerHttpRequest.get("/").contextPath("/context").toExchange();
this.exchange = MockServerHttpRequest.get("/context/path").contextPath("/context").toExchange();
}

View File

@@ -34,7 +34,8 @@ import static org.junit.Assert.assertEquals;
*/
public class RequestContextTests {
private final MockServerWebExchange exchange = MockServerHttpRequest.get("/").contextPath("foo/").toExchange();
private final MockServerWebExchange exchange = MockServerHttpRequest.get("/foo/path")
.contextPath("/foo").toExchange();
private GenericApplicationContext applicationContext;
@@ -50,7 +51,7 @@ public class RequestContextTests {
@Test
public void testGetContextUrl() throws Exception {
RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext);
assertEquals("foo/bar", context.getContextUrl("bar"));
assertEquals("/foo/bar", context.getContextUrl("bar"));
}
@Test
@@ -59,7 +60,7 @@ public class RequestContextTests {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("spam", "bucket");
assertEquals("foo/bar?spam=bucket", context.getContextUrl("{foo}?spam={spam}", map));
assertEquals("/foo/bar?spam=bucket", context.getContextUrl("{foo}?spam={spam}", map));
}
@Test
@@ -68,7 +69,7 @@ public class RequestContextTests {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar baz");
map.put("spam", "&bucket=");
assertEquals("foo/bar%20baz?spam=%26bucket%3D", context.getContextUrl("{foo}?spam={spam}", map));
assertEquals("/foo/bar%20baz?spam=%26bucket%3D", context.getContextUrl("{foo}?spam={spam}", map));
}
}