SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC
This commit is contained in:
@@ -1,155 +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.annotation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestCondition;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestConditionFactory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class RequestConditionFactoryTests {
|
||||
|
||||
@Test
|
||||
public void paramEquals() {
|
||||
assertEquals(getSingleParamCondition("foo"), getSingleParamCondition("foo"));
|
||||
assertFalse(getSingleParamCondition("foo").equals(getSingleParamCondition("bar")));
|
||||
assertFalse(getSingleParamCondition("foo").equals(getSingleParamCondition("FOO")));
|
||||
assertEquals(getSingleParamCondition("foo=bar"), getSingleParamCondition("foo=bar"));
|
||||
assertFalse(getSingleParamCondition("foo=bar").equals(getSingleParamCondition("FOO=bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerEquals() {
|
||||
assertEquals(getSingleHeaderCondition("foo"), getSingleHeaderCondition("foo"));
|
||||
assertEquals(getSingleHeaderCondition("foo"), getSingleHeaderCondition("FOO"));
|
||||
assertFalse(getSingleHeaderCondition("foo").equals(getSingleHeaderCondition("bar")));
|
||||
assertEquals(getSingleHeaderCondition("foo=bar"), getSingleHeaderCondition("foo=bar"));
|
||||
assertEquals(getSingleHeaderCondition("foo=bar"), getSingleHeaderCondition("FOO=bar"));
|
||||
assertEquals(getSingleHeaderCondition("content-type=text/xml"),
|
||||
getSingleHeaderCondition("Content-Type=TEXT/XML"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerPresent() {
|
||||
RequestCondition condition = getSingleHeaderCondition("accept");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerPresentNoMatch() {
|
||||
RequestCondition condition = getSingleHeaderCondition("foo");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("bar", "");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerNotPresent() {
|
||||
RequestCondition condition = getSingleHeaderCondition("!accept");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueMatch() {
|
||||
RequestCondition condition = getSingleHeaderCondition("foo=bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueNoMatch() {
|
||||
RequestCondition condition = getSingleHeaderCondition("foo=bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bazz");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerCaseSensitiveValueMatch() {
|
||||
RequestCondition condition = getSingleHeaderCondition("foo=Bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueMatchNegated() {
|
||||
RequestCondition condition = getSingleHeaderCondition("foo!=bar");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "baz");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaTypeHeaderValueMatch() {
|
||||
RequestCondition condition = getSingleHeaderCondition("accept=text/html");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "text/html");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaTypeHeaderValueMatchNegated() {
|
||||
RequestCondition condition = getSingleHeaderCondition("accept!=text/html");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/html");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
private RequestCondition getSingleHeaderCondition(String expression) {
|
||||
Set<RequestCondition> conditions = RequestConditionFactory.parseHeaders(expression);
|
||||
assertEquals(1, conditions.size());
|
||||
return conditions.iterator().next();
|
||||
}
|
||||
|
||||
private RequestCondition getSingleParamCondition(String expression) {
|
||||
Set<RequestCondition> conditions = RequestConditionFactory.parseParams(expression);
|
||||
assertEquals(1, conditions.size());
|
||||
return conditions.iterator().next();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,11 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
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.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -28,11 +23,13 @@ import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestConditionFactory;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestKey;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodMapping;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -54,8 +51,8 @@ public class RequestKeyComparatorTests {
|
||||
public void moreSpecificPatternWins() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(asList("/fo*"), null, null, null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo"), null, null, null);
|
||||
RequestKey key1 = new RequestKey(asList("/fo*"), null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo"), null);
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
@@ -64,8 +61,8 @@ public class RequestKeyComparatorTests {
|
||||
public void equalPatterns() {
|
||||
request.setRequestURI("/foo");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(asList("/foo*"), null, null, null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo*"), null, null, null);
|
||||
RequestKey key1 = new RequestKey(asList("/foo*"), null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo*"), null);
|
||||
|
||||
assertEquals(0, comparator.compare(key1, key2));
|
||||
}
|
||||
@@ -73,8 +70,8 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void greaterNumberOfMatchingPatternsWins() throws Exception {
|
||||
request.setRequestURI("/foo.html");
|
||||
RequestKey key1 = new RequestKey(asList("/foo", "*.jpeg"), null, null, null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo", "*.html"), null, null, null);
|
||||
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);
|
||||
@@ -86,18 +83,18 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
public void oneMethodWinsOverNone() {
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
RequestKey key1 = new RequestKey(null, null, null, null);
|
||||
RequestKey key2 = new RequestKey(null, asList(RequestMethod.GET), null, null);
|
||||
RequestKey key1 = new RequestKey(null, null);
|
||||
RequestKey key2 = new RequestKey(null, asList(RequestMethod.GET));
|
||||
|
||||
assertEquals(1, comparator.compare(key1, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsAndParams() {
|
||||
RequestKey empty = new RequestKey(null, null, null, null);
|
||||
RequestKey oneMethod = new RequestKey(null, asList(RequestMethod.GET), null, null);
|
||||
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);
|
||||
new RequestKey(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
|
||||
List<RequestKey> list = asList(empty, oneMethod, oneMethodOneParam);
|
||||
Collections.shuffle(list);
|
||||
Collections.sort(list, handlerMapping.getKeyComparator(request));
|
||||
@@ -110,9 +107,9 @@ public class RequestKeyComparatorTests {
|
||||
@Test
|
||||
@Ignore // TODO : remove ignore
|
||||
public void acceptHeaders() {
|
||||
RequestKey html = new RequestKey(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"));
|
||||
RequestKey xml = new RequestKey(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"));
|
||||
RequestKey none = new RequestKey(null, null, null, null);
|
||||
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);
|
||||
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
Comparator<RequestKey> comparator = handlerMapping.getKeyComparator(request);
|
||||
|
||||
@@ -16,22 +16,20 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
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.annotation.RequestConditionFactory;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestKey;
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -40,8 +38,8 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void equals() {
|
||||
RequestKey key1 = new RequestKey(asList("/foo"), asList(GET), null, null);
|
||||
RequestKey key2 = new RequestKey(asList("/foo"), asList(GET), null, null);
|
||||
RequestKey key1 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
RequestKey key2 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -49,8 +47,8 @@ public class RequestKeyTests {
|
||||
|
||||
@Test
|
||||
public void equalsPrependSlash() {
|
||||
RequestKey key1 = new RequestKey(asList("/foo"), asList(GET), null, null);
|
||||
RequestKey key2 = new RequestKey(asList("foo"), asList(GET), null, null);
|
||||
RequestKey key1 = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
RequestKey key2 = new RequestKey(singleton("foo"), singleton(GET));
|
||||
|
||||
assertEquals(key1, key2);
|
||||
assertEquals(key1.hashCode(), key2.hashCode());
|
||||
@@ -63,22 +61,22 @@ public class RequestKeyTests {
|
||||
RequestKey key1 = createKeyFromPatterns("/t1", "/t2");
|
||||
RequestKey key2 = createKeyFromPatterns("/m1", "/m2");
|
||||
RequestKey key3 = createKeyFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
|
||||
assertEquals(key3, key1.combine(key2, pathMatcher));
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
|
||||
key1 = createKeyFromPatterns("/t1");
|
||||
key2 = createKeyFromPatterns(new String[] {});
|
||||
key2 = createKeyFromPatterns();
|
||||
key3 = createKeyFromPatterns("/t1");
|
||||
assertEquals(key3, key1.combine(key2, pathMatcher));
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
|
||||
key1 = createKeyFromPatterns(new String[] {});
|
||||
key1 = createKeyFromPatterns();
|
||||
key2 = createKeyFromPatterns("/m1");
|
||||
key3 = createKeyFromPatterns("/m1");
|
||||
assertEquals(key3, key1.combine(key2, pathMatcher));
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
|
||||
key1 = createKeyFromPatterns(new String[] {});
|
||||
key2 = createKeyFromPatterns(new String[] {});
|
||||
key1 = createKeyFromPatterns();
|
||||
key2 = createKeyFromPatterns();
|
||||
key3 = createKeyFromPatterns("/");
|
||||
assertEquals(key3, key1.combine(key2, pathMatcher));
|
||||
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
|
||||
|
||||
}
|
||||
|
||||
@@ -88,39 +86,39 @@ public class RequestKeyTests {
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, null, null);
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo/bar");
|
||||
key = new RequestKey(asList("/foo/*"), null, null, null);
|
||||
key = new RequestKey(singleton("/foo/*"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull("Pattern match", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
key = new RequestKey(asList("/foo"), null, null, null);
|
||||
key = new RequestKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull("Implicit match by extension", match);
|
||||
assertEquals("Contains matched pattern", "/foo.*", match.getPatterns().iterator().next());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo/");
|
||||
key = new RequestKey(asList("/foo"), null, null, null);
|
||||
key = new RequestKey(singleton("/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
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(asList("/foo.jpg"), null, null, null);
|
||||
key = new RequestKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNull("Implicit match ignored if pattern has extension", match);
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/foo.html");
|
||||
key = new RequestKey(asList("/foo.jpg"), null, null, null);
|
||||
key = new RequestKey(singleton("/foo.jpg"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNull("Implicit match ignored on pattern with trailing slash", match);
|
||||
@@ -132,17 +130,17 @@ public class RequestKeyTests {
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, null, null);
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull("No method matches any method", match);
|
||||
|
||||
key = new RequestKey(asList("/foo"), asList(GET), null, null);
|
||||
key = new RequestKey(singleton("/foo"), singleton(GET));
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull("Exact match", match);
|
||||
|
||||
key = new RequestKey(asList("/foo"), asList(POST), null, null);
|
||||
key = new RequestKey(singleton("/foo"), singleton(POST));
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNull("No match", match);
|
||||
@@ -154,15 +152,15 @@ public class RequestKeyTests {
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo*", "/bar"), asList(GET, POST), null, null);
|
||||
RequestKey key = new RequestKey(asList("/foo*", "/bar"), asList(GET, POST));
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
RequestKey expected = new RequestKey(asList("/foo*"), asList(GET), null, null);
|
||||
RequestKey expected = new RequestKey(singleton("/foo*"), singleton(GET));
|
||||
|
||||
assertEquals("Matching RequestKey contains matched patterns and methods only", expected, match);
|
||||
|
||||
key = new RequestKey(asList("/**", "/foo*", "/foo"), null, null, null);
|
||||
key = new RequestKey(asList("/**", "/foo*", "/foo"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
expected = new RequestKey(asList("/foo", "/foo*", "/**"), null, null, null);
|
||||
expected = new RequestKey(asList("/foo", "/foo*", "/**"), null);
|
||||
|
||||
assertEquals("Matched patterns are sorted with best match at the top", expected, match);
|
||||
|
||||
@@ -175,12 +173,12 @@ public class RequestKeyTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setParameter("foo", "bar");
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null);
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null);
|
||||
key = new RequestKey(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNull(match);
|
||||
@@ -193,12 +191,12 @@ public class RequestKeyTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
RequestKey key = new RequestKey(asList("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"));
|
||||
RequestKey key = new RequestKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
|
||||
RequestKey match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNotNull(match);
|
||||
|
||||
key = new RequestKey(asList("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"));
|
||||
key = new RequestKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
|
||||
match = key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
|
||||
assertNull(match);
|
||||
@@ -208,11 +206,11 @@ public class RequestKeyTests {
|
||||
public void testCreateFromServletRequest() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestKey key = RequestKey.createFromServletRequest(request, new UrlPathHelper());
|
||||
assertEquals(new RequestKey(asList("/foo"), asList(RequestMethod.GET), null, null), key);
|
||||
assertEquals(new RequestKey(singleton("/foo"), singleton(RequestMethod.GET), null, null, null), key);
|
||||
}
|
||||
|
||||
private RequestKey createKeyFromPatterns(String... patterns) {
|
||||
return new RequestKey(asList(patterns), null, null, null);
|
||||
return new RequestKey(asList(patterns), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,20 +16,13 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -42,8 +35,8 @@ 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.servlet.mvc.method.annotation.RequestKey;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodMapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -96,7 +89,7 @@ public class RequestMappingHandlerMethodMappingTests {
|
||||
|
||||
@Test
|
||||
public void uriTemplateVariables() {
|
||||
RequestKey key = new RequestKey(Arrays.asList("/{path1}/{path2}"), null, null, null);
|
||||
RequestKey key = new RequestKey(Arrays.asList("/{path1}/{path2}"), null);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
|
||||
|
||||
mapping.handleMatch(key, request);
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class RequestConditionFactoryTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void andMatch() {
|
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition();
|
||||
RequestCondition condition2 = RequestConditionFactory.trueCondition();
|
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2);
|
||||
assertTrue(and.match(new MockHttpServletRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void andNoMatch() {
|
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition();
|
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition();
|
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2);
|
||||
assertFalse(and.match(new MockHttpServletRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orMatch() {
|
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition();
|
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition();
|
||||
RequestCondition and = RequestConditionFactory.or(condition1, condition2);
|
||||
assertTrue(and.match(new MockHttpServletRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orNoMatch() {
|
||||
RequestCondition condition1 = RequestConditionFactory.falseCondition();
|
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition();
|
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2);
|
||||
assertFalse(and.match(new MockHttpServletRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paramEquals() {
|
||||
assertEquals(RequestConditionFactory.parseParams("foo"), RequestConditionFactory.parseParams("foo"));
|
||||
assertFalse(RequestConditionFactory.parseParams("foo").equals(RequestConditionFactory.parseParams("bar")));
|
||||
assertFalse(RequestConditionFactory.parseParams("foo").equals(RequestConditionFactory.parseParams("FOO")));
|
||||
assertEquals(RequestConditionFactory.parseParams("foo=bar"), RequestConditionFactory.parseParams("foo=bar"));
|
||||
assertFalse(
|
||||
RequestConditionFactory.parseParams("foo=bar").equals(RequestConditionFactory.parseParams("FOO=bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerEquals() {
|
||||
assertEquals(RequestConditionFactory.parseHeaders("foo"), RequestConditionFactory.parseHeaders("foo"));
|
||||
assertEquals(RequestConditionFactory.parseHeaders("foo"), RequestConditionFactory.parseHeaders("FOO"));
|
||||
assertFalse(RequestConditionFactory.parseHeaders("foo").equals(RequestConditionFactory.parseHeaders("bar")));
|
||||
assertEquals(RequestConditionFactory.parseHeaders("foo=bar"), RequestConditionFactory.parseHeaders("foo=bar"));
|
||||
assertEquals(RequestConditionFactory.parseHeaders("foo=bar"), RequestConditionFactory.parseHeaders("FOO=bar"));
|
||||
assertEquals(RequestConditionFactory.parseHeaders("content-type=text/xml"),
|
||||
RequestConditionFactory.parseHeaders("Content-Type=TEXT/XML"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerPresent() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerPresentNoMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("bar", "");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerNotPresent() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("!accept");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueNoMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bazz");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerCaseSensitiveValueMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=Bar");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "bar");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerValueMatchNegated() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo!=bar");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("foo", "baz");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaTypeHeaderValueMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept=text/html");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "text/html");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaTypeHeaderValueMatchNegated() {
|
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept!=text/html");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/html");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType("text/plain");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesWildcardMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/*");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType("text/plain");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesMultipleMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain", "application/xml");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType("text/plain");
|
||||
|
||||
assertTrue(condition.match(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesSingleNoMatch() {
|
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType("application/xml");
|
||||
|
||||
assertFalse(condition.match(request));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user