RequestMappingInfoTests.java 9.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2017 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.web.servlet.mvc.method;
18

19 20 21
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
22
import javax.servlet.http.HttpServletRequest;
23

24
import org.junit.Test;
25

S
Sebastien Deleuze 已提交
26
import org.springframework.http.HttpHeaders;
R
Rob Winch 已提交
27
import org.springframework.mock.web.test.MockHttpServletRequest;
28
import org.springframework.web.bind.annotation.RequestMethod;
29

30 31 32 33 34 35 36
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
37
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
38
import static org.springframework.web.servlet.mvc.method.RequestMappingInfo.paths;
39

40
/**
R
Rossen Stoyanchev 已提交
41
 * Test fixture for {@link RequestMappingInfo} tests.
42
 *
43 44 45
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 */
46
public class RequestMappingInfoTests {
47 48

	@Test
49
	public void createEmpty() {
50
		RequestMappingInfo info = paths().build();
51

52 53
		assertEquals(0, info.getPatternsCondition().getPatterns().size());
		assertEquals(0, info.getMethodsCondition().getMethods().size());
54 55
		assertEquals(true, info.getConsumesCondition().isEmpty());
		assertEquals(true, info.getProducesCondition().isEmpty());
56 57 58
		assertNotNull(info.getParamsCondition());
		assertNotNull(info.getHeadersCondition());
		assertNull(info.getCustomCondition());
59
	}
60

61
	@Test
62
	public void matchPatternsCondition() {
63
		HttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
64

65 66
		RequestMappingInfo info = paths("/foo*", "/bar").build();
		RequestMappingInfo expected = paths("/foo*").build();
67

68
		assertEquals(expected, info.getMatchingCondition(request));
69

70 71
		info = paths("/**", "/foo*", "/foo").build();
		expected = paths("/foo", "/foo*", "/**").build();
72

73
		assertEquals(expected, info.getMatchingCondition(request));
74 75 76
	}

	@Test
77
	public void matchParamsCondition() {
78 79 80
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
		request.setParameter("foo", "bar");

81
		RequestMappingInfo info = paths("/foo").params("foo=bar").build();
82
		RequestMappingInfo match = info.getMatchingCondition(request);
83 84 85

		assertNotNull(match);

86
		info = paths("/foo").params("foo!=bar").build();
87
		match = info.getMatchingCondition(request);
88 89 90 91 92

		assertNull(match);
	}

	@Test
93
	public void matchHeadersCondition() {
94 95 96
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
		request.addHeader("foo", "bar");

97
		RequestMappingInfo info = paths("/foo").headers("foo=bar").build();
98
		RequestMappingInfo match = info.getMatchingCondition(request);
99 100 101

		assertNotNull(match);

102
		info = paths("/foo").headers("foo!=bar").build();
103
		match = info.getMatchingCondition(request);
104 105 106 107

		assertNull(match);
	}

108
	@Test
109
	public void matchConsumesCondition() {
110 111 112
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
		request.setContentType("text/plain");

113
		RequestMappingInfo info = paths("/foo").consumes("text/plain").build();
114
		RequestMappingInfo match = info.getMatchingCondition(request);
115 116 117

		assertNotNull(match);

118
		info = paths("/foo").consumes("application/xml").build();
119
		match = info.getMatchingCondition(request);
120 121 122 123 124

		assertNull(match);
	}

	@Test
125
	public void matchProducesCondition() {
126 127 128
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
		request.addHeader("Accept", "text/plain");

129
		RequestMappingInfo info = paths("/foo").produces("text/plain").build();
130
		RequestMappingInfo match = info.getMatchingCondition(request);
131 132 133

		assertNotNull(match);

134
		info = paths("/foo").produces("application/xml").build();
135
		match = info.getMatchingCondition(request);
136 137 138 139 140 141 142 143 144

		assertNull(match);
	}

