@@ -131,7 +131,7 @@ public void bestMatch() throws Exception {
131
131
HandlerMethod hm = (HandlerMethod ) this .mapping .getHandler (request ).getHandler ();
132
132
assertEquals (this .fooParamMethod .getMethod (), hm .getMethod ());
133
133
}
134
-
134
+
135
135
@ Test
136
136
public void requestMethodNotAllowed () throws Exception {
137
137
try {
@@ -143,7 +143,18 @@ public void requestMethodNotAllowed() throws Exception {
143
143
assertArrayEquals ("Invalid supported methods" , new String []{"GET" , "HEAD" }, ex .getSupportedMethods ());
144
144
}
145
145
}
146
-
146
+
147
+ // SPR-9603
148
+
149
+ @ Test (expected =HttpMediaTypeNotAcceptableException .class )
150
+ public void requestMethodMatchFalsePositive () throws Exception {
151
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/users" );
152
+ request .addHeader ("Accept" , "application/xml" );
153
+
154
+ this .mapping .registerHandler (new UserController ());
155
+ this .mapping .getHandler (request );
156
+ }
157
+
147
158
@ Test
148
159
public void mediaTypeNotSupported () throws Exception {
149
160
testMediaTypeNotSupported ("/person/1" );
@@ -159,11 +170,11 @@ private void testMediaTypeNotSupported(String url) throws Exception {
159
170
fail ("HttpMediaTypeNotSupportedException expected" );
160
171
}
161
172
catch (HttpMediaTypeNotSupportedException ex ) {
162
- assertEquals ("Invalid supported consumable media types" ,
173
+ assertEquals ("Invalid supported consumable media types" ,
163
174
Arrays .asList (new MediaType ("application" , "xml" )), ex .getSupportedMediaTypes ());
164
175
}
165
176
}
166
-
177
+
167
178
@ Test
168
179
public void mediaTypeNotAccepted () throws Exception {
169
180
testMediaTypeNotAccepted ("/persons" );
@@ -179,7 +190,7 @@ private void testMediaTypeNotAccepted(String url) throws Exception {
179
190
fail ("HttpMediaTypeNotAcceptableException expected" );
180
191
}
181
192
catch (HttpMediaTypeNotAcceptableException ex ) {
182
- assertEquals ("Invalid supported producible media types" ,
193
+ assertEquals ("Invalid supported producible media types" ,
183
194
Arrays .asList (new MediaType ("application" , "xml" )), ex .getSupportedMediaTypes ());
184
195
}
185
196
}
@@ -191,12 +202,12 @@ public void uriTemplateVariables() {
191
202
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/1/2" );
192
203
String lookupPath = new UrlPathHelper ().getLookupPathForRequest (request );
193
204
this .mapping .handleMatch (key , lookupPath , request );
194
-
205
+
195
206
@ SuppressWarnings ("unchecked" )
196
- Map <String , String > uriVariables =
207
+ Map <String , String > uriVariables =
197
208
(Map <String , String >) request .getAttribute (
198
209
HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE );
199
-
210
+
200
211
assertNotNull (uriVariables );
201
212
assertEquals ("1" , uriVariables .get ("path1" ));
202
213
assertEquals ("2" , uriVariables .get ("path2" ));
@@ -209,7 +220,7 @@ public void bestMatchingPatternAttribute() {
209
220
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/1/2" );
210
221
211
222
this .mapping .handleMatch (key , "/1/2" , request );
212
-
223
+
213
224
assertEquals ("/{path1}/2" , request .getAttribute (HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE ));
214
225
}
215
226
@@ -220,7 +231,7 @@ public void bestMatchingPatternAttributeNoPatternsDefined() {
220
231
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/1/2" );
221
232
222
233
this .mapping .handleMatch (key , "/1/2" , request );
223
-
234
+
224
235
assertEquals ("/1/2" , request .getAttribute (HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE ));
225
236
}
226
237
@@ -237,10 +248,10 @@ public void producibleMediaTypesAttribute() throws Exception {
237
248
request .addHeader ("Accept" , "application/json" );
238
249
this .mapping .getHandler (request );
239
250
240
- assertNull ("Negated expression should not be listed as a producible type" ,
251
+ assertNull ("Negated expression should not be listed as a producible type" ,
241
252
request .getAttribute (HandlerMapping .PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE ));
242
253
}
243
-
254
+
244
255
@ Test
245
256
public void mappedInterceptors () throws Exception {
246
257
String path = "/foo" ;
@@ -261,14 +272,13 @@ public void mappedInterceptors() throws Exception {
261
272
assertNull (chain );
262
273
}
263
274
264
- @ SuppressWarnings ("unused" )
265
275
@ Controller
266
276
private static class Handler {
267
277
268
278
@ RequestMapping (value = "/foo" , method = RequestMethod .GET )
269
279
public void foo () {
270
280
}
271
-
281
+
272
282
@ RequestMapping (value = "/foo" , method = RequestMethod .GET , params ="p" )
273
283
public void fooParam () {
274
284
}
@@ -301,12 +311,24 @@ public String nonXmlContent() {
301
311
}
302
312
}
303
313
314
+ @ Controller
315
+ private static class UserController {
316
+
317
+ @ RequestMapping (value = "/users" , method = RequestMethod .GET , produces = "application/json" )
318
+ public void getUser () {
319
+ }
320
+
321
+ @ RequestMapping (value = "/users" , method = RequestMethod .PUT )
322
+ public void saveUser () {
323
+ }
324
+ }
325
+
304
326
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
305
327
306
328
public void registerHandler (Object handler ) {
307
329
super .detectHandlerMethods (handler );
308
330
}
309
-
331
+
310
332
@ Override
311
333
protected boolean isHandler (Class <?> beanType ) {
312
334
return AnnotationUtils .findAnnotation (beanType , RequestMapping .class ) != null ;
@@ -329,5 +351,5 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
329
351
}
330
352
}
331
353
}
332
-
354
+
333
355
}
0 commit comments