commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jsde...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestResponseHeaders.java TestNoHost.java
Date Fri, 19 Jul 2002 20:50:37 GMT
jsdever     2002/07/19 13:50:37

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
                        java/org/apache/commons/httpclient/HttpMethodBase.java
                        /org/apache/commons/httpclient/TestNoHost.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java
                        java/org/apache/commons/httpclient/HttpMethodBase.java
                        /org/apache/commons/httpclient/TestNoHost.java
  Added:       httpclient/src/java/org/apache/commons/httpclient
                        test/org/apache/commons/httpclient/TestResponseHeaders.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestResponseHeaders.java
                        test/org/apache/commons/httpclient/TestResponseHeaders.java
  Log:
  Add patch for bug http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7754
  
  Tweaked the test cases to be more informative if they fail and added a test for a leading
space fold situation.  Tests run with the test-nohost ant target.
  
  Contributor: Davanum Srinivas
  
  Revision  Changes    Path
  1.33      +34 -7     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.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- HttpMethodBase.java	18 Jul 2002 17:05:51 -0000	1.32
  +++ HttpMethodBase.java	19 Jul 2002 20:50:36 -0000	1.33
  @@ -112,7 +112,8 @@
    * @author Rodney Waldhoff
    * @author Sean C. Sullivan
    * @author <a href="mailto:dion@apache.org">dIon Gillard</a>
  - * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  + * @author <a href="mailto:jsdever@sympatico.ca">Jeff Dever</a>
  + * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
    * @version $Revision$ $Date$
    */
   public abstract class HttpMethodBase implements HttpMethod {
  @@ -1079,6 +1080,8 @@
           }
           responseHeaders.clear();
   
  +        String name = null;
  +        String value = null;
           for(;;) {
               String line = conn.readLine();
               if ((line == null) || (line.length() < 1)) {
  @@ -1086,20 +1089,44 @@
               }
   
               // Parse the header name and value
  +            // Check for folded headers first
  +            // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
  +            // discussion on folded headers
  +
  +             boolean isFolded = false;
  +             if ( line.charAt(0) == ' ' || line.charAt(0) == '\t' )
  +             {
  +                 // we have continuation folded header
  +                 // so append value
  +                 isFolded = true;
  +                 value = line.substring(1).trim();
  +             }
  +             else
  +             {
  +                 // Otherwise we should have normal HTTP header line
  +            // Parse the header name and value
               int colon = line.indexOf(":");
               if (colon < 0) {
                   throw new HttpException("Unable to parse header: " + line);
               }
  -            String name = line.substring(0, colon).trim();
  +                name = line.substring(0, colon).trim();
  +                value = line.substring(colon + 1).trim();
  +            }
               String match = name.toLowerCase();
  -            String value = line.substring(colon + 1).trim();
               Header header = (Header)(responseHeaders.get(match));
               if (null == header) {
                   header = new Header(name, value);
               } else {
                   String oldvalue =  header.getValue();
                   if (null != oldvalue) {
  +                    if ( isFolded ) {
  +                        // LWS becomes space plus extended value
  +                        header = new Header(name,oldvalue + " " + value);
  +                    }
  +                    else {
  +                        // Append additional header value
                       header = new Header(name,oldvalue + ", " + value);
  +                    }
                   } else {
                       header = new Header(name,value);
                   }
  
  
  
                       
  
  
                       
  
  
                       
  
  
  1.9       +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestNoHost.java	16 Jul 2002 02:54:30 -0000	1.8
  +++ TestNoHost.java	19 Jul 2002 20:50:37 -0000	1.9
  @@ -95,6 +95,7 @@
           suite.addTest(TestURIUtil.suite());
           suite.addTest(TestMethodsNoHost.suite());
           suite.addTest(TestHttpState.suite());
  +        suite.addTest(TestResponseHeaders.suite());
           return suite;
       }
   
  
  
  
                       
  
  
                       
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java
  
  Index: TestResponseHeaders.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v
1.1 2002/07/19 20:50:37 jsdever Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/19 20:50:37 $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.commons.httpclient;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import java.io.BufferedReader;
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.StringReader;
  
  /**
   * Tests for reading response headers.
   *
   * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
   * @version $Id: TestResponseHeaders.java,v 1.1 2002/07/19 20:50:37 jsdever Exp $
   */
  public class TestResponseHeaders extends TestCase {
  
      // ------------------------------------------------------------ Constructor
      public TestResponseHeaders(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = {TestResponseHeaders.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
      public static Test suite() {
          return new TestSuite(TestResponseHeaders.class);
      }
  
      // ------------------------------------------------------------ Inner Class
      /**
       * Hack HttpConnection to test the response header reading mechanism.
       */
      private class SimpleHttpConnection extends HttpConnection {
          BufferedReader headerReader = null;
          ByteArrayInputStream bodyInputStream = null;
          public SimpleHttpConnection(String headers, String body) {
              super(null, -1, "localhost", 80, false);
              this.headerReader = new BufferedReader(new StringReader(headers));
              bodyInputStream = new ByteArrayInputStream(body.getBytes());
          }
          public void open() throws IOException {
          }
          public void write(byte[] data) throws IOException, IllegalStateException, HttpRecoverableException
{
          }
          public void writeLine() throws IOException, IllegalStateException, HttpRecoverableException
{
          }
          public String readLine() throws IOException, IllegalStateException {
              return headerReader.readLine();
          }
          public InputStream getResponseInputStream(HttpMethod method) {
              return bodyInputStream;
          }
      }
  
      /**
       * Simple extension of HttpMethodBase.
       */
      private class SimpleHttpMethod extends HttpMethodBase {
          public SimpleHttpMethod() {
              super("");
          }
          public String getName() {
              return "simple";
          }
      }
  
      // ----------------------------------------------------------- Test Methods
      public void testHeaders() throws Exception {
          String body = "XXX\r\nYYY\r\nZZZ";
          String headers =
                  "HTTP/1.1 200 OK\r\n" +
                  "Connection: close\r\n" +
                  "Content-Length: " + body.length() + "\r\n" +
                  "Content-Type: text/xml; charset=utf-8\r\n" +
                  "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" +
                  "Server: UserLand Frontier/7.0-WinNT\r\n";
          HttpState state = new HttpState();
          HttpMethod method = new SimpleHttpMethod();
          SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
          method.execute(state, conn);
          assertEquals("close", method.getResponseHeader("Connection").getValue());
          assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
          assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue());
          assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
          assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
      }
  
      public void testFoldedHeaders() throws Exception {
          String body = "XXX\r\nYYY\r\nZZZ";
          String headers =
                  "HTTP/1.1 200 OK\r\n" +
                  "Connection: close\r\n" +
                  "Content-Length: " + body.length() + "\r\n" +
                  "Content-Type: text/xml; charset=utf-8\r\n" +
                  "\tboundary=XXXX\r\n" +
                  "Date: Wed, 28 Mar 2001\r\n" + 
                  " 05:05:04 GMT\r\n" +
                  "Server: UserLand Frontier/7.0-WinNT\r\n";
          HttpState state = new HttpState();
          HttpMethod method = new SimpleHttpMethod();
          SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
          method.execute(state, conn);
          assertEquals("close", method.getResponseHeader("Connection").getValue());
          assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
          assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue());
          assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
          assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
          assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary")
!= -1);
      }
  }
  
  
  
                       
  
  

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


Mime
View raw message