Polish use of LookupPath
This commit is contained in:
@@ -22,9 +22,9 @@ import java.util.Set;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.support.HttpRequestPathHelper;
|
||||
import org.springframework.web.server.support.LookupPath;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -82,7 +82,7 @@ public class PatternsRequestConditionTests {
|
||||
@Test
|
||||
public void matchDirectPath() throws Exception {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo"));
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo"));
|
||||
|
||||
assertNotNull(match);
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class PatternsRequestConditionTests {
|
||||
@Test
|
||||
public void matchPattern() throws Exception {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo/*");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar"));
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar"));
|
||||
|
||||
assertNotNull(match);
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class PatternsRequestConditionTests {
|
||||
@Test
|
||||
public void matchSortPatterns() throws Exception {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/*/*", "/foo/bar", "/foo/*");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar"));
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar"));
|
||||
PatternsRequestCondition expected = new PatternsRequestCondition("/foo/bar", "/foo/*", "/*/*");
|
||||
|
||||
assertEquals(expected, match);
|
||||
@@ -106,7 +106,7 @@ public class PatternsRequestConditionTests {
|
||||
|
||||
@Test
|
||||
public void matchSuffixPattern() throws Exception {
|
||||
ServerWebExchange exchange = createExchange("/foo.html");
|
||||
ServerWebExchange exchange = initExchange("/foo.html");
|
||||
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/{foo}");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
|
||||
@@ -129,13 +129,13 @@ public class PatternsRequestConditionTests {
|
||||
Set<String> extensions = Collections.singleton("json");
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition(patterns, null, true, false, extensions);
|
||||
|
||||
MockServerWebExchange exchange = createExchange("/jobs/my.job");
|
||||
MockServerWebExchange exchange = initExchange("/jobs/my.job");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
|
||||
|
||||
assertNotNull(match);
|
||||
assertEquals("/jobs/{jobName}", match.getPatterns().iterator().next());
|
||||
|
||||
exchange = createExchange("/jobs/my.job.json");
|
||||
exchange = initExchange("/jobs/my.job.json");
|
||||
match = condition.getMatchingCondition(exchange);
|
||||
|
||||
assertNotNull(match);
|
||||
@@ -152,7 +152,7 @@ public class PatternsRequestConditionTests {
|
||||
|
||||
PatternsRequestCondition combined = condition1.combine(condition2);
|
||||
|
||||
MockServerWebExchange exchange = createExchange("/prefix/suffix.json");
|
||||
MockServerWebExchange exchange = initExchange("/prefix/suffix.json");
|
||||
PatternsRequestCondition match = combined.getMatchingCondition(exchange);
|
||||
|
||||
assertNotNull(match);
|
||||
@@ -160,7 +160,7 @@ public class PatternsRequestConditionTests {
|
||||
|
||||
@Test
|
||||
public void matchTrailingSlash() throws Exception {
|
||||
MockServerWebExchange exchange = createExchange("/foo/");
|
||||
MockServerWebExchange exchange = initExchange("/foo/");
|
||||
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
|
||||
@@ -175,7 +175,7 @@ public class PatternsRequestConditionTests {
|
||||
assertEquals("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)",
|
||||
"/foo/", match.getPatterns().iterator().next());
|
||||
|
||||
exchange = createExchange("/foo/");
|
||||
exchange = initExchange("/foo/");
|
||||
condition = new PatternsRequestCondition(new String[] {"/foo"}, null, false, false, null);
|
||||
match = condition.getMatchingCondition(exchange);
|
||||
|
||||
@@ -185,7 +185,7 @@ public class PatternsRequestConditionTests {
|
||||
@Test
|
||||
public void matchPatternContainsExtension() throws Exception {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo.jpg");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo.html"));
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo.html"));
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
@@ -195,7 +195,7 @@ public class PatternsRequestConditionTests {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo*");
|
||||
|
||||
assertEquals(0, c1.compareTo(c2, createExchange("/foo")));
|
||||
assertEquals(0, c1.compareTo(c2, initExchange("/foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -203,12 +203,12 @@ public class PatternsRequestConditionTests {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/fo*");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo");
|
||||
|
||||
assertEquals(1, c1.compareTo(c2, createExchange("/foo")));
|
||||
assertEquals(1, c1.compareTo(c2, initExchange("/foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareNumberOfMatchingPatterns() throws Exception {
|
||||
ServerWebExchange exchange = createExchange("/foo.html");
|
||||
ServerWebExchange exchange = initExchange("/foo.html");
|
||||
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html");
|
||||
@@ -220,10 +220,10 @@ public class PatternsRequestConditionTests {
|
||||
assertEquals(1, match1.compareTo(match2, exchange));
|
||||
}
|
||||
|
||||
private MockServerWebExchange createExchange(String path) {
|
||||
private MockServerWebExchange initExchange(String path) {
|
||||
MockServerWebExchange exchange = get(path).toExchange();
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
|
||||
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
|
||||
return exchange;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchPatternsCondition() {
|
||||
MockServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo*", "/bar").build();
|
||||
RequestMappingInfo expected = paths("/foo*").build();
|
||||
@@ -83,7 +83,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchParamsCondition() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo").params("foo=bar").build();
|
||||
RequestMappingInfo match = info.getMatchingCondition(exchange);
|
||||
@@ -99,7 +99,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchHeadersCondition() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").header("foo", "bar").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo").headers("foo=bar").build();
|
||||
RequestMappingInfo match = info.getMatchingCondition(exchange);
|
||||
@@ -115,7 +115,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchConsumesCondition() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.post("/foo").contentType(MediaType.TEXT_PLAIN).toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo").consumes("text/plain").build();
|
||||
RequestMappingInfo match = info.getMatchingCondition(exchange);
|
||||
@@ -131,7 +131,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchProducesCondition() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo").produces("text/plain").build();
|
||||
RequestMappingInfo match = info.getMatchingCondition(exchange);
|
||||
@@ -147,7 +147,7 @@ public class RequestMappingInfoTests {
|
||||
@Test
|
||||
public void matchCustomCondition() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
RequestMappingInfo info = paths("/foo").params("foo=bar").build();
|
||||
RequestMappingInfo match = info.getMatchingCondition(exchange);
|
||||
@@ -169,7 +169,7 @@ public class RequestMappingInfoTests {
|
||||
RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build();
|
||||
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange);
|
||||
|
||||
List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam);
|
||||
@@ -279,9 +279,9 @@ public class RequestMappingInfoTests {
|
||||
assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match);
|
||||
}
|
||||
|
||||
public void setLookupPathAttribute(ServerWebExchange exchange) {
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
|
||||
private void initLookupPath(ServerWebExchange exchange) {
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
|
||||
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -219,17 +219,17 @@ public class ViewResolutionResultHandlerTests {
|
||||
ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
|
||||
|
||||
MockServerWebExchange exchange = get("/account").toExchange();
|
||||
addLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
|
||||
assertResponseBody(exchange, "account: {id=123}");
|
||||
|
||||
exchange = get("/account/").toExchange();
|
||||
addLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
|
||||
assertResponseBody(exchange, "account: {id=123}");
|
||||
|
||||
exchange = get("/account.123").toExchange();
|
||||
addLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
|
||||
assertResponseBody(exchange, "account: {id=123}");
|
||||
}
|
||||
@@ -256,7 +256,7 @@ public class ViewResolutionResultHandlerTests {
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext);
|
||||
|
||||
MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange();
|
||||
addLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
|
||||
TestView defaultView = new TestView("jsonView", APPLICATION_JSON);
|
||||
|
||||
@@ -307,9 +307,9 @@ public class ViewResolutionResultHandlerTests {
|
||||
assertEquals("/", response.getHeaders().getLocation().toString());
|
||||
}
|
||||
|
||||
private void addLookupPathAttribute(ServerWebExchange exchange) {
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
|
||||
private void initLookupPath(ServerWebExchange exchange) {
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
|
||||
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
|
||||
}
|
||||
|
||||
|
||||
@@ -333,7 +333,7 @@ public class ViewResolutionResultHandlerTests {
|
||||
model.addAttribute("id", "123");
|
||||
HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
|
||||
MockServerWebExchange exchange = get(path).toExchange();
|
||||
addLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5));
|
||||
assertResponseBody(exchange, responseBody);
|
||||
return exchange;
|
||||
|
||||
Reference in New Issue
Block a user