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 TestHttpState.java TestNoHost.java
Date Tue, 16 Jul 2002 02:54:30 GMT
jsdever     2002/07/15 19:54:30

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpState.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestHttpState.java
  Log:
  The HttpStatus class has the concept of a default credential.  The
  handling of it is inconsistant however.
  
  The setCredentials would state that you could use null as the realm to
  flag the default credential.  Thats fine.
  The getCredentials would return null if the specified realm could not be
  found.  Well hey, whats that default for again?  You have to give a null
  realm to get back the "default".  This makes no sense, and forced user
  code to call getCredentials twice to get a credential, once with a
  realm, and if that fails with a null realm.
  
  This patch addresses this inconsistancy.
  
  A new test class was added (attached seperately) as there were no
  HttpState tests already present.
  
  Patch by Jeff Dever
  
  Revision  Changes    Path
  1.7       +45 -35    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java
  
  Index: HttpState.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpState.java	10 Jul 2002 02:53:31 -0000	1.6
  +++ HttpState.java	16 Jul 2002 02:54:30 -0000	1.7
  @@ -80,6 +80,7 @@
    * </p>
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    * @author Rodney Waldhoff
  + * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
    * @version $Revision$ $Date$
    */
   public class HttpState {
  @@ -197,17 +198,18 @@
           return removed;
       }
   
  +
       /**
  -     * <p>
  -     * Set the {@link Credentials} for the given authentication realm.
  -     * </p>
  -     * <p>
  +     * Set the Credentials for the given authentication realm.
  +     *
        * When <i>realm</i> is <code>null</code>, I'll use the given
        * <i>credentials</i> when no other {@link Credentials} have
        * been supplied for the given challenging realm.
        * (I.e., use a <code>null</code> realm to set the "default"
        * credentials.)
  -     * </p>
  +     * <p>
  +     * Any previous credentials for this realm will be overwritten.
  +     *
        * @param realm the authentication realm
        * @param credentials the authentication credentials for the given realm
        */
  @@ -217,33 +219,37 @@
   
   
       /**
  -     * <p>
  -     * Get the {@link Credentials} for the given authentication realm.
  -     * </p>
  -     * <p>
  -     * When <i>realm</i> is <code>null</code>, I'll return the
  -     * "default" credentials.
  -     * (See {@link #setCredentials setCredentials}.)
  -     * </p>
  +     * Get the Credentials for the given authentication realm.
  +     *
  +     * If the <i>realm</i> exists, return the coresponding credentials.  If
the 
  +     * <i>realm</i> does not exist, return the default Credentials.  If there
is 
  +     * no default credentials, return <code>null</code>.
  +     *
        * @param realm the authentication realm
  +     * @return the credentials 
  +     * @see #setCredentials
        */
       public Credentials getCredentials(String realm) {
  -        return (Credentials)(credMap.get(realm));
  +        Credentials creds = (Credentials) credMap.get(realm);
  +	if (creds == null) {
  +	   creds = (Credentials) credMap.get(null);
  +	}
  +	return creds;
       }
   
  +
       /**
  -     * <p>
  -     * Set the {@link Credentials} for the proxy with the given
  -     * authentication realm.
  -     * </p>
  -     * <p>
  +     * Set the for the proxy with the given authentication realm.
  +     *
        * When <i>realm</i> is <code>null</code>, I'll use the given
        * <i>credentials</i> when no other {@link Credentials} have
        * been supplied for the given challenging realm.
        * (I.e., use a <code>null</code> realm to set the "default"
        * credentials.) Realms rarely make much sense with proxies, so
        * <code>null</code> is normally a good choice here.
  -     * </p>
  +     * <p>
  +     * Any previous credentials for this realm will be overwritten.
  +     *
        * @param realm the authentication realm
        * @param credentials the authentication credentials for the given realm
        */
  @@ -251,20 +257,24 @@
           proxyCred.put(realm, credentials);
       }
   
  +
       /**
  -     * <p>
  -     * Get the {@link Credentials} for the proxy with the given authentication
  -     * realm.
  -     * </p>
  -     * <p>
  -     * When <i>realm</i> is <code>null</code>, I'll return the
  -     * "default" credentials.
  -     * (See {@link #setCredentials setCredentials}.)
  -     * </p>
  +     * Get the Credentials for the proxy with the given authentication realm.
  +     *
  +     * If the <i>realm</i> exists, return the coresponding credentials.  If
the 
  +     * <i>realm</i> does not exist, return the default Credentials.  If there
is 
  +     * no default credentials, return <code>null</code>.
  +     *
        * @param realm the authentication realm
  -     * @return
  +     * @return the credentials 
  +     * @see #setProxyCredentials
        */
       public Credentials getProxyCredentials(String realm) {
  -        return (Credentials) (proxyCred.get(realm));
  +        Credentials creds = (Credentials) proxyCred.get(realm);
  +	if (creds == null) {
  +	   creds = (Credentials) proxyCred.get(null);
  +	}
  +	return creds;
       }
  +
   }
  
  
  
  1.8       +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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestNoHost.java	24 Apr 2002 14:44:50 -0000	1.7
  +++ TestNoHost.java	16 Jul 2002 02:54:30 -0000	1.8
  @@ -71,6 +71,7 @@
    * (True unit tests, by some definitions.)
    *
    * @author Rodney Waldhoff
  + * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
    * @version $Revision$ $Date$
    */
   public class TestNoHost extends TestCase {
  @@ -93,6 +94,7 @@
           suite.addTest(TestHttpConnectionManager.suite());
           suite.addTest(TestURIUtil.suite());
           suite.addTest(TestMethodsNoHost.suite());
  +        suite.addTest(TestHttpState.suite());
           return suite;
       }
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java
  
  Index: TestHttpState.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java,v
