Introduce LookupPath in WebFlux request routing
This commit adds the `LookupPath` class that contains the full request path relative to the web context; the application can get from it various information, including the file extension and path parameters (if any). Since that operation is done multiple times for each request, this object is stored as an attribute at the `ServerWebExchange` level. Issue: SPR-15397
This commit is contained in:
@@ -21,6 +21,8 @@ import org.junit.Test;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.support.HttpRequestPathHelper;
|
||||
import org.springframework.web.server.support.LookupPath;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -39,6 +41,7 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
@Test
|
||||
public void empty() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -48,9 +51,11 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
this.configSource.registerCorsConfiguration("/bar/**", config);
|
||||
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
|
||||
exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
assertEquals(config, this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -59,4 +64,10 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
}
|
||||
|
||||
public void setLookupPathAttribute(ServerWebExchange exchange) {
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
|
||||
helper.getLookupPathForRequest(exchange));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.server.support;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link LookupPath}
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class LookupPathTests {
|
||||
|
||||
@Test
|
||||
public void parsePath() {
|
||||
LookupPath path = create("/foo");
|
||||
assertEquals("/foo", path.getPath());
|
||||
assertEquals("/foo", path.getPathWithoutExtension());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsePathWithExtension() {
|
||||
LookupPath path = create("/foo.txt");
|
||||
assertEquals("/foo.txt", path.getPath());
|
||||
assertEquals("/foo", path.getPathWithoutExtension());
|
||||
assertEquals(".txt", path.getFileExtension());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsePathWithParams() {
|
||||
LookupPath path = create("/test/foo.txt;foo=bar?framework=spring");
|
||||
assertEquals("/test/foo.txt;foo=bar", path.getPath());
|
||||
assertEquals("/test/foo", path.getPathWithoutExtension());
|
||||
assertEquals(".txt", path.getFileExtension());
|
||||
assertEquals("foo=bar", path.getPathParameters());
|
||||
}
|
||||
|
||||
private LookupPath create(String path) {
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get(path).build().toExchange();
|
||||
return helper.getLookupPathForRequest(exchange);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user