[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletUnitHttpResponse.java,1.10,1.11
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-22 15:48:47
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit In directory usw-pr-cvs1:/tmp/cvs-serv7190/src/com/meterware/servletunit Modified Files: ServletUnitHttpResponse.java ServletUnitHttpSession.java ServletUnitWebResponse.java Log Message: from Michael Reardon: Added support for ServletUnit headers and attribute names Index: ServletUnitHttpResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpResponse.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- ServletUnitHttpResponse.java 30 Jan 2002 21:21:11 -0000 1.10 +++ ServletUnitHttpResponse.java 22 Aug 2002 15:48:41 -0000 1.11 @@ -32,6 +32,10 @@ import java.util.Hashtable; import java.util.Locale; import java.util.Vector; +import java.util.ArrayList; +import java.util.TimeZone; +import java.util.Date; +import java.text.SimpleDateFormat; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; @@ -40,6 +44,9 @@ class ServletUnitHttpResponse implements HttpServletResponse { + // rfc1123-date is "Sun, 06 Nov 1994 08:49:37 GMT" + private static final String RFC1123_DATE_SPEC = "EEE, dd MMM yyyy hh:mm:ss z"; + /** * @deprecated Use encodeURL(String url) @@ -180,7 +187,11 @@ * value. **/ public void setHeader( String name, String value ) { - _headers.put( name.toUpperCase(), value ); + ArrayList values = new ArrayList(); + values.add( value ); + synchronized (_headers) { + _headers.put( name.toUpperCase(), values ); + } } @@ -192,12 +203,16 @@ * setting its value. **/ public void setIntHeader( String name, int value ) { - throw new RuntimeException( "setIntHeader not implemented" ); + setHeader( name, asHeaderValue( value ) ); + } + + + private String asHeaderValue( int value ) { + return Integer.toString( value ); } /** - * * Adds a field to the response header with the given name and * date-valued field. The date is specified in terms of * milliseconds since the epoch. If the date field had already @@ -206,7 +221,15 @@ * presence of a header before setting its value. **/ public void setDateHeader( String name, long date ) { - throw new RuntimeException( "setDateHeader not implemented" ); + setHeader( name, asDateHeaderValue( date ) ); + } + + + private String asDateHeaderValue( long date ) { + Date value = new Date( date ); + SimpleDateFormat formatter = new SimpleDateFormat( RFC1123_DATE_SPEC ); + formatter.setTimeZone( TimeZone.getTimeZone( "Greenwich Mean Time" ) ); + return formatter.format( value ); } @@ -307,7 +330,15 @@ * Adds a response header with the given name and value. This method allows response headers to have multiple values. **/ public void addHeader( String name, String value ) { - throw new RuntimeException( "addHeader not implemented" ); + synchronized (_headers) { + String key = name.toUpperCase(); + ArrayList values = (ArrayList) _headers.get( key ); + if (values == null) { + values = new ArrayList(); + _headers.put( key, values ); + } + values.add( value ); + } } @@ -315,7 +346,7 @@ * Adds a response header with the given name and value. This method allows response headers to have multiple values. **/ public void addIntHeader( String name, int value ) { - throw new RuntimeException( "addIntHeader not implemented" ); + addHeader( name, asHeaderValue( value ) ); } @@ -323,7 +354,7 @@ * Adds a response header with the given name and value. This method allows response headers to have multiple values. **/ public void addDateHeader( String name, long value ) { - throw new RuntimeException( "addDateHeader not implemented" ); + addHeader( name, asDateHeaderValue( value ) ); } @@ -472,9 +503,35 @@ **/ String getHeaderField( String name ) { if (!_headersComplete) completeHeaders(); - return (String) _headers.get( name.toUpperCase() ); - } + ArrayList values = null; + synchronized (_headers) { + values = (ArrayList) _headers.get( name.toUpperCase() ); + } + + return values == null ? null : (String) values.get( 0 ); + } + + + /** + * Return an array of all the header values associated with the + * specified header name, or an zero-length array if there are no such + * header values. + * + * @param name Header name to look up + */ + public String[] getHeaderFields(String name) { + if (!_headersComplete) completeHeaders(); + ArrayList values = null; + synchronized (_headers) { + values = (ArrayList) _headers.get(name.toUpperCase()); + } + if (values == null) + return (new String[0]); + String results[] = new String[values.size()]; + return ((String[]) values.toArray(results)); + + } //------------------------------------------- private members ------------------------------------ Index: ServletUnitHttpSession.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpSession.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ServletUnitHttpSession.java 26 Nov 2001 14:20:12 -0000 1.5 +++ ServletUnitHttpSession.java 22 Aug 2002 15:48:41 -0000 1.6 @@ -185,7 +185,7 @@ **/ public Enumeration getAttributeNames() { if (_isInvalid) throw new IllegalStateException(); - throw new RuntimeException( "getValueNames not implemented" ); + return _values.keys(); } Index: ServletUnitWebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitWebResponse.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- ServletUnitWebResponse.java 1 Aug 2002 14:58:59 -0000 1.10 +++ ServletUnitWebResponse.java 22 Aug 2002 15:48:41 -0000 1.11 @@ -1,51 +1,38 @@ package com.meterware.servletunit; /******************************************************************************************************************** -* $Id$ -* -* Copyright (c) 2000-2002, Russell Gold -* -* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated -* documentation files (the "Software"), to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and -* to permit persons to whom the Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all copies or substantial portions -* of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO -* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF -* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -* DEALINGS IN THE SOFTWARE. -* -*******************************************************************************************************************/ + * $Id$ + * + * Copyright (c) 2000-2002, Russell Gold + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + *******************************************************************************************************************/ +import com.meterware.httpunit.WebResponse; import java.io.ByteArrayInputStream; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.IOException; - -import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; - -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.StringTokenizer; import javax.servlet.http.HttpServletResponse; -import com.meterware.httpunit.*; - /** * A response from to a request from the simulated servlet environment. **/ class ServletUnitWebResponse extends WebResponse { - /** * Constructs a response object from a servlet response. * @param target the target frame on which the response will be displayed @@ -89,8 +76,7 @@ public String[] getHeaderFields( String fieldName ) { - String field = getHeaderField( fieldName ); - return field == null ? NO_HEADERS : new String[] { _response.getHeaderField( fieldName ) }; + return _response.getHeaderFields( fieldName ); } @@ -103,8 +89,6 @@ private ServletUnitHttpResponse _response; - - private static final String[] NO_HEADERS = new String[0]; } |