SPR-8247
This commit is contained in:
@@ -16,20 +16,21 @@
|
||||
|
||||
package org.springframework.web.servlet.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
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;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test for {@link AbstractHandlerMethodMapping}.
|
||||
@@ -54,14 +55,14 @@ public class HandlerMethodMappingTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void registerDuplicates() {
|
||||
mapping.registerHandlerMethod("foo", handlerMethod1);
|
||||
mapping.registerHandlerMethod("foo", handlerMethod2);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "foo", handlerMethod1);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "foo", handlerMethod2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directMatch() throws Exception {
|
||||
String key = "foo";
|
||||
mapping.registerHandlerMethod(key, handlerMethod1);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), key, handlerMethod1);
|
||||
|
||||
HandlerMethod result = mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
|
||||
assertEquals(handlerMethod1, result);
|
||||
@@ -69,8 +70,8 @@ public class HandlerMethodMappingTests {
|
||||
|
||||
@Test
|
||||
public void patternMatch() throws Exception {
|
||||
mapping.registerHandlerMethod("/fo*", handlerMethod1);
|
||||
mapping.registerHandlerMethod("/f*", handlerMethod1);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "/fo*", handlerMethod1);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "/f*", handlerMethod1);
|
||||
|
||||
HandlerMethod result = mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
|
||||
assertEquals(handlerMethod1, result);
|
||||
@@ -78,40 +79,29 @@ public class HandlerMethodMappingTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void ambiguousMatch() throws Exception {
|
||||
mapping.registerHandlerMethod("/f?o", handlerMethod1);
|
||||
mapping.registerHandlerMethod("/fo?", handlerMethod2);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "/f?o", handlerMethod1);
|
||||
mapping.registerHandlerMethod(new HashSet<String>(), "/fo?", handlerMethod2);
|
||||
|
||||
mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
|
||||
}
|
||||
|
||||
private static class MyHandlerMethodMapping extends AbstractHandlerMethodMapping<String> {
|
||||
|
||||
private UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
||||
private PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
@Override
|
||||
protected String getKeyForRequest(HttpServletRequest request) throws Exception {
|
||||
return urlPathHelper.getLookupPathForRequest(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMatchingKey(String pattern, HttpServletRequest request) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
|
||||
protected String getMatchingMappingKey(String pattern, String lookupPath, HttpServletRequest request) {
|
||||
return pathMatcher.match(pattern, lookupPath) ? pattern : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getKeyForMethod(String beanName, Method method) {
|
||||
protected String getMappingKeyForMethod(String beanName, Method method) {
|
||||
String methodName = method.getName();
|
||||
return methodName.startsWith("handler") ? methodName : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Comparator<String> getKeyComparator(HttpServletRequest request) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
|
||||
protected Comparator<String> getMappingKeyComparator(String lookupPath, HttpServletRequest request) {
|
||||
return pathMatcher.getPatternComparator(lookupPath);
|
||||
}
|
||||
|
||||
@@ -119,6 +109,11 @@ public class HandlerMethodMappingTests {
|
||||
protected boolean isHandler(String beanName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getMappingPaths(String key) {
|
||||
return new HashSet<String>();
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyHandler {
|
||||
|
||||
@@ -27,12 +27,13 @@ import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestMappingHandlerMethodMapping} testing its {@link RequestKey} comparator.
|
||||
* Test fixture with {@link RequestMappingHandlerMethodMapping} testing its {@link RequestMappingKey} comparator.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -52,9 +53,10 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void moreSpecificPatternWins() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(asList("/fo*"), null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo"), null);
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator(lookupPath, request);
|
||||
RequestMappingKey key1 = new RequestMappingKey(asList("/fo*"), null);
|
||||
RequestMappingKey key2 = new RequestMappingKey(asList("/foo"), null);
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
@@ -62,9 +64,10 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void equalPatterns() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(asList("/foo*"), null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo*"), null);
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator(lookupPath, request);
|
||||
RequestMappingKey key1 = new RequestMappingKey(asList("/foo*"), null);
|
||||
RequestMappingKey key2 = new RequestMappingKey(asList("/foo*"), null);
|
||||
|
||||
assertEquals(0, comparator.compare(key1, key2));
|
||||
}
|
||||
@@ -72,34 +75,35 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void greaterNumberOfMatchingPatternsWins() throws Exception {
|
||||
request.setRequestURI("/foo.html");
|
||||
RequestKey key1 = new RequestKey(asList("/foo", "*.jpeg"), null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo", "*.html"), null);
|
||||
RequestKey match1 = handlerMapping.getMatchingKey(key1, request);
|
||||
RequestKey match2 = handlerMapping.getMatchingKey(key2, request);
|
||||
List<RequestKey> matches = asList(match1, match2);
|
||||
Collections.sort(matches, handlerMapping.getKeyComparator(request));
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
RequestMappingKey key1 = new RequestMappingKey(asList("/foo", "*.jpeg"), null);
|
||||
RequestMappingKey key2 = new RequestMappingKey(asList("/foo", "*.html"), null);
|
||||
RequestMappingKey match1 = handlerMapping.getMatchingMappingKey(key1, lookupPath, request);
|
||||
RequestMappingKey match2 = handlerMapping.getMatchingMappingKey(key2, lookupPath, request);
|
||||
List<RequestMappingKey> matches = asList(match1, match2);
|
||||
Collections.sort(matches, handlerMapping.getMappingKeyComparator(lookupPath, request));
|
||||
|
||||
assertSame(match2.getPatterns(), matches.get(0).getPatterns());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneMethodWinsOverNone() {
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(null, null);
|
||||
RequestKey key2 = new RequestKey(null, asList(RequestMethod.GET));
|
||||
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator("", request);
|
||||
RequestMappingKey key1 = new RequestMappingKey(null, null);
|
||||
RequestMappingKey key2 = new RequestMappingKey(null, asList(RequestMethod.GET));
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsAndParams() {
|
||||
RequestKey empty = new RequestKey(null, null);
|
||||
RequestKey oneMethod = new RequestKey(null, asList(RequestMethod.GET));
|
||||
RequestKey oneMethodOneParam =
|
||||
new RequestKey(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
|
||||
List<RequestKey> list = asList(empty, oneMethod, oneMethodOneParam);
|
||||
RequestMappingKey empty = new RequestMappingKey(null, null);
|
||||
RequestMappingKey oneMethod = new RequestMappingKey(null, asList(RequestMethod.GET));
|
||||
RequestMappingKey oneMethodOneParam =
|
||||
new RequestMappingKey(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
|
||||
List<RequestMappingKey> list = asList(empty, oneMethod, oneMethodOneParam);
|
||||
Collections.shuffle(list);
|
||||
Collections.sort(list, handlerMapping.getKeyComparator(request));
|
||||
Collections.sort(list, handlerMapping.getMappingKeyComparator("", request));
|
||||
|
||||
assertEquals(oneMethodOneParam, list.get(0));
|
||||
assertEquals(oneMethod, list.get(1));
|
||||
@@ -109,12 +113,12 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
@Ignore // TODO : remove ignore
|
||||
public void acceptHeaders() {
|
||||
RequestKey html = new RequestKey(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"), null);
|
||||
RequestKey xml = new RequestKey(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"), null);
|
||||
RequestKey none = new RequestKey(null, null);
|
||||
RequestMappingKey html = new RequestMappingKey(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"), null);
|
||||
RequestMappingKey xml = new RequestMappingKey(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"), null);
|
||||
RequestMappingKey none = new RequestMappingKey(null, null);
|
||||
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator("", request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
@@ -125,14 +129,14 @@ public class RequestKeyComparatorTests {
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/*");
|
||||
comparator = handlerMapping.getKeyComparator(request);
|
||||
comparator = handlerMapping.getMappingKeyComparator("", request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/pdf");
|
||||
comparator = handlerMapping.getKeyComparator(request);
|
||||
comparator = handlerMapping.getMappingKeyComparator("", request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) == 0);
|
||||
assertTrue(comparator.compare(xml, html) == 0);
|
||||
@@ -140,7 +144,7 @@ public class RequestKeyComparatorTests {
|
||||
// See SPR-7000
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "text/html;q=0.9,application/xml");
|
||||
comparator = handlerMapping.getKeyComparator(request);
|
||||
comparator = handlerMapping.getMappingKeyComparator("", request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
|
||||
@@ -16,22 +16,23 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import org.junit.Test;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link RequestKey} tests.
|
||||
* Test fixture for {@link RequestMappingKey} tests.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -40,8 +41,8 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void equals() {
|
||||
RequestKey key1 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
RequestKey key2 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
RequestMappingKey key1 = new RequestMappingKey(singleton("/foo"), singleton(GET));
|
||||
RequestMappingKey key2 = new RequestMappingKey(singleton("/foo"), singleton(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -49,8 +50,8 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void equalsPrependSlash() {
|
||||
RequestKey key1 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
RequestKey key2 = new RequestKey(singleton("foo"), singleton(GET));
|
||||
RequestMappingKey key1 = new RequestMappingKey(singleton("/foo"), singleton(GET));
|
||||
RequestMappingKey key2 = new RequestMappingKey(singleton("foo"), singleton(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -60,9 +61,9 @@ public class RequestKeyTests {
|
||||
public void combinePatterns() {
|
||||
AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
RequestKey key1 = createKeyFromPatterns("/t1", "/t2");
|
||||
RequestKey key2 = createKeyFromPatterns("/m1", "/m2");
|
||||
RequestKey key3 = createKeyFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
|
||||
RequestMappingKey key1 = createKeyFromPatterns("/t1", "/t2");
|
||||
RequestMappingKey key2 = createKeyFromPatterns("/m1", "/m2");
|
||||
RequestMappingKey key3 = createKeyFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
|
||||
key1 = createKeyFromPatterns("/t1");
|
||||
@@ -88,154 +89,147 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void matchPatternsToRequest() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
UrlPathHelper pathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null);
|
||||
RequestMappingKey match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo/bar");
|
||||
key = new RequestKey(singleton("/foo/*"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo/*"), null);
|
||||
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNotNull("Pattern match", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
key = new RequestKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNotNull("Implicit match by extension", match);
|
||||
assertEquals("Contains matched pattern", "/foo.*", match.getPatterns().iterator().next());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo/");
|
||||
key = new RequestKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNotNull("Implicit match by trailing slash", match);
|
||||
assertEquals("Contains matched pattern", "/foo/", match.getPatterns().iterator().next());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
key = new RequestKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNull("Implicit match ignored if pattern has extension", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
key = new RequestKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNull("Implicit match ignored on pattern with trailing slash", match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchRequestMethods() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null);
|
||||
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull("No method matches any method", match);
|
||||
|
||||
key = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), singleton(GET));
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull("Exact match", match);
|
||||
|
||||
key = new RequestKey(singleton("/foo"), singleton(POST));
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), singleton(POST));
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull("No match", match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchingKeyContent() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo*", "/bar"), asList(GET, POST));
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestKey expected = new RequestKey(singleton("/foo*"), singleton(GET));
|
||||
RequestMappingKey key = new RequestMappingKey(asList("/foo*", "/bar"), asList(GET, POST));
|
||||
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
RequestMappingKey expected = new RequestMappingKey(singleton("/foo*"), singleton(GET));
|
||||
|
||||
assertEquals("Matching RequestKey contains matched patterns and methods only", expected, match);
|
||||
|
||||
key = new RequestKey(asList("/**", "/foo*", "/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
expected = new RequestKey(asList("/foo", "/foo*", "/**"), null);
|
||||
key = new RequestMappingKey(asList("/**", "/foo*", "/foo"), null);
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
expected = new RequestMappingKey(asList("/foo", "/foo*", "/**"), null);
|
||||
|
||||
assertEquals("Matched patterns are sorted with best match at the top", expected, match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paramsCondition() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setParameter("foo", "bar");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestMappingKey key = new RequestMappingKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
|
||||
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestKey(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headersCondition() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.addHeader("foo", "bar");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
|
||||
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
key = new RequestMappingKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesCondition() {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setContentType("text/plain");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
"text/plain"));
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
key = new RequestMappingKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
"application/xml"));
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
match = key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFromServletRequest() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestKey key = RequestKey.createFromServletRequest(request, new UrlPathHelper());
|
||||
assertEquals(new RequestKey(singleton("/foo"), singleton(RequestMethod.GET), null, null, null), key);
|
||||
}
|
||||
|
||||
private RequestKey createKeyFromPatterns(String... patterns) {
|
||||
return new RequestKey(asList(patterns), null);
|
||||
private RequestMappingKey createKeyFromPatterns(String... patterns) {
|
||||
return new RequestMappingKey(asList(patterns), null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,6 @@ import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -41,6 +40,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
import org.springframework.web.servlet.handler.MappedInterceptor;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestMappingHandlerMethodMapping}.
|
||||
@@ -102,8 +102,6 @@ public class RequestMappingHandlerMethodMappingTests {
|
||||
assertEquals(emptyMethod.getMethod(), hm.getMethod());
|
||||
}
|
||||
|
||||
// TODO: SPR-8247
|
||||
@Ignore
|
||||
@Test
|
||||
public void bestMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
@@ -126,10 +124,11 @@ public class RequestMappingHandlerMethodMappingTests {
|
||||
|
||||
@Test
|
||||
public void uriTemplateVariables() {
|
||||
RequestKey key = new RequestKey(Arrays.asList("/{path1}/{path2}"), null);
|
||||
RequestMappingKey key = new RequestMappingKey(Arrays.asList("/{path1}/{path2}"), null);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
mapping.handleMatch(key, request);
|
||||
mapping.handleMatch(key, lookupPath, request);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, String> actual = (Map<String, String>) request.getAttribute(
|
||||
|
||||
Reference in New Issue
Block a user