Polish
This commit is contained in:
parent
2b3ad218e5
commit
fb7dfc4569
|
|
@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,12 +16,19 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.condition;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
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
|
||||
|
|
@ -30,8 +37,7 @@ public class RequestMethodsRequestConditionTests {
|
|||
|
||||
@Test
|
||||
public void methodMatch() {
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
RequestCondition condition = new RequestMethodsRequestCondition(GET);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
assertNotNull(condition.getMatchingCondition(request));
|
||||
|
|
@ -39,8 +45,7 @@ public class RequestMethodsRequestConditionTests {
|
|||
|
||||
@Test
|
||||
public void methodNoMatch() {
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
RequestCondition condition = new RequestMethodsRequestCondition(GET);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo");
|
||||
|
||||
assertNull(condition.getMatchingCondition(request));
|
||||
|
|
@ -48,16 +53,17 @@ public class RequestMethodsRequestConditionTests {
|
|||
|
||||
@Test
|
||||
public void multipleMethodsMatch() {
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
|
||||
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
RequestMethodsRequestCondition actual = condition.getMatchingCondition(request);
|
||||
|
||||
assertNotNull(condition.getMatchingCondition(request));
|
||||
assertNotNull(actual);
|
||||
assertEquals(Collections.singleton(GET), actual.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noMethodsMatchAll() {
|
||||
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition();
|
||||
public void noDeclaredMethodsMatchesAllMethods() {
|
||||
RequestCondition condition = new RequestMethodsRequestCondition();
|
||||
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
|
||||
|
|
@ -66,8 +72,7 @@ public class RequestMethodsRequestConditionTests {
|
|||
|
||||
@Test
|
||||
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");
|
||||
|
||||
assertNull(condition.getMatchingCondition(request));
|
||||
|
|
@ -75,33 +80,32 @@ public class RequestMethodsRequestConditionTests {
|
|||
|
||||
@Test
|
||||
public void compareTo() {
|
||||
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.HEAD);
|
||||
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST);
|
||||
RequestMethodsRequestCondition condition3 = new RequestMethodsRequestCondition();
|
||||
RequestMethodsRequestCondition c1 = new RequestMethodsRequestCondition(GET, HEAD);
|
||||
RequestMethodsRequestCondition c2 = new RequestMethodsRequestCondition(POST);
|
||||
RequestMethodsRequestCondition c3 = new RequestMethodsRequestCondition();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
int result = condition1.compareTo(condition2, request);
|
||||
int result = c1.compareTo(c2, request);
|
||||
assertTrue("Invalid comparison result: " + result, result < 0);
|
||||
|
||||
result = condition2.compareTo(condition1, request);
|
||||
result = c2.compareTo(c1, request);
|
||||
assertTrue("Invalid comparison result: " + result, result > 0);
|
||||
|
||||
result = condition2.compareTo(condition3, request);
|
||||
result = c2.compareTo(c3, request);
|
||||
assertTrue("Invalid comparison result: " + result, result < 0);
|
||||
|
||||
result = condition1.compareTo(condition1, request);
|
||||
result = c1.compareTo(c1, request);
|
||||
assertEquals("Invalid comparison result ", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.POST);
|
||||
RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(GET);
|
||||
RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(POST);
|
||||
|
||||
RequestMethodsRequestCondition result = condition1.combine(condition2);
|
||||
assertEquals(2, result.getContent().size());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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.util.UrlPathHelper;
|
||||
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestMappingInfoHandlerMapping}.
|
||||
*
|
||||
|
|
@ -77,6 +78,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
|
||||
private HandlerMethod emptyMethod;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
TestController testController = new TestController();
|
||||
|
|
@ -91,96 +93,87 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
this.handlerMapping.setRemoveSemicolonContent(false);
|
||||
}
|
||||
|
||||
|
||||
@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 = this.handlerMapping.getMappingPathPatterns(info);
|
||||
HashSet<String> expected = new HashSet<String>(Arrays.asList("/foo/*", "/foo", "/bar/*", "/bar"));
|
||||
String[] patterns = {"/foo/*", "/foo", "/bar/*", "/bar"};
|
||||
RequestMappingInfo info = RequestMappingInfo.paths(patterns).build();
|
||||
Set<String> actual = this.handlerMapping.getMappingPathPatterns(info);
|
||||
|
||||
assertEquals(expected, paths);
|
||||
assertEquals(new HashSet<>(Arrays.asList(patterns)), actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directMatch() throws Exception {
|
||||
public void getHandlerDirectMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler();
|
||||
assertEquals(this.fooMethod.getMethod(), hm.getMethod());
|
||||
HandlerMethod handlerMethod = getHandler(request);
|
||||
|
||||
assertEquals(this.fooMethod.getMethod(), handlerMethod.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void globMatch() throws Exception {
|
||||
public void getHandlerGlobMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bar");
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler();
|
||||
assertEquals(this.barMethod.getMethod(), hm.getMethod());
|
||||
HandlerMethod handlerMethod = getHandler(request);
|
||||
assertEquals(this.barMethod.getMethod(), handlerMethod.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyPathMatch() throws Exception {
|
||||
public void getHandlerEmptyPathMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler();
|
||||
assertEquals(this.emptyMethod.getMethod(), hm.getMethod());
|
||||
HandlerMethod handlerMethod = getHandler(request);
|
||||
|
||||
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/");
|
||||
hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler();
|
||||
assertEquals(this.emptyMethod.getMethod(), hm.getMethod());
|
||||
handlerMethod = getHandler(request);
|
||||
|
||||
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bestMatch() throws Exception {
|
||||
public void getHandlerBestMatch() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setParameter("p", "anything");
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(request).getHandler();
|
||||
assertEquals(this.fooParamMethod.getMethod(), hm.getMethod());
|
||||
HandlerMethod handlerMethod = getHandler(request);
|
||||
|
||||
assertEquals(this.fooParamMethod.getMethod(), handlerMethod.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestMethodNotAllowed() throws Exception {
|
||||
public void getHandlerRequestMethodNotAllowed() throws Exception {
|
||||
try {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
|
||||
this.handlerMapping.getHandler(request);
|
||||
fail("HttpRequestMethodNotSupportedException expected");
|
||||
}
|
||||
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
|
||||
|
||||
@Test(expected=HttpMediaTypeNotAcceptableException.class)
|
||||
public void requestMethodMatchFalsePositive() throws Exception {
|
||||
public void getHandlerRequestMethodMatchFalsePositive() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users");
|
||||
request.addHeader("Accept", "application/xml");
|
||||
|
||||
this.handlerMapping.registerHandler(new UserController());
|
||||
this.handlerMapping.getHandler(request);
|
||||
}
|
||||
|
||||
// SPR-8462
|
||||
|
||||
@Test
|
||||
public void mediaTypeNotSupported() throws Exception {
|
||||
testMediaTypeNotSupported("/person/1");
|
||||
|
||||
// SPR-8462
|
||||
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());
|
||||
}
|
||||
public void getHandlerMediaTypeNotSupported() throws Exception {
|
||||
testHttpMediaTypeNotSupportedException("/person/1");
|
||||
testHttpMediaTypeNotSupportedException("/person/1/");
|
||||
testHttpMediaTypeNotSupportedException("/person/1.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMediaTypeNotValue() throws Exception {
|
||||
public void getHandlerTestInvalidContentType() throws Exception {
|
||||
try {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1");
|
||||
request.setContentType("bogus");
|
||||
|
|
@ -192,32 +185,19 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
}
|
||||
|
||||
// SPR-8462
|
||||
|
||||
@Test
|
||||
public void mediaTypeNotAccepted() throws Exception {
|
||||
testMediaTypeNotAccepted("/persons");
|
||||
|
||||
// SPR-8462
|
||||
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());
|
||||
}
|
||||
public void getHandlerMediaTypeNotAccepted() throws Exception {
|
||||
testHttpMediaTypeNotAcceptableException("/persons");
|
||||
testHttpMediaTypeNotAcceptableException("/persons/");
|
||||
testHttpMediaTypeNotAcceptableException("/persons.json");
|
||||
}
|
||||
|
||||
// SPR-12854
|
||||
|
||||
@Test
|
||||
public void testUnsatisfiedServletRequestParameterException() throws Exception {
|
||||
public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception {
|
||||
try {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params");
|
||||
this.handlerMapping.getHandler(request);
|
||||
|
|
@ -232,17 +212,51 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void uriTemplateVariables() {
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/{path2}");
|
||||
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
public void getHandlerProducibleMediaTypesAttribute() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/content");
|
||||
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");
|
||||
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
|
||||
this.handlerMapping.handleMatch(key, lookupPath, request);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, String> uriVariables =
|
||||
(Map<String, String>) request.getAttribute(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
||||
Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
|
||||
|
||||
assertNotNull(uriVariables);
|
||||
assertEquals("1", uriVariables.get("path1"));
|
||||
|
|
@ -251,10 +265,10 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
|
||||
// SPR-9098
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void uriTemplateVariablesDecode() {
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition("/{group}/{identifier}");
|
||||
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
public void handleMatchUriTemplateVariablesDecode() {
|
||||
RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb");
|
||||
|
||||
UrlPathHelper pathHelper = new UrlPathHelper();
|
||||
|
|
@ -264,9 +278,8 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
this.handlerMapping.setUrlPathHelper(pathHelper);
|
||||
this.handlerMapping.handleMatch(key, lookupPath, request);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, String> uriVariables =
|
||||
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
||||
Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
|
||||
|
||||
assertNotNull(uriVariables);
|
||||
assertEquals("group", uriVariables.get("group"));
|
||||
|
|
@ -274,20 +287,17 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void bestMatchingPatternAttribute() {
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/2", "/**");
|
||||
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
public void handleMatchBestMatchingPatternAttribute() {
|
||||
RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/2", "/**").build();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
|
||||
|
||||
this.handlerMapping.handleMatch(key, "/1/2", request);
|
||||
|
||||
assertEquals("/{path1}/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bestMatchingPatternAttributeNoPatternsDefined() {
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition();
|
||||
RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() {
|
||||
RequestMappingInfo key = RequestMappingInfo.paths().build();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
|
||||
|
||||
this.handlerMapping.handleMatch(key, "/1/2", request);
|
||||
|
|
@ -296,51 +306,13 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void producibleMediaTypesAttribute() throws Exception {
|
||||
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() {
|
||||
|
||||
public void handleMatchMatrixVariables() {
|
||||
MockHttpServletRequest request;
|
||||
MultiValueMap<String, String> matrixVariables;
|
||||
Map<String, String> uriVariables;
|
||||
|
||||
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");
|
||||
uriVariables = getUriTemplateVariables(request);
|
||||
|
|
@ -351,7 +323,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
assertEquals("cars", uriVariables.get("cars"));
|
||||
|
||||
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");
|
||||
uriVariables = getUriTemplateVariables(request);
|
||||
|
|
@ -363,7 +335,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params"));
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
testHandleMatch(request, "/{cars:[^;]+}{params}", "/cars");
|
||||
handleMatch(request, "/{cars:[^;]+}{params}", "/cars");
|
||||
|
||||
matrixVariables = getMatrixVariables(request, "params");
|
||||
uriVariables = getUriTemplateVariables(request);
|
||||
|
|
@ -374,7 +346,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void matrixVariablesDecoding() {
|
||||
public void handleMatchMatrixVariablesDecoding() {
|
||||
|
||||
MockHttpServletRequest request;
|
||||
|
||||
|
|
@ -385,20 +357,53 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
this.handlerMapping.setUrlPathHelper(urlPathHelper );
|
||||
|
||||
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");
|
||||
Map<String, String> uriVariables = getUriTemplateVariables(request);
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
|
||||
private void testHandleMatch(MockHttpServletRequest request, String pattern, String lookupPath) {
|
||||
PatternsRequestCondition patterns = new PatternsRequestCondition(pattern);
|
||||
RequestMappingInfo info = new RequestMappingInfo(patterns, null, null, null, null, null, null);
|
||||
private HandlerMethod getHandler(MockHttpServletRequest request) throws Exception {
|
||||
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -491,15 +496,15 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
|
||||
@Override
|
||||
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
|
||||
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
|
||||
if (annotation != null) {
|
||||
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
|
||||
if (annot != null) {
|
||||
return new RequestMappingInfo(
|
||||
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true),
|
||||
new RequestMethodsRequestCondition(annotation.method()),
|
||||
new ParamsRequestCondition(annotation.params()),
|
||||
new HeadersRequestCondition(annotation.headers()),
|
||||
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
|
||||
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
|
||||
new PatternsRequestCondition(annot.value(), getUrlPathHelper(), getPathMatcher(), true, true),
|
||||
new RequestMethodsRequestCondition(annot.method()),
|
||||
new ParamsRequestCondition(annot.params()),
|
||||
new HeadersRequestCondition(annot.headers()),
|
||||
new ConsumesRequestCondition(annot.consumes(), annot.headers()),
|
||||
new ProducesRequestCondition(annot.produces(), annot.headers()), null);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Reference in New Issue