|
18 | 18 |
|
19 | 19 | import java.util.Collections; |
20 | 20 |
|
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | + |
21 | 23 | import org.junit.Test; |
22 | 24 |
|
23 | 25 | import org.springframework.mock.web.test.MockHttpServletRequest; |
| 26 | +import org.springframework.web.bind.annotation.RequestMethod; |
24 | 27 |
|
25 | 28 | import static org.junit.Assert.assertEquals; |
26 | 29 | import static org.junit.Assert.assertNotNull; |
27 | 30 | import static org.junit.Assert.assertNull; |
28 | 31 | import static org.junit.Assert.assertTrue; |
29 | 32 | import static org.springframework.web.bind.annotation.RequestMethod.GET; |
30 | 33 | import static org.springframework.web.bind.annotation.RequestMethod.HEAD; |
| 34 | +import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS; |
31 | 35 | import static org.springframework.web.bind.annotation.RequestMethod.POST; |
32 | 36 |
|
33 | 37 | /** |
|
37 | 41 | public class RequestMethodsRequestConditionTests { |
38 | 42 |
|
39 | 43 | @Test |
40 | | - public void methodMatch() { |
41 | | - RequestCondition condition = new RequestMethodsRequestCondition(GET); |
42 | | - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); |
43 | | - |
44 | | - assertNotNull(condition.getMatchingCondition(request)); |
45 | | - } |
46 | | - |
47 | | - @Test |
48 | | - public void methodNoMatch() { |
49 | | - RequestCondition condition = new RequestMethodsRequestCondition(GET); |
50 | | - MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); |
51 | | - |
52 | | - assertNull(condition.getMatchingCondition(request)); |
| 44 | + public void getMatchingCondition() { |
| 45 | + testMatch(new RequestMethodsRequestCondition(GET), GET); |
| 46 | + testMatch(new RequestMethodsRequestCondition(GET, POST), GET); |
| 47 | + testNoMatch(new RequestMethodsRequestCondition(GET), POST); |
53 | 48 | } |
54 | 49 |
|
55 | 50 | @Test |
56 | | - public void multipleMethodsMatch() { |
57 | | - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST); |
58 | | - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); |
59 | | - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); |
60 | | - |
61 | | - assertNotNull(actual); |
62 | | - assertEquals(Collections.singleton(GET), actual.getContent()); |
| 51 | + public void getMatchingConditionWithHttpHead() { |
| 52 | + testMatch(new RequestMethodsRequestCondition(HEAD), HEAD); |
| 53 | + testMatch(new RequestMethodsRequestCondition(GET), HEAD); |
| 54 | + testNoMatch(new RequestMethodsRequestCondition(POST), HEAD); |
63 | 55 | } |
64 | 56 |
|
65 | 57 | @Test |
66 | | - public void methodHeadMatch() throws Exception { |
67 | | - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(GET, POST); |
68 | | - MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/foo"); |
69 | | - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); |
70 | | - |
71 | | - assertNotNull(actual); |
72 | | - assertEquals("GET should also match HEAD", Collections.singleton(HEAD), actual.getContent()); |
| 58 | + public void getMatchingConditionWithEmptyConditions() { |
| 59 | + RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); |
| 60 | + for (RequestMethod method : RequestMethod.values()) { |
| 61 | + if (!OPTIONS.equals(method)) { |
| 62 | + HttpServletRequest request = new MockHttpServletRequest(method.name(), ""); |
| 63 | + assertNotNull(condition.getMatchingCondition(request)); |
| 64 | + } |
| 65 | + } |
| 66 | + testNoMatch(condition, OPTIONS); |
73 | 67 | } |
74 | 68 |
|
75 | 69 | @Test |
76 | | - public void methodHeadNoMatch() throws Exception { |
77 | | - RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(POST); |
78 | | - MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/foo"); |
79 | | - RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); |
80 | | - |
81 | | - assertNull("HEAD should match only if GET is declared", actual); |
82 | | - } |
83 | | - |
84 | | - @Test |
85 | | - public void emptyMatchesAnythingExceptHttpOptions() { |
86 | | - RequestCondition condition = new RequestMethodsRequestCondition(); |
87 | | - |
88 | | - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", ""))); |
89 | | - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", ""))); |
90 | | - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", ""))); |
91 | | - assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("CUSTOM", ""))); |
92 | | - assertNull(condition.getMatchingCondition(new MockHttpServletRequest("OPTIONS", ""))); |
93 | | - } |
94 | | - |
95 | | - @Test |
96 | | - public void unknownMethodType() throws Exception { |
97 | | - RequestCondition condition = new RequestMethodsRequestCondition(GET, POST); |
98 | | - MockHttpServletRequest request = new MockHttpServletRequest("PROPFIND", "/foo"); |
99 | | - |
100 | | - assertNull(condition.getMatchingCondition(request)); |
| 70 | + public void getMatchingConditionWithCustomMethod() { |
| 71 | + HttpServletRequest request = new MockHttpServletRequest("PROPFIND", ""); |
| 72 | + assertNotNull(new RequestMethodsRequestCondition().getMatchingCondition(request)); |
| 73 | + assertNull(new RequestMethodsRequestCondition(GET, POST).getMatchingCondition(request)); |
101 | 74 | } |
102 | 75 |
|
103 | 76 | @Test |
@@ -130,4 +103,17 @@ public void combine() { |
130 | 103 | assertEquals(2, result.getContent().size()); |
131 | 104 | } |
132 | 105 |
|
| 106 | + |
| 107 | + private void testMatch(RequestMethodsRequestCondition condition, RequestMethod method) { |
| 108 | + MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); |
| 109 | + RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); |
| 110 | + assertNotNull(actual); |
| 111 | + assertEquals(Collections.singleton(method), actual.getContent()); |
| 112 | + } |
| 113 | + |
| 114 | + private void testNoMatch(RequestMethodsRequestCondition condition, RequestMethod method) { |
| 115 | + MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); |
| 116 | + assertNull(condition.getMatchingCondition(request)); |
| 117 | + } |
| 118 | + |
133 | 119 | } |
0 commit comments