You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/06/13 23:32:18 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestMethodsNoHost.java

olegk       2003/06/13 14:32:18

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestMethodsNoHost.java
  Log:
  Fixed the bug causing HttpMethodBase.getResponseBody() to throw NullPointerException when response body is empty. Test case added.
  
  Reported by Thomas Winkler <ca...@firemail.de>
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.152     +20 -19    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.151
  retrieving revision 1.152
  diff -u -r1.151 -r1.152
  --- HttpMethodBase.java	10 Jun 2003 22:42:51 -0000	1.151
  +++ HttpMethodBase.java	13 Jun 2003 21:32:17 -0000	1.152
  @@ -718,26 +718,27 @@
        * @return The response body as a byte array.
        */
       public byte[] getResponseBody() {
  -        if (responseBody == null) {
  +        if (this.responseBody == null) {
               try {
  -                ByteArrayOutputStream os = new ByteArrayOutputStream();
  -                InputStream is = getResponseBodyAsStream();
  -                byte[] buffer = new byte[4096];
  -                int len;
  -                while ((len = is.read(buffer)) > 0) {
  -                    os.write(buffer, 0, len);
  +                InputStream instream = getResponseBodyAsStream();
  +                if (instream != null) {
  +                    LOG.debug("Buffering response body");
  +                    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
  +                    byte[] buffer = new byte[4096];
  +                    int len;
  +                    while ((len = instream.read(buffer)) > 0) {
  +                        outstream.write(buffer, 0, len);
  +                    }
  +                    outstream.close();
  +                    setResponseStream(null);
  +                    this.responseBody = outstream.toByteArray();
                   }
  -                is.close();
  -                os.close();
  -                responseBody = os.toByteArray();
  -                setResponseStream(null);
  -                LOG.debug("buffering response body");
               } catch (IOException e) {
  -                LOG.error("getResponseBody failed", e);
  -                responseBody = null;
  +                LOG.error("I/O failure reading response body", e);
  +                this.responseBody = null;
               }
           }
  -        return responseBody;
  +        return this.responseBody;
       }
   
       /**
  
  
  
  1.18      +43 -4     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java
  
  Index: TestMethodsNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- TestMethodsNoHost.java	17 Apr 2003 11:34:19 -0000	1.17
  +++ TestMethodsNoHost.java	13 Jun 2003 21:32:17 -0000	1.18
  @@ -255,4 +255,43 @@
           method.releaseConnection();
       }
   
  +
  +    /** 
  +     * Tests empty body response
  +     */
  +    
  +    public void testEmptyBodyAsString() throws Exception {
  +        SimpleHttpConnection conn = new SimpleHttpConnection();
  +        String headers = "HTTP/1.1 200 OK\r\n"
  +                       +"Connection: close\r\n"
  +                       +"Transfer-Encoding: chunked\r\n"
  +                       +"Content-Length: 0\r\n";
  +        String body = "";
  +        conn.addResponse(headers, body);
  +        conn.open();
  +        HttpMethodBase method = new GetMethod("/");
  +        method.execute(new HttpState(), conn);
  +
  +        String response = method.getResponseBodyAsString();
  +        assertNull(response);
  +    }
  +
  +
  +    public void testEmptyBodyAsByteArray() throws Exception {
  +        SimpleHttpConnection conn = new SimpleHttpConnection();
  +        String headers = "HTTP/1.1 200 OK\r\n"
  +                       +"Connection: close\r\n"
  +                       +"Transfer-Encoding: chunked\r\n"
  +                       +"Content-Length: 0\r\n";
  +        String body = "";
  +        conn.addResponse(headers, body);
  +        conn.open();
  +        HttpMethodBase method = new GetMethod("/");
  +        method.execute(new HttpState(), conn);
  +
  +        byte[] response = method.getResponseBody();
  +        assertNull(response);
  +    }
  +
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org