SPR-7812 Add CustomRequestCondition, PatternsRequestCondition, and other condition related tests
This commit is contained in:
parent
4d27cde6b7
commit
6007801537
|
|
@ -204,16 +204,18 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
|||
logger.info("Mapped \"" + mapping + "\" onto " + handlerMethod);
|
||||
}
|
||||
|
||||
Set<String> paths = getMappingPaths(mapping);
|
||||
for (String path : paths) {
|
||||
urlMap.add(path, mapping);
|
||||
Set<String> patterns = getMappingPathPatterns(mapping);
|
||||
for (String pattern : patterns) {
|
||||
if (!getPathMatcher().isPattern(pattern)) {
|
||||
urlMap.add(pattern, mapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL paths associated with the given mapping.
|
||||
*/
|
||||
protected abstract Set<String> getMappingPaths(T mapping);
|
||||
protected abstract Set<String> getMappingPathPatterns(T mapping);
|
||||
|
||||
@Override
|
||||
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
|
||||
|
|
|
|||
|
|
@ -25,10 +25,10 @@ import java.util.Iterator;
|
|||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> implements RequestCondition<T> {
|
||||
public abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> implements RequestCondition<T> {
|
||||
|
||||
/**
|
||||
* Returns the discrete expressions a request condition is composed of such as URL patterns,
|
||||
* Returns the discrete items a request condition is composed of such as URL patterns,
|
||||
* HTTP request methods, parameter expressions, etc.
|
||||
*/
|
||||
protected abstract Collection<?> getContent();
|
||||
|
|
@ -65,8 +65,8 @@ abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> i
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the individual expressions of the condition are combined via logical
|
||||
* conjunction (" && "); or {@code false} otherwise.
|
||||
* The notation to use when printing discrete items of content in the toString() method.
|
||||
* For example URL patterns use " || " while parameter expressions use " && ".
|
||||
*/
|
||||
protected abstract String getToStringInfix();
|
||||
|
||||
|
|
|
|||
|
|
@ -14,42 +14,44 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.condition;
|
||||
package org.springframework.web.servlet.mvc.method;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestCondition;
|
||||
|
||||
/**
|
||||
* Wraps and delegates operations to a custom {@link RequestCondition} whose type is not known and even its
|
||||
* presence is not guaranteed ahead of time. The main purpose of this class is to ensure a type-safe and
|
||||
* null-safe way of combining and comparing custom request conditions.
|
||||
* Wraps and delegates operations to a {@link RequestCondition} whose type is not known ahead of time. The main goal
|
||||
* is to provide type-safe and null-safe way of comparing and combining optional custom {@link RequestCondition}s.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
public final class CustomRequestCondition extends AbstractRequestCondition<CustomRequestCondition> {
|
||||
final class CustomRequestCondition extends AbstractRequestCondition<CustomRequestCondition> {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private final RequestCondition customCondition;
|
||||
|
||||
/**
|
||||
* Creates a {@link CustomRequestCondition} that wraps the given {@link RequestCondition} instance.
|
||||
* @param requestCondition the custom request condition to wrap
|
||||
* Creates an instance that wraps the given custom request condition.
|
||||
* @param requestCondition the custom request condition
|
||||
*/
|
||||
public CustomRequestCondition(RequestCondition<?> requestCondition) {
|
||||
CustomRequestCondition(RequestCondition<?> requestCondition) {
|
||||
this.customCondition = requestCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty {@link CustomRequestCondition}.
|
||||
* Creates an instance that does not wrap any custom request condition.
|
||||
*/
|
||||
public CustomRequestCondition() {
|
||||
CustomRequestCondition() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public RequestCondition<?> getRequestCondition() {
|
||||
public RequestCondition<?> getCondition() {
|
||||
return customCondition;
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +62,7 @@ public final class CustomRequestCondition extends AbstractRequestCondition<Custo
|
|||
|
||||
@Override
|
||||
protected String getToStringInfix() {
|
||||
return "";
|
||||
return " ";
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -18,9 +18,7 @@ package org.springframework.web.servlet.mvc.method;
|
|||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.CustomRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
|
||||
|
|
@ -39,8 +37,7 @@ import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondit
|
|||
* <li>{@link ProducesRequestCondition}</li>
|
||||
* </ul>
|
||||
*
|
||||
* Optionally a custom request condition may also be provided by wrapping it in an instance
|
||||
* of {@link CustomRequestCondition}.
|
||||
* Optionally a custom request condition may also be provided.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
|
|
@ -60,95 +57,82 @@ public final class RequestMappingInfo {
|
|||
|
||||
private final ProducesRequestCondition producesCondition;
|
||||
|
||||
private CustomRequestCondition customCondition = new CustomRequestCondition();
|
||||
private final CustomRequestCondition customCondition;
|
||||
|
||||
private int hash;
|
||||
|
||||
/**
|
||||
* Creates a new {@code RequestMappingInfo} instance.
|
||||
*/
|
||||
public RequestMappingInfo(PatternsRequestCondition patterns,
|
||||
RequestMethodsRequestCondition methods,
|
||||
ParamsRequestCondition params,
|
||||
HeadersRequestCondition headers,
|
||||
ConsumesRequestCondition consumes,
|
||||
ProducesRequestCondition produces) {
|
||||
this(patterns, methods, params, headers, consumes, produces, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RequestMappingInfo} instance also providing a custom {@link RequestCondition}.
|
||||
*/
|
||||
public RequestMappingInfo(PatternsRequestCondition patterns,
|
||||
RequestMethodsRequestCondition methods,
|
||||
ParamsRequestCondition params,
|
||||
HeadersRequestCondition headers,
|
||||
ConsumesRequestCondition consumes,
|
||||
ProducesRequestCondition produces,
|
||||
CustomRequestCondition custom) {
|
||||
RequestCondition<?> custom) {
|
||||
this.patternsCondition = patterns != null ? patterns : new PatternsRequestCondition();
|
||||
this.methodsCondition = methods != null ? methods : new RequestMethodsRequestCondition();
|
||||
this.paramsCondition = params != null ? params : new ParamsRequestCondition();
|
||||
this.headersCondition = headers != null ? headers : new HeadersRequestCondition();
|
||||
this.consumesCondition = consumes != null ? consumes : new ConsumesRequestCondition();
|
||||
this.producesCondition = produces != null ? produces : new ProducesRequestCondition();
|
||||
this.customCondition = custom != null ? custom : new CustomRequestCondition();
|
||||
this.customCondition = custom != null ? new CustomRequestCondition(custom) : new CustomRequestCondition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Package protected constructor for tests.
|
||||
*/
|
||||
RequestMappingInfo(String[] patterns, RequestMethod... methods) {
|
||||
this(new PatternsRequestCondition(patterns), new RequestMethodsRequestCondition(methods), null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL patterns of this request mapping info.
|
||||
* Returns the URL patterns of this {@link RequestMappingInfo};
|
||||
* or instance with 0 patterns, never {@code null}
|
||||
*/
|
||||
public PatternsRequestCondition getPatternsCondition() {
|
||||
return patternsCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTTP request methods of this {@link RequestMappingInfo}.
|
||||
* Returns the HTTP request methods of this {@link RequestMappingInfo};
|
||||
* or instance with 0 request methods, never {@code null}
|
||||
*/
|
||||
public RequestMethodsRequestCondition getMethodsCondition() {
|
||||
return methodsCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "parameters" condition of this {@link RequestMappingInfo}.
|
||||
* Returns the "parameters" condition of this {@link RequestMappingInfo};
|
||||
* or instance with 0 parameter expressions, never {@code null}
|
||||
*/
|
||||
public ParamsRequestCondition getParamsCondition() {
|
||||
return paramsCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "headers" condition of this {@link RequestMappingInfo}.
|
||||
* Returns the "headers" condition of this {@link RequestMappingInfo};
|
||||
* or instance with 0 header expressions, never {@code null}
|
||||
*/
|
||||
public HeadersRequestCondition getHeadersCondition() {
|
||||
return headersCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "consumes" condition of this {@link RequestMappingInfo}.
|
||||
* Returns the "consumes" condition of this {@link RequestMappingInfo};
|
||||
* or instance with 0 consumes expressions, never {@code null}
|
||||
*/
|
||||
public ConsumesRequestCondition getConsumesCondition() {
|
||||
return consumesCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "produces" condition of this {@link RequestMappingInfo}.
|
||||
* Returns the "produces" condition of this {@link RequestMappingInfo};
|
||||
* or instance with 0 produces expressions, never {@code null}
|
||||
*/
|
||||
public ProducesRequestCondition getProducesCondition() {
|
||||
return producesCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom request condition.
|
||||
* Returns the "custom" condition of this {@link RequestMappingInfo}; or {@code null}
|
||||
*/
|
||||
public void setCustomCondition(CustomRequestCondition customCondition) {
|
||||
this.customCondition = customCondition;
|
||||
public RequestCondition<?> getCustomCondition() {
|
||||
return customCondition.getCondition();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -165,7 +149,7 @@ public final class RequestMappingInfo {
|
|||
ProducesRequestCondition produces = this.producesCondition.combine(other.producesCondition);
|
||||
CustomRequestCondition custom = this.customCondition.combine(other.customCondition);
|
||||
|
||||
return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom);
|
||||
return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom.getCondition());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,7 +159,7 @@ public final class RequestMappingInfo {
|
|||
* the current request, sorted with best matching patterns on top.
|
||||
* @return a new instance in case all conditions match; or {@code null} otherwise
|
||||
*/
|
||||
public RequestMappingInfo getMatchingRequestMappingInfo(HttpServletRequest request) {
|
||||
public RequestMappingInfo getMatchingInfo(HttpServletRequest request) {
|
||||
RequestMethodsRequestCondition methods = methodsCondition.getMatchingCondition(request);
|
||||
ParamsRequestCondition params = paramsCondition.getMatchingCondition(request);
|
||||
HeadersRequestCondition headers = headersCondition.getMatchingCondition(request);
|
||||
|
|
@ -196,13 +180,13 @@ public final class RequestMappingInfo {
|
|||
return null;
|
||||
}
|
||||
|
||||
return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom);
|
||||
return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom.getCondition());
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares "this" info (i.e. the current instance) with another info in the context of a request.
|
||||
* <p>Note: it is assumed both instances have been obtained via
|
||||
* {@link #getMatchingRequestMappingInfo(HttpServletRequest)} to ensure they have conditions with
|
||||
* {@link #getMatchingInfo(HttpServletRequest)} to ensure they have conditions with
|
||||
* content relevant to current request.
|
||||
*/
|
||||
public int compareTo(RequestMappingInfo other, HttpServletRequest request) {
|
||||
|
|
|
|||
|
|
@ -48,27 +48,21 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
|
||||
@Override
|
||||
protected void handlerMethodsInitialized(Map<RequestMappingInfo, HandlerMethod> handlerMethods) {
|
||||
List<RequestMappingInfo> mappings = new ArrayList<RequestMappingInfo>(handlerMethods.keySet());
|
||||
while (mappings.size() > 1) {
|
||||
RequestMappingInfo mapping = mappings.remove(0);
|
||||
for (RequestMappingInfo otherMapping : mappings) {
|
||||
// TODO: further validate mapping conditions
|
||||
List<RequestMappingInfo> infos = new ArrayList<RequestMappingInfo>(handlerMethods.keySet());
|
||||
while (infos.size() > 1) {
|
||||
RequestMappingInfo info1 = infos.remove(0);
|
||||
for (RequestMappingInfo info2 : infos) {
|
||||
// TODO: validate duplicate consumable and producible media types
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL paths associated with this {@link RequestMappingInfo}.
|
||||
* Get the URL path patterns associated with this {@link RequestMappingInfo}.
|
||||
*/
|
||||
@Override
|
||||
protected Set<String> getMappingPaths(RequestMappingInfo mapping) {
|
||||
Set<String> paths = new HashSet<String>();
|
||||
for (String pattern : mapping.getPatternsCondition().getPatterns()) {
|
||||
if (!getPathMatcher().isPattern(pattern)) {
|
||||
paths.add(pattern);
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
protected Set<String> getMappingPathPatterns(RequestMappingInfo info) {
|
||||
return info.getPatternsCondition().getPatterns();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -79,8 +73,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
* @returns a RequestMappingInfo instance in case of a match; or {@code null} in case of no match.
|
||||
*/
|
||||
@Override
|
||||
protected RequestMappingInfo getMatchingMapping(RequestMappingInfo mapping, HttpServletRequest request) {
|
||||
return mapping.getMatchingRequestMappingInfo(request);
|
||||
protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {
|
||||
return info.getMatchingInfo(request);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -89,8 +83,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
@Override
|
||||
protected Comparator<RequestMappingInfo> getMappingComparator(final HttpServletRequest request) {
|
||||
return new Comparator<RequestMappingInfo>() {
|
||||
public int compare(RequestMappingInfo info, RequestMappingInfo otherInfo) {
|
||||
return info.compareTo(otherInfo, request);
|
||||
public int compare(RequestMappingInfo info1, RequestMappingInfo info2) {
|
||||
return info1.compareTo(info2, request);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
|||
new ParamsRequestCondition(annotation.params()),
|
||||
new HeadersRequestCondition(annotation.headers()),
|
||||
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
|
||||
new ProducesRequestCondition(annotation.produces(), annotation.headers()));
|
||||
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ public class HandlerMethodMappingTests {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getMappingPaths(String key) {
|
||||
protected Set<String> getMappingPathPatterns(String key) {
|
||||
return new HashSet<String>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* 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.condition;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class PatternsRequestConditionTests {
|
||||
|
||||
@Test
|
||||
public void prependSlash() {
|
||||
PatternsRequestCondition c = new PatternsRequestCondition("foo");
|
||||
assertEquals("/foo", c.getPatterns().iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prependNonEmptyPatternsOnly() {
|
||||
PatternsRequestCondition c = new PatternsRequestCondition("");
|
||||
assertEquals("Do not prepend empty patterns (SPR-8255)", "", c.getPatterns().iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineEmptySets() {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition();
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition();
|
||||
|
||||
assertEquals(new PatternsRequestCondition(""), c1.combine(c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineOnePatternWithEmptySet() {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/type1", "/type2");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition();
|
||||
|
||||
assertEquals(new PatternsRequestCondition("/type1", "/type2"), c1.combine(c2));
|
||||
|
||||
c1 = new PatternsRequestCondition();
|
||||
c2 = new PatternsRequestCondition("/method1", "/method2");
|
||||
|
||||
assertEquals(new PatternsRequestCondition("/method1", "/method2"), c1.combine(c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineMultiplePatterns() {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/t1", "/t2");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/m1", "/m2");
|
||||
|
||||
assertEquals(new PatternsRequestCondition("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2"), c1.combine(c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchDirectPath() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo"));
|
||||
|
||||
assertNotNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchPattern() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo/*");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo/bar"));
|
||||
|
||||
assertNotNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchSortPatterns() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/**", "/foo/bar", "/foo/*");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo/bar"));
|
||||
PatternsRequestCondition expected = new PatternsRequestCondition("/foo/bar", "/foo/*", "/**");
|
||||
|
||||
assertEquals(expected, match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchImplicitByExtension() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo.html"));
|
||||
|
||||
assertNotNull(match);
|
||||
assertEquals("/foo.*", match.getPatterns().iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchImplicitTrailingSlash() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo/"));
|
||||
|
||||
assertNotNull(match);
|
||||
assertEquals("/foo/", match.getPatterns().iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchPatternContainsExtension() {
|
||||
PatternsRequestCondition condition = new PatternsRequestCondition("/foo.jpg");
|
||||
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo.html"));
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareEqualPatterns() {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo*");
|
||||
|
||||
assertEquals(0, c1.compareTo(c2, new MockHttpServletRequest("GET", "/foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparePatternSpecificity() {
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/fo*");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo");
|
||||
|
||||
assertEquals(1, c1.compareTo(c2, new MockHttpServletRequest("GET", "/foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareNumberOfMatchingPatterns() throws Exception {
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
|
||||
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg");
|
||||
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html");
|
||||
|
||||
PatternsRequestCondition match1 = c1.getMatchingCondition(request);
|
||||
PatternsRequestCondition match2 = c2.getMatchingCondition(request);
|
||||
|
||||
assertEquals(1, match1.compareTo(match2, request));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -26,7 +26,6 @@ import java.util.Collection;
|
|||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression;
|
||||
|
||||
/**
|
||||
|
|
@ -84,6 +83,42 @@ public class ProducesRequestConditionTests {
|
|||
assertNull(condition.getMatchingCondition(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() {
|
||||
ProducesRequestCondition html = new ProducesRequestCondition("text/html");
|
||||
ProducesRequestCondition xml = new ProducesRequestCondition("application/xml");
|
||||
ProducesRequestCondition none = new ProducesRequestCondition();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
|
||||
assertTrue(html.compareTo(xml, request) > 0);
|
||||
assertTrue(xml.compareTo(html, request) < 0);
|
||||
assertTrue(xml.compareTo(none, request) < 0);
|
||||
assertTrue(none.compareTo(xml, request) > 0);
|
||||
assertTrue(html.compareTo(none, request) < 0);
|
||||
assertTrue(none.compareTo(html, request) > 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/*");
|
||||
|
||||
assertTrue(html.compareTo(xml, request) > 0);
|
||||
assertTrue(xml.compareTo(html, request) < 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/pdf");
|
||||
|
||||
assertTrue(html.compareTo(xml, request) == 0);
|
||||
assertTrue(xml.compareTo(html, request) == 0);
|
||||
|
||||
// See SPR-7000
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "text/html;q=0.9,application/xml");
|
||||
|
||||
assertTrue(html.compareTo(xml, request) > 0);
|
||||
assertTrue(xml.compareTo(html, request) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareToSingle() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
|
@ -151,7 +186,7 @@ public class ProducesRequestConditionTests {
|
|||
result = condition2.compareTo(condition1, request);
|
||||
assertTrue("Invalid comparison result: " + result, result < 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain");
|
||||
|
|
@ -212,12 +247,6 @@ public class ProducesRequestConditionTests {
|
|||
fail("Condition [" + s + "] not found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,13 +16,14 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.condition;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -56,6 +57,14 @@ public class RequestMethodsRequestConditionTests {
|
|||
assertNotNull(condition.getMatchingCondition(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noMethodsMatchAll() {
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition();
|
||||
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", "")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class CustomRequestConditionTests {
|
||||
|
||||
@Test
|
||||
public void combineEmpty() {
|
||||
CustomRequestCondition empty = new CustomRequestCondition();
|
||||
CustomRequestCondition custom = new CustomRequestCondition(new ParamsRequestCondition("name"));
|
||||
|
||||
assertSame(empty, empty.combine(new CustomRequestCondition()));
|
||||
assertSame(custom, custom.combine(empty));
|
||||
assertSame(custom, empty.combine(custom));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
CustomRequestCondition params1 = new CustomRequestCondition(new ParamsRequestCondition("name1"));
|
||||
CustomRequestCondition params2 = new CustomRequestCondition(new ParamsRequestCondition("name2"));
|
||||
CustomRequestCondition expected = new CustomRequestCondition(new ParamsRequestCondition("name1", "name2"));
|
||||
|
||||
assertEquals(expected, params1.combine(params2));
|
||||
}
|
||||
|
||||
@Test(expected=ClassCastException.class)
|
||||
public void combineIncompatible() {
|
||||
CustomRequestCondition params = new CustomRequestCondition(new ParamsRequestCondition("name"));
|
||||
CustomRequestCondition headers = new CustomRequestCondition(new HeadersRequestCondition("name"));
|
||||
params.combine(headers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void match() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.setParameter("name1", "value1");
|
||||
|
||||
RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
|
||||
CustomRequestCondition custom = new CustomRequestCondition(rm);
|
||||
RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
assertEquals(expected, custom.getMatchingCondition(request).getCondition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchEmpty() {
|
||||
CustomRequestCondition empty = new CustomRequestCondition();
|
||||
assertSame(empty, empty.getMatchingCondition(new MockHttpServletRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare() {
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
CustomRequestCondition params11 = new CustomRequestCondition(new ParamsRequestCondition("1"));
|
||||
CustomRequestCondition params12 = new CustomRequestCondition(new ParamsRequestCondition("1", "2"));
|
||||
|
||||
assertEquals(1, params11.compareTo(params12, request));
|
||||
assertEquals(-1, params12.compareTo(params11, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareEmpty() {
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
CustomRequestCondition empty = new CustomRequestCondition();
|
||||
CustomRequestCondition empty2 = new CustomRequestCondition();
|
||||
CustomRequestCondition custom = new CustomRequestCondition(new ParamsRequestCondition("name"));
|
||||
|
||||
assertEquals(0, empty.compareTo(empty2, request));
|
||||
assertEquals(-1, custom.compareTo(empty, request));
|
||||
assertEquals(1, empty.compareTo(custom, request));
|
||||
}
|
||||
|
||||
@Test(expected=ClassCastException.class)
|
||||
public void compareIncompatible() {
|
||||
CustomRequestCondition params = new CustomRequestCondition(new ParamsRequestCondition("name"));
|
||||
CustomRequestCondition headers = new CustomRequestCondition(new HeadersRequestCondition("name"));
|
||||
params.compareTo(headers, new MockHttpServletRequest());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,167 +0,0 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestMappingHandlerMapping} testing its {@link RequestMappingInfo} comparator.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RequestMappingInfoComparatorTests {
|
||||
|
||||
private TestRequestMappingInfoHandlerMapping handlerMapping;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
|
||||
this.request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moreSpecificPatternWins() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/fo*"});
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo"});
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalPatterns() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/foo*"});
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo*"});
|
||||
|
||||
assertEquals(0, comparator.compare(key1, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void greaterNumberOfMatchingPatternsWins() throws Exception {
|
||||
request.setRequestURI("/foo.html");
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(new String[]{"/foo", "*.jpeg"});
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(new String[]{"/foo", "*.html"});
|
||||
RequestMappingInfo match1 = handlerMapping.getMatchingMapping(key1, request);
|
||||
RequestMappingInfo match2 = handlerMapping.getMatchingMapping(key2, request);
|
||||
List<RequestMappingInfo> matches = asList(match1, match2);
|
||||
Collections.sort(matches, handlerMapping.getMappingComparator(request));
|
||||
|
||||
assertSame(match2.getPatternsCondition(), matches.get(0).getPatternsCondition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneMethodWinsOverNone() {
|
||||
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(null);
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(null, new RequestMethod[] {RequestMethod.GET});
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsAndParams() {
|
||||
RequestMappingInfo empty = new RequestMappingInfo(null);
|
||||
RequestMappingInfo oneMethod = new RequestMappingInfo(null, new RequestMethod[] {RequestMethod.GET});
|
||||
RequestMappingInfo oneMethodOneParam =
|
||||
new RequestMappingInfo(null, new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo"), null, null, null);
|
||||
List<RequestMappingInfo> list = asList(empty, oneMethod, oneMethodOneParam);
|
||||
Collections.shuffle(list);
|
||||
Collections.sort(list, handlerMapping.getMappingComparator(request));
|
||||
|
||||
assertEquals(oneMethodOneParam, list.get(0));
|
||||
assertEquals(oneMethod, list.get(1));
|
||||
assertEquals(empty, list.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void produces() {
|
||||
RequestMappingInfo html = new RequestMappingInfo(null, null, null, null, null, new ProducesRequestCondition("text/html"));
|
||||
RequestMappingInfo xml = new RequestMappingInfo(null, null, null, null, null, new ProducesRequestCondition("application/xml"));
|
||||
RequestMappingInfo none = new RequestMappingInfo(null);
|
||||
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(request);
|
||||
|
||||
int result = comparator.compare(html, xml);
|
||||
assertTrue("Invalid comparison result: " + result, result > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
assertTrue(comparator.compare(xml, none) < 0);
|
||||
assertTrue(comparator.compare(none, xml) > 0);
|
||||
assertTrue(comparator.compare(html, none) < 0);
|
||||
assertTrue(comparator.compare(none, html) > 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/*");
|
||||
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);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) == 0);
|
||||
assertTrue(comparator.compare(xml, html) == 0);
|
||||
|
||||
// See SPR-7000
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "text/html;q=0.9,application/xml");
|
||||
comparator = handlerMapping.getMappingComparator(request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
}
|
||||
|
||||
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
|
||||
|
||||
@Override
|
||||
protected boolean isHandler(Class<?> beanType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,7 +24,10 @@ import static org.junit.Assert.assertSame;
|
|||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
@ -85,6 +88,16 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
mapping.setApplicationContext(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMappingPathPatterns() throws Exception {
|
||||
RequestMappingInfo info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo/*", "/foo", "/bar/*", "/bar"), null, null, null, null, null, null);
|
||||
Set<String> paths = mapping.getMappingPathPatterns(info);
|
||||
HashSet<String> expected = new HashSet<String>(Arrays.asList("/foo/*", "/foo", "/bar/*", "/bar"));
|
||||
|
||||
assertEquals(expected, paths);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
|
@ -132,7 +145,8 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
|
||||
@Test
|
||||
public void uriTemplateVariables() {
|
||||
RequestMappingInfo key = new RequestMappingInfo(new String[] {"/{path1}/{path2}"});
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/{path2}");
|
||||
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
|
||||
|
|
@ -206,7 +220,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
new ParamsRequestCondition(annotation.params()),
|
||||
new HeadersRequestCondition(annotation.headers()),
|
||||
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
|
||||
new ProducesRequestCondition(annotation.produces(), annotation.headers()));
|
||||
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,19 +16,26 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.method;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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 static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link RequestMappingInfo} tests.
|
||||
|
|
@ -39,209 +46,276 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
|
|||
public class RequestMappingInfoTests {
|
||||
|
||||
@Test
|
||||
public void equals() {
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(new String[] {"/foo"}, GET);
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(new String[] {"/foo"}, GET);
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
public void createEmpty() {
|
||||
RequestMappingInfo info = new RequestMappingInfo(null, null, null, null, null, null, null);
|
||||
|
||||
assertEquals(0, info.getPatternsCondition().getPatterns().size());
|
||||
assertEquals(0, info.getMethodsCondition().getMethods().size());
|
||||
assertEquals(0, info.getConsumesCondition().getMediaTypes().size());
|
||||
assertEquals(0, info.getProducesCondition().getMediaTypes().size());
|
||||
assertNotNull(info.getParamsCondition());
|
||||
assertNotNull(info.getHeadersCondition());
|
||||
assertNull(info.getCustomCondition());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void equalsPrependSlash() {
|
||||
RequestMappingInfo key1 = new RequestMappingInfo(new String[] {"/foo"}, GET);
|
||||
RequestMappingInfo key2 = new RequestMappingInfo(new String[] {"foo"}, GET);
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combinePatterns() {
|
||||
RequestMappingInfo key1 = createFromPatterns("/t1", "/t2");
|
||||
RequestMappingInfo key2 = createFromPatterns("/m1", "/m2");
|
||||
RequestMappingInfo key3 = createFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
|
||||
assertEquals(key3.getPatternsCondition(), key1.combine(key2).getPatternsCondition());
|
||||
|
||||
key1 = createFromPatterns("/t1");
|
||||
key2 = createFromPatterns();
|
||||
key3 = createFromPatterns("/t1");
|
||||
assertEquals(key3.getPatternsCondition(), key1.combine(key2).getPatternsCondition());
|
||||
|
||||
key1 = createFromPatterns();
|
||||
key2 = createFromPatterns("/m1");
|
||||
key3 = createFromPatterns("/m1");
|
||||
assertEquals(key3.getPatternsCondition(), key1.combine(key2).getPatternsCondition());
|
||||
|
||||
key1 = createFromPatterns();
|
||||
key2 = createFromPatterns();
|
||||
key3 = createFromPatterns("");
|
||||
assertEquals(key3.getPatternsCondition(), key1.combine(key2).getPatternsCondition());
|
||||
|
||||
key1 = createFromPatterns("/t1");
|
||||
key2 = createFromPatterns("");
|
||||
key3 = createFromPatterns("/t1");
|
||||
assertEquals(key3.getPatternsCondition(), key1.combine(key2).getPatternsCondition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchPatternsToRequest() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo/bar");
|
||||
match = createFromPatterns("/foo/*").getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNotNull("Pattern match", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
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").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").getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNull("Implicit match ignored if pattern has extension", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
match = createFromPatterns("/foo.jpg").getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNull("Implicit match ignored on pattern with trailing slash", match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchRequestMethods() {
|
||||
public void matchPatternsCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
RequestMappingInfo key = createFromPatterns("/foo");
|
||||
RequestMappingInfo match = createFromPatterns("/foo").getMatchingRequestMappingInfo(request);
|
||||
RequestMappingInfo info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo*", "/bar"), null, null, null, null, null, null);
|
||||
RequestMappingInfo expected = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo*"), null, null, null, null, null, null);
|
||||
|
||||
assertEquals(expected, info.getMatchingInfo(request));
|
||||
|
||||
assertNotNull("No method matches any method", match);
|
||||
|
||||
key = new RequestMappingInfo(new String[]{"/foo"}, GET);
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNotNull("Exact match", match);
|
||||
|
||||
key = new RequestMappingInfo(new String[]{"/foo"}, POST);
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
|
||||
assertNull("No match", match);
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/**", "/foo*", "/foo"), null, null, null, null, null, null);
|
||||
expected = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo", "/foo*", "/**"), null, null, null, null, null, null);
|
||||
|
||||
assertEquals(expected, info.getMatchingInfo(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchingKeyContent() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
RequestMappingInfo key = new RequestMappingInfo(new String[] {"/foo*", "/bar"}, GET, POST);
|
||||
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.getMatchingRequestMappingInfo(request);
|
||||
expected = createFromPatterns("/foo", "/foo*", "/**");
|
||||
|
||||
assertEquals("Matched patterns are sorted with best match at the top", expected, match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paramsCondition() {
|
||||
public void matchParamsCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setParameter("foo", "bar");
|
||||
|
||||
RequestMappingInfo key =
|
||||
RequestMappingInfo info =
|
||||
new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null,
|
||||
new ParamsRequestCondition("foo=bar"), null, null, null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
|
||||
new ParamsRequestCondition("foo=bar"), null, null, null, null);
|
||||
RequestMappingInfo match = info.getMatchingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null,
|
||||
new ParamsRequestCondition("foo!=bar"), null, null, null);
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
new ParamsRequestCondition("foo!=bar"), null, null, null, null);
|
||||
match = info.getMatchingInfo(request);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headersCondition() {
|
||||
public void matchHeadersCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
RequestMappingInfo key =
|
||||
RequestMappingInfo info =
|
||||
new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null,
|
||||
new HeadersRequestCondition("foo=bar"), null, null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
|
||||
new HeadersRequestCondition("foo=bar"), null, null, null);
|
||||
RequestMappingInfo match = info.getMatchingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null,
|
||||
new HeadersRequestCondition("foo!=bar"), null, null);
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
new HeadersRequestCondition("foo!=bar"), null, null, null);
|
||||
match = info.getMatchingInfo(request);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesCondition() {
|
||||
public void matchConsumesCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setContentType("text/plain");
|
||||
|
||||
RequestMappingInfo key =
|
||||
RequestMappingInfo info =
|
||||
new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null, null,
|
||||
new ConsumesRequestCondition("text/plain"), null);
|
||||
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
|
||||
new ConsumesRequestCondition("text/plain"), null, null);
|
||||
RequestMappingInfo match = info.getMatchingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null, null,
|
||||
new ConsumesRequestCondition("application/xml"), null);
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
new ConsumesRequestCondition("application/xml"), null, null);
|
||||
match = info.getMatchingInfo(request);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void producesCondition() {
|
||||
public void matchProducesCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.addHeader("Accept", "text/plain");
|
||||
|
||||
RequestMappingInfo key =
|
||||
RequestMappingInfo info =
|
||||
new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null, null, null,
|
||||
new ProducesRequestCondition("text/plain"));
|
||||
RequestMappingInfo match = key.getMatchingRequestMappingInfo(request);
|
||||
new ProducesRequestCondition("text/plain"), null);
|
||||
RequestMappingInfo match = info.getMatchingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestMappingInfo(
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null, null, null,
|
||||
new ProducesRequestCondition("application/xml"));
|
||||
match = key.getMatchingRequestMappingInfo(request);
|
||||
new ProducesRequestCondition("application/xml"), null);
|
||||
match = info.getMatchingInfo(request);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
private RequestMappingInfo createFromPatterns(String... patterns) {
|
||||
return new RequestMappingInfo(patterns);
|
||||
@Test
|
||||
public void matchCustomCondition() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setParameter("foo", "bar");
|
||||
|
||||
RequestMappingInfo info =
|
||||
new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null, null, null, null, null,
|
||||
new ParamsRequestCondition("foo=bar"));
|
||||
RequestMappingInfo match = info.getMatchingInfo(request);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
info = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"), null,
|
||||
new ParamsRequestCondition("foo!=bar"), null, null, null,
|
||||
new ParamsRequestCondition("foo!=bar"));
|
||||
match = info.getMatchingInfo(request);
|
||||
|
||||
assertNull(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTwoHttpMethodsOneParam() {
|
||||
RequestMappingInfo none = new RequestMappingInfo(null, null, null, null, null, null, null);
|
||||
RequestMappingInfo oneMethod =
|
||||
new RequestMappingInfo(null,
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET), null, null, null, null, null);
|
||||
RequestMappingInfo oneMethodOneParam =
|
||||
new RequestMappingInfo(null,
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo"), null, null, null, null);
|
||||
|
||||
Comparator<RequestMappingInfo> comparator = new Comparator<RequestMappingInfo>() {
|
||||
public int compare(RequestMappingInfo info, RequestMappingInfo otherInfo) {
|
||||
return info.compareTo(otherInfo, new MockHttpServletRequest());
|
||||
}
|
||||
};
|
||||
|
||||
List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam);
|
||||
Collections.shuffle(list);
|
||||
Collections.sort(list, comparator);
|
||||
|
||||
assertEquals(oneMethodOneParam, list.get(0));
|
||||
assertEquals(oneMethod, list.get(1));
|
||||
assertEquals(none, list.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals() {
|
||||
|
||||
RequestMappingInfo info1 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
RequestMappingInfo info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertEquals(info1, info2);
|
||||
assertEquals(info1.hashCode(), info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo", "/NOOOOOO"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("/NOOOOOO"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("/NOOOOOO"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/NOOOOOO"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/NOOOOOO"),
|
||||
new ParamsRequestCondition("customFoo=customBar"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
|
||||
info2 = new RequestMappingInfo(
|
||||
new PatternsRequestCondition("/foo"),
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET),
|
||||
new ParamsRequestCondition("foo=bar"),
|
||||
new HeadersRequestCondition("foo=bar"),
|
||||
new ConsumesRequestCondition("text/plain"),
|
||||
new ProducesRequestCondition("text/plain"),
|
||||
new ParamsRequestCondition("customFoo=NOOOOOO"));
|
||||
|
||||
assertFalse(info1.equals(info2));
|
||||
assertTrue(info1.hashCode() != info2.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue