19
19
import java .util .Collections ;
20
20
import java .util .Comparator ;
21
21
import java .util .List ;
22
+ import javax .servlet .http .HttpServletRequest ;
22
23
23
24
import org .junit .Test ;
24
25
25
26
import org .springframework .http .HttpHeaders ;
26
27
import org .springframework .mock .web .test .MockHttpServletRequest ;
27
28
import org .springframework .web .bind .annotation .RequestMethod ;
28
- import org .springframework .web .servlet .mvc .condition .ConsumesRequestCondition ;
29
- import org .springframework .web .servlet .mvc .condition .HeadersRequestCondition ;
30
- import org .springframework .web .servlet .mvc .condition .ParamsRequestCondition ;
31
- import org .springframework .web .servlet .mvc .condition .PatternsRequestCondition ;
32
- import org .springframework .web .servlet .mvc .condition .ProducesRequestCondition ;
33
- import org .springframework .web .servlet .mvc .condition .RequestMethodsRequestCondition ;
34
29
35
- import static java .util .Arrays .*;
36
- import static org .junit .Assert .*;
30
+ import static java .util .Arrays .asList ;
31
+ import static org .junit .Assert .assertEquals ;
32
+ import static org .junit .Assert .assertFalse ;
33
+ import static org .junit .Assert .assertNotEquals ;
34
+ import static org .junit .Assert .assertNotNull ;
35
+ import static org .junit .Assert .assertNull ;
36
+ import static org .springframework .web .bind .annotation .RequestMethod .GET ;
37
+ import static org .springframework .web .servlet .mvc .method .RequestMappingInfo .paths ;
37
38
38
39
/**
39
40
* Test fixture for {@link RequestMappingInfo} tests.
43
44
*/
44
45
public class RequestMappingInfoTests {
45
46
47
+
46
48
@ Test
47
49
public void createEmpty () {
48
- RequestMappingInfo info = new RequestMappingInfo ( null , null , null , null , null , null , null );
50
+ RequestMappingInfo info = paths (). build ( );
49
51
50
52
assertEquals (0 , info .getPatternsCondition ().getPatterns ().size ());
51
53
assertEquals (0 , info .getMethodsCondition ().getMethods ().size ());
@@ -58,19 +60,15 @@ public void createEmpty() {
58
60
59
61
@ Test
60
62
public void matchPatternsCondition () {
61
- MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
63
+ HttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
62
64
63
- RequestMappingInfo info = new RequestMappingInfo (
64
- new PatternsRequestCondition ("/foo*" , "/bar" ), null , null , null , null , null , null );
65
- RequestMappingInfo expected = new RequestMappingInfo (
66
- new PatternsRequestCondition ("/foo*" ), null , null , null , null , null , null );
65
+ RequestMappingInfo info = paths ("/foo*" , "/bar" ).build ();
66
+ RequestMappingInfo expected = paths ("/foo*" ).build ();
67
67
68
68
assertEquals (expected , info .getMatchingCondition (request ));
69
69
70
- info = new RequestMappingInfo (
71
- new PatternsRequestCondition ("/**" , "/foo*" , "/foo" ), null , null , null , null , null , null );
72
- expected = new RequestMappingInfo (
73
- new PatternsRequestCondition ("/foo" , "/foo*" , "/**" ), null , null , null , null , null , null );
70
+ info = paths ("/**" , "/foo*" , "/foo" ).build ();
71
+ expected = paths ("/foo" , "/foo*" , "/**" ).build ();
74
72
75
73
assertEquals (expected , info .getMatchingCondition (request ));
76
74
}
@@ -80,17 +78,12 @@ public void matchParamsCondition() {
80
78
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
81
79
request .setParameter ("foo" , "bar" );
82
80
83
- RequestMappingInfo info =
84
- new RequestMappingInfo (
85
- new PatternsRequestCondition ("/foo" ), null ,
86
- new ParamsRequestCondition ("foo=bar" ), null , null , null , null );
81
+ RequestMappingInfo info = paths ("/foo" ).params ("foo=bar" ).build ();
87
82
RequestMappingInfo match = info .getMatchingCondition (request );
88
83
89
84
assertNotNull (match );
90
85
91
- info = new RequestMappingInfo (
92
- new PatternsRequestCondition ("/foo" ), null ,
93
- new ParamsRequestCondition ("foo!=bar" ), null , null , null , null );
86
+ info = paths ("/foo" ).params ("foo!=bar" ).build ();
94
87
match = info .getMatchingCondition (request );
95
88
96
89
assertNull (match );
@@ -101,17 +94,12 @@ public void matchHeadersCondition() {
101
94
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
102
95
request .addHeader ("foo" , "bar" );
103
96
104
- RequestMappingInfo info =
105
- new RequestMappingInfo (
106
- new PatternsRequestCondition ("/foo" ), null , null ,
107
- new HeadersRequestCondition ("foo=bar" ), null , null , null );
97
+ RequestMappingInfo info = paths ("/foo" ).headers ("foo=bar" ).build ();
108
98
RequestMappingInfo match = info .getMatchingCondition (request );
109
99
110
100
assertNotNull (match );
111
101
112
- info = new RequestMappingInfo (
113
- new PatternsRequestCondition ("/foo" ), null , null ,
114
- new HeadersRequestCondition ("foo!=bar" ), null , null , null );
102
+ info = paths ("/foo" ).headers ("foo!=bar" ).build ();
115
103
match = info .getMatchingCondition (request );
116
104
117
105
assertNull (match );
@@ -122,17 +110,12 @@ public void matchConsumesCondition() {
122
110
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
123
111
request .setContentType ("text/plain" );
124
112
125
- RequestMappingInfo info =
126
- new RequestMappingInfo (
127
- new PatternsRequestCondition ("/foo" ), null , null , null ,
128
- new ConsumesRequestCondition ("text/plain" ), null , null );
113
+ RequestMappingInfo info = paths ("/foo" ).consumes ("text/plain" ).build ();
129
114
RequestMappingInfo match = info .getMatchingCondition (request );
130
115
131
116
assertNotNull (match );
132
117
133
- info = new RequestMappingInfo (
134
- new PatternsRequestCondition ("/foo" ), null , null , null ,
135
- new ConsumesRequestCondition ("application/xml" ), null , null );
118
+ info = paths ("/foo" ).consumes ("application/xml" ).build ();
136
119
match = info .getMatchingCondition (request );
137
120
138
121
assertNull (match );
@@ -143,17 +126,12 @@ public void matchProducesCondition() {
143
126
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
144
127
request .addHeader ("Accept" , "text/plain" );
145
128
146
- RequestMappingInfo info =
147
- new RequestMappingInfo (
148
- new PatternsRequestCondition ("/foo" ), null , null , null , null ,
149
- new ProducesRequestCondition ("text/plain" ), null );
129
+ RequestMappingInfo info = paths ("/foo" ).produces ("text/plain" ).build ();
150
130
RequestMappingInfo match = info .getMatchingCondition (request );
151
131
152
132
assertNotNull (match );
153
133
154
- info = new RequestMappingInfo (
155
- new PatternsRequestCondition ("/foo" ), null , null , null , null ,
156
- new ProducesRequestCondition ("application/xml" ), null );
134
+ info = paths ("/foo" ).produces ("application/xml" ).build ();
157
135
match = info .getMatchingCondition (request );
158
136
159
137
assertNull (match );
@@ -164,153 +142,102 @@ public void matchCustomCondition() {
164
142
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/foo" );
165
143
request .setParameter ("foo" , "bar" );
166
144
167
- RequestMappingInfo info =
168
- new RequestMappingInfo (
169
- new PatternsRequestCondition ("/foo" ), null , null , null , null , null ,
170
- new ParamsRequestCondition ("foo=bar" ));
145
+ RequestMappingInfo info = paths ("/foo" ).params ("foo=bar" ).build ();
171
146
RequestMappingInfo match = info .getMatchingCondition (request );
172
147
173
148
assertNotNull (match );
174
149
175
- info = new RequestMappingInfo (
176
- new PatternsRequestCondition ("/foo" ), null ,
177
- new ParamsRequestCondition ("foo!=bar" ), null , null , null ,
178
- new ParamsRequestCondition ("foo!=bar" ));
150
+ info = paths ("/foo" ).params ("foo!=bar" ).params ("foo!=bar" ).build ();
179
151
match = info .getMatchingCondition (request );
180
152
181
153
assertNull (match );
182
154
}
183
155
184
156
@ Test
185
- public void compareTwoHttpMethodsOneParam () {
186
- RequestMappingInfo none = new RequestMappingInfo (null , null , null , null , null , null , null );
187
- RequestMappingInfo oneMethod =
188
- new RequestMappingInfo (null ,
189
- new RequestMethodsRequestCondition (RequestMethod .GET ), null , null , null , null , null );
190
- RequestMappingInfo oneMethodOneParam =
191
- new RequestMappingInfo (null ,
192
- new RequestMethodsRequestCondition (RequestMethod .GET ),
193
- new ParamsRequestCondition ("foo" ), null , null , null , null );
194
-
195
- Comparator <RequestMappingInfo > comparator = new Comparator <RequestMappingInfo >() {
196
- @ Override
197
- public int compare (RequestMappingInfo info , RequestMappingInfo otherInfo ) {
198
- return info .compareTo (otherInfo , new MockHttpServletRequest ());
199
- }
200
- };
201
-
202
- List <RequestMappingInfo > list = asList (none , oneMethod , oneMethodOneParam );
157
+ public void compareToWithImpicitVsExplicitHttpMethodDeclaration () {
158
+ RequestMappingInfo noMethods = paths ().build ();
159
+ RequestMappingInfo oneMethod = paths ().methods (GET ).build ();
160
+ RequestMappingInfo oneMethodOneParam = paths ().methods (GET ).params ("foo" ).build ();
161
+
162
+ Comparator <RequestMappingInfo > comparator =
163
+ (info , otherInfo ) -> info .compareTo (otherInfo , new MockHttpServletRequest ());
164
+
165
+ List <RequestMappingInfo > list = asList (noMethods , oneMethod , oneMethodOneParam );
203
166
Collections .shuffle (list );
204
167
Collections .sort (list , comparator );
205
168
206
169
assertEquals (oneMethodOneParam , list .get (0 ));
207
170
assertEquals (oneMethod , list .get (1 ));
208
- assertEquals (none , list .get (2 ));
171
+ assertEquals (noMethods , list .get (2 ));
209
172
}
210
173
211
174
@ Test
212
175
public void equals () {
213
- RequestMappingInfo info1 = new RequestMappingInfo (
214
- new PatternsRequestCondition ("/foo" ),
215
- new RequestMethodsRequestCondition (RequestMethod .GET ),
216
- new ParamsRequestCondition ("foo=bar" ),
217
- new HeadersRequestCondition ("foo=bar" ),
218
- new ConsumesRequestCondition ("text/plain" ),
219
- new ProducesRequestCondition ("text/plain" ),
220
- new ParamsRequestCondition ("customFoo=customBar" ));
221
-
222
- RequestMappingInfo info2 = new RequestMappingInfo (
223
- new PatternsRequestCondition ("/foo" ),
224
- new RequestMethodsRequestCondition (RequestMethod .GET ),
225
- new ParamsRequestCondition ("foo=bar" ),
226
- new HeadersRequestCondition ("foo=bar" ),
227
- new ConsumesRequestCondition ("text/plain" ),
228
- new ProducesRequestCondition ("text/plain" ),
229
- new ParamsRequestCondition ("customFoo=customBar" ));
176
+ RequestMappingInfo info1 = paths ("/foo" ).methods (GET )
177
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
178
+ .consumes ("text/plain" ).produces ("text/plain" )
179
+ .build ();
180
+
181
+ RequestMappingInfo info2 = paths ("/foo" ).methods (GET )
182
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
183
+ .consumes ("text/plain" ).produces ("text/plain" )
184
+ .build ();
230
185
231
186
assertEquals (info1 , info2 );
232
187
assertEquals (info1 .hashCode (), info2 .hashCode ());
233
188
234
- info2 = new RequestMappingInfo (
235
- new PatternsRequestCondition ("/foo" , "/NOOOOOO" ),
236
- new RequestMethodsRequestCondition (RequestMethod .GET ),
237
- new ParamsRequestCondition ("foo=bar" ),
238
- new HeadersRequestCondition ("foo=bar" ),
239
- new ConsumesRequestCondition ("text/plain" ),
240
- new ProducesRequestCondition ("text/plain" ),
241
- new ParamsRequestCondition ("customFoo=customBar" ));
189
+ info2 = paths ("/foo" , "/NOOOOOO" ).methods (GET )
190
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
191
+ .consumes ("text/plain" ).produces ("text/plain" )
192
+ .build ();
242
193
243
194
assertFalse (info1 .equals (info2 ));
244
195
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
245
196
246
- info2 = new RequestMappingInfo (
247
- new PatternsRequestCondition ("/foo" ),
248
- new RequestMethodsRequestCondition (RequestMethod .GET , RequestMethod .POST ),
249
- new ParamsRequestCondition ("foo=bar" ),
250
- new HeadersRequestCondition ("foo=bar" ),
251
- new ConsumesRequestCondition ("text/plain" ),
252
- new ProducesRequestCondition ("text/plain" ),
253
- new ParamsRequestCondition ("customFoo=customBar" ));
197
+ info2 = paths ("/foo" ).methods (GET , RequestMethod .POST )
198
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
199
+ .consumes ("text/plain" ).produces ("text/plain" )
200
+ .build ();
254
201
255
202
assertFalse (info1 .equals (info2 ));
256
203
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
257
204
258
- info2 = new RequestMappingInfo (
259
- new PatternsRequestCondition ("/foo" ),
260
- new RequestMethodsRequestCondition (RequestMethod .GET ),
261
- new ParamsRequestCondition ("/NOOOOOO" ),
262
- new HeadersRequestCondition ("foo=bar" ),
263
- new ConsumesRequestCondition ("text/plain" ),
264
- new ProducesRequestCondition ("text/plain" ),
265
- new ParamsRequestCondition ("customFoo=customBar" ));
205
+ info2 = paths ("/foo" ).methods (GET )
206
+ .params ("/NOOOOOO" , "customFoo=customBar" ).headers ("foo=bar" )
207
+ .consumes ("text/plain" ).produces ("text/plain" )
208
+ .build ();
266
209
267
210
assertFalse (info1 .equals (info2 ));
268
211
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
269
212
270
- info2 = new RequestMappingInfo (
271
- new PatternsRequestCondition ("/foo" ),
272
- new RequestMethodsRequestCondition (RequestMethod .GET ),
273
- new ParamsRequestCondition ("foo=bar" ),
274
- new HeadersRequestCondition ("/NOOOOOO" ),
275
- new ConsumesRequestCondition ("text/plain" ),
276
- new ProducesRequestCondition ("text/plain" ),
277
- new ParamsRequestCondition ("customFoo=customBar" ));
213
+ info2 = paths ("/foo" ).methods (GET )
214
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("/NOOOOOO" )
215
+ .consumes ("text/plain" ).produces ("text/plain" )
216
+ .build ();
278
217
279
218
assertFalse (info1 .equals (info2 ));
280
219
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
281
220
282
- info2 = new RequestMappingInfo (
283
- new PatternsRequestCondition ("/foo" ),
284
- new RequestMethodsRequestCondition (RequestMethod .GET ),
285
- new ParamsRequestCondition ("foo=bar" ),
286
- new HeadersRequestCondition ("foo=bar" ),
287
- new ConsumesRequestCondition ("text/NOOOOOO" ),
288
- new ProducesRequestCondition ("text/plain" ),
289
- new ParamsRequestCondition ("customFoo=customBar" ));
221
+ info2 = paths ("/foo" ).methods (GET )
222
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
223
+ .consumes ("text/NOOOOOO" ).produces ("text/plain" )
224
+ .build ();
290
225
291
226
assertFalse (info1 .equals (info2 ));
292
227
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
293
228
294
- info2 = new RequestMappingInfo (
295
- new PatternsRequestCondition ("/foo" ),
296
- new RequestMethodsRequestCondition (RequestMethod .GET ),
297
- new ParamsRequestCondition ("foo=bar" ),
298
- new HeadersRequestCondition ("foo=bar" ),
299
- new ConsumesRequestCondition ("text/plain" ),
300
- new ProducesRequestCondition ("text/NOOOOOO" ),
301
- new ParamsRequestCondition ("customFoo=customBar" ));
229
+ info2 = paths ("/foo" ).methods (GET )
230
+ .params ("foo=bar" , "customFoo=customBar" ).headers ("foo=bar" )
231
+ .consumes ("text/plain" ).produces ("text/NOOOOOO" )
232
+ .build ();
302
233
303
234
assertFalse (info1 .equals (info2 ));
304
235
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
305
236
306
- info2 = new RequestMappingInfo (
307
- new PatternsRequestCondition ("/foo" ),
308
- new RequestMethodsRequestCondition (RequestMethod .GET ),
309
- new ParamsRequestCondition ("foo=bar" ),
310
- new HeadersRequestCondition ("foo=bar" ),
311
- new ConsumesRequestCondition ("text/plain" ),
312
- new ProducesRequestCondition ("text/plain" ),
313
- new ParamsRequestCondition ("customFoo=NOOOOOO" ));
237
+ info2 = paths ("/foo" ).methods (GET )
238
+ .params ("foo=bar" , "customFoo=NOOOOOO" ).headers ("foo=bar" )
239
+ .consumes ("text/plain" ).produces ("text/plain" )
240
+ .build ();
314
241
315
242
assertFalse (info1 .equals (info2 ));
316
243
assertNotEquals (info1 .hashCode (), info2 .hashCode ());
@@ -322,15 +249,11 @@ public void preFlightRequest() {
322
249
request .addHeader (HttpHeaders .ORIGIN , "http://domain.com" );
323
250
request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "POST" );
324
251
325
- RequestMappingInfo info = new RequestMappingInfo (
326
- new PatternsRequestCondition ("/foo" ), new RequestMethodsRequestCondition (RequestMethod .POST ), null ,
327
- null , null , null , null );
252
+ RequestMappingInfo info = paths ("/foo" ).methods (RequestMethod .POST ).build ();
328
253
RequestMappingInfo match = info .getMatchingCondition (request );
329
254
assertNotNull (match );
330
255
331
- info = new RequestMappingInfo (
332
- new PatternsRequestCondition ("/foo" ), new RequestMethodsRequestCondition (RequestMethod .OPTIONS ), null ,
333
- null , null , null , null );
256
+ info = paths ("/foo" ).methods (RequestMethod .OPTIONS ).build ();
334
257
match = info .getMatchingCondition (request );
335
258
assertNull ("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD" , match );
336
259
}
0 commit comments