This commit is contained in:
Rossen Stoyanchev 2016-01-24 19:00:18 -05:00
parent 2b3ad218e5
commit fb7dfc4569
2 changed files with 173 additions and 164 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,12 +16,19 @@
package org.springframework.web.servlet.mvc.condition; package org.springframework.web.servlet.mvc.condition;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.junit.Assert.*; 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 static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -30,8 +37,7 @@ public class RequestMethodsRequestConditionTests {
@Test @Test
public void methodMatch() { public void methodMatch() {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET); RequestCondition condition = new RequestMethodsRequestCondition(GET);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
assertNotNull(condition.getMatchingCondition(request)); assertNotNull(condition.getMatchingCondition(request));
@ -39,8 +45,7 @@ public class RequestMethodsRequestConditionTests {
@Test @Test
public void methodNoMatch() { public void methodNoMatch() {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET); RequestCondition condition = new RequestMethodsRequestCondition(GET);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo");
assertNull(condition.getMatchingCondition(request)); assertNull(condition.getMatchingCondition(request));
@ -48,16 +53,17 @@ public class RequestMethodsRequestConditionTests {
@Test @Test
public void multipleMethodsMatch() { public void multipleMethodsMatch() {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
RequestMethodsRequestCondition actual = condition.getMatchingCondition(request);
assertNotNull(condition.getMatchingCondition(request)); assertNotNull(actual);
assertEquals(Collections.singleton(GET), actual.getContent());
} }
@Test @Test
public void noMethodsMatchAll() { public void noDeclaredMethodsMatchesAllMethods() {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); RequestCondition condition = new RequestMethodsRequestCondition();
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", ""))); assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", ""))); assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
@ -66,8 +72,7 @@ public class RequestMethodsRequestConditionTests {
@Test @Test
public void unknownMethodType() throws Exception { public void unknownMethodType() throws Exception {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestCondition condition = new RequestMethodsRequestCondition(GET, POST);
MockHttpServletRequest request = new MockHttpServletRequest("PROPFIND", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("PROPFIND", "/foo");
assertNull(condition.getMatchingCondition(request)); assertNull(condition.getMatchingCondition(request));
@ -75,33 +80,32 @@ public class RequestMethodsRequestConditionTests {
@Test @Test
public void compareTo() { public void compareTo() {
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.HEAD); RequestMethodsRequestCondition c1 = new RequestMethodsRequestCondition(GET, HEAD);
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST); RequestMethodsRequestCondition c2 = new RequestMethodsRequestCondition(POST);
RequestMethodsRequestCondition condition3 = new RequestMethodsRequestCondition(); RequestMethodsRequestCondition c3 = new RequestMethodsRequestCondition();
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
int result = condition1.compareTo(condition2, request); int result = c1.compareTo(c2, request);
assertTrue("Invalid comparison result: " + result, result < 0); assertTrue("Invalid comparison result: " + result, result < 0);
result = condition2.compareTo(condition1, request); result = c2.compareTo(c1, request);
assertTrue("Invalid comparison result: " + result, result > 0); assertTrue("Invalid comparison result: " + result, result > 0);
result = condition2.compareTo(condition3, request); result = c2.compareTo(c3, request);
assertTrue("Invalid comparison result: " + result, result < 0); assertTrue("Invalid comparison result: " + result, result < 0);
result = condition1.compareTo(condition1, request); result = c1.compareTo(c1, request);
assertEquals("Invalid comparison result ", 0, result); assertEquals("Invalid comparison result ", 0, result);
} }
@Test @Test
public void combine() { public void combine() {
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET); RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(GET);
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST); RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(POST);
RequestMethodsRequestCondition result = condition1.combine(condition2); RequestMethodsRequestCondition result = condition1.combine(condition2);
assertEquals(2, result.getContent().size()); assertEquals(2, result.getContent().size());
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,6 +59,7 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
/** /**
* Test fixture with {@link RequestMappingInfoHandlerMapping}. * Test fixture with {@link RequestMappingInfoHandlerMapping}.
* *
@ -77,6 +78,7 @@ public class RequestMappingInfoHandlerMappingTests {
private HandlerMethod emptyMethod; private HandlerMethod emptyMethod;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
TestController testController = new TestController(); TestController testController = new TestController();
@ -91,96 +93,87 @@ public class RequestMappingInfoHandlerMappingTests {
this.handlerMapping.setRemoveSemicolonContent(false); this.handlerMapping.setRemoveSemicolonContent(false);
} }
@Test @Test
public void getMappingPathPatterns() throws Exception { public void getMappingPathPatterns() throws Exception {
RequestMappingInfo info = new RequestMappingInfo( String[] patterns = {"/foo/*", "/foo", "/bar/*", "/bar"};
new PatternsRequestCondition("/foo/*", "/foo", "/bar/*", "/bar"), null, null, null, null, null, null); RequestMappingInfo info = RequestMappingInfo.paths(patterns).build();
Set<String> paths = this.handlerMapping.getMappingPathPatterns(info); Set<String> actual = this.handlerMapping.getMappingPathPatterns(info);
HashSet<String> expected = new HashSet<String>(Arrays.asList("/foo/*", "/foo", "/bar/*", "/bar"));
assertEquals(expected, paths); assertEquals(new HashSet<>(Arrays.asList(patterns)), actual);
} }
@Test @Test
public void directMatch() throws Exception { public void getHandlerDirectMatch() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler(); HandlerMethod handlerMethod = getHandler(request);
assertEquals(this.fooMethod.getMethod(), hm.getMethod());
assertEquals(this.fooMethod.getMethod(), handlerMethod.getMethod());
} }
@Test @Test
public void globMatch() throws Exception { public void getHandlerGlobMatch() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bar"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bar");
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler(); HandlerMethod handlerMethod = getHandler(request);
assertEquals(this.barMethod.getMethod(), hm.getMethod()); assertEquals(this.barMethod.getMethod(), handlerMethod.getMethod());
} }
@Test @Test
public void emptyPathMatch() throws Exception { public void getHandlerEmptyPathMatch() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler(); HandlerMethod handlerMethod = getHandler(request);
assertEquals(this.emptyMethod.getMethod(), hm.getMethod());
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
request = new MockHttpServletRequest("GET", "/"); request = new MockHttpServletRequest("GET", "/");
hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler(); handlerMethod = getHandler(request);
assertEquals(this.emptyMethod.getMethod(), hm.getMethod());
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
} }
@Test @Test
public void bestMatch() throws Exception { public void getHandlerBestMatch() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setParameter("p", "anything"); request.setParameter("p", "anything");
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler(); HandlerMethod handlerMethod = getHandler(request);
assertEquals(this.fooParamMethod.getMethod(), hm.getMethod());
assertEquals(this.fooParamMethod.getMethod(), handlerMethod.getMethod());
} }
@Test @Test
public void requestMethodNotAllowed() throws Exception { public void getHandlerRequestMethodNotAllowed() throws Exception {
try { try {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar"); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
this.handlerMapping.getHandler(request); this.handlerMapping.getHandler(request);
fail("HttpRequestMethodNotSupportedException expected"); fail("HttpRequestMethodNotSupportedException expected");
} }
catch (HttpRequestMethodNotSupportedException ex) { catch (HttpRequestMethodNotSupportedException ex) {
assertArrayEquals("Invalid supported methods", new String[]{"GET", "HEAD"}, ex.getSupportedMethods()); assertArrayEquals("Invalid supported methods", new String[]{"GET", "HEAD"},
ex.getSupportedMethods());
} }
} }
// SPR-9603 // SPR-9603
@Test(expected=HttpMediaTypeNotAcceptableException.class) @Test(expected=HttpMediaTypeNotAcceptableException.class)
public void requestMethodMatchFalsePositive() throws Exception { public void getHandlerRequestMethodMatchFalsePositive() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users");
request.addHeader("Accept", "application/xml"); request.addHeader("Accept", "application/xml");
this.handlerMapping.registerHandler(new UserController()); this.handlerMapping.registerHandler(new UserController());
this.handlerMapping.getHandler(request); this.handlerMapping.getHandler(request);
} }
// SPR-8462
@Test @Test
public void mediaTypeNotSupported() throws Exception { public void getHandlerMediaTypeNotSupported() throws Exception {
testMediaTypeNotSupported("/person/1"); testHttpMediaTypeNotSupportedException("/person/1");
testHttpMediaTypeNotSupportedException("/person/1/");
// SPR-8462 testHttpMediaTypeNotSupportedException("/person/1.json");
testMediaTypeNotSupported("/person/1/");
testMediaTypeNotSupported("/person/1.json");
}
private void testMediaTypeNotSupported(String url) throws Exception {
try {
MockHttpServletRequest request = new MockHttpServletRequest("PUT", url);
request.setContentType("application/json");
this.handlerMapping.getHandler(request);
fail("HttpMediaTypeNotSupportedException expected");
}
catch (HttpMediaTypeNotSupportedException ex) {
assertEquals("Invalid supported consumable media types",
Arrays.asList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
}
} }
@Test @Test
public void testMediaTypeNotValue() throws Exception { public void getHandlerTestInvalidContentType() throws Exception {
try { try {
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1"); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1");
request.setContentType("bogus"); request.setContentType("bogus");
@ -192,32 +185,19 @@ public class RequestMappingInfoHandlerMappingTests {
} }
} }
// SPR-8462
@Test @Test
public void mediaTypeNotAccepted() throws Exception { public void getHandlerMediaTypeNotAccepted() throws Exception {
testMediaTypeNotAccepted("/persons"); testHttpMediaTypeNotAcceptableException("/persons");
testHttpMediaTypeNotAcceptableException("/persons/");
// SPR-8462 testHttpMediaTypeNotAcceptableException("/persons.json");
testMediaTypeNotAccepted("/persons/");
testMediaTypeNotAccepted("/persons.json");
}
private void testMediaTypeNotAccepted(String url) throws Exception {
try {
MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
request.addHeader("Accept", "application/json");
this.handlerMapping.getHandler(request);
fail("HttpMediaTypeNotAcceptableException expected");
}
catch (HttpMediaTypeNotAcceptableException ex) {
assertEquals("Invalid supported producible media types",
Arrays.asList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
}
} }
// SPR-12854 // SPR-12854
@Test @Test
public void testUnsatisfiedServletRequestParameterException() throws Exception { public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception {
try { try {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params");
this.handlerMapping.getHandler(request); this.handlerMapping.getHandler(request);
@ -232,17 +212,51 @@ public class RequestMappingInfoHandlerMappingTests {
} }
@Test @Test
public void uriTemplateVariables() { public void getHandlerProducibleMediaTypesAttribute() throws Exception {
PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/{path2}"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/content");
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null); request.addHeader("Accept", "application/xml");
this.handlerMapping.getHandler(request);
String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
assertEquals(Collections.singleton(MediaType.APPLICATION_XML), request.getAttribute(name));
request = new MockHttpServletRequest("GET", "/content");
request.addHeader("Accept", "application/json");
this.handlerMapping.getHandler(request);
assertNull("Negated expression shouldn't be listed as producible type", request.getAttribute(name));
}
@Test
public void getHandlerMappedInterceptors() throws Exception {
String path = "/foo";
HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);
TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
mapping.registerHandler(new TestController());
mapping.setInterceptors(new Object[] { mappedInterceptor });
mapping.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
assertNotNull(chain);
assertNotNull(chain.getInterceptors());
assertSame(interceptor, chain.getInterceptors()[0]);
chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
assertNull(chain);
}
@SuppressWarnings("unchecked")
@Test
public void handleMatchUriTemplateVariables() {
RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
this.handlerMapping.handleMatch(key, lookupPath, request); this.handlerMapping.handleMatch(key, lookupPath, request);
@SuppressWarnings("unchecked") String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map<String, String> uriVariables = Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
(Map<String, String>) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
assertNotNull(uriVariables); assertNotNull(uriVariables);
assertEquals("1", uriVariables.get("path1")); assertEquals("1", uriVariables.get("path1"));
@ -251,10 +265,10 @@ public class RequestMappingInfoHandlerMappingTests {
// SPR-9098 // SPR-9098
@SuppressWarnings("unchecked")
@Test @Test
public void uriTemplateVariablesDecode() { public void handleMatchUriTemplateVariablesDecode() {
PatternsRequestCondition patterns = new PatternsRequestCondition("/{group}/{identifier}"); RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build();
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb");
UrlPathHelper pathHelper = new UrlPathHelper(); UrlPathHelper pathHelper = new UrlPathHelper();
@ -264,9 +278,8 @@ public class RequestMappingInfoHandlerMappingTests {
this.handlerMapping.setUrlPathHelper(pathHelper); this.handlerMapping.setUrlPathHelper(pathHelper);
this.handlerMapping.handleMatch(key, lookupPath, request); this.handlerMapping.handleMatch(key, lookupPath, request);
@SuppressWarnings("unchecked") String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map<String, String> uriVariables = Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
assertNotNull(uriVariables); assertNotNull(uriVariables);
assertEquals("group", uriVariables.get("group")); assertEquals("group", uriVariables.get("group"));
@ -274,20 +287,17 @@ public class RequestMappingInfoHandlerMappingTests {
} }
@Test @Test
public void bestMatchingPatternAttribute() { public void handleMatchBestMatchingPatternAttribute() {
PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/2", "/**"); RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/2", "/**").build();
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
this.handlerMapping.handleMatch(key, "/1/2", request); this.handlerMapping.handleMatch(key, "/1/2", request);
assertEquals("/{path1}/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); assertEquals("/{path1}/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
} }
@Test @Test
public void bestMatchingPatternAttributeNoPatternsDefined() { public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() {
PatternsRequestCondition patterns = new PatternsRequestCondition(); RequestMappingInfo key = RequestMappingInfo.paths().build();
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
this.handlerMapping.handleMatch(key, "/1/2", request); this.handlerMapping.handleMatch(key, "/1/2", request);
@ -296,51 +306,13 @@ public class RequestMappingInfoHandlerMappingTests {
} }
@Test @Test
public void producibleMediaTypesAttribute() throws Exception { public void handleMatchMatrixVariables() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/content");
request.addHeader("Accept", "application/xml");
this.handlerMapping.getHandler(request);
assertEquals(Collections.singleton(MediaType.APPLICATION_XML),
request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE));
request = new MockHttpServletRequest("GET", "/content");
request.addHeader("Accept", "application/json");
this.handlerMapping.getHandler(request);
assertNull("Negated expression should not be listed as a producible type",
request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE));
}
@Test
public void mappedInterceptors() throws Exception {
String path = "/foo";
HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);
TestRequestMappingInfoHandlerMapping hm = new TestRequestMappingInfoHandlerMapping();
hm.registerHandler(new TestController());
hm.setInterceptors(new Object[] { mappedInterceptor });
hm.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path));
assertNotNull(chain);
assertNotNull(chain.getInterceptors());
assertSame(interceptor, chain.getInterceptors()[0]);
chain = hm.getHandler(new MockHttpServletRequest("GET", "/invalid"));
assertNull(chain);
}
@Test
public void matrixVariables() {
MockHttpServletRequest request; MockHttpServletRequest request;
MultiValueMap<String, String> matrixVariables; MultiValueMap<String, String> matrixVariables;
Map<String, String> uriVariables; Map<String, String> uriVariables;
request = new MockHttpServletRequest(); request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars}", "/cars;colors=red,blue,green;year=2012"); handleMatch(request, "/{cars}", "/cars;colors=red,blue,green;year=2012");
matrixVariables = getMatrixVariables(request, "cars"); matrixVariables = getMatrixVariables(request, "cars");
uriVariables = getUriTemplateVariables(request); uriVariables = getUriTemplateVariables(request);
@ -351,7 +323,7 @@ public class RequestMappingInfoHandlerMappingTests {
assertEquals("cars", uriVariables.get("cars")); assertEquals("cars", uriVariables.get("cars"));
request = new MockHttpServletRequest(); request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars:[^;]+}{params}", "/cars;colors=red,blue,green;year=2012"); handleMatch(request, "/{cars:[^;]+}{params}", "/cars;colors=red,blue,green;year=2012");
matrixVariables = getMatrixVariables(request, "params"); matrixVariables = getMatrixVariables(request, "params");
uriVariables = getUriTemplateVariables(request); uriVariables = getUriTemplateVariables(request);
@ -363,7 +335,7 @@ public class RequestMappingInfoHandlerMappingTests {
assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params")); assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params"));
request = new MockHttpServletRequest(); request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars:[^;]+}{params}", "/cars"); handleMatch(request, "/{cars:[^;]+}{params}", "/cars");
matrixVariables = getMatrixVariables(request, "params"); matrixVariables = getMatrixVariables(request, "params");
uriVariables = getUriTemplateVariables(request); uriVariables = getUriTemplateVariables(request);
@ -374,7 +346,7 @@ public class RequestMappingInfoHandlerMappingTests {
} }
@Test @Test
public void matrixVariablesDecoding() { public void handleMatchMatrixVariablesDecoding() {
MockHttpServletRequest request; MockHttpServletRequest request;
@ -385,20 +357,53 @@ public class RequestMappingInfoHandlerMappingTests {
this.handlerMapping.setUrlPathHelper(urlPathHelper ); this.handlerMapping.setUrlPathHelper(urlPathHelper );
request = new MockHttpServletRequest(); request = new MockHttpServletRequest();
testHandleMatch(request, "/path{filter}", "/path;mvar=a%2fb"); handleMatch(request, "/path{filter}", "/path;mvar=a%2fb");
MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "filter"); MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "filter");
Map<String, String> uriVariables = getUriTemplateVariables(request); Map<String, String> uriVariables = getUriTemplateVariables(request);
assertNotNull(matrixVariables); assertNotNull(matrixVariables);
assertEquals(Arrays.asList("a/b"), matrixVariables.get("mvar")); assertEquals(Collections.singletonList("a/b"), matrixVariables.get("mvar"));
assertEquals(";mvar=a/b", uriVariables.get("filter")); assertEquals(";mvar=a/b", uriVariables.get("filter"));
} }
private void testHandleMatch(MockHttpServletRequest request, String pattern, String lookupPath) { private HandlerMethod getHandler(MockHttpServletRequest request) throws Exception {
PatternsRequestCondition patterns = new PatternsRequestCondition(pattern); HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
RequestMappingInfo info = new RequestMappingInfo(patterns, null, null, null, null, null, null); assertNotNull(chain);
return (HandlerMethod) chain.getHandler();
}
private void testHttpMediaTypeNotSupportedException(String url) throws Exception {
try {
MockHttpServletRequest request = new MockHttpServletRequest("PUT", url);
request.setContentType("application/json");
this.handlerMapping.getHandler(request);
fail("HttpMediaTypeNotSupportedException expected");
}
catch (HttpMediaTypeNotSupportedException ex) {
assertEquals("Invalid supported consumable media types",
Collections.singletonList(new MediaType("application", "xml")),
ex.getSupportedMediaTypes());
}
}
private void testHttpMediaTypeNotAcceptableException(String url) throws Exception {
try {
MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
request.addHeader("Accept", "application/json");
this.handlerMapping.getHandler(request);
fail("HttpMediaTypeNotAcceptableException expected");
}
catch (HttpMediaTypeNotAcceptableException ex) {
assertEquals("Invalid supported producible media types",
Collections.singletonList(new MediaType("application", "xml")),
ex.getSupportedMediaTypes());
}
}
private void handleMatch(MockHttpServletRequest request, String pattern, String lookupPath) {
RequestMappingInfo info = RequestMappingInfo.paths(pattern).build();
this.handlerMapping.handleMatch(info, lookupPath, request); this.handlerMapping.handleMatch(info, lookupPath, request);
} }
@ -491,15 +496,15 @@ public class RequestMappingInfoHandlerMappingTests {
@Override @Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annotation != null) { if (annot != null) {
return new RequestMappingInfo( return new RequestMappingInfo(
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true), new PatternsRequestCondition(annot.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annotation.method()), new RequestMethodsRequestCondition(annot.method()),
new ParamsRequestCondition(annotation.params()), new ParamsRequestCondition(annot.params()),
new HeadersRequestCondition(annotation.headers()), new HeadersRequestCondition(annot.headers()),
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ConsumesRequestCondition(annot.consumes(), annot.headers()),
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null); new ProducesRequestCondition(annot.produces(), annot.headers()), null);
} }
else { else {
return null; return null;