commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods PostMethod.java
Date Wed, 10 Jul 2002 04:54:58 GMT
dion        2002/07/09 21:54:58

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestMethodsNoHost.java
               httpclient/src/java/org/apache/commons/httpclient/methods
                        PostMethod.java
  Log:
  Fix for Bug http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10553
  as supplied by Jeff Dever
  
  Revision  Changes    Path
  1.2       +71 -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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestMethodsNoHost.java	24 Apr 2002 14:44:50 -0000	1.1
  +++ TestMethodsNoHost.java	10 Jul 2002 04:54:58 -0000	1.2
  @@ -71,6 +71,16 @@
    */
   public class TestMethodsNoHost extends TestCase {
   
  +    static final String NAME = "name", VALUE = "value";
  +    static final String NAME0 = "name0", VALUE0 = "value0";
  +    static final String NAME1 = "name1", VALUE1 = "value1";
  +    static final String NAME2 = "name2", VALUE2 = "value2";
  +    static final NameValuePair PAIR = new NameValuePair(NAME, VALUE);
  +    static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0);
  +    static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
  +    static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
  +    
  +
       // ------------------------------------------------------------ Constructor
   
       public TestMethodsNoHost(String testName) {
  @@ -106,4 +116,61 @@
               // expected
           }
       }
  +
  +      
  +    /** Ensure that parameters can be added and getted. */
  +    public void testPostAddGetParameter() {
  +        PostMethod post = new PostMethod();
  +       
  +        //no parameters
  +        assertNull(post.getParameter("non-existant"));
  +        assertEquals(0, post.getParameters().length);
  +
  +        //add one parameter
  +        post.addParameter(NAME,VALUE);
  +        assertEquals(PAIR, post.getParameter(NAME));
  +        assertEquals(1, post.getParameters().length);
  +
  +        //add another parameter
  +        post.addParameter(PAIR0);
  +        assertEquals(PAIR0, post.getParameter(PAIR0.getName()));
  +        assertEquals(2, post.getParameters().length);
  +        
  +        //add two more parameters
  +        post.addParameters(new NameValuePair[]{ PAIR1, PAIR2 });
  +        assertEquals(4, post.getParameters().length);
  +        NameValuePair[] parameters = post.getParameters();
  +        for (int i=0; i<parameters.length; i++){
  +            NameValuePair pair = parameters[i];
  +            assertEquals(pair, post.getParameter(pair.getName()));
  +        }
  +    }
  +
  +    /** Exercise some unusual and error conditions. */
  +    public void testPostAddParameterErrorCases() {
  +        PostMethod post = new PostMethod();
  +
  +        //add same parameter twice
  +        post.addParameter(PAIR0);
  +        post.addParameter(PAIR0);
  +        assertEquals(1, post.getParameters().length);
  +
  +        //add null paramter
  +        post.addParameter(null);
  +        post.addParameter(null, "value");
  +        post.addParameter("name", null);
  +    }
  +        
  + 
  +    public void testPostSetRequestBodyThrowsIllegalState() throws Exception {
  +        PostMethod post = new PostMethod("/foo");
  +        post.addParameter(NAME0, VALUE0);
  +        try {
  +            post.setRequestBody("this+is+the+body");
  +            fail("Expected IllegalStateException");
  +        } catch(IllegalStateException e) {
  +            // expected
  +        }
  +    }
  +
   }
  
  
  
  1.10      +90 -8     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PostMethod.java	24 Apr 2002 14:44:50 -0000	1.9
  +++ PostMethod.java	10 Jul 2002 04:54:58 -0000	1.10
  @@ -69,6 +69,7 @@
   import org.apache.commons.httpclient.HttpMethodBase;
   import org.apache.commons.httpclient.HttpException;
   import org.apache.commons.httpclient.URIUtil;
  +import org.apache.commons.httpclient.NameValuePair;
   import java.util.Iterator;
   import java.util.HashMap;
   import java.util.List;
  @@ -157,12 +158,10 @@
       }
   
       /**
  -     * Overrides method of {@link HttpMethodBase}
  -     * to throw {@link IllegalStateException} if
  -     * my request body has already been
  -     * {@link #generateRequestBody generated}
  -     * or {@link #setRequestBody set}.
  +     * Add a new parameter to be used in the POST request body.
        *
  +     * @param parameterName The parameter name to add.
  +     * @param parameterValue The parameter value to add.
        * @throws IllegalStateException if my request body has already been generated.
        */
       public void addParameter(String parameterName, String parameterValue) {
  @@ -186,6 +185,89 @@
               v.add(parameterValue);
               parameters.put(parameterName,v);
           }
  +    }
  +
  +
  +    /**
  +     * Add a new parameter to be used in the POST request body.
  +     * Logs a warning if the parameter argument is null.
  +     *
  +     * @param parameter The parameter to add.
  +     * @throws IllegalStateException if my request body has already been generated.
  +     * @see #addParameter(java.lang.String,java.lang.String)
  +     * @since 2.0
  +     */
  +    public void addParameter(NameValuePair parameter) {
  +        if(null == parameter){
  +            log.warn("Attempt to addParameter(null) ignored");
  +        }else{
  +            addParameter(parameter.getName(), parameter.getValue());
  +        }
  +    }
  +
  +    /**
  +     * Add a list of parameters to be used in the POST request body.
  +     * Logs a warning if the parameters argument is null.
  +     *
  +     * @param parameter The array of parameters to add.
  +     * @throws IllegalStateException if my request body has already been generated.
  +     * @see #addParameter(org.apache.commons.httpclient.NameValuePair)
  +     * @since 2.0
  +     */
  +    public void addParameters(NameValuePair[] parameters) {
  +        if(null == parameters){
  +            log.warn("Attempt to addParameters(null) ignored");
  +        }else{
  +            for (int i=0; i<parameters.length; i++){
  +                addParameter(parameters[i]);
  +            }
  +        }
  +    }
  +
  +
  +    /**
  +     * Gets the parameter of the specified name.
  +     * The returned object is new and is <b>not</b> a refrence to any 
  +     * internal data members.
  +     * 
  +     * @return If a parameter exists with the name argument, the coresponding
  +     * NameValuePair is returned.  Otherwise null.
  +     * @since 2.0
  +     */
  +    public NameValuePair getParameter(String name){
  +        String value = null;
  +        try{
  +            value = (String)parameters.get(name);
  +        }catch (ClassCastException e){
  +            log.warn("Object of non-string type has been added as a parameter");
  +            value = null;
  +        }
  +        if (name == null || value == null){
  +            return null;
  +        }else{
  +            return new NameValuePair(name, value);
  +        }
  +    }
  +
  +    /**
  +     * Gets the parameters currently added to the PostMethod.
  +     * The returned object is new and is <b>not</b> a refrence to any 
  +     * internal data members.
  +     * 
  +     * @return An array of the current parameters
  +     * @see #getParameter(java.lang.String);
  +     * @since 2.0
  +     */
  +    public NameValuePair[] getParameters(){
  +        Set keySet = parameters.keySet();
  +        Object[] keyArray = keySet.toArray();
  +        int numKeys = keyArray.length;
  +        NameValuePair[] nvPairs = new NameValuePair[numKeys];
  +        Object key;
  +        for (int i=0; i<numKeys; i++){
  +            nvPairs[i] = this.getParameter((String)keyArray[i]);
  +        }
  +        return nvPairs;
       }
   
       /**
  
  
  

--
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