1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import java .util .Locale ;
36
36
import java .util .Map ;
37
37
import java .util .Set ;
38
- import java . util . Vector ;
38
+
39
39
import javax .servlet .RequestDispatcher ;
40
40
import javax .servlet .ServletContext ;
41
41
import javax .servlet .ServletException ;
58
58
* @author Rod Johnson
59
59
* @author Rick Evans
60
60
* @author Mark Fisher
61
+ * @author Sam Brannen
61
62
* @since 1.0.2
62
63
*/
63
64
public class MockHttpServletRequest implements HttpServletRequest {
@@ -93,13 +94,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
93
94
public static final String DEFAULT_REMOTE_HOST = "localhost" ;
94
95
95
96
private static final String CONTENT_TYPE_HEADER = "Content-Type" ;
96
-
97
+
97
98
private static final String CHARSET_PREFIX = "charset=" ;
98
99
99
-
100
100
private boolean active = true ;
101
101
102
-
103
102
// ---------------------------------------------------------------------
104
103
// ServletRequest properties
105
104
// ---------------------------------------------------------------------
@@ -141,7 +140,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
141
140
142
141
private int localPort = DEFAULT_SERVER_PORT ;
143
142
144
-
145
143
// ---------------------------------------------------------------------
146
144
// HttpServletRequest properties
147
145
// ---------------------------------------------------------------------
@@ -186,46 +184,49 @@ public class MockHttpServletRequest implements HttpServletRequest {
186
184
// ---------------------------------------------------------------------
187
185
188
186
/**
189
- * Create a new MockHttpServletRequest with a default
190
- * {@link org.springframework.mock.web. MockServletContext}.
191
- * @see org.springframework.mock.web.MockServletContext
187
+ * Create a new {@code MockHttpServletRequest} with a default
188
+ * {@link MockServletContext}.
189
+ * @see #MockHttpServletRequest(ServletContext, String, String)
192
190
*/
193
191
public MockHttpServletRequest () {
194
192
this (null , "" , "" );
195
193
}
196
194
197
195
/**
198
- * Create a new MockHttpServletRequest with a default
199
- * {@link org.springframework.mock.web. MockServletContext}.
196
+ * Create a new {@code MockHttpServletRequest} with a default
197
+ * {@link MockServletContext}.
200
198
* @param method the request method (may be <code>null</code>)
201
199
* @param requestURI the request URI (may be <code>null</code>)
202
200
* @see #setMethod
203
201
* @see #setRequestURI
204
- * @see org.springframework.mock.web.MockServletContext
202
+ * @see #MockHttpServletRequest(ServletContext, String, String)
205
203
*/
206
204
public MockHttpServletRequest (String method , String requestURI ) {
207
205
this (null , method , requestURI );
208
206
}
209
207
210
208
/**
211
- * Create a new MockHttpServletRequest.
209
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext} .
212
210
* @param servletContext the ServletContext that the request runs in (may be
213
- * <code>null</code> to use a default MockServletContext)
214
- * @see org.springframework.mock.web.MockServletContext
211
+ * <code>null</code> to use a default {@link MockServletContext} )
212
+ * @see #MockHttpServletRequest(ServletContext, String, String)
215
213
*/
216
214
public MockHttpServletRequest (ServletContext servletContext ) {
217
215
this (servletContext , "" , "" );
218
216
}
219
217
220
218
/**
221
- * Create a new MockHttpServletRequest.
219
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext},
220
+ * {@code method}, and {@code requestURI}.
221
+ * <p>The preferred locale will be set to {@link Locale#ENGLISH}.
222
222
* @param servletContext the ServletContext that the request runs in (may be
223
- * <code>null</code> to use a default MockServletContext)
223
+ * <code>null</code> to use a default {@link MockServletContext} )
224
224
* @param method the request method (may be <code>null</code>)
225
225
* @param requestURI the request URI (may be <code>null</code>)
226
226
* @see #setMethod
227
227
* @see #setRequestURI
228
- * @see org.springframework.mock.web.MockServletContext
228
+ * @see #setPreferredLocales
229
+ * @see MockServletContext
229
230
*/
230
231
public MockHttpServletRequest (ServletContext servletContext , String method , String requestURI ) {
231
232
this .servletContext = (servletContext != null ? servletContext : new MockServletContext ());
@@ -234,7 +235,6 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
234
235
this .locales .add (Locale .ENGLISH );
235
236
}
236
237
237
-
238
238
// ---------------------------------------------------------------------
239
239
// Lifecycle methods
240
240
// ---------------------------------------------------------------------
@@ -279,7 +279,6 @@ protected void checkActive() throws IllegalStateException {
279
279
}
280
280
}
281
281
282
-
283
282
// ---------------------------------------------------------------------
284
283
// ServletRequest interface
285
284
// ---------------------------------------------------------------------
@@ -291,7 +290,7 @@ public Object getAttribute(String name) {
291
290
292
291
public Enumeration <String > getAttributeNames () {
293
292
checkActive ();
294
- return new Vector < String > (this .attributes .keySet ()). elements ( );
293
+ return Collections . enumeration (this .attributes .keySet ());
295
294
}
296
295
297
296
public String getCharacterEncoding () {
@@ -302,11 +301,11 @@ public void setCharacterEncoding(String characterEncoding) {
302
301
this .characterEncoding = characterEncoding ;
303
302
updateContentTypeHeader ();
304
303
}
305
-
304
+
306
305
private void updateContentTypeHeader () {
307
306
if (this .contentType != null ) {
308
307
StringBuilder sb = new StringBuilder (this .contentType );
309
- if (this .contentType .toLowerCase ().indexOf (CHARSET_PREFIX ) == - 1 && this .characterEncoding != null ) {
308
+ if (! this .contentType .toLowerCase ().contains (CHARSET_PREFIX ) && this .characterEncoding != null ) {
310
309
sb .append (";" ).append (CHARSET_PREFIX ).append (this .characterEncoding );
311
310
}
312
311
doAddHeaderValue (CONTENT_TYPE_HEADER , sb .toString (), true );
@@ -348,8 +347,7 @@ public ServletInputStream getInputStream() {
348
347
349
348
/**
350
349
* Set a single value for the specified HTTP parameter.
351
- * <p>
352
- * If there are already one or more values registered for the given
350
+ * <p>If there are already one or more values registered for the given
353
351
* parameter name, they will be replaced.
354
352
*/
355
353
public void setParameter (String name , String value ) {
@@ -358,8 +356,7 @@ public void setParameter(String name, String value) {
358
356
359
357
/**
360
358
* Set an array of values for the specified HTTP parameter.
361
- * <p>
362
- * If there are already one or more values registered for the given
359
+ * <p>If there are already one or more values registered for the given
363
360
* parameter name, they will be replaced.
364
361
*/
365
362
public void setParameter (String name , String [] values ) {
@@ -368,7 +365,7 @@ public void setParameter(String name, String[] values) {
368
365
}
369
366
370
367
/**
371
- * Sets all provided parameters <emphasis >replacing</emphasis > any existing
368
+ * Sets all provided parameters <strong >replacing</strong > any existing
372
369
* values for the provided parameter names. To add without replacing
373
370
* existing values, use {@link #addParameters(java.util.Map)}.
374
371
*/
@@ -393,8 +390,7 @@ else if (value instanceof String[]) {
393
390
394
391
/**
395
392
* Add a single value for the specified HTTP parameter.
396
- * <p>
397
- * If there are already one or more values registered for the given
393
+ * <p>If there are already one or more values registered for the given
398
394
* parameter name, the given value will be added to the end of the list.
399
395
*/
400
396
public void addParameter (String name , String value ) {
@@ -403,8 +399,7 @@ public void addParameter(String name, String value) {
403
399
404
400
/**
405
401
* Add an array of values for the specified HTTP parameter.
406
- * <p>
407
- * If there are already one or more values registered for the given
402
+ * <p>If there are already one or more values registered for the given
408
403
* parameter name, the given values will be added to the end of the list.
409
404
*/
410
405
public void addParameter (String name , String [] values ) {
@@ -422,7 +417,7 @@ public void addParameter(String name, String[] values) {
422
417
}
423
418
424
419
/**
425
- * Adds all provided parameters <emphasis >without</emphasis > replacing any
420
+ * Adds all provided parameters <strong >without</strong > replacing any
426
421
* existing values. To replace existing values, use
427
422
* {@link #setParameters(java.util.Map)}.
428
423
*/
@@ -566,12 +561,25 @@ public void clearAttributes() {
566
561
567
562
/**
568
563
* Add a new preferred locale, before any existing locales.
564
+ * @see #setPreferredLocales
569
565
*/
570
566
public void addPreferredLocale (Locale locale ) {
571
567
Assert .notNull (locale , "Locale must not be null" );
572
568
this .locales .add (0 , locale );
573
569
}
574
570
571
+ /**
572
+ * Set the list of preferred locales, in descending order, effectively replacing
573
+ * any existing locales.
574
+ * @see #addPreferredLocale
575
+ * @since 3.2
576
+ */
577
+ public void setPreferredLocales (List <Locale > locales ) {
578
+ Assert .notEmpty (locales , "preferred locales list must not be empty" );
579
+ this .locales .clear ();
580
+ this .locales .addAll (locales );
581
+ }
582
+
575
583
public Locale getLocale () {
576
584
return this .locales .get (0 );
577
585
}
@@ -628,7 +636,6 @@ public int getLocalPort() {
628
636
return this .localPort ;
629
637
}
630
638
631
-
632
639
// ---------------------------------------------------------------------
633
640
// HttpServletRequest interface
634
641
// ---------------------------------------------------------------------
@@ -673,7 +680,7 @@ public void addHeader(String name, Object value) {
673
680
}
674
681
doAddHeaderValue (name , value , false );
675
682
}
676
-
683
+
677
684
@ SuppressWarnings ("rawtypes" )
678
685
private void doAddHeaderValue (String name , Object value , boolean replace ) {
679
686
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
@@ -711,6 +718,20 @@ else if (value != null) {
711
718
}
712
719
}
713
720
721
+ public String getHeader (String name ) {
722
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
723
+ return (header != null ? header .getStringValue () : null );
724
+ }
725
+
726
+ public Enumeration <String > getHeaders (String name ) {
727
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
728
+ return Collections .enumeration (header != null ? header .getStringValues () : new LinkedList <String >());
729
+ }
730
+
731
+ public Enumeration <String > getHeaderNames () {
732
+ return Collections .enumeration (this .headers .keySet ());
733
+ }
734
+
714
735
public int getIntHeader (String name ) {
715
736
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
716
737
Object value = (header != null ? header .getValue () : null );
@@ -728,20 +749,6 @@ else if (value != null) {
728
749
}
729
750
}
730
751
731
- public String getHeader (String name ) {
732
- HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
733
- return (header != null ? header .getStringValue () : null );
734
- }
735
-
736
- public Enumeration <String > getHeaders (String name ) {
737
- HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
738
- return Collections .enumeration (header != null ? header .getStringValues () : new LinkedList <String >());
739
- }
740
-
741
- public Enumeration <String > getHeaderNames () {
742
- return Collections .enumeration (this .headers .keySet ());
743
- }
744
-
745
752
public void setMethod (String method ) {
746
753
this .method = method ;
747
754
}
@@ -791,8 +798,8 @@ public void addUserRole(String role) {
791
798
}
792
799
793
800
public boolean isUserInRole (String role ) {
794
- return (this .userRoles .contains (role ) || (this .servletContext instanceof MockServletContext &&
795
- (( MockServletContext ) this . servletContext ). getDeclaredRoles (). contains ( role )));
801
+ return (this .userRoles .contains (role ) || (this .servletContext instanceof MockServletContext && (( MockServletContext ) this . servletContext ). getDeclaredRoles (). contains (
802
+ role )));
796
803
}
797
804
798
805
public void setUserPrincipal (Principal userPrincipal ) {
0 commit comments