1.1 2002/07/16 02:54:30 jsdever Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/16 02:54:30 $
   * ====================================================================
   *
   * 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.*;
  
  /**
   * Simple tests for {@link NameValuePair}.
   *
   * @author Rodney Waldhoff
   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
   * @version $Id: TestHttpState.java,v 1.1 2002/07/16 02:54:30 jsdever Exp $
   */
  public class TestHttpState extends TestCase {
  
      public Credentials creds1 = new UsernamePasswordCredentials("user1", "pass1");
      public Credentials creds2 = new UsernamePasswordCredentials("user2", "pass2");
  
      public String realm1 = "realm1";
      public String realm2 = "realm2";
  
  
      // ------------------------------------------------------------ Constructor
      public TestHttpState(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = { TestHttpState.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestHttpState.class);
      }
  
  
      // ----------------------------------------------------------- Test Methods
  
      public void testHttpStateCredentials() {
          HttpState state = new HttpState();
  	state.setCredentials(realm1, creds1);
  	state.setCredentials(realm2, creds2);
          assertEquals(creds1, state.getCredentials(realm1));
          assertEquals(creds2, state.getCredentials(realm2));
      }
  
      public void testHttpStateNoCredentials() {
          HttpState state = new HttpState();
          assertEquals(null, state.getCredentials("bogus"));
      }
  
      public void testHttpStateDefaultCredentials() {
          HttpState state = new HttpState();
  	state.setCredentials(null, creds1);
  	state.setCredentials(realm2, creds2);
          assertEquals(creds1, state.getCredentials("bogus"));
      }
  
  
      public void testHttpStateProxyCredentials() {
          HttpState state = new HttpState();
  	state.setProxyCredentials(realm1, creds1);
  	state.setProxyCredentials(realm2, creds2);
          assertEquals(creds1, state.getProxyCredentials(realm1));
          assertEquals(creds2, state.getProxyCredentials(realm2));
      }
  
      public void testHttpStateProxyNoCredentials() {
          HttpState state = new HttpState();
          assertEquals(null, state.getProxyCredentials("bogus"));
      }
  
      public void testHttpStateProxyDefaultCredentials() {
          HttpState state = new HttpState();
  	state.setProxyCredentials(null, creds1);
  	state.setProxyCredentials(realm2, creds2);
          assertEquals(creds1, state.getProxyCredentials("bogus"));
      }
  
  }
  
  
  

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