commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params TestHttpParams.java
Date Wed, 15 Sep 2004 20:42:17 GMT
olegk       2004/09/15 13:42:17

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        Header.java HttpMethodBase.java
                        HttpMethodDirector.java
               httpclient/src/java/org/apache/commons/httpclient/params
                        HostParams.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java
  Added:       httpclient/src/test/org/apache/commons/httpclient/params
                        TestHttpParams.java
  Log:
  PR #10793 (User definable default headers support)
  
  Changelog:
  
  * default headers can be defined at the host or agent level
  * Header class made immutable
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke
  
  Revision  Changes    Path
  1.17      +5 -19     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Header.java
  
  Index: Header.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Header.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Header.java	13 May 2004 04:03:25 -0000	1.16
  +++ Header.java	15 Sep 2004 20:42:17 -0000	1.17
  @@ -137,22 +137,8 @@
        * 
        * @since 3.0
        */
  -    public boolean isAutogenerated()
  -    {
  +    public boolean isAutogenerated() {
           return isAutogenerated;
  -    }
  -
  -    /**
  -     * Sets the values of the auto-generated header flag.
  -     * 
  -     * @return b <tt>true</tt> if the header is autogenerated,
  -     *  <tt>false</tt> otherwise.
  -     * 
  -     * @since 3.0
  -     */
  -    public void setAutogenerated(boolean b)
  -    {
  -        isAutogenerated = b;
       }
   
   }
  
  
  
  1.213     +8 -10     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.212
  retrieving revision 1.213
  diff -u -r1.212 -r1.213
  --- HttpMethodBase.java	14 Sep 2004 20:11:31 -0000	1.212
  +++ HttpMethodBase.java	15 Sep 2004 20:42:17 -0000	1.213
  @@ -1166,15 +1166,13 @@
           if ((cookies != null) && (cookies.length > 0)) {
               if (getParams().isParameterTrue(HttpMethodParams.SINGLE_COOKIE_HEADER)) {
                   // In strict mode put all cookies on the same header
  -                Header header = matcher.formatCookieHeader(cookies);
  -                header.setAutogenerated(true);                 
  -                getRequestHeaderGroup().addHeader(header);
  +                String s = matcher.formatCookies(cookies);
  +                getRequestHeaderGroup().addHeader(new Header("Cookie", s, true));
               } else {
                   // In non-strict mode put each cookie on a separate header
                   for (int i = 0; i < cookies.length; i++) {
  -                    Header header = matcher.formatCookieHeader(cookies[i]);
  -                    header.setAutogenerated(true);                 
  -                    getRequestHeaderGroup().addHeader(header);
  +                    String s = matcher.formatCookie(cookies[i]);
  +                    getRequestHeaderGroup().addHeader(new Header("Cookie", s, true));
                   }
               }
           }
  
  
  
  1.30      +17 -3     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java
  
  Index: HttpMethodDirector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- HttpMethodDirector.java	14 Sep 2004 20:11:31 -0000	1.29
  +++ HttpMethodDirector.java	15 Sep 2004 20:42:17 -0000	1.30
  @@ -30,7 +30,9 @@
   package org.apache.commons.httpclient;
   
   import java.io.IOException;
  +import java.util.Collection;
   import java.util.HashSet;
  +import java.util.Iterator;
   import java.util.Map;
   import java.util.Set;
   
  @@ -44,6 +46,7 @@
   import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
   import org.apache.commons.httpclient.auth.AuthScope;
   import org.apache.commons.httpclient.auth.MalformedChallengeException;
  +import org.apache.commons.httpclient.params.HostParams;
   import org.apache.commons.httpclient.params.HttpClientParams;
   import org.apache.commons.httpclient.params.HttpConnectionParams;
   import org.apache.commons.httpclient.params.HttpMethodParams;
  @@ -130,6 +133,17 @@
           // Global -> HttpClient -> HostConfiguration -> HttpMethod
           this.hostConfiguration.getParams().setDefaults(this.params);
           method.getParams().setDefaults(this.hostConfiguration.getParams());
  +        
  +        // Generate default request headers
  +        Collection defaults = (Collection)this.hostConfiguration.getParams().
  +			getParameter(HostParams.DEFAULT_HEADERS);
  +        if (defaults != null) {
  +        	Iterator i = defaults.iterator();
  +        	while (i.hasNext()) {
  +        		method.addRequestHeader((Header)i.next());
  +        	}
  +        }
  +        
           try {
               int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS,
100);
   
  
  
  
  1.3       +13 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HostParams.java
  
  Index: HostParams.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HostParams.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HostParams.java	13 May 2004 04:01:22 -0000	1.2
  +++ HostParams.java	15 Sep 2004 20:42:17 -0000	1.3
  @@ -51,6 +51,15 @@
       private static final Log LOG = LogFactory.getLog(HttpParams.class);
   
       /**
  +     * Defines the request headers to be sent per default with each request.
  +     * <p>
  +     * This parameter expects a value of type {@link java.util.Collection}. The 
  +     * collection is expected to contain {@link Header}s. 
  +     * </p>
  +     */
  +    public static final String DEFAULT_HEADERS = "http.default-headers"; 
  +
  +    /**
        * Creates a new collection of parameters with the collection returned
        * by {@link #getDefaultParams()} as a parent. The collection will defer
        * to its parent for a default value if a particular parameter is not 
  
  
  
  1.39      +6 -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.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- TestNoHost.java	11 May 2004 20:43:55 -0000	1.38
  +++ TestNoHost.java	15 Sep 2004 20:42:17 -0000	1.39
  @@ -38,6 +38,7 @@
   import org.apache.commons.httpclient.auth.TestChallengeParser;
   import org.apache.commons.httpclient.auth.TestChallengeProcessor;
   import org.apache.commons.httpclient.cookie.TestCookieAll;
  +import org.apache.commons.httpclient.params.TestHttpParams;
   
   /**
    * Tests that don't require any external host.
  @@ -90,6 +91,7 @@
           suite.addTest(TestEquals.suite());
           suite.addTestSuite(TestIdleConnectionTimeout.class);
           suite.addTest(TestMethodAbort.suite());
  +        suite.addTest(TestHttpParams.suite());
           return suite;
       }
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java
  
  Index: TestHttpParams.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java,v
1.1 2004/09/15 20:42:17 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/15 20:42:17 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * 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.params;
  
  import java.io.IOException;
  import java.util.ArrayList;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.HttpClientTestBase;
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.params.HostParams;
  import org.apache.commons.httpclient.protocol.Protocol;
  import org.apache.commons.httpclient.server.HttpService;
  import org.apache.commons.httpclient.server.SimpleRequest;
  import org.apache.commons.httpclient.server.SimpleResponse;
  
  /**
   * HTTP preference framework tests.
   *
   * @author Oleg Kalnichevski
   * 
   * @version $Revision: 1.1 $
   */
  public class TestHttpParams extends HttpClientTestBase {
  
      // ------------------------------------------------------------ Constructor
      public TestHttpParams(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = { TestHttpParams.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestHttpParams.class);
      }
  
      private class SimpleService implements HttpService {
  
          public SimpleService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse response)
              throws IOException
          {
              String uri = request.getRequestLine().getUri();  
          	HttpVersion httpversion = request.getRequestLine().getHttpVersion();
          	
          	if ("/miss/".equals(uri)) {
                  response.setStatusLine(httpversion, HttpStatus.SC_MOVED_TEMPORARILY);
                  response.addHeader(new Header("Location", "/hit/"));
                  response.setBodyString("Missed!");
          	} else if ("/hit/".equals(uri)) {
                  response.setStatusLine(httpversion, HttpStatus.SC_OK);
                  response.setBodyString("Hit!");
          	} else {
                  response.setStatusLine(httpversion, HttpStatus.SC_NOT_FOUND);
                  response.setBodyString(uri + " not found");
          	}
              return true;
          }
      }
  
      public void testDefaultHeaders() throws IOException {
          this.server.setHttpService(new SimpleService());
  
          ArrayList defaults = new ArrayList();
          defaults.add(new Header("this-header", "value1"));
          defaults.add(new Header("that-header", "value1"));
          defaults.add(new Header("that-header", "value2"));
          defaults.add(new Header("User-Agent", "test"));
  
          HostConfiguration hostconfig = new HostConfiguration();
          hostconfig.setHost(
                  this.server.getLocalAddress(), 
  	            this.server.getLocalPort(),
  	            Protocol.getProtocol("http"));
          hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults);
          
          GetMethod httpget = new GetMethod("/miss/");
          try {
              this.client.executeMethod(hostconfig, httpget);
          } finally {
              httpget.releaseConnection();
          }
          assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
          Header[] thisheader = httpget.getRequestHeaders("this-header");
          assertEquals(1, thisheader.length);
          Header[] thatheader = httpget.getRequestHeaders("that-header");
          assertEquals(2, thatheader.length);
          assertEquals("test", httpget.getRequestHeader("User-Agent").getValue());
      }
  }
  
  
  

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


Mime
View raw message