50
50
import org .springframework .web .context .request .ServletRequestAttributes ;
51
51
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
52
52
import org .springframework .web .servlet .DispatcherServlet ;
53
+ import org .springframework .web .servlet .ModelAndView ;
53
54
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
54
55
import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
55
56
import org .springframework .web .util .UriComponents ;
@@ -85,38 +86,38 @@ public void reset() {
85
86
86
87
87
88
@ Test
88
- public void testFromController () {
89
+ public void fromControllerPlain () {
89
90
UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
90
91
assertThat (uriComponents .toUriString (), Matchers .endsWith ("/people" ));
91
92
}
92
93
93
94
@ Test
94
- public void testFromControllerUriTemplate () {
95
+ public void fromControllerUriTemplate () {
95
96
UriComponents uriComponents = fromController (PersonsAddressesController .class ).buildAndExpand (15 );
96
97
assertThat (uriComponents .toUriString (), endsWith ("/people/15/addresses" ));
97
98
}
98
99
99
100
@ Test
100
- public void testFromControllerSubResource () {
101
+ public void fromControllerSubResource () {
101
102
UriComponents uriComponents = fromController (PersonControllerImpl .class ).pathSegment ("something" ).build ();
102
103
103
104
assertThat (uriComponents .toUriString (), endsWith ("/people/something" ));
104
105
}
105
106
106
107
@ Test
107
- public void testFromControllerTwoTypeLevelMappings () {
108
+ public void fromControllerTwoTypeLevelMappings () {
108
109
UriComponents uriComponents = fromController (InvalidController .class ).build ();
109
110
assertThat (uriComponents .toUriString (), is ("http://localhost/persons" ));
110
111
}
111
112
112
113
@ Test
113
- public void testFromControllerNotMapped () {
114
+ public void fromControllerNotMapped () {
114
115
UriComponents uriComponents = fromController (UnmappedController .class ).build ();
115
116
assertThat (uriComponents .toUriString (), is ("http://localhost/" ));
116
117
}
117
118
118
119
@ Test
119
- public void testFromControllerWithCustomBaseUrlViaStaticCall () {
120
+ public void fromControllerWithCustomBaseUrlViaStaticCall () {
120
121
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
121
122
UriComponents uriComponents = fromController (builder , PersonControllerImpl .class ).build ();
122
123
@@ -125,25 +126,49 @@ public void testFromControllerWithCustomBaseUrlViaStaticCall() {
125
126
}
126
127
127
128
@ Test
128
- public void testFromControllerWithCustomBaseUrlViaInstance () {
129
+ public void fromControllerWithCustomBaseUrlViaInstance () {
129
130
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
130
- MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder . relativeTo (builder );
131
+ MvcUriComponentsBuilder mvcBuilder = relativeTo (builder );
131
132
UriComponents uriComponents = mvcBuilder .withController (PersonControllerImpl .class ).build ();
132
133
133
134
assertEquals ("http://example.org:9090/base/people" , uriComponents .toString ());
134
135
assertEquals ("http://example.org:9090/base" , builder .toUriString ());
135
136
}
136
137
137
138
@ Test
138
- public void testFromMethodNamePathVariable () {
139
+ public void usesForwardedHostAsHostIfHeaderIsSet () {
140
+ this .request .addHeader ("X-Forwarded-Host" , "somethingDifferent" );
141
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
142
+
143
+ assertThat (uriComponents .toUriString (), startsWith ("http://somethingDifferent" ));
144
+ }
145
+
146
+ @ Test
147
+ public void usesForwardedHostAndPortFromHeader () {
148
+ request .addHeader ("X-Forwarded-Host" , "foobar:8088" );
149
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
150
+
151
+ assertThat (uriComponents .toUriString (), startsWith ("http://foobar:8088" ));
152
+ }
153
+
154
+ @ Test
155
+ public void usesFirstHostOfXForwardedHost () {
156
+ request .addHeader ("X-Forwarded-Host" , "barfoo:8888, localhost:8088" );
157
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
158
+
159
+ assertThat (uriComponents .toUriString (), startsWith ("http://barfoo:8888" ));
160
+ }
161
+
162
+ @ Test
163
+ public void fromMethodNamePathVariable () {
139
164
UriComponents uriComponents = fromMethodName (ControllerWithMethods .class ,
140
165
"methodWithPathVariable" , "1" ).build ();
141
166
142
167
assertThat (uriComponents .toUriString (), is ("http://localhost/something/1/foo" ));
143
168
}
144
169
145
170
@ Test
146
- public void testFromMethodNameTypeLevelPathVariable () {
171
+ public void fromMethodNameTypeLevelPathVariable () {
147
172
this .request .setContextPath ("/myapp" );
148
173
UriComponents uriComponents = fromMethodName (
149
174
PersonsAddressesController .class , "getAddressesForCountry" , "DE" ).buildAndExpand ("1" );
@@ -152,7 +177,7 @@ public void testFromMethodNameTypeLevelPathVariable() {
152
177
}
153
178
154
179
@ Test
155
- public void testFromMethodNameTwoPathVariables () {
180
+ public void fromMethodNameTwoPathVariables () {
156
181
DateTime now = DateTime .now ();
157
182
UriComponents uriComponents = fromMethodName (
158
183
ControllerWithMethods .class , "methodWithTwoPathVariables" , 1 , now ).build ();
@@ -161,7 +186,7 @@ public void testFromMethodNameTwoPathVariables() {
161
186
}
162
187
163
188
@ Test
164
- public void testFromMethodNameWithPathVarAndRequestParam () {
189
+ public void fromMethodNameWithPathVarAndRequestParam () {
165
190
UriComponents uriComponents = fromMethodName (
166
191
ControllerWithMethods .class , "methodForNextPage" , "1" , 10 , 5 ).build ();
167
192
@@ -179,21 +204,21 @@ public void fromMethodNameWithBridgedMethod() {
179
204
}
180
205
181
206
@ Test // SPR-11391
182
- public void testFromMethodNameTypeLevelPathVariableWithoutArgumentValue () {
207
+ public void fromMethodNameTypeLevelPathVariableWithoutArgumentValue () {
183
208
UriComponents uriComponents = fromMethodName (UserContactController .class , "showCreate" , 123 ).build ();
184
209
185
210
assertThat (uriComponents .getPath (), is ("/user/123/contacts/create" ));
186
211
}
187
212
188
213
@ Test
189
- public void testFromMethodNameNotMapped () {
214
+ public void fromMethodNameNotMapped () {
190
215
UriComponents uriComponents = fromMethodName (UnmappedController .class , "unmappedMethod" ).build ();
191
216
192
217
assertThat (uriComponents .toUriString (), is ("http://localhost/" ));
193
218
}
194
219
195
220
@ Test
196
- public void testFromMethodNameWithCustomBaseUrlViaStaticCall () {
221
+ public void fromMethodNameWithCustomBaseUrlViaStaticCall () {
197
222
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
198
223
UriComponents uriComponents = fromMethodName (builder , ControllerWithMethods .class ,
199
224
"methodWithPathVariable" , "1" ).build ();
@@ -203,57 +228,57 @@ public void testFromMethodNameWithCustomBaseUrlViaStaticCall() {
203
228
}
204
229
205
230
@ Test
206
- public void testFromMethodNameWithCustomBaseUrlViaInstance () {
231
+ public void fromMethodNameWithCustomBaseUrlViaInstance () {
207
232
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
208
- MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder . relativeTo (builder );
233
+ MvcUriComponentsBuilder mvcBuilder = relativeTo (builder );
209
234
UriComponents uriComponents = mvcBuilder .withMethodName (ControllerWithMethods .class ,
210
235
"methodWithPathVariable" , "1" ).build ();
211
236
212
237
assertEquals ("http://example.org:9090/base/something/1/foo" , uriComponents .toString ());
213
238
assertEquals ("http://example.org:9090/base" , builder .toUriString ());
214
239
}
215
240
216
- @ Test
217
- public void testFromMethodNameWithMetaAnnotation () {
218
- UriComponents uriComponents = fromMethodName (MetaAnnotationController .class , "handleInput" ).build ();
219
- assertThat (uriComponents .toUriString (), is ("http://localhost/input" ));
220
- }
221
-
222
241
@ Test // SPR-14405
223
- public void testFromMappingNameWithOptionalParam () {
242
+ public void fromMethodNameWithOptionalParam () {
224
243
UriComponents uriComponents = fromMethodName (ControllerWithMethods .class ,
225
244
"methodWithOptionalParam" , new Object [] {null }).build ();
226
245
227
246
assertThat (uriComponents .toUriString (), is ("http://localhost/something/optional-param" ));
228
247
}
229
248
230
249
@ Test
231
- public void testFromMethodCall () {
250
+ public void fromMethodNameWithMetaAnnotation () {
251
+ UriComponents uriComponents = fromMethodName (MetaAnnotationController .class , "handleInput" ).build ();
252
+
253
+ assertThat (uriComponents .toUriString (), is ("http://localhost/input" ));
254
+ }
255
+
256
+ @ Test
257
+ public void fromMethodCallPlain () {
232
258
UriComponents uriComponents = fromMethodCall (on (ControllerWithMethods .class ).myMethod (null )).build ();
233
259
234
260
assertThat (uriComponents .toUriString (), startsWith ("http://localhost" ));
235
261
assertThat (uriComponents .toUriString (), endsWith ("/something/else" ));
236
262
}
237
263
238
264
@ Test
239
- public void testFromMethodCallOnSubclass () {
265
+ public void fromMethodCallOnSubclass () {
240
266
UriComponents uriComponents = fromMethodCall (on (ExtendedController .class ).myMethod (null )).build ();
241
267
242
268
assertThat (uriComponents .toUriString (), startsWith ("http://localhost" ));
243
269
assertThat (uriComponents .toUriString (), endsWith ("/extended/else" ));
244
270
}
245
271
246
272
@ Test
247
- public void testFromMethodCallWithTypeLevelUriVars () {
273
+ public void fromMethodCallWithTypeLevelUriVars () {
248
274
UriComponents uriComponents = fromMethodCall (
249
275
on (PersonsAddressesController .class ).getAddressesForCountry ("DE" )).buildAndExpand (15 );
250
276
251
277
assertThat (uriComponents .toUriString (), endsWith ("/people/15/addresses/DE" ));
252
278
}
253
279
254
-
255
280
@ Test
256
- public void testFromMethodCallWithPathVar () {
281
+ public void fromMethodCallWithPathVariable () {
257
282
UriComponents uriComponents = fromMethodCall (
258
283
on (ControllerWithMethods .class ).methodWithPathVariable ("1" )).build ();
259
284
@@ -262,7 +287,7 @@ public void testFromMethodCallWithPathVar() {
262
287
}
263
288
264
289
@ Test
265
- public void testFromMethodCallWithPathVarAndRequestParams () {
290
+ public void fromMethodCallWithPathVariableAndRequestParams () {
266
291
UriComponents uriComponents = fromMethodCall (
267
292
on (ControllerWithMethods .class ).methodForNextPage ("1" , 10 , 5 )).build ();
268
293
@@ -274,7 +299,7 @@ public void testFromMethodCallWithPathVarAndRequestParams() {
274
299
}
275
300
276
301
@ Test
277
- public void testFromMethodCallWithPathVarAndMultiValueRequestParams () {
302
+ public void fromMethodCallWithPathVariableAndMultiValueRequestParams () {
278
303
UriComponents uriComponents = fromMethodCall (
279
304
on (ControllerWithMethods .class ).methodWithMultiValueRequestParams ("1" , Arrays .asList (3 , 7 ), 5 )).build ();
280
305
@@ -286,7 +311,7 @@ public void testFromMethodCallWithPathVarAndMultiValueRequestParams() {
286
311
}
287
312
288
313
@ Test
289
- public void testFromMethodCallWithCustomBaseUrlViaStaticCall () {
314
+ public void fromMethodCallWithCustomBaseUrlViaStaticCall () {
290
315
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
291
316
UriComponents uriComponents = fromMethodCall (builder , on (ControllerWithMethods .class ).myMethod (null )).build ();
292
317
@@ -295,17 +320,49 @@ public void testFromMethodCallWithCustomBaseUrlViaStaticCall() {
295
320
}
296
321
297
322
@ Test
298
- public void testFromMethodCallWithCustomBaseUrlViaInstance () {
323
+ public void fromMethodCallWithCustomBaseUrlViaInstance () {
299
324
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
300
- MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder . relativeTo (builder );
325
+ MvcUriComponentsBuilder mvcBuilder = relativeTo (builder );
301
326
UriComponents result = mvcBuilder .withMethodCall (on (ControllerWithMethods .class ).myMethod (null )).build ();
302
327
303
328
assertEquals ("http://example.org:9090/base/something/else" , result .toString ());
304
329
assertEquals ("http://example.org:9090/base" , builder .toUriString ());
305
330
}
306
331
332
+ @ Test // SPR-16710
333
+ public void fromMethodCallWithModelAndViewReturnType () {
334
+ UriComponents uriComponents = fromMethodCall (
335
+ on (BookingControllerWithModelAndView .class ).getBooking (21L )).buildAndExpand (42 );
336
+
337
+ assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
338
+ }
339
+
340
+ @ Test // SPR-16710
341
+ public void fromMethodCallWithObjectReturnType () {
342
+ UriComponents uriComponents = fromMethodCall (
343
+ on (BookingControllerWithObject .class ).getBooking (21L )).buildAndExpand (42 );
344
+
345
+ assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
346
+ }
347
+
348
+ @ Test (expected = IllegalStateException .class ) // SPR-16710
349
+ public void fromMethodCallWithStringReturnType () {
350
+ UriComponents uriComponents = fromMethodCall (
351
+ on (BookingControllerWithString .class ).getBooking (21L )).buildAndExpand (42 );
352
+
353
+ assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
354
+ }
355
+
356
+ @ Test // SPR-16710
357
+ public void fromMethodNameWithStringReturnType () {
358
+ UriComponents uriComponents = fromMethodName (
359
+ BookingControllerWithString .class , "getBooking" , 21L ).buildAndExpand (42 );
360
+
361
+ assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
362
+ }
363
+
307
364
@ Test
308
- public void testFromMappingName () {
365
+ public void fromMappingNamePlain () {
309
366
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ();
310
367
context .setServletContext (new MockServletContext ());
311
368
context .register (WebConfig .class );
@@ -317,12 +374,12 @@ public void testFromMappingName() {
317
374
this .request .setContextPath ("/base" );
318
375
319
376
String mappingName = "PAC#getAddressesForCountry" ;
320
- String url = MvcUriComponentsBuilder . fromMappingName (mappingName ).arg (0 , "DE" ).buildAndExpand (123 );
377
+ String url = fromMappingName (mappingName ).arg (0 , "DE" ).buildAndExpand (123 );
321
378
assertEquals ("/base/people/123/addresses/DE" , url );
322
379
}
323
380
324
381
@ Test
325
- public void testFromMappingNameWithCustomBaseUrl () {
382
+ public void fromMappingNameWithCustomBaseUrl () {
326
383
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ();
327
384
context .setServletContext (new MockServletContext ());
328
385
context .register (WebConfig .class );
@@ -331,42 +388,11 @@ public void testFromMappingNameWithCustomBaseUrl() {
331
388
this .request .setAttribute (DispatcherServlet .WEB_APPLICATION_CONTEXT_ATTRIBUTE , context );
332
389
333
390
UriComponentsBuilder baseUrl = UriComponentsBuilder .fromUriString ("http://example.org:9999/base" );
334
- MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder . relativeTo (baseUrl );
391
+ MvcUriComponentsBuilder mvcBuilder = relativeTo (baseUrl );
335
392
String url = mvcBuilder .withMappingName ("PAC#getAddressesForCountry" ).arg (0 , "DE" ).buildAndExpand (123 );
336
393
assertEquals ("http://example.org:9999/base/people/123/addresses/DE" , url );
337
394
}
338
395
339
- @ Test
340
- public void usesForwardedHostAsHostIfHeaderIsSet () {
341
- this .request .addHeader ("X-Forwarded-Host" , "somethingDifferent" );
342
- UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
343
-
344
- assertThat (uriComponents .toUriString (), startsWith ("http://somethingDifferent" ));
345
- }
346
-
347
- @ Test
348
- public void usesForwardedHostAndPortFromHeader () {
349
- request .addHeader ("X-Forwarded-Host" , "foobar:8088" );
350
- UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
351
-
352
- assertThat (uriComponents .toUriString (), startsWith ("http://foobar:8088" ));
353
- }
354
-
355
- @ Test
356
- public void usesFirstHostOfXForwardedHost () {
357
- request .addHeader ("X-Forwarded-Host" , "barfoo:8888, localhost:8088" );
358
- UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
359
-
360
- assertThat (uriComponents .toUriString (), startsWith ("http://barfoo:8888" ));
361
- }
362
-
363
- @ Test // SPR-16710
364
- public void withStringReturnType () {
365
- UriComponents uriComponents = MvcUriComponentsBuilder .fromMethodCall (
366
- on (BookingController .class ).getBooking (21L )).buildAndExpand (42 );
367
- assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
368
- }
369
-
370
396
371
397
static class Person {
372
398
@@ -516,12 +542,34 @@ public PersonsAddressesController controller() {
516
542
517
543
@ Controller
518
544
@ RequestMapping ("/hotels/{hotel}" )
519
- public class BookingController {
545
+ static class BookingControllerWithModelAndView {
546
+
547
+ @ GetMapping ("/bookings/{booking}" )
548
+ public ModelAndView getBooking (@ PathVariable Long booking ) {
549
+ return new ModelAndView ("url" );
550
+ }
551
+ }
552
+
553
+
554
+ @ Controller
555
+ @ RequestMapping ("/hotels/{hotel}" )
556
+ static class BookingControllerWithObject {
520
557
521
558
@ GetMapping ("/bookings/{booking}" )
522
559
public Object getBooking (@ PathVariable Long booking ) {
523
560
return "url" ;
524
561
}
525
562
}
526
563
564
+
565
+ @ Controller
566
+ @ RequestMapping ("/hotels/{hotel}" )
567
+ static class BookingControllerWithString {
568
+
569
+ @ GetMapping ("/bookings/{booking}" )
570
+ public String getBooking (@ PathVariable Long booking ) {
571
+ return "url" ;
572
+ }
573
+ }
574
+
527
575
}
0 commit comments