diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java index 45de6ab031c..ac51a5fae4e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java @@ -18,9 +18,12 @@ package org.springframework.web.servlet.mvc.condition; import java.util.Collections; +import javax.servlet.http.HttpServletRequest; + import org.junit.Test; import org.springframework.mock.web.test.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -28,6 +31,7 @@ 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.OPTIONS; import static org.springframework.web.bind.annotation.RequestMethod.POST; /** @@ -37,67 +41,36 @@ import static org.springframework.web.bind.annotation.RequestMethod.POST; public class RequestMethodsRequestConditionTests { @Test - public void methodMatch() { - RequestCondition condition = new RequestMethodsRequestCondition(GET); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); - - assertNotNull(condition.getMatchingCondition(request)); + public void getMatchingCondition() { + testMatch(new RequestMethodsRequestCondition(GET), GET); + testMatch(new RequestMethodsRequestCondition(GET, POST), GET); + testNoMatch(new RequestMethodsRequestCondition(GET), POST); } @Test - public void methodNoMatch() { - RequestCondition condition = new RequestMethodsRequestCondition(GET); - MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); - - assertNull(condition.getMatchingCondition(request)); + public void getMatchingConditionWithHttpHead() { + testMatch(new RequestMethodsRequestCondition(HEAD), HEAD); + testMatch(new RequestMethodsRequestCondition(GET), HEAD); + testNoMatch(new RequestMethodsRequestCondition(POST), HEAD); } @Test - public void multipleMethodsMatch() { - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); - - assertNotNull(actual); - assertEquals(Collections.singleton(GET), actual.getContent()); + public void getMatchingConditionWithEmptyConditions() { + RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); + for (RequestMethod method : RequestMethod.values()) { + if (!OPTIONS.equals(method)) { + HttpServletRequest request = new MockHttpServletRequest(method.name(), ""); + assertNotNull(condition.getMatchingCondition(request)); + } + } + testNoMatch(condition, OPTIONS); } @Test - public void methodHeadMatch() throws Exception { - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST); - MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/foo"); - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); - - assertNotNull(actual); - assertEquals("GET should also match HEAD", Collections.singleton(HEAD), actual.getContent()); - } - - @Test - public void methodHeadNoMatch() throws Exception { - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(POST); - MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/foo"); - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); - - assertNull("HEAD should match only if GET is declared", actual); - } - - @Test - public void emptyMatchesAnythingExceptHttpOptions() { - RequestCondition condition = new RequestMethodsRequestCondition(); - - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", ""))); - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", ""))); - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", ""))); - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("CUSTOM", ""))); - assertNull(condition.getMatchingCondition(new MockHttpServletRequest("OPTIONS", ""))); - } - - @Test - public void unknownMethodType() throws Exception { - RequestCondition condition = new RequestMethodsRequestCondition(GET, POST); - MockHttpServletRequest request = new MockHttpServletRequest("PROPFIND", "/foo"); - - assertNull(condition.getMatchingCondition(request)); + public void getMatchingConditionWithCustomMethod() { + HttpServletRequest request = new MockHttpServletRequest("PROPFIND", ""); + assertNotNull(new RequestMethodsRequestCondition().getMatchingCondition(request)); + assertNull(new RequestMethodsRequestCondition(GET, POST).getMatchingCondition(request)); } @Test @@ -130,4 +103,17 @@ public class RequestMethodsRequestConditionTests { assertEquals(2, result.getContent().size()); } + + private void testMatch(RequestMethodsRequestCondition condition, RequestMethod method) { + MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); + RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); + assertNotNull(actual); + assertEquals(Collections.singleton(method), actual.getContent()); + } + + private void testNoMatch(RequestMethodsRequestCondition condition, RequestMethod method) { + MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); + assertNull(condition.getMatchingCondition(request)); + } + }