[Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit HttpRequestStream.java,NONE,1.1 UnknownM
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-06-11 21:21:12
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv18011/test/com/meterware/httpunit
Modified Files:
FileUploadTest.java HttpUnitTest.java
MessageBodyRequestTest.java PseudoServer.java
PseudoServerTest.java PseudoServlet.java WebFormTest.java
WebLinkTest.java WebPageTest.java WebResource.java
Added Files:
HttpRequestStream.java UnknownMethodException.java
Log Message:
Added support for binary downloads
***** Error reading new file[Errno 2] No such file or directory: 'HttpRequestStream.java'
***** Error reading new file[Errno 2] No such file or directory: 'UnknownMethodException.java'
Index: FileUploadTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/FileUploadTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- FileUploadTest.java 2001/05/16 21:05:30 1.5
+++ FileUploadTest.java 2001/06/11 21:21:09 1.6
@@ -209,10 +209,10 @@
}
-class StringDataSource implements DataSource {
- StringDataSource( String contentType, String contents ) {
+class ByteArrayDataSource implements DataSource {
+ ByteArrayDataSource( String contentType, byte[] body ) {
_contentType = contentType;
- _inputStream = new ByteArrayInputStream( contents.getBytes() );
+ _inputStream = new ByteArrayInputStream( body );
}
@@ -243,12 +243,11 @@
class MimeEcho extends PseudoServlet {
- public WebResource getPostResponse( Dictionary parameters, Dictionary headers ) {
+ public WebResource getPostResponse() {
StringBuffer sb = new StringBuffer();
try {
- String contentType = (String) headers.get( "CONTENT-TYPE" );
- String contents = (String) headers.get( PseudoServlet.CONTENTS );
- DataSource ds = new StringDataSource( contentType, contents );
+ String contentType = getHeader( "Content-Type" );
+ DataSource ds = new ByteArrayDataSource( contentType, getBody() );
MimeMultipart mm = new MimeMultipart( ds );
int numParts = mm.getCount();
for (int i = 0; i < numParts; i++) {
Index: HttpUnitTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/HttpUnitTest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- HttpUnitTest.java 2001/04/03 16:49:08 1.11
+++ HttpUnitTest.java 2001/06/11 21:21:09 1.12
@@ -136,6 +136,41 @@
}
+
+
+ protected void assertEquals( String comment, byte[] expected, byte[] actual )
+ {
+ if (!equals( expected, actual ))
+ {
+ fail( comment + " expected:\n" + toString( expected ) + ", but was:\n" + toString( actual ) );
+ }
+ }
+
+
+ private boolean equals( byte[] first, byte[] second )
+ {
+ if (first.length != second.length) return false;
+ for (int i = 0; i < first.length; i++)
+ {
+ if (first[i] != second[i]) return false;
+ }
+ return true;
+ }
+
+
+ private String toString( byte[] message )
+ {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < message.length; i++)
+ {
+ if (i != 0 && (i % 4) == 0) sb.append( ' ' );
+ if (message[i] >= 0 && message[i] < 16) sb.append( '0' );
+ sb.append( Integer.toHexString( 0xff & (int) message[i] ) );
+ }
+ return sb.toString();
+ }
+
+
//---------------------------------------- private members -----------------------------------------
private String _hostPath;
Index: MessageBodyRequestTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/MessageBodyRequestTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MessageBodyRequestTest.java 2001/06/06 13:25:33 1.1
+++ MessageBodyRequestTest.java 2001/06/11 21:21:09 1.2
@@ -89,12 +89,51 @@
}
+ public void testDownloadRequest() throws Exception {
+ defineResource( "ReportData", new BodyEcho() );
+ byte[] binaryData = new byte[] { 0x01, 0x05, 0x0d, 0x0a, 0x02 };
+
+ InputStream source = new ByteArrayInputStream( binaryData );
+
+ WebConversation wc = new WebConversation();
+ WebRequest wr = new PutMethodWebRequest( getHostPath() + "/ReportData", source, "application/random" );
+ WebResponse response = wc.getResponse( wr );
+
+ byte[] download = getDownload( response );
+ assertEquals( "Body response", binaryData, download );
+ }
+
+
+ private byte[] getDownload( WebResponse response ) throws IOException {
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ InputStream inputStream = response.getInputStream();
+
+ byte[] buffer = new byte[8 * 1024];
+ int count = 0;
+ do {
+ outputStream.write( buffer, 0, count );
+ count = inputStream.read( buffer, 0, buffer.length );
+ } while (count != -1);
+
+ inputStream.close();
+ return outputStream.toByteArray();
+ }
+
+
}
class BodyEcho extends PseudoServlet {
- public WebResource getResponse( String method, Dictionary parameters, Dictionary headers ) {
- return new WebResource( "\n" + method + "\n" + (String) headers.get( PseudoServlet.CONTENTS ), (String) headers.get( "CONTENT-TYPE" ) );
+ /**
+ * Returns a resource object as a result of a get request.
+ **/
+ public WebResource getResponse( String method ) {
+ String contentType = getHeader( "Content-type" );
+ if (contentType.startsWith( "text" )) {
+ return new WebResource( "\n" + method + "\n" + new String( getBody() ), contentType );
+ } else {
+ return new WebResource( getBody(), contentType );
+ }
}
}
Index: PseudoServer.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServer.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- PseudoServer.java 2001/06/04 20:45:33 1.13
+++ PseudoServer.java 2001/06/11 21:21:09 1.14
@@ -97,6 +97,14 @@
/**
+ * Defines the contents of an expected resource.
+ **/
+ public void setResource( String name, byte[] value, String contentType ) {
+ _resources.put( asResourceName( name ), new WebResource( value, contentType ) );
+ }
+
+
+ /**
* Defines a resource which will result in an error message.
**/
public void setErrorResource( String name, int errorCode, String errorMessage ) {
@@ -146,11 +154,6 @@
private boolean _active = true;
- /** The encoding used for HTTP headers. **/
- final private static String HEADER_ENCODING = "us-ascii";
-
- final private static String CRLF = "\r\n";
-
private String asResourceName( String rawName ) {
if (rawName.startsWith( "/" )) {
@@ -163,207 +166,162 @@
private void handleConnection() throws IOException {
Socket socket = getServerSocket().accept();
-
- BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream(), HEADER_ENCODING ) );
- PrintWriter pw = new PrintWriter( new OutputStreamWriter( socket.getOutputStream(), HEADER_ENCODING ) );
-
socket.setSoTimeout( 1000 );
socket.setTcpNoDelay( true );
- String commandLine = br.readLine(); // get the first line only
- StringTokenizer st = new StringTokenizer( commandLine );
- String command = st.nextToken();
- String uri = st.nextToken();
- String protocol = st.nextToken();
+ HttpRequestStream request = new HttpRequestStream( socket.getInputStream() );
+ HttpResponseStream response = new HttpResponseStream( socket.getOutputStream() );
- if (!command.equals( "GET" ) && !command.equals( "POST" ) && !command.equals( "PUT" )) {
- sendResponse( pw, HttpURLConnection.HTTP_BAD_METHOD, "unsupported method: " + command );
- } else {
- try {
- WebResource resource = getResource( command, uri, br );
- if (resource == null) {
- sendResponse( pw, HttpURLConnection.HTTP_NOT_FOUND, "unable to find " + uri );
- } else if (resource.getResponseCode() != HttpURLConnection.HTTP_OK) {
- sendResponse( pw, resource.getResponseCode(), resource.getContents() );
- sendLine( pw, "" );
- pw.flush();
- } else {
- sendResponse( pw, HttpURLConnection.HTTP_OK, "OK" );
- sendLine( pw, "Content-type: " + resource.getContentType() + resource.getCharacterSetParameter() );
- String[] headers = resource.getHeaders();
- for (int i = 0; i < headers.length; i++) {
- sendLine( pw, headers[i] );
- }
- sendLine( pw, "" );
- pw.flush();
- pw = new PrintWriter( new OutputStreamWriter( socket.getOutputStream(), resource.getCharacterSet() ) );
- sendText( pw, resource.getContents() );
+ try {
+ WebResource resource = getResource( request );
+ if (resource == null) {
+ response.setResponse( HttpURLConnection.HTTP_NOT_FOUND, "unable to find " + request.getURI() );
+ } else if (resource.getResponseCode() != HttpURLConnection.HTTP_OK) {
+ response.setResponse( resource.getResponseCode(), resource.getResponseText() );
+ } else {
+ response.addHeader( "Content-type: " + resource.getContentType() + resource.getCharacterSetParameter() );
+ String[] headers = resource.getHeaders();
+ for (int i = 0; i < headers.length; i++) {
+ response.addHeader( headers[i] );
}
- } catch (IOException e) {
- e.fillInStackTrace();
- pw.close();
- socket.close();
- throw e;
- } catch (Throwable t) {
- System.out.println( "Internal error: " + t );
- t.printStackTrace();
- sendResponse( pw, HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() );
+ response.write( resource.getContents() );
}
+ } catch (UnknownMethodException e) {
+ response.setResponse( HttpURLConnection.HTTP_BAD_METHOD, "unsupported method: " + e.getMethod() );
+ } catch (IOException e) {
+ e.fillInStackTrace();
+ throw e;
+ } catch (Throwable t) {
+ t.printStackTrace();
+ response.setResponse( HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() );
+ } finally {
+ response.close();
+ socket.close();
}
-
- pw.close();
- socket.close();
}
+
+ private WebResource getResource( HttpRequestStream request ) throws IOException {
+ Object resource = _resources.get( request.getURI() );
- private WebResource getResource( String command, String uri, BufferedReader br ) throws IOException {
- Object resource = _resources.get( uri );
- if (command.equals( "GET" ) && resource instanceof WebResource) {
+ if (request.getCommand().equals( "GET" ) && resource instanceof WebResource) {
return (WebResource) resource;
} else if (resource instanceof PseudoServlet) {
- Dictionary requestData = readRequest( br );
- return ((PseudoServlet) resource).getResponse( command, getParameters( (String) requestData.get( PseudoServlet.CONTENTS ) ), requestData );
+ return getResource( (PseudoServlet) resource, request );
} else {
return null;
}
}
- private Dictionary readRequest( BufferedReader br ) throws IOException {
- Hashtable headers = new Hashtable();
- String lastHeader = null;
-
- String header = br.readLine();
- while (header.length() > 0) {
- if (header.charAt(0) <= ' ') {
- if (lastHeader == null) continue;
- headers.put( lastHeader, headers.get( lastHeader ) + header.trim() );
- } else {
- lastHeader = header.substring( 0, header.indexOf(':') ).toUpperCase();
- headers.put( lastHeader, header.substring( header.indexOf(':')+1 ).trim() );
- }
- header = br.readLine();
- }
- readContent( headers, br );
- return headers;
+ private WebResource getResource( PseudoServlet servlet, HttpRequestStream request ) {
+ servlet.init( request );
+ return servlet.getResponse( request.getCommand() );
}
- private void readContent( Hashtable headers, BufferedReader br ) throws IOException {
- if (headers.get( "CONTENT-LENGTH" ) == null) return;
- try {
- int contentLength = Integer.parseInt( (String) headers.get( "CONTENT-LENGTH" ) );
- char[] content = new char[ contentLength ];
- br.read( content );
- headers.put( PseudoServlet.CONTENTS, new String( content ) );
- } catch (NumberFormatException e) {
+ private ServerSocket getServerSocket() throws IOException {
+ synchronized (this) {
+ if (_serverSocket == null) _serverSocket = new ServerSocket(0);
+ _serverSocket.setSoTimeout( 1000 );
}
+ return _serverSocket;
}
- private void printContent( char[] content ) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < content.length; i++) {
- if (i == 0) {
- } else if ((i % 40) == 0) {
- sb.append( '\n' );
- } else if (i % 4 == 0) {
- sb.append( ' ' );
- }
- sb.append( Integer.toHexString( content[i] ) );
- }
- System.out.println( sb.toString() );
- }
-
-
- private Dictionary getParameters( String content ) throws IOException {
- Hashtable parameters = new Hashtable();
- if (content == null || content.trim().length() == 0) return parameters;
-
- StringTokenizer st = new StringTokenizer( content, "&=" );
- while (st.hasMoreTokens()) {
- String name = st.nextToken();
- if (st.hasMoreTokens()) {
- addParameter( parameters, decode( name ), decode( st.nextToken() ) );
- }
- }
- return parameters;
+ private ServerSocket _serverSocket;
+
+}
+
+
+
+
+class HttpResponseStream {
+
+ final private static String CRLF = "\r\n";
+
+
+ void close() throws IOException {
+ flushHeaders();
+ _pw.close();
}
- private void addParameter( Hashtable parameters, String name, String value ) {
- String[] oldValues = (String[]) parameters.get( name );
- if (oldValues == null) {
- parameters.put( name, new String[] { value } );
- } else {
- String[] values = new String[ oldValues.length+1 ];
- System.arraycopy( oldValues, 0, values, 0, oldValues.length );
- values[ oldValues.length ] = value;
- parameters.put( name, values );
+ HttpResponseStream( OutputStream stream ) {
+ _stream = stream;
+ try {
+ setCharacterSet( "us-ascii" );
+ } catch (UnsupportedEncodingException e) {
+ _pw = new PrintWriter( new OutputStreamWriter( _stream ) );
}
}
- private String decode( String byteString ) {
- StringBuffer sb = new StringBuffer();
- char[] chars = byteString.toCharArray();
- char[] hexNum = { '0', '0', '0' };
-
- int i = 0;
- while (i < chars.length) {
- if (chars[i] == '+') {
- i++;
- sb.append( ' ' );
- } else if (chars[i] == '%') {
- i++;
- hexNum[1] = chars[i++];
- hexNum[2] = chars[i++];
- sb.append( (char) Integer.parseInt( new String( hexNum ), 16 ) );
- } else {
- sb.append( chars[i++] );
- }
- }
- return sb.toString();
+ void setResponse( int responseCode, String responseText ) {
+ _responseCode = responseCode;
+ _responseText = responseText;
}
- private void sendResponse( PrintWriter pw, int responseCode, String responseText ) throws IOException {
- sendLine( pw, "HTTP/1.0 " + responseCode + ' ' + responseText );
+ void addHeader( String header ) {
+ _headers.addElement( header );
}
- private void sendLine( PrintWriter pw, String text ) throws IOException {
- sendText( pw, text );
- sendText( pw, CRLF );
+ void write( String contents, String charset ) throws IOException {
+ flushHeaders();
+ setCharacterSet( charset );
+ sendText( contents );
}
- private void sendText( PrintWriter pw, String text ) throws IOException {
- pw.write( text );
+ void write( byte[] contents ) throws IOException {
+ flushHeaders();
+ _stream.write( contents, 0, contents.length );
}
+ private void setCharacterSet( String characterSet ) throws UnsupportedEncodingException {
+ if (_pw != null) _pw.flush();
+ _pw = new PrintWriter( new OutputStreamWriter( _stream, characterSet ) );
+ }
+
- private String toUnicode( String string ) {
- StringBuffer sb = new StringBuffer( );
- char[] chars = string.toCharArray();
- for (int i = 0; i < chars.length; i++) {
- sb.append( "\\u" );
- sb.append( Integer.toHexString( chars[i] ) );
+ private void flushHeaders() throws IOException {
+ if (!_headersWritten) {
+ sendResponse( _responseCode, _responseText );
+ for (Enumeration e = _headers.elements(); e.hasMoreElements();) {
+ sendLine( (String) e.nextElement() );
+ }
+ sendText( CRLF );
+ _headersWritten = true;
+ _pw.flush();
}
- return sb.toString();
}
-
- private ServerSocket getServerSocket() throws IOException {
- synchronized (this) {
- if (_serverSocket == null) _serverSocket = new ServerSocket(0);
- _serverSocket.setSoTimeout( 1000 );
- }
- return _serverSocket;
+
+ private void sendResponse( int responseCode, String responseText ) throws IOException {
+ sendLine( "HTTP/1.0 " + responseCode + ' ' + responseText );
}
- private ServerSocket _serverSocket;
+ private void sendLine( String text ) throws IOException {
+ sendText( text );
+ sendText( CRLF );
+ }
+
+
+ private void sendText( String text ) throws IOException {
+ _pw.write( text );
+ }
+
+
+ private OutputStream _stream;
+ private PrintWriter _pw;
+
+ private Vector _headers = new Vector();
+ private int _responseCode = HttpURLConnection.HTTP_OK;
+ private String _responseText = "OK";
+ private boolean _headersWritten;
}
Index: PseudoServerTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServerTest.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- PseudoServerTest.java 2001/06/04 20:45:33 1.12
+++ PseudoServerTest.java 2001/06/11 21:21:09 1.13
@@ -212,8 +212,8 @@
try {
ps.setResource( resourceName, new PseudoServlet() {
- public WebResource getPostResponse( Dictionary parameters, Dictionary headers ) {
- return new WebResource( prefix + ((String[]) parameters.get( "name" ))[0], "text/plain" );
+ public WebResource getPostResponse() {
+ return new WebResource( prefix + getParameter( "name" )[0], "text/plain" );
}
} );
@@ -246,8 +246,8 @@
try {
ps.setResource( resourceName, new PseudoServlet() {
- public WebResource getGetResponse( Dictionary parameters, Dictionary headers ) {
- String referer = (String) headers.get( "REFERER" );
+ public WebResource getGetResponse() {
+ String referer = getHeader( "Referer" );
return new WebResource( referer == null ? "null" : referer, "text/plain" );
}
} );
Index: PseudoServlet.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServlet.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PseudoServlet.java 2001/06/04 20:45:33 1.4
+++ PseudoServlet.java 2001/06/11 21:21:09 1.5
@@ -21,6 +21,8 @@
*******************************************************************************************************************/
import java.util.Dictionary;
+import java.io.Reader;
+
/**
* A basic simulated servlet for testing the HttpUnit library.
**/
@@ -33,17 +35,14 @@
/**
* Returns a resource object as a result of a get request.
- * @param parameters a mapping of parameter names to arrays of value string.
- * @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
- * which is the raw bytes of the request contents stored in a string.
**/
- public WebResource getResponse( String methodType, Dictionary parameters, Dictionary headers ) {
+ public WebResource getResponse( String methodType ) {
if (methodType.equalsIgnoreCase( "GET" )) {
- return getGetResponse( parameters, headers );
+ return getGetResponse();
} else if (methodType.equalsIgnoreCase( "PUT" )) {
- return getPutResponse( parameters, headers );
+ return getPutResponse();
} else if (methodType.equalsIgnoreCase( "POST" )) {
- return getPostResponse( parameters, headers );
+ return getPostResponse();
} else {
throw new RuntimeException( methodType + " not implemented" );
}
@@ -52,35 +51,64 @@
/**
* Returns a resource object as a result of a get request.
- * @param parameters a mapping of parameter names to arrays of value string.
- * @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
- * which is the raw bytes of the request contents stored in a string.
**/
- public WebResource getGetResponse( Dictionary parameters, Dictionary headers ) {
+ public WebResource getGetResponse() {
throw new RuntimeException( "get not implemented" );
}
/*
* Returns a resource object as a result of a post request.
- * @param parameters a mapping of parameter names to arrays of value string.
- * @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
- * which is the raw bytes of the request contents stored in a string.
**/
- public WebResource getPostResponse( Dictionary parameters, Dictionary headers ) {
+ public WebResource getPostResponse() {
throw new RuntimeException( "post not implemented" );
}
/*
* Returns a resource object as a result of a put request.
- * @param parameters a mapping of parameter names to arrays of value string.
- * @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
- * which is the raw bytes of the request contents stored in a string.
**/
- public WebResource getPutResponse( Dictionary parameters, Dictionary headers ) {
+ public WebResource getPutResponse() {
throw new RuntimeException( "put not implemented" );
}
+
+
+ void init( HttpRequestStream requestStream ) {
+ _requestStream = requestStream;
+ }
+
+
+ /**
+ * Returns the header with the specified name. If no such header exists, will return null.
+ **/
+ protected String getHeader( String name ) {
+ return _requestStream.getHeader( name );
+ }
+
+
+ /**
+ * Returns the values for the parameter with the specified name. If no values exist
+ * will return null.
+ **/
+ protected String[] getParameter( String name ) {
+ return _requestStream.getParameter( name );
+ }
+
+
+ /**
+ * Returns a reader for the body of the request.
+ **/
+ protected Reader getReader() {
+ return _requestStream.getReader();
+ }
+
+
+ protected byte[] getBody() {
+ return _requestStream.getBody();
+ }
+
+
+ private HttpRequestStream _requestStream;
}
Index: WebFormTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/WebFormTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- WebFormTest.java 2001/04/18 03:18:35 1.10
+++ WebFormTest.java 2001/06/11 21:21:09 1.11
@@ -147,7 +147,8 @@
}
- public void testNullTextValues() throws Exception {
+ // XXX turn this back on when Tidy handles it properly
+ public void notestNullTextValues() throws Exception {
defineWebPage( "Default", "<form method=POST action = \"/servlet/Login\">" +
"<Input name=\"secret\" type=\"hidden\" value=>" +
"<br><Input name=typeless value=>" +
Index: WebLinkTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/WebLinkTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- WebLinkTest.java 2000/10/06 14:52:53 1.7
+++ WebLinkTest.java 2001/06/11 21:21:09 1.8
@@ -156,7 +156,7 @@
WebRequest request = link.getRequest();
assertEquals( "Destination for link", getHostPath() + "/alternate/Target.html", request.getURL().toExternalForm() );
WebResponse nextPage = wc.getResponse( request );
- assert( "Did not find the target", nextPage.toString().indexOf( "Found" ) >= 0 );
+ assert( "Did not find the target", nextPage.getText().indexOf( "Found" ) >= 0 );
}
Index: WebPageTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/WebPageTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- WebPageTest.java 2001/05/22 22:18:55 1.10
+++ WebPageTest.java 2001/06/11 21:21:09 1.11
@@ -181,9 +181,9 @@
"<input type=text name=name><input type=submit></form></body></html>" );
setResourceCharSet( "HebrewForm.html", "iso-8859-8", true );
defineResource( "SayHello", new PseudoServlet() {
- public WebResource getPostResponse( Dictionary parameters, Dictionary headers ) {
+ public WebResource getPostResponse() {
try {
- String name = ((String[]) parameters.get( "name" ))[0];
+ String name = getParameter( "name" )[0];
WebResource result = new WebResource( "<html><body><table><tr><td>Hello, " +
new String( name.getBytes( "iso-8859-1" ), "iso-8859-8" ) +
"</td></tr></table></body></html>" );
Index: WebResource.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/WebResource.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WebResource.java 2001/03/20 23:35:43 1.3
+++ WebResource.java 2001/06/11 21:21:09 1.4
@@ -34,7 +34,7 @@
final static String DEFAULT_CONTENT_TYPE = "text/html";
- final static String DEFAULT_CHARACTER_SET = "us-ascii";
+ final static String DEFAULT_CHARACTER_SET = "iso-8859-1";
WebResource( String contents ) {
this( contents, DEFAULT_CONTENT_TYPE );
@@ -47,11 +47,24 @@
WebResource( int responseCode, String responseText ) {
- this( responseText, DEFAULT_CONTENT_TYPE, responseCode );
+ this( "", DEFAULT_CONTENT_TYPE, responseCode );
+ _responseText = responseText;
}
WebResource( String contents, String contentType, int responseCode ) {
+ _string = contents;
+ _contentType = contentType;
+ _responseCode = responseCode;
+ }
+
+
+ WebResource( byte[] contents, String contentType ) {
+ this( contents, contentType, HttpURLConnection.HTTP_OK );
+ }
+
+
+ WebResource( byte[] contents, String contentType, int responseCode ) {
_contents = contents;
_contentType = contentType;
_responseCode = responseCode;
@@ -80,8 +93,12 @@
}
- String getContents() {
- return _contents;
+ byte[] getContents() throws UnsupportedEncodingException {
+ if (_string != null) {
+ return _string.getBytes( getCharacterSet() );
+ } else {
+ return _contents;
+ }
}
@@ -108,16 +125,31 @@
return _responseCode;
}
+ String getResponseText() {
+ return _responseText;
+ }
+
public String toString() {
return "WebResource [code=" + _responseCode + "; type = " + _contentType
- + "; charset = " + _characterSet + "]\n" + _contents;
+ + "; charset = " + _characterSet + "]\n" + getContentsAsString();
}
+ private String getContentsAsString() {
+ if (_string != null) {
+ return _string;
+ } else {
+ return "<< hex bytes >>";
+ }
+ }
+
+
+ private byte[] _contents;
+ private String _string;
private int _responseCode;
private boolean _sendCharacterSet;
- private String _contents;
+ private String _responseText = "";
private String _contentType;
private String _characterSet = DEFAULT_CHARACTER_SET;
private Vector _headers = new Vector();
|