15
15
*/
16
16
package org .springframework .test .web .servlet .request ;
17
17
18
+ import java .io .IOException ;
18
19
import java .security .Principal ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collections ;
@@ -53,7 +54,7 @@ public class MockHttpServletRequestBuilderTests {
53
54
54
55
55
56
@ Before
56
- public void setUp () throws Exception {
57
+ public void setUp () {
57
58
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/foo/bar" );
58
59
servletContext = new MockServletContext ();
59
60
}
@@ -66,7 +67,7 @@ public void method() {
66
67
}
67
68
68
69
@ Test
69
- public void uri () throws Exception {
70
+ public void uri () {
70
71
String uri = "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)" ;
71
72
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , uri );
72
73
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -81,15 +82,15 @@ public void uri() throws Exception {
81
82
}
82
83
83
84
@ Test
84
- public void requestUriWithEncoding () throws Exception {
85
+ public void requestUriWithEncoding () {
85
86
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/foo bar" );
86
87
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
87
88
88
89
assertEquals ("/foo%20bar" , request .getRequestURI ());
89
90
}
90
91
91
92
@ Test
92
- public void contextPathEmpty () throws Exception {
93
+ public void contextPathEmpty () {
93
94
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/foo" );
94
95
95
96
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -100,7 +101,7 @@ public void contextPathEmpty() throws Exception {
100
101
}
101
102
102
103
@ Test
103
- public void contextPathServletPathEmpty () throws Exception {
104
+ public void contextPathServletPathEmpty () {
104
105
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/travel/hotels/42" );
105
106
this .builder .contextPath ("/travel" );
106
107
@@ -112,7 +113,7 @@ public void contextPathServletPathEmpty() throws Exception {
112
113
}
113
114
114
115
@ Test
115
- public void contextPathServletPath () throws Exception {
116
+ public void contextPathServletPath () {
116
117
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/travel/main/hotels/42" );
117
118
this .builder .contextPath ("/travel" );
118
119
this .builder .servletPath ("/main" );
@@ -125,7 +126,7 @@ public void contextPathServletPath() throws Exception {
125
126
}
126
127
127
128
@ Test
128
- public void contextPathServletPathInfoEmpty () throws Exception {
129
+ public void contextPathServletPathInfoEmpty () {
129
130
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/travel/hotels/42" );
130
131
131
132
this .builder .contextPath ("/travel" );
@@ -139,7 +140,7 @@ public void contextPathServletPathInfoEmpty() throws Exception {
139
140
}
140
141
141
142
@ Test
142
- public void contextPathServletPathInfo () throws Exception {
143
+ public void contextPathServletPathInfo () {
143
144
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/" );
144
145
this .builder .servletPath ("/index.html" );
145
146
this .builder .pathInfo (null );
@@ -152,7 +153,7 @@ public void contextPathServletPathInfo() throws Exception {
152
153
}
153
154
154
155
@ Test
155
- public void contextPathServletPathInvalid () throws Exception {
156
+ public void contextPathServletPathInvalid () {
156
157
157
158
testContextPathServletPathInvalid ("/Foo" , "" , "requestURI [/foo/bar] does not start with contextPath [/Foo]" );
158
159
testContextPathServletPathInvalid ("foo" , "" , "Context path must start with a '/'" );
@@ -175,7 +176,7 @@ private void testContextPathServletPathInvalid(String contextPath, String servle
175
176
}
176
177
177
178
@ Test
178
- public void requestUriAndFragment () throws Exception {
179
+ public void requestUriAndFragment () {
179
180
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/foo#bar" );
180
181
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
181
182
@@ -189,22 +190,22 @@ public void requestParameter() {
189
190
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
190
191
Map <String , String []> parameterMap = request .getParameterMap ();
191
192
192
- assertArrayEquals (new String []{"bar" , "baz" }, parameterMap .get ("foo" ));
193
+ assertArrayEquals (new String [] {"bar" , "baz" }, parameterMap .get ("foo" ));
193
194
}
194
195
195
196
@ Test
196
- public void requestParameterFromQuery () throws Exception {
197
+ public void requestParameterFromQuery () {
197
198
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/?foo=bar&foo=baz" );
198
199
199
200
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
200
201
Map <String , String []> parameterMap = request .getParameterMap ();
201
202
202
- assertArrayEquals (new String []{"bar" , "baz" }, parameterMap .get ("foo" ));
203
+ assertArrayEquals (new String [] {"bar" , "baz" }, parameterMap .get ("foo" ));
203
204
assertEquals ("foo=bar&foo=baz" , request .getQueryString ());
204
205
}
205
206
206
207
@ Test
207
- public void requestParameterFromQueryList () throws Exception {
208
+ public void requestParameterFromQueryList () {
208
209
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/?foo[0]=bar&foo[1]=baz" );
209
210
210
211
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -215,7 +216,7 @@ public void requestParameterFromQueryList() throws Exception {
215
216
}
216
217
217
218
@ Test
218
- public void requestParameterFromQueryWithEncoding () throws Exception {
219
+ public void requestParameterFromQueryWithEncoding () {
219
220
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/?foo={value}" , "bar=baz" );
220
221
221
222
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -227,18 +228,18 @@ public void requestParameterFromQueryWithEncoding() throws Exception {
227
228
// SPR-11043
228
229
229
230
@ Test
230
- public void requestParameterFromQueryNull () throws Exception {
231
+ public void requestParameterFromQueryNull () {
231
232
this .builder = new MockHttpServletRequestBuilder (HttpMethod .GET , "/?foo" );
232
233
233
234
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
234
235
Map <String , String []> parameterMap = request .getParameterMap ();
235
236
236
- assertArrayEquals (new String []{null }, parameterMap .get ("foo" ));
237
+ assertArrayEquals (new String [] {null }, parameterMap .get ("foo" ));
237
238
assertEquals ("foo" , request .getQueryString ());
238
239
}
239
240
240
241
@ Test
241
- public void acceptHeader () throws Exception {
242
+ public void acceptHeader () {
242
243
this .builder .accept (MediaType .TEXT_HTML , MediaType .APPLICATION_XML );
243
244
244
245
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -251,7 +252,7 @@ public void acceptHeader() throws Exception {
251
252
}
252
253
253
254
@ Test
254
- public void contentType () throws Exception {
255
+ public void contentType () {
255
256
this .builder .contentType (MediaType .TEXT_HTML );
256
257
257
258
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -263,10 +264,23 @@ public void contentType() throws Exception {
263
264
assertEquals ("text/html" , contentTypes .get (0 ));
264
265
}
265
266
267
+ @ Test
268
+ public void contentTypeViaString () {
269
+ this .builder .contentType ("text/html" );
270
+
271
+ MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
272
+ String contentType = request .getContentType ();
273
+ List <String > contentTypes = Collections .list (request .getHeaders ("Content-Type" ));
274
+
275
+ assertEquals ("text/html" , contentType );
276
+ assertEquals (1 , contentTypes .size ());
277
+ assertEquals ("text/html" , contentTypes .get (0 ));
278
+ }
279
+
266
280
// SPR-11308
267
281
268
282
@ Test
269
- public void contentTypeViaHeader () throws Exception {
283
+ public void contentTypeViaHeader () {
270
284
this .builder .header ("Content-Type" , MediaType .TEXT_HTML_VALUE );
271
285
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
272
286
String contentType = request .getContentType ();
@@ -277,7 +291,7 @@ public void contentTypeViaHeader() throws Exception {
277
291
// SPR-11308
278
292
279
293
@ Test
280
- public void contentTypeViaMultipleHeaderValues () throws Exception {
294
+ public void contentTypeViaMultipleHeaderValues () {
281
295
this .builder .header ("Content-Type" , MediaType .TEXT_HTML_VALUE , MediaType .ALL_VALUE );
282
296
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
283
297
String contentType = request .getContentType ();
@@ -286,7 +300,7 @@ public void contentTypeViaMultipleHeaderValues() throws Exception {
286
300
}
287
301
288
302
@ Test
289
- public void body () throws Exception {
303
+ public void body () throws IOException {
290
304
byte [] body = "Hello World" .getBytes ("UTF-8" );
291
305
this .builder .content (body );
292
306
@@ -297,7 +311,7 @@ public void body() throws Exception {
297
311
}
298
312
299
313
@ Test
300
- public void header () throws Exception {
314
+ public void header () {
301
315
this .builder .header ("foo" , "bar" , "baz" );
302
316
303
317
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
@@ -309,7 +323,7 @@ public void header() throws Exception {
309
323
}
310
324
311
325
@ Test
312
- public void headers () throws Exception {
326
+ public void headers () {
313
327
HttpHeaders httpHeaders = new HttpHeaders ();
314
328
httpHeaders .setContentType (MediaType .APPLICATION_JSON );
315
329
httpHeaders .put ("foo" , Arrays .asList ("bar" , "baz" ));
@@ -325,7 +339,7 @@ public void headers() throws Exception {
325
339
}
326
340
327
341
@ Test
328
- public void cookie () throws Exception {
342
+ public void cookie () {
329
343
Cookie cookie1 = new Cookie ("foo" , "bar" );
330
344
Cookie cookie2 = new Cookie ("baz" , "qux" );
331
345
this .builder .cookie (cookie1 , cookie2 );
@@ -341,7 +355,7 @@ public void cookie() throws Exception {
341
355
}
342
356
343
357
@ Test
344
- public void locale () throws Exception {
358
+ public void locale () {
345
359
Locale locale = new Locale ("nl" , "nl" );
346
360
this .builder .locale (locale );
347
361
@@ -351,7 +365,7 @@ public void locale() throws Exception {
351
365
}
352
366
353
367
@ Test
354
- public void characterEncoding () throws Exception {
368
+ public void characterEncoding () {
355
369
String encoding = "UTF-8" ;
356
370
this .builder .characterEncoding (encoding );
357
371
@@ -361,23 +375,23 @@ public void characterEncoding() throws Exception {
361
375
}
362
376
363
377
@ Test
364
- public void requestAttribute () throws Exception {
378
+ public void requestAttribute () {
365
379
this .builder .requestAttr ("foo" , "bar" );
366
380
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
367
381
368
382
assertEquals ("bar" , request .getAttribute ("foo" ));
369
383
}
370
384
371
385
@ Test
372
- public void sessionAttribute () throws Exception {
386
+ public void sessionAttribute () {
373
387
this .builder .sessionAttr ("foo" , "bar" );
374
388
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
375
389
376
390
assertEquals ("bar" , request .getSession ().getAttribute ("foo" ));
377
391
}
378
392
379
393
@ Test
380
- public void sessionAttributes () throws Exception {
394
+ public void sessionAttributes () {
381
395
Map <String , Object > map = new HashMap <String , Object >();
382
396
map .put ("foo" , "bar" );
383
397
this .builder .sessionAttrs (map );
@@ -388,7 +402,7 @@ public void sessionAttributes() throws Exception {
388
402
}
389
403
390
404
@ Test
391
- public void session () throws Exception {
405
+ public void session () {
392
406
MockHttpSession session = new MockHttpSession (this .servletContext );
393
407
session .setAttribute ("foo" , "bar" );
394
408
this .builder .session (session );
@@ -402,7 +416,7 @@ public void session() throws Exception {
402
416
}
403
417
404
418
@ Test
405
- public void flashAttribute () throws Exception {
419
+ public void flashAttribute () {
406
420
this .builder .flashAttr ("foo" , "bar" );
407
421
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
408
422
@@ -412,7 +426,7 @@ public void flashAttribute() throws Exception {
412
426
}
413
427
414
428
@ Test
415
- public void principal () throws Exception {
429
+ public void principal () {
416
430
User user = new User ();
417
431
this .builder .principal (user );
418
432
MockHttpServletRequest request = this .builder .buildRequest (this .servletContext );
0 commit comments