From: <rb...@us...> - 2013-06-19 18:58:16
|
Revision: 8348 http://sourceforge.net/p/htmlunit/code/8348 Author: rbri Date: 2013-06-19 18:58:08 +0000 (Wed, 19 Jun 2013) Log Message: ----------- make the handling of empty responses more error proof Issue 1510 Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/DownloadedContent.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponseData.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebResponseDataTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/DownloadedContent.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/DownloadedContent.java 2013-06-16 08:00:03 UTC (rev 8347) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/DownloadedContent.java 2013-06-19 18:58:08 UTC (rev 8348) @@ -30,6 +30,7 @@ * * @version $Revision$ * @author Marc Guillemot + * @author Ronald Brill */ public interface DownloadedContent extends Serializable { /** @@ -37,6 +38,7 @@ */ static class InMemory implements DownloadedContent { private final byte[] bytes_; + public InMemory(final byte[] byteArray) { if (byteArray == null) { bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY; @@ -51,7 +53,12 @@ } public void cleanUp() { + // nothing to do } + + public boolean isEmpty() { + return bytes_.length == 0; + } } /** @@ -79,6 +86,10 @@ FileUtils.deleteQuietly(file_); } } + + public boolean isEmpty() { + return false; + } } /** @@ -93,4 +104,9 @@ */ void cleanUp(); + /** + * Returns true if the content is empty. + * @return true or false + */ + boolean isEmpty(); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponseData.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponseData.java 2013-06-16 08:00:03 UTC (rev 8347) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponseData.java 2013-06-19 18:58:08 UTC (rev 8348) @@ -86,10 +86,18 @@ downloadedContent_ = responseBody; } - private InputStream getStream(InputStream stream, final List<NameValuePair> headers) throws IOException { + private InputStream getStream(final DownloadedContent downloadedContent, + final List<NameValuePair> headers) throws IOException { + + InputStream stream = downloadedContent_.getInputStream(); if (stream == null) { return null; } + + if (downloadedContent.isEmpty()) { + return stream; + } + final String encoding = getHeader(headers, "content-encoding"); if (encoding != null) { // check content length @@ -153,7 +161,7 @@ * @throws IOException in case of IO problems */ public InputStream getInputStream() throws IOException { - return getStream(downloadedContent_.getInputStream(), getResponseHeaders()); + return getStream(downloadedContent_, getResponseHeaders()); } /** Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebResponseDataTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebResponseDataTest.java 2013-06-16 08:00:03 UTC (rev 8347) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebResponseDataTest.java 2013-06-19 18:58:08 UTC (rev 8348) @@ -43,6 +43,7 @@ * @version $Revision$ * @author Daniel Gredler * @author Ahmed Ashour + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class WebResponseDataTest extends WebServerTestCase { @@ -72,11 +73,41 @@ */ @Test public void testEmptyGZippedContent() throws Exception { + testEmptyGZippedContent(HttpStatus.SC_OK, 0, null); + } + + /** + * Tests that empty gzipped content is handled correctly (bug #1510). + * @throws Exception if the test fails + */ + @Test + public void contentLengthIsZero() throws Exception { + testEmptyGZippedContent(HttpStatus.SC_OK, 0, "text/html"); + } + + /** + * Tests that empty gzipped content is handled correctly (bug #1510). + * @throws Exception if the test fails + */ + @Test + public void contentLengthIsMissing() throws Exception { + testEmptyGZippedContent(HttpStatus.SC_NO_CONTENT, -1, null); + } + + private void testEmptyGZippedContent(final int statusCode, final int contentLength, + final String contentType) throws Exception { final List<NameValuePair> headers = new ArrayList<NameValuePair>(); - headers.add(new NameValuePair("Content-Length", "0")); headers.add(new NameValuePair("Content-Encoding", "gzip")); - final WebResponseData data = new WebResponseData("".getBytes(), HttpStatus.SC_OK, "OK", headers); + if (contentLength != -1) { + headers.add(new NameValuePair("Content-Length", String.valueOf(contentLength))); + } + + if (contentType != null) { + headers.add(new NameValuePair("Content-Type", contentType)); + } + + final WebResponseData data = new WebResponseData("".getBytes(), statusCode, "OK", headers); data.getBody(); } |