1
1
/*
2
- * Copyright 2002-2010 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.
29
29
import java .util .Locale ;
30
30
import java .util .Map ;
31
31
import java .util .Set ;
32
-
33
32
import javax .servlet .ServletOutputStream ;
34
33
import javax .servlet .http .Cookie ;
35
34
import javax .servlet .http .HttpServletResponse ;
40
39
41
40
/**
42
41
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse}
43
- * interface. Supports the Servlet 2.5 API level.
42
+ * interface. Supports the Servlet 3.0 API level
44
43
*
45
44
* <p>Used for testing the web framework; also useful for testing
46
45
* application controllers.
51
50
*/
52
51
public class MockHttpServletResponse implements HttpServletResponse {
53
52
54
- public static final int DEFAULT_SERVER_PORT = 80 ;
55
-
56
53
private static final String CHARSET_PREFIX = "charset=" ;
57
54
58
55
private static final String CONTENT_TYPE_HEADER = "Content-Type" ;
59
-
56
+
60
57
private static final String CONTENT_LENGTH_HEADER = "Content-Length" ;
61
58
59
+ private static final String LOCATION_HEADER = "Location" ;
62
60
63
61
//---------------------------------------------------------------------
64
62
// ServletResponse properties
@@ -101,8 +99,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
101
99
102
100
private String errorMessage ;
103
101
104
- private String redirectedUrl ;
105
-
106
102
private String forwardedUrl ;
107
103
108
104
private final List <String > includedUrls = new ArrayList <String >();
@@ -147,7 +143,7 @@ public void setCharacterEncoding(String characterEncoding) {
147
143
this .charset = true ;
148
144
updateContentTypeHeader ();
149
145
}
150
-
146
+
151
147
private void updateContentTypeHeader () {
152
148
if (this .contentType != null ) {
153
149
StringBuilder sb = new StringBuilder (this .contentType );
@@ -302,31 +298,69 @@ public boolean containsHeader(String name) {
302
298
303
299
/**
304
300
* Return the names of all specified headers as a Set of Strings.
301
+ * <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
305
302
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
306
303
*/
307
304
public Set <String > getHeaderNames () {
308
305
return this .headers .keySet ();
309
306
}
310
307
308
+ /**
309
+ * Return the primary value for the given header as a String, if any.
310
+ * Will return the first value in case of multiple values.
311
+ * <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
312
+ * As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
313
+ * Consider using {@link #getHeaderValue(String)} for raw Object access.
314
+ * @param name the name of the header
315
+ * @return the associated header value, or <code>null<code> if none
316
+ */
317
+ public String getHeader (String name ) {
318
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
319
+ return (header != null ? header .getStringValue () : null );
320
+ }
321
+
322
+ /**
323
+ * Return all values for the given header as a List of Strings.
324
+ * <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
325
+ * As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
326
+ * Consider using {@link #getHeaderValues(String)} for raw Object access.
327
+ * @param name the name of the header
328
+ * @return the associated header values, or an empty List if none
329
+ */
330
+ public List <String > getHeaders (String name ) {
331
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
332
+ if (header != null ) {
333
+ return header .getStringValues ();
334
+ }
335
+ else {
336
+ return Collections .emptyList ();
337
+ }
338
+ }
339
+
311
340
/**
312
341
* Return the primary value for the given header, if any.
313
342
* <p>Will return the first value in case of multiple values.
314
343
* @param name the name of the header
315
344
* @return the associated header value, or <code>null<code> if none
316
345
*/
317
- public String getHeader (String name ) {
346
+ public Object getHeaderValue (String name ) {
318
347
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
319
- return (header != null ? header .getValue (). toString () : null );
348
+ return (header != null ? header .getValue () : null );
320
349
}
321
350
322
351
/**
323
352
* Return all values for the given header as a List of value objects.
324
353
* @param name the name of the header
325
354
* @return the associated header values, or an empty List if none
326
355
*/
327
- public List <String > getHeaders (String name ) {
356
+ public List <Object > getHeaderValues (String name ) {
328
357
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
329
- return (header != null ? header .getStringValues () : Collections .<String >emptyList ());
358
+ if (header != null ) {
359
+ return header .getValues ();
360
+ }
361
+ else {
362
+ return Collections .emptyList ();
363
+ }
330
364
}
331
365
332
366
/**
@@ -342,7 +376,7 @@ public String encodeURL(String url) {
342
376
* returning the given URL String as-is.
343
377
* <p>Can be overridden in subclasses, appending a session id or the like
344
378
* in a redirect-specific fashion. For general URL encoding rules,
345
- * override the common {@link #encodeURL} method instead, appyling
379
+ * override the common {@link #encodeURL} method instead, applying
346
380
* to redirect URLs as well as to general URLs.
347
381
*/
348
382
public String encodeRedirectURL (String url ) {
@@ -379,12 +413,13 @@ public void sendRedirect(String url) throws IOException {
379
413
throw new IllegalStateException ("Cannot send redirect - response is already committed" );
380
414
}
381
415
Assert .notNull (url , "Redirect URL must not be null" );
382
- this .redirectedUrl = url ;
416
+ setHeader (LOCATION_HEADER , url );
417
+ setStatus (HttpServletResponse .SC_MOVED_TEMPORARILY );
383
418
setCommitted (true );
384
419
}
385
420
386
421
public String getRedirectedUrl () {
387
- return this . redirectedUrl ;
422
+ return getHeader ( LOCATION_HEADER ) ;
388
423
}
389
424
390
425
public void setDateHeader (String name , long value ) {
@@ -424,7 +459,7 @@ private void addHeaderValue(String name, Object value) {
424
459
}
425
460
doAddHeaderValue (name , value , false );
426
461
}
427
-
462
+
428
463
private boolean setSpecialHeader (String name , Object value ) {
429
464
if (CONTENT_TYPE_HEADER .equalsIgnoreCase (name )) {
430
465
setContentType ((String ) value );
0 commit comments