Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv6049/src/com/meterware/httpunit
Modified Files:
HttpUnitOptions.java WebResponse.java
Log Message:
Robert Watkins: Added check for message truncated
Index: HttpUnitOptions.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- HttpUnitOptions.java 20 May 2002 16:00:42 -0000 1.19
+++ HttpUnitOptions.java 17 Jun 2002 21:22:05 -0000 1.20
@@ -43,6 +43,7 @@
_matchesIgnoreCase = true;
_autoRefresh = false;
_autoRedirect = true;
+ _checkContentLength = false;
_redirectDelay = 0;
_characterSet = HttpUnitUtils.DEFAULT_CHARACTER_SET;
_contentType = DEFAULT_CONTENT_TYPE;
@@ -102,6 +103,24 @@
/**
+ * Returns true if HttpUnit will throw an exception when a message is only partially received. The default is
+ * to avoid such checks.
+ */
+ public static boolean isCheckContentLength() {
+ return _checkContentLength;
+ }
+
+
+ /**
+ * Specifies whether HttpUnit should throw an exception when the content length of a message does not match its
+ * actual received length. Defaults to false.
+ */
+ public static void setCheckContentLength( boolean checkContentLength ) {
+ _checkContentLength = checkContentLength;
+ }
+
+
+ /**
* Determines whether a normal POST request will include the character set in the content-type header.
* The default is to include it; however, some older servlet engines (most notably Tomcat 3.1) get confused
* when they see it.
@@ -340,6 +359,8 @@
private static boolean _autoRedirect = true;
private static boolean _postIncludesCharset = false;
+
+ private static boolean _checkContentLength = false;
private static int _redirectDelay;
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -r1.65 -r1.66
--- WebResponse.java 16 May 2002 17:44:20 -0000 1.65
+++ WebResponse.java 17 Jun 2002 21:22:05 -0000 1.66
@@ -158,6 +158,19 @@
/**
+ * Returns the content length of this response.
+ * @return the content length, if known, or -1.
+ */
+ public int getContentLength() {
+ if (_contentLength == UNINITIALIZED_INT) {
+ String length = getHeaderField( "Content-Length" );
+ _contentLength = (length == null) ? -1 : Integer.parseInt( length );
+ }
+ return _contentLength;
+ }
+
+
+ /**
* Returns the content type of this response.
**/
public String getContentType() {
@@ -551,12 +564,16 @@
final private static String HTML_CONTENT = "text/html";
+ final private static int UNINITIALIZED_INT = -2;
+
private WebFrame[] _frames;
private ReceivedPage _page;
private String _contentHeader;
+ private int _contentLength = UNINITIALIZED_INT;
+
private String _contentType;
private String _characterSet;
@@ -600,6 +617,11 @@
readMetaTags( bytes );
_responseText = new String( bytes, getCharacterSet() );
_inputStream = new ByteArrayInputStream( bytes );
+
+ if (HttpUnitOptions.isCheckContentLength() && getContentLength() >= 0 && bytes.length != getContentLength()) {
+ throw new IOException("Truncated message. Expected length: " + getContentLength() +
+ ", Actual length: " + bytes.length);
+ }
} finally {
inputStream.close();
}
|