Make HTTP methods a RequestCondition
This commit is contained in:
@@ -38,7 +38,7 @@ import static org.junit.Assert.*;
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RequestKeyComparatorTests {
|
||||
public class RequestMappingInfoComparatorTests {
|
||||
|
||||
private RequestMappingHandlerMapping handlerMapping;
|
||||
|
||||
@@ -90,7 +90,7 @@ public class RequestKeyComparatorTests {
|
||||
public void oneMethodWinsOverNone() {
|
||||
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator("", request);
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(null, null);
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(null, asList(RequestMethod.GET));
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(null, new RequestMethod[] {RequestMethod.GET});
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
@@ -98,9 +98,9 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void methodsAndParams() {
|
||||
RequestMappingInfo empty = new RequestMappingInfo(null, null);
|
||||
RequestMappingInfo oneMethod = new RequestMappingInfo(null, asList(RequestMethod.GET));
|
||||
RequestMappingInfo oneMethod = new RequestMappingInfo(null, new RequestMethod[] {RequestMethod.GET});
|
||||
RequestMappingInfo oneMethodOneParam =
|
||||
new RequestMappingInfo(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
|
||||
new RequestMappingInfo(null, RequestConditionFactory.parseMethods(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
|
||||
List<RequestMappingInfo> list = asList(empty, oneMethod, oneMethodOneParam);
|
||||
Collections.shuffle(list);
|
||||
Collections.sort(list, handlerMapping.getMappingComparator("", request));
|
||||
@@ -16,33 +16,32 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
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 RequestMappingInfo} tests.
|
||||
*
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RequestKeyTests {
|
||||
public class RequestMappingInfoTests {
|
||||
|
||||
@Test
|
||||
public void equals() {
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), methods(GET));
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(singleton("/foo"), methods(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -50,8 +49,8 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void equalsPrependSlash() {
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(singleton("foo"), singleton(GET));
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), methods(GET));
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(singleton("foo"), methods(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -86,7 +85,7 @@ public class RequestKeyTests {
|
||||
key3 = createKeyFromPatterns("/t1");
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void matchPatternsToRequest() {
|
||||
UrlPathHelper pathHelper = new UrlPathHelper();
|
||||
@@ -94,7 +93,8 @@ public class RequestKeyTests {
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
RequestMappingInfo match =
|
||||
key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
@@ -142,12 +142,12 @@ public class RequestKeyTests {
|
||||
|
||||
assertNotNull("No method matches any method", match);
|
||||
|
||||
key = new RequestMappingInfo(singleton("/foo"), singleton(GET));
|
||||
key = new RequestMappingInfo(singleton("/foo"), methods(GET));
|
||||
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull("Exact match", match);
|
||||
|
||||
key = new RequestMappingInfo(singleton("/foo"), singleton(POST));
|
||||
key = new RequestMappingInfo(singleton("/foo"), methods(POST));
|
||||
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull("No match", match);
|
||||
@@ -159,9 +159,9 @@ public class RequestKeyTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestMappingInfo key = new RequestMappingInfo(asList("/foo*", "/bar"), asList(GET, POST));
|
||||
RequestMappingInfo key = new RequestMappingInfo(asList("/foo*", "/bar"), methods(GET, POST));
|
||||
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
RequestMappingInfo expected = new RequestMappingInfo(singleton("/foo*"), singleton(GET));
|
||||
RequestMappingInfo expected = new RequestMappingInfo(singleton("/foo*"), methods(GET));
|
||||
|
||||
assertEquals("Matching RequestKey contains matched patterns and methods only", expected, match);
|
||||
|
||||
@@ -179,12 +179,15 @@ public class RequestKeyTests {
|
||||
request.setParameter("foo", "bar");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestMappingInfo key = new RequestMappingInfo(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
|
||||
RequestMappingInfo key =
|
||||
new RequestMappingInfo(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null,
|
||||
null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null,
|
||||
null);
|
||||
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
@@ -197,12 +200,15 @@ public class RequestKeyTests {
|
||||
request.addHeader("foo", "bar");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
|
||||
RequestMappingInfo key =
|
||||
new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"),
|
||||
null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"),
|
||||
null);
|
||||
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
@@ -215,14 +221,14 @@ public class RequestKeyTests {
|
||||
request.setContentType("text/plain");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
"text/plain"));
|
||||
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, null,
|
||||
RequestConditionFactory.parseConsumes("text/plain"));
|
||||
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
|
||||
"application/xml"));
|
||||
key = new RequestMappingInfo(singleton("/foo"), null, null, null,
|
||||
RequestConditionFactory.parseConsumes("application/xml"));
|
||||
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
|
||||
|
||||
assertNull(match);
|
||||
@@ -232,4 +238,13 @@ public class RequestKeyTests {
|
||||
return new RequestMappingInfo(asList(patterns), null);
|
||||
}
|
||||
|
||||
private RequestMethod[] methods(RequestMethod... methods) {
|
||||
if (methods != null) {
|
||||
return methods;
|
||||
}
|
||||
else {
|
||||
return new RequestMethod[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.servlet.mvc.method.condition;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class RequestMethodsRequestConditionTests {
|
||||
|
||||
@Test
|
||||
public void methodMatch() {
|
||||
RequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodNoMatch() {
|
||||
RequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleMethodsMatch() {
|
||||
RequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void compareTo() {
|
||||
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.HEAD);
|
||||
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST);
|
||||
RequestMethodsRequestCondition condition3 = new RequestMethodsRequestCondition();
|
||||
|
||||
int result = condition1.compareTo(condition2);
|
||||
assertTrue("Invalid comparison result: " + result, result < 0);
|
||||
|
||||
result = condition2.compareTo(condition1);
|
||||
assertTrue("Invalid comparison result: " + result, result > 0);
|
||||
|
||||
result = condition2.compareTo(condition3);
|
||||
assertTrue("Invalid comparison result: " + result, result < 0);
|
||||
|
||||
result = condition1.compareTo(condition1);
|
||||
assertEquals("Invalid comparison result ", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST);
|
||||
|
||||
RequestMethodsRequestCondition result = condition1.combine(condition2);
|
||||
assertEquals(2, result.getConditions().size());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user