Menu

#327 FileNotFoundException using Sun JDK 1.5 on empty error pages

closed-accepted
None
5
2008-04-30
2005-09-07
No

When using httpunit on Sun JDK 1.5 I have problems
getting the response code for error pages (such as 404)
that does not contain any content. I have tracked it
down to the following methods in
com.meterware.httpunit.HttpWebResponse
getInputStream(URLConenction) and
isResponseOnErrorStream(URLConnection).

The problem is that these methods assume that if no
error stream exists, then a regular input stream will
exist. However, as of JDK 1.5 error responses (>=400)
that does not have a body no longer returns an empty
error stream, but instead returns null. This leads the
current code to instead fetch the regular input stream
which generates and exception.

I patched my local copy to instead look like:
private InputStream getInputStream( URLConnection
connection ) throws IOException {
try {
return isResponseOnErrorStream( connection )
? ((HttpURLConnection)
connection).getErrorStream()
: connection.getInputStream();
} catch (IOException e) {
return new ByteArrayInputStream(new byte[0]);
}
}

Also, I have not taken into account the
throwExceptionOnError setting so my workaround will not
work for all cases. I'd be happy to make a more general
patch available.

Discussion

  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-06

    Logged In: YES
    user_id=1220573
    Originator: NO

    Dear Roger Lindsjö,

    thank you for your bug report.
    For the upcoming 1.7 and 2.0 releases I'd be most interested in your patches. Would you please try to create a Junit test cases to show your point?

    Yours
    Wolfgang

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-06

    Logged In: YES
    user_id=1220573
    Originator: NO

    Dear httpunit user!

    Thank you for your bug report. We appreciate the time and effort you are putting into this.

    Please supply a testcase with the expected result for the bug report that you are asking a solution for and we'll look into implementing it. For a start you might want to get the trunk version from the subversion repository (see https://sourceforge.net/svn/?group_id=6550\)
    and have a look at the source code of some of the more than 700 JUnit based testcase in there.

    If you do not use or have subversion tool you can still directly browse our test cases via:
    http://httpunit.svn.sourceforge.net/viewvc/httpunit/trunk/httpunit/test/com/meterware/httpunit/
    Looking into one or more of the Junit Java source files
    should give you a clue on what a proper testcase for httpunit looks like, often you will probably only have to "clone" an existing testcase and modify it slightly to your needs.

    When you are ready you might want to attach the testcase (and if you already have started implementing a solution for it it also the actual code) to the patch section of the sourceforge.net tracker for patches of the httpunit project at
    https://sourceforge.net/tracker/?atid=306550&group_id=6550&func=browse.

    The main communication about further details of the development is via the httpunit developer mailinglist. You are most welcome to sign up via
    https://lists.sourceforge.net/lists/listinfo/httpunit-develop

    Yours
    The httpunit developer team

    (Russell, Wolfgang, Mark, Patrick and Tim as of 2008-04)

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-06
    • assigned_to: nobody --> wolfgang_fahl
    • status: open --> pending-accepted
     
  • SourceForge Robot

    • status: pending-accepted --> closed-accepted
     
  • SourceForge Robot

    Logged In: YES
    user_id=1312539
    Originator: NO

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
  • Roger Lindsjö

    Roger Lindsjö - 2008-04-21
    • status: closed-accepted --> pending-accepted
     
  • Roger Lindsjö

    Roger Lindsjö - 2008-04-21

    Logged In: YES
    user_id=643287
    Originator: YES

    I finally got around creating a test case and possibly a fix. Not sure what format they are wanted in, so I attach the outputs of svn diff.

    Test:

    [roger@pooh httpunit]$ svn diff test
    Index: test/com/meterware/httpunit/WebClientTest.java
    ===================================================================
    --- test/com/meterware/httpunit/WebClientTest.java (revision 940)
    +++ test/com/meterware/httpunit/WebClientTest.java (working copy)
    @@ -985,4 +985,19 @@
    assertEquals( "Submitted cookie header", "found cookies: type=short", wr.getText() );
    }

    + public void testEmptyErrorPage() throws Exception {
    + boolean originalState = HttpUnitOptions.getExceptionsThrownOnErrorStatus();
    + HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
    + try {
    + WebConversation wc = new WebConversation();
    + defineResource( "emptyError", "", 404);
    + WebRequest request = new GetMethodWebRequest( getHostPath() + "/emptyError" );
    + WebResponse response = wc.getResponse( request );
    + assertEquals( 404, response.getResponseCode() );
    + assertEquals( 0, response.getContentLength() );
    + } finally {
    + // Restore exceptions state
    + HttpUnitOptions.setExceptionsThrownOnErrorStatus( originalState );
    + }
    + }
    }

    Fix:

    [roger@pooh httpunit]$ svn diff src
    Index: src/com/meterware/httpunit/HttpWebResponse.java
    ===================================================================
    --- src/com/meterware/httpunit/HttpWebResponse.java (revision 940)
    +++ src/com/meterware/httpunit/HttpWebResponse.java (working copy)
    @@ -20,6 +20,7 @@
    *
    *******************************************************************************************************************/
    import java.io.BufferedInputStream;
    +import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;

    @@ -71,14 +72,18 @@

    private InputStream getInputStream( URLConnection connection ) throws IOException {
    - return isResponseOnErrorStream( connection )
    - ? ((HttpURLConnection) connection).getErrorStream()
    - : connection.getInputStream();
    + InputStream stream = getErrorStream( connection );
    + return stream != null ? stream : connection.getInputStream();
    }

    - private boolean isResponseOnErrorStream( URLConnection connection ) {
    - return _responseCode >= HttpURLConnection.HTTP_BAD_REQUEST && ((HttpURLConnection) connection).getErrorStream() != null;
    + private InputStream getErrorStream( URLConnection connection ) {
    + if( _responseCode < HttpURLConnection.HTTP_BAD_REQUEST ) {
    + return null;
    + } else {
    + InputStream stream = ((HttpURLConnection) connection).getErrorStream();
    + return stream != null ? stream : new ByteArrayInputStream(new byte[0]);
    + }
    }

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-30

    Logged In: YES
    user_id=1220573
    Originator: NO

    Dear Roger,
    excellent - thank you very much for this contribution:
    http://httpunit.svn.sourceforge.net/viewvc/httpunit?view=rev&revision=946

    has your changes.

    Yours
    Wolfgang

     
  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-30
    • status: pending-accepted --> closed-accepted
     
  • Wolfgang Fahl

    Wolfgang Fahl - 2008-04-30

    Logged In: YES
    user_id=1220573
    Originator: NO

    The change is now in the subversion repository and will be in the next build

     

Log in to post a comment.