SPR-7812 Add CustomRequestCondition

This commit is contained in:
Rossen Stoyanchev
2011-06-17 23:19:14 +00:00
parent 7dd6932910
commit 4826cae064
16 changed files with 407 additions and 239 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.util.UrlPathHelper;
/**
* Test for {@link AbstractHandlerMethodMapping}.
@@ -89,10 +90,13 @@ public class HandlerMethodMappingTests {
private static class MyHandlerMethodMapping extends AbstractHandlerMethodMapping<String> {
private UrlPathHelper pathHelper = new UrlPathHelper();
private PathMatcher pathMatcher = new AntPathMatcher();
@Override
protected String getMatchingMapping(String pattern, String lookupPath, HttpServletRequest request) {
protected String getMatchingMapping(String pattern, HttpServletRequest request) {
String lookupPath = pathHelper.getLookupPathForRequest(request);
return pathMatcher.match(pattern, lookupPath) ? pattern : null;
}
@@ -103,7 +107,8 @@ public class HandlerMethodMappingTests {
}
@Override
protected Comparator<String> getMappingComparator(String lookupPath, HttpServletRequest request) {
protected Comparator<String> getMappingComparator(HttpServletRequest request) {
String lookupPath = pathHelper.getLookupPathForRequest(request);
return pathMatcher.getPatternComparator(lookupPath);
}

View File

@@ -34,7 +34,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import org.springframework.web.servlet.mvc.method.condition.ParamsRequestCondition;
import org.springframework.web.servlet.mvc.method.condition.ProducesRequestCondition;
import org.springframework.web.servlet.mvc.method.condition.RequestMethodsRequestCondition;
import org.springframework.web.util.UrlPathHelper;
/**
* Test fixture with {@link RequestMappingHandlerMapping} testing its {@link RequestMappingInfo} comparator.
@@ -57,8 +56,7 @@ public class RequestMappingInfoComparatorTests {
@Test
public void moreSpecificPatternWins() {
request.setRequestURI("/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(lookupPath, request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/fo*"});
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo"});
@@ -68,8 +66,7 @@ public class RequestMappingInfoComparatorTests {
@Test
public void equalPatterns() {
request.setRequestURI("/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(lookupPath, request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/foo*"});
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo*"});
@@ -79,20 +76,19 @@ public class RequestMappingInfoComparatorTests {
@Test
public void greaterNumberOfMatchingPatternsWins() throws Exception {
request.setRequestURI("/foo.html");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/foo", "*.jpeg"});
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo", "*.html"});
RequestMappingInfo match1 = handlerMapping.getMatchingMapping(key1, lookupPath, request);
RequestMappingInfo match2 = handlerMapping.getMatchingMapping(key2, lookupPath, request);
RequestMappingInfo match1 = handlerMapping.getMatchingMapping(key1, request);
RequestMappingInfo match2 = handlerMapping.getMatchingMapping(key2, request);
List<RequestMappingInfo> matches = asList(match1, match2);
Collections.sort(matches, handlerMapping.getMappingComparator(lookupPath, request));
Collections.sort(matches, handlerMapping.getMappingComparator(request));
assertSame(match2.getPatternsCondition(), matches.get(0).getPatternsCondition());
}
@Test
public void oneMethodWinsOverNone() {
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator("", request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
RequestMappingInfo key1 = new RequestMappingInfo(null);
RequestMappingInfo key2 = new RequestMappingInfo(null, new RequestMethod[] {RequestMethod.GET});
@@ -108,7 +104,7 @@ public class RequestMappingInfoComparatorTests {
new ParamsRequestCondition("foo"), null, null, null);
List<RequestMappingInfo> list = asList(empty, oneMethod, oneMethodOneParam);
Collections.shuffle(list);
Collections.sort(list, handlerMapping.getMappingComparator("", request));
Collections.sort(list, handlerMapping.getMappingComparator(request));
assertEquals(oneMethodOneParam, list.get(0));
assertEquals(oneMethod, list.get(1));
@@ -122,7 +118,7 @@ public class RequestMappingInfoComparatorTests {
RequestMappingInfo none = new RequestMappingInfo(null);
request.addHeader("Accept", "application/xml, text/html");
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator("", request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
int result = comparator.compare(html, xml);
assertTrue("Invalid comparison result: " + result, result > 0);
@@ -134,14 +130,14 @@ public class RequestMappingInfoComparatorTests {
request = new MockHttpServletRequest();
request.addHeader("Accept", "application/xml, text/*");
comparator = handlerMapping.getMappingComparator("", request);
comparator = handlerMapping.getMappingComparator(request);
assertTrue(comparator.compare(html, xml) > 0);
assertTrue(comparator.compare(xml, html) < 0);
request = new MockHttpServletRequest();
request.addHeader("Accept", "application/pdf");
comparator = handlerMapping.getMappingComparator("", request);
comparator = handlerMapping.getMappingComparator(request);
assertTrue(comparator.compare(html, xml) == 0);
assertTrue(comparator.compare(xml, html) == 0);
@@ -149,7 +145,7 @@ public class RequestMappingInfoComparatorTests {
// See SPR-7000
request = new MockHttpServletRequest();
request.addHeader("Accept", "text/html;q=0.9,application/xml");
comparator = handlerMapping.getMappingComparator("", request);
comparator = handlerMapping.getMappingComparator(request);
assertTrue(comparator.compare(html, xml) > 0);
assertTrue(comparator.compare(xml, html) < 0);

View File

@@ -87,34 +87,34 @@ public class RequestMappingInfoTests {
@Test
public void matchPatternsToRequest() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMapping(request);
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
assertNotNull(match);
request = new MockHttpServletRequest("GET", "/foo/bar");
match = createFromPatterns("/foo/*").getMatchingRequestMapping(request);
match = createFromPatterns("/foo/*").getMatchingRequestMappingInfo(request);
assertNotNull("Pattern match", match);
request = new MockHttpServletRequest("GET", "/foo.html");
match = createFromPatterns("/foo").getMatchingRequestMapping(request);
match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
assertNotNull("Implicit match by extension", match);
assertEquals("Contains matched pattern", "/foo.*", match.getPatternsCondition().getPatterns().iterator().next());
request = new MockHttpServletRequest("GET", "/foo/");
match = createFromPatterns("/foo").getMatchingRequestMapping(request);
match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
assertNotNull("Implicit match by trailing slash", match);
assertEquals("Contains matched pattern", "/foo/", match.getPatternsCondition().getPatterns().iterator().next());
request = new MockHttpServletRequest("GET", "/foo.html");
match = createFromPatterns("/foo.jpg").getMatchingRequestMapping(request);
match = createFromPatterns("/foo.jpg").getMatchingRequestMappingInfo(request);
assertNull("Implicit match ignored if pattern has extension", match);
request = new MockHttpServletRequest("GET", "/foo.html");
match = createFromPatterns("/foo.jpg").getMatchingRequestMapping(request);
match = createFromPatterns("/foo.jpg").getMatchingRequestMappingInfo(request);
assertNull("Implicit match ignored on pattern with trailing slash", match);
}
@@ -124,17 +124,17 @@ public class RequestMappingInfoTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
RequestMappingInfo key = createFromPatterns("/foo");
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMapping(request);
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
assertNotNull("No method matches any method", match);
key = new RequestMappingInfo(new String[]{"/foo"}, GET);
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNotNull("Exact match", match);
key = new RequestMappingInfo(new String[]{"/foo"}, POST);
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNull("No match", match);
}
@@ -144,13 +144,13 @@ public class RequestMappingInfoTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
RequestMappingInfo key = new RequestMappingInfo(new String[] {"/foo*", "/bar"}, GET, POST);
RequestMappingInfo match = key.getMatchingRequestMapping(request);
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
RequestMappingInfo expected = new RequestMappingInfo(new String[] {"/foo*"}, GET);
assertEquals("Matching RequestKey contains matched patterns and methods only", expected, match);
key = createFromPatterns("/**", "/foo*", "/foo");
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
expected = createFromPatterns("/foo", "/foo*", "/**");
assertEquals("Matched patterns are sorted with best match at the top", expected, match);
@@ -165,14 +165,14 @@ public class RequestMappingInfoTests {
new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null,
new ParamsRequestCondition("foo=bar"), null, null, null);
RequestMappingInfo match = key.getMatchingRequestMapping(request);
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
assertNotNull(match);
key = new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null,
new ParamsRequestCondition("foo!=bar"), null, null, null);
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNull(match);
}
@@ -186,14 +186,14 @@ public class RequestMappingInfoTests {
new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null,
new HeadersRequestCondition("foo=bar"), null, null);
RequestMappingInfo match = key.getMatchingRequestMapping(request);
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
assertNotNull(match);
key = new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null,
new HeadersRequestCondition("foo!=bar"), null, null);
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNull(match);
}
@@ -207,14 +207,14 @@ public class RequestMappingInfoTests {
new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null, null,
new ConsumesRequestCondition("text/plain"), null);
RequestMappingInfo match = key.getMatchingRequestMapping(request);
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
assertNotNull(match);
key = new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null, null,
new ConsumesRequestCondition("application/xml"), null);
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNull(match);
}
@@ -228,14 +228,14 @@ public class RequestMappingInfoTests {
new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null, null, null,
new ProducesRequestCondition("text/plain"));
RequestMappingInfo match = key.getMatchingRequestMapping(request);
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
assertNotNull(match);
key = new RequestMappingInfo(
new PatternsRequestCondition("/foo"), null, null, null, null,
new ProducesRequestCondition("application/xml"));
match = key.getMatchingRequestMapping(request);
match = key.getMatchingRequestMappingInfo(request);
assertNull(match);
}