9
9
10
10
import static org .junit .Assert .*;
11
11
import org .junit .Test ;
12
+ import org .junit .Ignore ;
12
13
13
14
import org .springframework .beans .BeansException ;
14
15
import org .springframework .beans .factory .support .RootBeanDefinition ;
21
22
import org .springframework .web .bind .annotation .InitBinder ;
22
23
import org .springframework .web .bind .annotation .PathVariable ;
23
24
import org .springframework .web .bind .annotation .RequestMapping ;
25
+ import org .springframework .web .bind .annotation .RequestMethod ;
24
26
import org .springframework .web .context .WebApplicationContext ;
25
27
import org .springframework .web .context .support .GenericWebApplicationContext ;
26
28
import org .springframework .web .servlet .DispatcherServlet ;
@@ -44,10 +46,10 @@ public void simple() throws Exception {
44
46
public void multiple () throws Exception {
45
47
initServlet (MultipleUriTemplateController .class );
46
48
47
- MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels/42/bookings/21" );
49
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels/42/bookings/21-other " );
48
50
MockHttpServletResponse response = new MockHttpServletResponse ();
49
51
servlet .service (request , response );
50
- assertEquals ("test-42-21" , response .getContentAsString ());
52
+ assertEquals ("test-42-21-other " , response .getContentAsString ());
51
53
}
52
54
53
55
@ Test
@@ -71,13 +73,19 @@ public void ambiguous() throws Exception {
71
73
}
72
74
73
75
@ Test
76
+ @ Ignore ("In progress" )
74
77
public void relative () throws Exception {
75
78
initServlet (RelativePathUriTemplateController .class );
76
79
77
80
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels/42/bookings/21" );
78
81
MockHttpServletResponse response = new MockHttpServletResponse ();
79
82
servlet .service (request , response );
80
83
assertEquals ("test-42-21" , response .getContentAsString ());
84
+
85
+ request = new MockHttpServletRequest ("GET" , "/hotels/42/bookings/21.html" );
86
+ response = new MockHttpServletResponse ();
87
+ servlet .service (request , response );
88
+ assertEquals ("test-42-21" , response .getContentAsString ());
81
89
}
82
90
83
91
@ Test
@@ -101,6 +109,48 @@ public void typeConversionError() throws Exception {
101
109
assertEquals ("Invalid response status code" , HttpServletResponse .SC_BAD_REQUEST , response .getStatus ());
102
110
}
103
111
112
+ @ Test
113
+ public void explicitSubPath () throws Exception {
114
+ initServlet (ExplicitSubPathController .class );
115
+
116
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels/42" );
117
+ MockHttpServletResponse response = new MockHttpServletResponse ();
118
+ servlet .service (request , response );
119
+ assertEquals ("test-42" , response .getContentAsString ());
120
+ }
121
+
122
+ @ Test
123
+ @ Ignore ("In progress" )
124
+ public void implicitSubPath () throws Exception {
125
+ initServlet (ImplicitSubPathController .class );
126
+
127
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels/42" );
128
+ MockHttpServletResponse response = new MockHttpServletResponse ();
129
+ servlet .service (request , response );
130
+ assertEquals ("test-42" , response .getContentAsString ());
131
+ }
132
+
133
+ @ Test
134
+ @ Ignore ("In progress" )
135
+ public void crud () throws Exception {
136
+ initServlet (CrudController .class );
137
+
138
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/hotels" );
139
+ MockHttpServletResponse response = new MockHttpServletResponse ();
140
+ servlet .service (request , response );
141
+ assertEquals ("getHotels" , response .getContentAsString ());
142
+
143
+ request = new MockHttpServletRequest ("POST" , "/hotels" );
144
+ response = new MockHttpServletResponse ();
145
+ servlet .service (request , response );
146
+ assertEquals ("newHotel" , response .getContentAsString ());
147
+
148
+ request = new MockHttpServletRequest ("POST" , "/hotels" );
149
+ response = new MockHttpServletResponse ();
150
+ servlet .service (request , response );
151
+ assertEquals ("newHotel" , response .getContentAsString ());
152
+ }
153
+
104
154
private void initServlet (final Class <?> controllerclass ) throws ServletException {
105
155
servlet = new DispatcherServlet () {
106
156
@ Override
@@ -133,12 +183,14 @@ public void handle(@PathVariable("root") int root, Writer writer) throws IOExcep
133
183
@ Controller
134
184
public static class MultipleUriTemplateController {
135
185
136
- @ RequestMapping ("/hotels/{hotel}/bookings/{booking}" )
137
- public void handle (@ PathVariable ("hotel" ) String hotel , @ PathVariable int booking , Writer writer )
138
- throws IOException {
186
+ @ RequestMapping ("/hotels/{hotel}/bookings/{booking}-{other}" )
187
+ public void handle (@ PathVariable ("hotel" ) String hotel ,
188
+ @ PathVariable int booking ,
189
+ @ PathVariable String other ,
190
+ Writer writer ) throws IOException {
139
191
assertEquals ("Invalid path variable value" , "42" , hotel );
140
192
assertEquals ("Invalid path variable value" , 21 , booking );
141
- writer .write ("test-" + hotel + "-" + booking );
193
+ writer .write ("test-" + hotel + "-" + booking + "-" + other );
142
194
}
143
195
144
196
}
@@ -166,7 +218,7 @@ public void handle(@PathVariable("hotel") String hotel, @PathVariable Date date,
166
218
}
167
219
168
220
@ Controller
169
- @ RequestMapping ("/hotels/{hotel}/** " )
221
+ @ RequestMapping ("/hotels/{hotel}" )
170
222
public static class RelativePathUriTemplateController {
171
223
172
224
@ RequestMapping ("bookings/{booking}" )
@@ -198,7 +250,59 @@ public void handleWildCard(Writer writer) throws IOException {
198
250
writer .write ("wildcard" );
199
251
}
200
252
253
+ }
254
+
255
+ @ Controller
256
+ @ RequestMapping ("/hotels/*" )
257
+ public static class ExplicitSubPathController {
258
+
259
+ @ RequestMapping ("{hotel}" )
260
+ public void handleHotel (@ PathVariable String hotel , Writer writer ) throws IOException {
261
+ writer .write ("test-" + hotel );
262
+ }
201
263
202
264
}
203
265
266
+ @ Controller
267
+ @ RequestMapping ("hotels" )
268
+ public static class ImplicitSubPathController {
269
+
270
+ @ RequestMapping ("{hotel}" )
271
+ public void handleHotel (@ PathVariable String hotel , Writer writer ) throws IOException {
272
+ writer .write ("test-" + hotel );
273
+ }
274
+ }
275
+
276
+ @ Controller
277
+ @ RequestMapping ("hotels" )
278
+ public static class CrudController {
279
+
280
+ @ RequestMapping (method = RequestMethod .GET )
281
+ public void list (Writer writer ) throws IOException {
282
+ writer .write ("list" );
283
+ }
284
+
285
+ @ RequestMapping (method = RequestMethod .POST )
286
+ public void create (Writer writer ) throws IOException {
287
+ writer .write ("create" );
288
+ }
289
+
290
+ @ RequestMapping (value = "{hotel}" , method = RequestMethod .GET )
291
+ public void show (@ PathVariable String hotel , Writer writer ) throws IOException {
292
+ writer .write ("show-" + hotel );
293
+ }
294
+
295
+ @ RequestMapping (value = "{hotel}" , method = RequestMethod .PUT )
296
+ public void createOrUpdate (@ PathVariable String hotel , Writer writer ) throws IOException {
297
+ writer .write ("createOrUpdate-" + hotel );
298
+ }
299
+
300
+ @ RequestMapping (value = "{hotel}" , method = RequestMethod .DELETE )
301
+ public void remove (@ PathVariable String hotel , Writer writer ) throws IOException {
302
+ writer .write ("remove-" + hotel );
303
+ }
304
+
305
+ }
306
+
307
+
204
308
}
0 commit comments