	@Test
	public void matchCustomCondition() {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
		request.setParameter("foo", "bar");

145
		RequestMappingInfo info = paths("/foo").params("foo=bar").build();
146
		RequestMappingInfo match = info.getMatchingCondition(request);
147 148 149

		assertNotNull(match);

150
		info = paths("/foo").params("foo!=bar").params("foo!=bar").build();
151
		match = info.getMatchingCondition(request);
152 153

		assertNull(match);
154 155
	}

156
	@Test
157 158 159 160 161 162 163 164 165
	public void compareToWithImpicitVsExplicitHttpMethodDeclaration() {
		RequestMappingInfo noMethods = paths().build();
		RequestMappingInfo oneMethod = paths().methods(GET).build();
		RequestMappingInfo oneMethodOneParam = paths().methods(GET).params("foo").build();

		Comparator<RequestMappingInfo> comparator =
				(info, otherInfo) -> info.compareTo(otherInfo, new MockHttpServletRequest());

		List<RequestMappingInfo> list = asList(noMethods, oneMethod, oneMethodOneParam);
166 167 168 169 170
		Collections.shuffle(list);
		Collections.sort(list, comparator);

		assertEquals(oneMethodOneParam, list.get(0));
		assertEquals(oneMethod, list.get(1));
171
		assertEquals(noMethods, list.get(2));
172
	}
R
Rossen Stoyanchev 已提交
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	@Test // SPR-14383
	public void compareToWithHttpHeadMapping() {
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.setMethod("HEAD");
		request.addHeader("Accept", "application/json");

		RequestMappingInfo noMethods = paths().build();
		RequestMappingInfo getMethod = paths().methods(GET).produces("application/json").build();
		RequestMappingInfo headMethod = paths().methods(HEAD).build();

		Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, request);

		List<RequestMappingInfo> list = asList(noMethods, getMethod, headMethod);
		Collections.shuffle(list);
		Collections.sort(list, comparator);

		assertEquals(headMethod, list.get(0));
		assertEquals(getMethod, list.get(1));
		assertEquals(noMethods, list.get(2));
	}

195 196
	@Test
	public void equals() {
197 198 199 200 201 202 203 204 205
		RequestMappingInfo info1 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();

		RequestMappingInfo info2 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();
206 207 208 209

		assertEquals(info1, info2);
		assertEquals(info1.hashCode(), info2.hashCode());

210 211 212 213
		info2 = paths("/foo", "/NOOOOOO").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();
214

215
		assertFalse(info1.equals(info2));
216
		assertNotEquals(info1.hashCode(), info2.hashCode());
217

218 219 220 221
		info2 = paths("/foo").methods(GET, RequestMethod.POST)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();
222

223
		assertFalse(info1.equals(info2));
224
		assertNotEquals(info1.hashCode(), info2.hashCode());
225

226 227 228 229
		info2 = paths("/foo").methods(GET)
				.params("/NOOOOOO", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();
230

231
		assertFalse(info1.equals(info2));
232
		assertNotEquals(info1.hashCode(), info2.hashCode());
233

234 235 236 237
		info2 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("/NOOOOOO")
				.consumes("text/plain").produces("text/plain")
				.build();
238

239
		assertFalse(info1.equals(info2));
240
		assertNotEquals(info1.hashCode(), info2.hashCode());
241

242 243 244 245
		info2 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/NOOOOOO").produces("text/plain")
				.build();
246

247
		assertFalse(info1.equals(info2));
248
		assertNotEquals(info1.hashCode(), info2.hashCode());
249

250 251 252 253
		info2 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=customBar").headers("foo=bar")
				.consumes("text/plain").produces("text/NOOOOOO")
				.build();
254

255
		assertFalse(info1.equals(info2));
256
		assertNotEquals(info1.hashCode(), info2.hashCode());
257

258 259 260 261
		info2 = paths("/foo").methods(GET)
				.params("foo=bar", "customFoo=NOOOOOO").headers("foo=bar")
				.consumes("text/plain").produces("text/plain")
				.build();
262

263
		assertFalse(info1.equals(info2));
264
		assertNotEquals(info1.hashCode(), info2.hashCode());
265 266
	}

S
Sebastien Deleuze 已提交
267 268 269
	@Test
	public void preFlightRequest() {
		MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
S
Spring Operator 已提交
270
		request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
271
		request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
S
Sebastien Deleuze 已提交
272

273
		RequestMappingInfo info = paths("/foo").methods(RequestMethod.POST).build();
S
Sebastien Deleuze 已提交
274 275 276
		RequestMappingInfo match = info.getMatchingCondition(request);
		assertNotNull(match);

277
		info = paths("/foo").methods(RequestMethod.OPTIONS).build();
S
Sebastien Deleuze 已提交
278
		match = info.getMatchingCondition(request);
279
		assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match);
S
Sebastien Deleuze 已提交
280 281
	}

282
}