You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2002/04/24 16:44:50 UTC

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

rwaldhoff    02/04/24 07:44:50

  Modified:    httpclient/src/java/org/apache/commons/httpclient/methods
                        PostMethod.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java TestWebappMethods.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestMethodsNoHost.java
  Log:
  add setRequestBody(String) method to PostMethod, and some tests for it
  
  Revision  Changes    Path
  1.9       +31 -13    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
  
  Index: PostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- PostMethod.java	5 Jan 2002 11:16:01 -0000	1.8
  +++ PostMethod.java	24 Apr 2002 14:44:50 -0000	1.9
  @@ -1,12 +1,12 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v 1.8 2002/01/05 11:16:01 vmassol Exp $
  - * $Revision: 1.8 $
  - * $Date: 2002/01/05 11:16:01 $
  + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v 1.9 2002/04/24 14:44:50 rwaldhoff Exp $
  + * $Revision: 1.9 $
  + * $Date: 2002/04/24 14:44:50 $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -141,9 +141,11 @@
       }
   
       /**
  -     * Override method of {@link HttpMethodBase}
  +     * Overrides method of {@link HttpMethodBase}
        * to throw {@link IllegalStateException} if
  -     * my request body has already been generated.
  +     * my request body has already been
  +     * {@link #generateRequestBody generated}
  +     * or {@link #setRequestBody set}.
        *
        * @throws IllegalStateException if my request body has already been generated.
        */
  @@ -155,9 +157,11 @@
       }
   
       /**
  -     * Override method of {@link HttpMethodBase}
  +     * Overrides method of {@link HttpMethodBase}
        * to throw {@link IllegalStateException} if
  -     * my request body has already been generated.
  +     * my request body has already been
  +     * {@link #generateRequestBody generated}
  +     * or {@link #setRequestBody set}.
        *
        * @throws IllegalStateException if my request body has already been generated.
        */
  @@ -185,9 +189,11 @@
       }
   
       /**
  -     * Override method of {@link HttpMethodBase}
  +     * Overrides method of {@link HttpMethodBase}
        * to throw {@link IllegalStateException} if
  -     * my request body has already been generated.
  +     * my request body has already been
  +     * {@link #generateRequestBody generated}
  +     * or {@link #setRequestBody set}.
        *
        * @throws IllegalStateException if my request body has already been generated.
        */
  @@ -199,9 +205,11 @@
       }
   
       /**
  -     * Override method of {@link HttpMethodBase}
  +     * Overrides method of {@link HttpMethodBase}
        * to throw {@link IllegalStateException} if
  -     * my request body has already been generated.
  +     * my request body has already been
  +     * {@link #generateRequestBody generated}
  +     * or {@link #setRequestBody set}.
        *
        * @throws IllegalStateException if my request body has already been generated.
        */
  @@ -229,6 +237,16 @@
       }
   
       /**
  +     * @throws IllegalStateException if request params have been added
  +     */
  +    public void setRequestBody(String body) {
  +        if(!parameters.isEmpty()) {
  +            throw new IllegalStateException("Request parameters have already been added.");
  +        }
  +        requestBody = body;
  +    }
  +
  +    /**
        * Override method of {@link HttpMethodBase}
        * to also add <tt>Content-Type</tt> header
        * when appropriate.
  @@ -269,7 +287,7 @@
           return requestBody.getBytes().length;
       }
   
  -    protected String generateRequestBody(HashMap params) {
  +    protected String generateRequestBody(Map params) {
           if (!params.isEmpty()) {
               StringBuffer sb = new StringBuffer();
               Iterator it = parameters.keySet().iterator();
  
  
  
  1.7       +6 -7      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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestNoHost.java	12 Apr 2002 21:17:06 -0000	1.6
  +++ TestNoHost.java	24 Apr 2002 14:44:50 -0000	1.7
  @@ -1,12 +1,12 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.6 2002/04/12 21:17:06 marcsaeg Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/04/12 21:17:06 $
  + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.7 2002/04/24 14:44:50 rwaldhoff Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/04/24 14:44:50 $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -71,7 +71,7 @@
    * (True unit tests, by some definitions.)
    *
    * @author Rodney Waldhoff
  - * @version $Id: TestNoHost.java,v 1.6 2002/04/12 21:17:06 marcsaeg Exp $
  + * @version $Revision: 1.7 $ $Date: 2002/04/24 14:44:50 $
    */
   public class TestNoHost extends TestCase {
   
  @@ -87,13 +87,12 @@
           suite.addTest(TestNVP.suite());
           suite.addTest(TestHeader.suite());
           suite.addTest(TestHeaderElement.suite());
  -        // MD5Encoder removed
  -        // suite.addTest(TestMD5Encoder.suite());
           suite.addTest(TestAuthenticator.suite());
           suite.addTest(TestHttpClientNoHost.suite());
           suite.addTest(TestHttpUrlMethod.suite());
           suite.addTest(TestHttpConnectionManager.suite());
           suite.addTest(TestURIUtil.suite());
  +        suite.addTest(TestMethodsNoHost.suite());
           return suite;
       }
   
  
  
  
  1.4       +20 -6     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java
  
  Index: TestWebappMethods.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestWebappMethods.java	4 Feb 2002 15:26:43 -0000	1.3
  +++ TestWebappMethods.java	24 Apr 2002 14:44:50 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.3 2002/02/04 15:26:43 dion Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/02/04 15:26:43 $
  + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.4 2002/04/24 14:44:50 rwaldhoff Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/04/24 14:44:50 $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -82,7 +82,7 @@
    * "httpclient.test.webappContext" property.
    *
    * @author Rodney Waldhoff
  - * @version $Id: TestWebappMethods.java,v 1.3 2002/02/04 15:26:43 dion Exp $
  + * @version $Id: TestWebappMethods.java,v 1.4 2002/04/24 14:44:50 rwaldhoff Exp $
    */
   public class TestWebappMethods extends TestWebappBase {
   
  @@ -291,7 +291,7 @@
           assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
       }
   
  -    public void testPostBody() throws Exception {
  +    public void testPostBodyNVP() throws Exception {
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           PostMethod method = new PostMethod("/" + context + "/body");
  @@ -307,6 +307,21 @@
           assertEquals(200,method.getStatusCode());
       }
   
  +    public void testPostBody() throws Exception {
  +        HttpClient client = new HttpClient();
  +        client.startSession(host, port);
  +        PostMethod method = new PostMethod("/" + context + "/body");
  +        method.setUseDisk(false);
  +        method.setRequestBody("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.");
  +        try {
  +            client.executeMethod(method);
  +        } catch (Throwable t) {
  +            t.printStackTrace();
  +            fail("Unable to execute method : " + t.toString());
  +        }
  +        assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
  +        assertEquals(200,method.getStatusCode());
  +    }
   
       public void testPutBody() throws Exception {
           HttpClient client = new HttpClient();
  @@ -322,6 +337,5 @@
           assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<tt>This is data to be sent in the body of an HTTP PUT.</tt>") >= 0);
           assertEquals(200,method.getStatusCode());
       }
  -
   
   }
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java
  
  Index: TestMethodsNoHost.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v 1.1 2002/04/24 14:44:50 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2002/04/24 14:44:50 $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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.*;
  import org.apache.commons.httpclient.methods.*;
  
  /**
   * @author Rodney Waldhoff
   * @version $Revision: 1.1 $ $Date: 2002/04/24 14:44:50 $
   */
  public class TestMethodsNoHost extends TestCase {
  
      // ------------------------------------------------------------ Constructor
  
      public TestMethodsNoHost(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestMethodsNoHost.class);
      }
  
      // ----------------------------------------------------------------- Tests
  
      public void testPostCantAddParamAfterBodySet() throws Exception {
          PostMethod post = new PostMethod("/foo");
          post.setRequestBody("this+is+the+body");
          try {
              post.addParameter("name","value");
              fail("Expected IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
  
      public void testPostCantSetBodyAfterAddParam() throws Exception {
          PostMethod post = new PostMethod("/foo");
          post.addParameter("name","value");
          try {
              post.setRequestBody("this+is+the+body");
              fail("Expected IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>