[Httpunit-commit] SF.net SVN: httpunit:[1043] trunk/httpunit
Brought to you by:
russgold
|
From: <wol...@us...> - 2009-08-20 08:44:07
|
Revision: 1043
http://httpunit.svn.sourceforge.net/httpunit/?rev=1043&view=rev
Author: wolfgang_fahl
Date: 2009-08-20 08:43:55 +0000 (Thu, 20 Aug 2009)
Log Message:
-----------
getBytes added to WebResponse according to a CR by Oliver Wahlen
getDownload helper function is superfluous now
Modified Paths:
--------------
trunk/httpunit/src/com/meterware/httpunit/WebResponse.java
trunk/httpunit/src/com/meterware/pseudoserver/HttpUserAgentTest.java
trunk/httpunit/test/com/meterware/httpunit/MessageBodyRequestTest.java
Modified: trunk/httpunit/src/com/meterware/httpunit/WebResponse.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/WebResponse.java 2009-08-20 07:07:28 UTC (rev 1042)
+++ trunk/httpunit/src/com/meterware/httpunit/WebResponse.java 2009-08-20 08:43:55 UTC (rev 1043)
@@ -276,10 +276,21 @@
abstract
public String getHeaderField( String fieldName );
-
/**
+ * Returns the actual byte stream of the response e.g. for download results
+ * @return the byte array read for this response
+ * @throws IOException
+ */
+ public byte[] getBytes() throws IOException {
+ if (_responseText == null)
+ loadResponseText();
+ return _bytes;
+ }
+
+ /**
* Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString'
* which may be used to represent internal state of this object.
+ * @return the response text
**/
public String getText() throws IOException {
if (_responseText == null)
@@ -1055,7 +1066,15 @@
private int _refreshDelay = -1; // initialized to invalid value
+ /**
+ * the response as a String
+ */
private String _responseText;
+
+ /**
+ * the response as a byte array
+ */
+ private byte[] _bytes;
private InputStream _inputStream;
@@ -1082,15 +1101,15 @@
try {
final int contentLength = this.encodedUsingGZIP() ? -1 : getContentLength();
int bytesRemaining = contentLength < 0 ? Integer.MAX_VALUE : contentLength;
- byte[] bytes = readFromStream( inputStream, bytesRemaining );
+ _bytes = readFromStream( inputStream, bytesRemaining );
- readTags( bytes );
- _responseText = new String( bytes, getCharacterSet() );
- _inputStream = new ByteArrayInputStream( bytes );
+ readTags( _bytes );
+ _responseText = new String( _bytes, getCharacterSet() );
+ _inputStream = new ByteArrayInputStream( _bytes );
- if (HttpUnitOptions.isCheckContentLength() && contentLength >= 0 && bytes.length != contentLength) {
+ if (HttpUnitOptions.isCheckContentLength() && contentLength >= 0 && _bytes.length != contentLength) {
throw new IOException("Truncated message. Expected length: " + contentLength +
- ", Actual length: " + bytes.length);
+ ", Actual length: " + _bytes.length);
}
} finally {
inputStream.close();
Modified: trunk/httpunit/src/com/meterware/pseudoserver/HttpUserAgentTest.java
===================================================================
--- trunk/httpunit/src/com/meterware/pseudoserver/HttpUserAgentTest.java 2009-08-20 07:07:28 UTC (rev 1042)
+++ trunk/httpunit/src/com/meterware/pseudoserver/HttpUserAgentTest.java 2009-08-20 08:43:55 UTC (rev 1043)
@@ -255,9 +255,7 @@
protected void assertEquals( String comment, byte[] expected, byte[] actual ) {
- if (!equals( expected, actual )) {
- fail( comment + " expected:\n" + toString( expected ) + ", but was:\n" + toString( actual ) );
- }
+ assertEquals(comment,toString(expected),toString(actual));
}
Modified: trunk/httpunit/test/com/meterware/httpunit/MessageBodyRequestTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/httpunit/MessageBodyRequestTest.java 2009-08-20 07:07:28 UTC (rev 1042)
+++ trunk/httpunit/test/com/meterware/httpunit/MessageBodyRequestTest.java 2009-08-20 08:43:55 UTC (rev 1043)
@@ -1,24 +1,25 @@
package com.meterware.httpunit;
+
/********************************************************************************************************************
-* $Id$
-*
-* Copyright (c) 2000-2001, 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-2001, 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.pseudoserver.PseudoServlet;
import com.meterware.pseudoserver.WebResource;
@@ -30,109 +31,142 @@
import junit.framework.Test;
import junit.framework.TestSuite;
-
/**
* A unit test to verify miscellaneous requests with message bodies.
**/
public class MessageBodyRequestTest extends HttpUnitTest {
- public static void main(String args[]) {
- junit.textui.TestRunner.run( suite() );
- }
-
-
- public static Test suite() {
- return new TestSuite( MessageBodyRequestTest.class );
- }
+ public static void main(String args[]) {
+ junit.textui.TestRunner.run(suite());
+ }
+ public static Test suite() {
+ return new TestSuite(MessageBodyRequestTest.class);
+ }
- public MessageBodyRequestTest( String name ) {
- super( name );
- }
+ public MessageBodyRequestTest(String name) {
+ super(name);
+ }
+ public void setUp() throws Exception {
+ super.setUp();
+ }
- public void setUp() throws Exception {
- super.setUp();
- }
-
-
- public void testGenericPostRequest() throws Exception {
- defineResource( "ReportData", new BodyEcho() );
- String sourceData = "This is an interesting test\nWith two lines";
- InputStream source = new ByteArrayInputStream( sourceData.getBytes( "iso-8859-1" ) );
+ public void testGenericPostRequest() throws Exception {
+ defineResource("ReportData", new BodyEcho());
+ String sourceData = "This is an interesting test\nWith two lines";
+ InputStream source = new ByteArrayInputStream(sourceData
+ .getBytes("iso-8859-1"));
- WebConversation wc = new WebConversation();
- WebRequest wr = new PostMethodWebRequest( getHostPath() + "/ReportData", source, "text/sample" );
- WebResponse response = wc.getResponse( wr );
- assertEquals( "Body response", "\nPOST\n" + sourceData, response.getText() );
- assertEquals( "Content-type", "text/sample", response.getContentType() );
- }
+ WebConversation wc = new WebConversation();
+ WebRequest wr = new PostMethodWebRequest(getHostPath() + "/ReportData",
+ source, "text/sample");
+ WebResponse response = wc.getResponse(wr);
+ assertEquals("Body response", "\nPOST\n" + sourceData, response
+ .getText());
+ assertEquals("Content-type", "text/sample", response.getContentType());
+ }
+ public void testPutRequest() throws Exception {
+ defineResource("ReportData", new BodyEcho());
+ String sourceData = "This is an interesting test\nWith two lines";
+ InputStream source = new ByteArrayInputStream(sourceData
+ .getBytes("iso-8859-1"));
- public void testPutRequest() throws Exception {
- defineResource( "ReportData", new BodyEcho() );
- String sourceData = "This is an interesting test\nWith two lines";
- InputStream source = new ByteArrayInputStream( sourceData.getBytes( "iso-8859-1" ) );
+ WebConversation wc = new WebConversation();
+ WebRequest wr = new PutMethodWebRequest(getHostPath() + "/ReportData",
+ source, "text/plain");
+ WebResponse response = wc.getResponse(wr);
+ assertEquals("Body response", "\nPUT\n" + sourceData, response
+ .getText());
+ }
- WebConversation wc = new WebConversation();
- WebRequest wr = new PutMethodWebRequest( getHostPath() + "/ReportData", source, "text/plain" );
- WebResponse response = wc.getResponse( wr );
- assertEquals( "Body response", "\nPUT\n" + sourceData, response.getText() );
- }
+ /**
+ * test for download problem described by Oliver Wahlen
+ */
+ public void testDownloadRequestUsingGetText() throws Exception {
+ defineResource( "ReportData", new BodyEcho() );
+ byte[] binaryData = new byte[256];
+ for(int i=0;i<=255;i++) {
+ binaryData[i] = (byte)i;
+ }
+ InputStream source = new ByteArrayInputStream( binaryData );
- public void testDownloadRequest() throws Exception {
- defineResource( "ReportData", new BodyEcho() );
- byte[] binaryData = new byte[] { 0x01, 0x05, 0x0d, 0x0a, 0x02 };
+ WebConversation wc = new WebConversation();
+ WebRequest wr = new PutMethodWebRequest( getHostPath() + "/ReportData", source, "application/random" );
+ WebResponse response = wc.getResponse( wr );
+ // currently the following line does not work:
+ byte[] download = response.getText().getBytes();
+ // currently the following line works:
+ // byte[] download = getDownload( response );
+ // and this one is now available ...
+ download=response.getBytes();
+ assertEquals("Body response", binaryData, download);
+ }
+
+ public void testDownloadRequest() throws Exception {
+ defineResource("ReportData", new BodyEcho());
+ byte[] binaryData = new byte[] { 0x01, 0x05, 0x0d, 0x0a, 0x02 };
- InputStream source = new ByteArrayInputStream( binaryData );
+ InputStream source = new ByteArrayInputStream(binaryData);
- WebConversation wc = new WebConversation();
- WebRequest wr = new PutMethodWebRequest( getHostPath() + "/ReportData", source, "application/random" );
- WebResponse response = wc.getResponse( wr );
+ WebConversation wc = new WebConversation();
+ WebRequest wr = new PutMethodWebRequest(getHostPath() + "/ReportData",
+ source, "application/random");
+ WebResponse response = wc.getResponse(wr);
+ byte[] download = response.getBytes();
+ assertEquals("Body response", binaryData, download);
- byte[] download = getDownload( response );
- assertEquals( "Body response", binaryData, download );
- }
-
- /**
- * test for BR [ 1964665 ] HeaderOnlyRequest cannot be constructed
- */
- public void testHeaderOnlyWebRequest() throws Exception {
- HeaderOnlyWebRequest r = new HeaderOnlyWebRequest("http://www.google.com");
- }
+ download = getDownload(response);
+ assertEquals("Body response", binaryData, download);
+
+ download = response.getBytes();
+ assertEquals("Body response", binaryData, download);
+ }
+ /**
+ * test for BR [ 1964665 ] HeaderOnlyRequest cannot be constructed
+ */
+ public void testHeaderOnlyWebRequest() throws Exception {
+ HeaderOnlyWebRequest r = new HeaderOnlyWebRequest(
+ "http://www.google.com");
+ }
- private byte[] getDownload( WebResponse response ) throws IOException {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- InputStream inputStream = response.getInputStream();
+ /**
+ * please do not copy this function any more - use getBytes instead ...
+ * @param response
+ * @return
+ * @throws IOException
+ */
+ 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);
+ 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();
- }
+ inputStream.close();
+ return outputStream.toByteArray();
+ }
-
}
-
class BodyEcho extends PseudoServlet {
- /**
- * 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 );
- }
- }
+ /**
+ * 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);
+ }
+ }
}
-
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|