olegk       2004/10/07 09:14:16

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpClient.java HttpMethod.java HttpMethodBase.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestMethodCharEncoding.java TestNoHost.java
                        TestRequestHeaders.java TestRequestLine.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        SimpleHttpServer.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestHttpMethodFundamentals.java TestPostMethod.java
  Removed:     httpclient/src/test/org/apache/commons/httpclient
                        TestHttpUrlMethod.java TestMethods.java
                        TestMethodsNoHost.java
  Log:
  Changelog:
  
  * Minor corrective patch to the PR #31471
  * Another round of test cases clean up
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.98      +7 -6      
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java
  
  Index: HttpClient.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
  retrieving revision 1.97
  retrieving revision 1.98
  diff -u -r1.97 -r1.98
  --- HttpClient.java   6 Oct 2004 17:32:03 -0000       1.97
  +++ HttpClient.java   7 Oct 2004 16:14:15 -0000       1.98
  @@ -379,8 +379,9 @@
           if (hostConfiguration == null || hostConfiguration == defaulthostconfig) {
               // make a deep copy of the host defaults
               hostConfiguration = new HostConfiguration(defaulthostconfig);
  -            if (method.getHost() != null) {
  -                hostConfiguration.setHost(method.getHost());
  +            URI uri = method.getURI(); 
  +            if (uri.isAbsoluteURI()) {
  +                hostConfiguration.setHost(uri);
               }
           }
           
  
  
  
  1.43      +4 -11     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java
  
  Index: HttpMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- HttpMethod.java   6 Oct 2004 17:32:03 -0000       1.42
  +++ HttpMethod.java   7 Oct 2004 16:14:15 -0000       1.43
  @@ -74,13 +74,6 @@
       HostConfiguration getHostConfiguration();
   
       /**
  -     * Gets the target host for this method. 
  -     * 
  -     * @return the [EMAIL PROTECTED] HttpHost} or <code>null</code> if none is set
  -     */
  -    HttpHost getHost();
  -
  -    /**
        * Sets the path of the HTTP method.
        * It is responsibility of the caller to ensure that the path is
        * properly encoded (URL safe).
  
  
  
  1.218     +4 -8      
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.217
  retrieving revision 1.218
  diff -u -r1.217 -r1.218
  --- HttpMethodBase.java       6 Oct 2004 17:32:03 -0000       1.217
  +++ HttpMethodBase.java       7 Oct 2004 16:14:15 -0000       1.218
  @@ -2277,10 +2277,6 @@
           }
       }
   
  -    public HttpHost getHost() {
  -        return this.httphost;
  -    }
  -    
       /**
        * Returns the [EMAIL PROTECTED] HostConfiguration host configuration}.
        * 
  
  
  
  1.11      +25 -33    
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java
  
  Index: TestMethodCharEncoding.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TestMethodCharEncoding.java       3 Jul 2004 14:27:03 -0000       1.10
  +++ TestMethodCharEncoding.java       7 Oct 2004 16:14:15 -0000       1.11
  @@ -28,7 +28,6 @@
   package org.apache.commons.httpclient;
   
   import junit.framework.Test;
  -import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
   import java.io.ByteArrayInputStream;
  @@ -51,7 +50,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Laura Werner</a>
    */
   
  -public class TestMethodCharEncoding extends TestCase {
  +public class TestMethodCharEncoding extends HttpClientTestBase {
   
       static final String CHARSET_DEFAULT = "ISO-8859-1";
       static final String CHARSET_ASCII = "US-ASCII";
  @@ -119,40 +118,33 @@
           
       }
   
  -    public void testResponseCharEncoding() throws Exception {
  +    public void testNoExplicitCharEncoding() throws Exception {
  +        this.server.setHttpService(new EchoService());
           
  -        SimpleHttpConnection conn = new SimpleHttpConnection();
  -        String body = "stuff";
  -        String headers1 = "HTTP/1.1 200 OK\r\n"
  -                       +"Content-Length: 5\r\n";
  -        conn.addResponse(headers1, body);
  -        conn.open();
  -        GetMethod httpget = new GetMethod("/");
  -        httpget.execute(new HttpState(), conn);
  -        assertEquals(CHARSET_DEFAULT, httpget.getResponseCharSet());
  -        conn.close();
  -        
  -        httpget = new GetMethod("/");
  -        String headers2 = "HTTP/1.1 200 OK\r\n"
  -                       +"Content-Type: text/plain\r\n"
  -                       +"Content-Length: 5\r\n";
  -        conn.addResponse(headers2, body);
  -        conn.open();
  -        httpget.execute(new HttpState(), conn);
  -        assertEquals(CHARSET_DEFAULT, httpget.getResponseCharSet());
  -        conn.close();
  -
  -        httpget = new GetMethod("/");
  -        String headers3 = "HTTP/1.1 200 OK\r\n"
  -                       +"Content-Type: text/plain; charset=" + CHARSET_UTF8 + "\r\n"
  -                       +"Content-Length: 5\r\n";
  -        conn.addResponse(headers3, body);
  -        conn.open();
  -        httpget.execute(new HttpState(), conn);
  -        assertEquals(CHARSET_UTF8, httpget.getResponseCharSet());
  -        conn.close();
  +        GetMethod httpget = new GetMethod("/test/");
  +        httpget.setRequestHeader("Content-Type", "text/plain"); 
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
  +            assertEquals(CHARSET_DEFAULT, httpget.getResponseCharSet());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
       }
   
  +    public void testExplicitCharEncoding() throws Exception {
  +        this.server.setHttpService(new EchoService());
  +        
  +        GetMethod httpget = new GetMethod("/test/");
  +        httpget.setRequestHeader("Content-Type", "text/plain; charset=" + 
CHARSET_UTF8); 
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
  +            assertEquals(CHARSET_UTF8, httpget.getResponseCharSet());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
   
       private String constructString(int [] unicodeChars) {
           StringBuffer buffer = new StringBuffer();
  
  
  
  1.43      +32 -26    
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.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- TestNoHost.java   6 Oct 2004 17:32:04 -0000       1.42
  +++ TestNoHost.java   7 Oct 2004 16:14:15 -0000       1.43
  @@ -23,9 +23,6 @@
    * 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;
  @@ -58,40 +55,49 @@
   
       public static Test suite() {
           TestSuite suite = new TestSuite();
  +        // Fundamentals
  +        suite.addTest(TestHttpMethodFundamentals.suite());
           suite.addTest(TestHttpStatus.suite());
  -        suite.addTest(TestCookieAll.suite());
  -        suite.addTest(TestNVP.suite());
  +        suite.addTest(TestStatusLine.suite());
  +        suite.addTest(TestRequestLine.suite());
           suite.addTest(TestHeader.suite());
  -        suite.addTest(TestParameterParser.suite());
           suite.addTest(TestHeaderElement.suite());
  -        suite.addTest(TestChallengeParser.suite());
  -        suite.addTest(TestChallengeProcessor.suite());
  -        suite.addTest(TestAuthenticator.suite());
  -        suite.addTest(TestBasicAuth.suite());
  -        suite.addTest(TestRedirects.suite());
  -        suite.addTest(TestHttpUrlMethod.suite());
  -        suite.addTest(TestURI.suite());
  -        suite.addTest(TestURIUtil.suite());
  -        suite.addTest(TestURIUtil2.suite());
  -        suite.addTest(TestMethodsNoHost.suite());
  -        suite.addTest(TestHttpState.suite());
           suite.addTest(TestResponseHeaders.suite());
           suite.addTest(TestRequestHeaders.suite());
           suite.addTest(TestStreams.suite());
  -        suite.addTest(TestStatusLine.suite());
  -        suite.addTest(TestRequestLine.suite());
  -        suite.addTest(TestPartsNoHost.suite());
  +        suite.addTest(TestParameterParser.suite());
  +        suite.addTest(TestNVP.suite());
           suite.addTest(TestMethodCharEncoding.suite());
  -        suite.addTest(TestExceptions.suite());
           suite.addTest(TestHttpVersion.suite());
           suite.addTest(TestEffectiveHttpVersion.suite());
           suite.addTest(TestHttpParser.suite());
           suite.addTest(TestBadContentLength.suite());
           suite.addTest(TestEquals.suite());
  +        // Exceptions
  +        suite.addTest(TestExceptions.suite());        
  +        // HTTP state management
  +        suite.addTest(TestHttpState.suite());
  +        suite.addTest(TestCookieAll.suite());
  +        // Authentication 
  +        suite.addTest(TestChallengeParser.suite());
  +        suite.addTest(TestChallengeProcessor.suite());
  +        suite.addTest(TestAuthenticator.suite());
  +        suite.addTest(TestBasicAuth.suite());
  +        // Redirects
  +        suite.addTest(TestRedirects.suite());
  +        // Connection management
           suite.addTestSuite(TestIdleConnectionTimeout.class);
           suite.addTest(TestMethodAbort.suite());
  +        // Preferences
           suite.addTest(TestHttpParams.suite());
  -        suite.addTest(TestVirtualHost.suite());
  +        suite.addTest(TestVirtualHost.suite());        
  +        // URIs
  +        suite.addTest(TestURI.suite());
  +        suite.addTest(TestURIUtil.suite());
  +        suite.addTest(TestURIUtil2.suite());
  +        // Method specific
  +        suite.addTest(TestPostMethod.suite());
  +        suite.addTest(TestPartsNoHost.suite());
           suite.addTest(TestMultipartPost.suite());
           return suite;
       }
  
  
  
  1.7       +32 -35    
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestHeaders.java
  
  Index: TestRequestHeaders.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestHeaders.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestRequestHeaders.java   22 Feb 2004 18:08:49 -0000      1.6
  +++ TestRequestHeaders.java   7 Oct 2004 16:14:15 -0000       1.7
  @@ -30,6 +30,8 @@
   
   package org.apache.commons.httpclient;
   
  +import org.apache.commons.httpclient.protocol.Protocol;
  +
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  @@ -42,10 +44,6 @@
    */
   public class TestRequestHeaders extends TestCase {
   
  -       HttpState state = null;
  -       SimpleHttpMethod method = null;
  -       HttpConnection conn = null;
  -
       // ------------------------------------------------------------ Constructor
       public TestRequestHeaders(String testName) {
           super(testName);
  @@ -62,49 +60,48 @@
           return new TestSuite(TestRequestHeaders.class);
       }
   
  -    public void setUp() {
  -       state = new HttpState();
  -       method = new SimpleHttpMethod("/some/absolute/path/");
  -       //assign conn in test case
  -    }
  -
  -    public void tearDown() {
  -       state = null;
  -       method = null;
  -       conn = null;
  -    }
  -
       public void testNullHeader() throws Exception {
  -       conn = new SimpleHttpConnection("some.host.name", 80, "http");
  -       assertEquals(null, method.getRequestHeader(null));
  -       assertEquals(null, method.getRequestHeader("bogus"));
  +        SimpleHttpMethod method = new SimpleHttpMethod();
  +        assertEquals(null, method.getRequestHeader(null));
  +        assertEquals(null, method.getRequestHeader("bogus"));
       }
   
       public void testHostHeaderPortHTTP80() throws Exception {
  -       conn = new SimpleHttpConnection("some.host.name", 80, "http");
  -       method.addRequestHeaders(state, conn);
  -       assertEquals("Host: some.host.name", 
method.getRequestHeader("Host").toString().trim());
  +        HttpConnection conn = new HttpConnection("some.host.name", 80);
  +        HttpState state = new HttpState();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
  +        method.addRequestHeaders(state, conn);
  +        assertEquals("Host: some.host.name", 
method.getRequestHeader("Host").toString().trim());
       }
   
       public void testHostHeaderPortHTTP81() throws Exception {
  -       conn = new SimpleHttpConnection("some.host.name", 81, "http");
  -       method.addRequestHeaders(state, conn);
  -       assertEquals("Host: some.host.name:81", 
method.getRequestHeader("Host").toString().trim());
  +        HttpConnection conn = new HttpConnection("some.host.name", 81);
  +        HttpState state = new HttpState();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
  +        method.addRequestHeaders(state, conn);
  +        assertEquals("Host: some.host.name:81", 
method.getRequestHeader("Host").toString().trim());
       }
   
       public void testHostHeaderPortHTTPS443() throws Exception {
  -       conn = new SimpleHttpConnection("some.host.name", 443, "https");
  -       method.addRequestHeaders(state, conn);
  -       assertEquals("Host: some.host.name", 
method.getRequestHeader("Host").toString().trim());
  +        HttpConnection conn = new HttpConnection("some.host.name", 443, 
  +                Protocol.getProtocol("https"));
  +        HttpState state = new HttpState();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
  +        method.addRequestHeaders(state, conn);
  +        assertEquals("Host: some.host.name", 
method.getRequestHeader("Host").toString().trim());
       }
   
       public void testHostHeaderPortHTTPS444() throws Exception {
  -       conn = new SimpleHttpConnection("some.host.name", 444, "https");
  -       method.addRequestHeaders(state, conn);
  -       assertEquals("Host: some.host.name:444", 
method.getRequestHeader("Host").toString().trim());
  +        HttpConnection conn = new HttpConnection("some.host.name", 444, 
  +                Protocol.getProtocol("https"));
  +        HttpState state = new HttpState();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
  +        method.addRequestHeaders(state, conn);
  +        assertEquals("Host: some.host.name:444", 
method.getRequestHeader("Host").toString().trim());
       }
   
       public void testHeadersPreserveCaseKeyIgnoresCase() throws Exception {
  +        SimpleHttpMethod method = new SimpleHttpMethod();
           method.addRequestHeader(new Header("NAME", "VALUE"));
           Header upHeader =  method.getRequestHeader("NAME");
           Header loHeader =  method.getRequestHeader("name");
  
  
  
  1.5       +13 -26    
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java
  
  Index: TestRequestLine.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestRequestLine.java      22 Feb 2004 18:08:49 -0000      1.4
  +++ TestRequestLine.java      7 Oct 2004 16:14:15 -0000       1.5
  @@ -20,9 +20,6 @@
    * 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;
  @@ -64,18 +61,14 @@
       // ----------------------------------------------------------- Test Methods
   
       public void testRequestLineGeneral() throws Exception {
  -        SimpleHttpConnection conn = null;
  -        SimpleHttpMethod method = null;
  -                
  -        conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, 
Protocol.getProtocol("http"));
  -
  -        method = new SimpleHttpMethod();
  +        HttpConnection conn = new HttpConnection("localhost", 80);
  +        SimpleHttpMethod method = new SimpleHttpMethod();
           assertEquals("Simple / HTTP/1.1\r\n", method.getTestRequestLine(conn));
   
           method = new SimpleHttpMethod("stuff");
           assertEquals("Simple stuff HTTP/1.1\r\n", method.getTestRequestLine(conn));
   
  -        conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, 80, 
Protocol.getProtocol("http"));
  +        conn = new HttpConnection("proxy", 8080, "localhost", 80, 
Protocol.getProtocol("http"));
   
           method = new SimpleHttpMethod();
           assertEquals("Simple http://localhost/ HTTP/1.1\r\n", 
method.getTestRequestLine(conn));
  @@ -83,7 +76,7 @@
           method = new SimpleHttpMethod("stuff");
           assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", 
method.getTestRequestLine(conn));
   
  -        conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, -1, 
Protocol.getProtocol("http"));
  +        conn = new HttpConnection("proxy", 8080, "localhost", -1, 
Protocol.getProtocol("http"));
   
           method = new SimpleHttpMethod();
           assertEquals("Simple http://localhost/ HTTP/1.1\r\n", 
method.getTestRequestLine(conn));
  @@ -91,7 +84,7 @@
           method = new SimpleHttpMethod("stuff");
           assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", 
method.getTestRequestLine(conn));
   
  -        conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, 666, 
Protocol.getProtocol("http"));
  +        conn = new HttpConnection("proxy", 8080, "localhost", 666, 
Protocol.getProtocol("http"));
   
           method = new SimpleHttpMethod();
           assertEquals("Simple http://localhost:666/ HTTP/1.1\r\n", 
method.getTestRequestLine(conn));
  @@ -101,33 +94,27 @@
       }
   
       public void testRequestLineQuery() throws Exception {
  -        SimpleHttpConnection conn = null;
  -        SimpleHttpMethod method = null;
  -                
  -        conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, 
Protocol.getProtocol("http"));
  +        HttpConnection conn = new HttpConnection("localhost", 80);
   
  -        method = new SimpleHttpMethod();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
           method.setQueryString( new NameValuePair[] {
               new NameValuePair("param1", " !#$%&\'()*+,-./:;<=>[EMAIL 
PROTECTED]|}~"),
               new NameValuePair("param2", "some stuff")
             } );
           assertEquals("Simple 
/?param1=+%21%23%24%25%26%27%28%29*%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D%7E&param2=some+stuff
 HTTP/1.1\r\n", 
  -          method.getTestRequestLine(conn));
  +        method.getTestRequestLine(conn));
       }
   
       public void testRequestLinePath() throws Exception {
  -        SimpleHttpConnection conn = null;
  -        SimpleHttpMethod method = null;
  -                
  -        conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, 
Protocol.getProtocol("http"));
  +        HttpConnection conn = new HttpConnection("localhost", 80);
   
  -        method = new SimpleHttpMethod();
  +        SimpleHttpMethod method = new SimpleHttpMethod();
           method.setPath("/some%20stuff/");
           assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
  -          method.getTestRequestLine(conn));
  +        method.getTestRequestLine(conn));
   
           method = new SimpleHttpMethod("/some%20stuff/");
           assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
  -          method.getTestRequestLine(conn));
  +        method.getTestRequestLine(conn));
       }
   }
  
  
  
  1.1                  
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
  
  Index: TestHttpMethodFundamentals.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java,v
 1.1 2004/10/07 16:14:15 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/10/07 16:14:15 $
   * ====================================================================
   *
   *  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/>.
   */
  
  package org.apache.commons.httpclient;
  
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.Reader;
  
  import org.apache.commons.httpclient.methods.GetMethod;
  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;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Tests basic method functionality.
   *
   * @author Remy Maucherat
   * @author Rodney Waldhoff
   * @author Oleg Kalnichevski
   * 
   * @version $Id: TestHttpMethodFundamentals.java,v 1.1 2004/10/07 16:14:15 olegk Exp 
$
   */
  public class TestHttpMethodFundamentals extends HttpClientTestBase {
  
      public TestHttpMethodFundamentals(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestHttpMethodFundamentals.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestHttpMethodFundamentals.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
      
      class ManyAService implements HttpService {
  
          public ManyAService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse 
response)
              throws IOException
          {
              HttpVersion httpversion = request.getRequestLine().getHttpVersion();
              response.setStatusLine(httpversion, HttpStatus.SC_OK);
              response.addHeader(new Header("Content-Type", "text/plain"));            
              response.addHeader(new Header("Connection", "close"));            
              StringBuffer buffer = new StringBuffer(1024);
              for (int i = 0; i < 1024; i++) {
                  buffer.append('A');
              }
              response.setBodyString(buffer.toString());
              return true;
          }
      }
  
      class SimpleChunkedService implements HttpService {
  
          public SimpleChunkedService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse 
response)
              throws IOException
          {
              HttpVersion httpversion = request.getRequestLine().getHttpVersion();
              response.setStatusLine(httpversion, HttpStatus.SC_OK);
              response.addHeader(new Header("Content-Type", "text/plain"));            
              response.addHeader(new Header("Content-Length", "garbage")); 
              response.addHeader(new Header("Transfer-Encoding", "chunked")); 
              response.addHeader(new Header("Connection", "close"));            
              response.setBodyString("0a\r\n1234567890\r\n3\r\n123\r\n0\r\n");
              return true;
          }
      }
  
      class EmptyResponseService implements HttpService {
  
          public EmptyResponseService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse 
response)
              throws IOException
          {
              HttpVersion httpversion = request.getRequestLine().getHttpVersion();
              response.setStatusLine(httpversion, HttpStatus.SC_OK);
              response.addHeader(new Header("Content-Type", "text/plain"));            
              response.addHeader(new Header("Transfer-Encoding", "chunked")); 
              response.addHeader(new Header("Connection", "close"));            
              response.setBodyString(null);
              return true;
          }
      }
  
      public void testRelativeURLHitWithDefaultHost() throws IOException {
          this.server.setHttpService(new EchoService());
          // Set default host
          this.client.getHostConfiguration().setHost(
                  this.server.getLocalAddress(), 
                  this.server.getLocalPort(),
                  Protocol.getProtocol("http"));
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testRelativeURLHitWithoutDefaultHost() throws IOException {
          this.server.setHttpService(new EchoService());
          // reset default host configuration
          this.client.setHostConfiguration(new HostConfiguration());
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              fail("IllegalArgumentException should have been thrown");
          } catch (IllegalArgumentException expected) { 
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
          this.server.setHttpService(new EchoService());
          // reset default host configuration
          this.client.setHostConfiguration(new HostConfiguration());
          
          GetMethod httpget = new GetMethod("http://"; + 
                  this.server.getLocalAddress() + ":" + this.server.getLocalPort() + 
"/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testAbsoluteURLHitWithDefaultHost() throws IOException {
          this.server.setHttpService(new EchoService());
          // Somewhere out there in pampa
          this.client.getHostConfiguration().setHost(
                  "somewhere.in.pampa", 
                  9999,
                  Protocol.getProtocol("http"));
          
          GetMethod httpget = new GetMethod("http://"; + 
                  this.server.getLocalAddress() + ":" + this.server.getLocalPort() + 
"/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testHttpMethodBasePaths() throws Exception {
          HttpMethod simple = new SimpleHttpMethod();
          String[] paths = {
             "/some/absolute/path",
             "../some/relative/path",
             "/",
             "/some/path/with?query=string"
         };
      
          for (int i=0; i<paths.length; i++){
              simple.setPath(paths[i]);
              assertEquals(paths[i], simple.getPath());
          }
      }
  
      public void testHttpMethodBaseDefaultPath() throws Exception {
          HttpMethod simple = new SimpleHttpMethod();
          assertEquals("/", simple.getPath());
  
          simple.setPath("");
          assertEquals("/", simple.getPath());
  
          simple.setPath(null);
          assertEquals("/", simple.getPath());
      }
  
      public void testHttpMethodBasePathConstructor() throws Exception {
          HttpMethod simple = new SimpleHttpMethod();
          assertEquals("/", simple.getPath());
  
          simple = new SimpleHttpMethod("");
          assertEquals("/", simple.getPath());
  
          simple = new SimpleHttpMethod("/some/path/");
          assertEquals("/some/path/", simple.getPath());
      }
  
      /** 
       * Tests response with a Trasfer-Encoding and Content-Length 
       */
      public void testHttpMethodBaseTEandCL() throws Exception {
          this.server.setHttpService(new SimpleChunkedService());
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
              assertEquals("1234567890123", httpget.getResponseBodyAsString());
              assertTrue(this.client.getHttpConnectionManager() instanceof 
SimpleHttpConnectionManager);
              HttpConnection conn = this.client.getHttpConnectionManager().
                  getConnection(this.client.getHostConfiguration());
              assertNotNull(conn);
              conn.assertNotOpen();
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testConnectionAutoClose() throws Exception {
          this.server.setHttpService(new ManyAService());
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
              Reader response = new 
InputStreamReader(httpget.getResponseBodyAsStream());
              int c;
              while ((c = response.read()) != -1) {
                 assertEquals((int) 'A', c);
              }
              assertTrue(this.client.getHttpConnectionManager() instanceof 
SimpleHttpConnectionManager);
              HttpConnection conn = this.client.getHttpConnectionManager().
                  getConnection(this.client.getHostConfiguration());
              assertNotNull(conn);
              conn.assertNotOpen();
          } finally {
              httpget.releaseConnection();
          }
      }
  
      public void testSetGetQueryString1() {
          HttpMethod method = new GetMethod();
          String qs1 = "name1=value1&name2=value2";
          method.setQueryString(qs1);
          assertEquals(qs1, method.getQueryString());
      }
  
      public void testQueryURIEncoding() {
          HttpMethod method = new 
GetMethod("http://server/servlet?foo=bar&baz=schmoo";);
          assertEquals("foo=bar&baz=schmoo", method.getQueryString());
      }
  
      public void testSetGetQueryString2() {
          HttpMethod method = new GetMethod();
          NameValuePair[] q1 = new NameValuePair[] {
              new NameValuePair("name1", "value1"),
              new NameValuePair("name2", "value2")
          };
          method.setQueryString(q1);
          String qs1 = "name1=value1&name2=value2";
          assertEquals(qs1, method.getQueryString());
      }
  
      /**
       * Make sure that its OK to call releaseConnection if the connection has not 
been.
       */
      public void testReleaseConnection() {
          HttpMethod method = new GetMethod("http://bogus.url/path/";);
          method.releaseConnection();
      }
  
      /** 
       * Tests empty body response
       */
      public void testEmptyBodyAsString() throws Exception {
          this.server.setHttpService(new EmptyResponseService());
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
              String response = httpget.getResponseBodyAsString();
              assertNull(response);
          } finally {
              httpget.releaseConnection();
          }
      }
  
  
      public void testEmptyBodyAsByteArray() throws Exception {
          this.server.setHttpService(new EmptyResponseService());
          
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
              byte[] response = httpget.getResponseBody();
              assertNull(response);
          } finally {
              httpget.releaseConnection();
          }
      }
      
      public void testUrlGetMethodWithPathQuery() {
          GetMethod method = new 
GetMethod("http://www.fubar.com/path1/path2?query=string";);
          try {
              assertEquals(
                  "Get URL",
                  "http://www.fubar.com/path1/path2?query=string";,
                  method.getURI().toString()
              );
          } catch ( URIException e ) {
              fail( "trouble getting URI: " + e );
          }
          assertEquals("Get Path", "/path1/path2", method.getPath());
          assertEquals("Get query string", "query=string", method.getQueryString());
       
      }
  
      public void testUrlGetMethodWithPath() {
          GetMethod method = new GetMethod("http://www.fubar.com/path1/path2";);
          try {
              assertEquals(
                  "Get URL",
                  "http://www.fubar.com/path1/path2";,
                  method.getURI().toString()
              );
          } catch ( URIException e ) {
              fail( "trouble getting URI: " + e );
          }
          assertEquals("Get Path", "/path1/path2", method.getPath());
          assertEquals("Get query string", null, method.getQueryString());
      }
  
      public void testUrlGetMethod() {
          GetMethod method = new GetMethod("http://www.fubar.com/";);
          try {
              assertEquals(
                  "Get URL",
                  "http://www.fubar.com/";,
                  method.getURI().toString()
              );
          } catch ( URIException e ) {
              fail( "trouble getting URI: " + e );
          }
          assertEquals("Get Path", "/", method.getPath());
          assertEquals("Get query string", null, method.getQueryString());
  
      }
      
  
      public void testUrlGetMethodWithInvalidProtocol() {
          try {
              GetMethod method = new GetMethod("crap://www.fubar.com/");
              fail("The use of invalid protocol must have resulted in an 
IllegalStateException");
          }
          catch(IllegalStateException expected) {
          }
      }
  }
  
  
  
  1.1                  
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java
  
  Index: TestPostMethod.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java,v
 1.1 2004/10/07 16:14:15 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/10/07 16:14:15 $
   * ====================================================================
   *
   *  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;
  
  import java.io.ByteArrayOutputStream;
  
  import org.apache.commons.httpclient.methods.PostMethod;
  import org.apache.commons.httpclient.methods.RequestEntity;
  import org.apache.commons.httpclient.methods.StringRequestEntity;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Tests basic method functionality.
   *
   * @author Remy Maucherat
   * @author Rodney Waldhoff
   * 
   * @version $Id: TestPostMethod.java,v 1.1 2004/10/07 16:14:15 olegk Exp $
   */
  public class TestPostMethod extends HttpClientTestBase {
  
      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);
  
      public TestPostMethod(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestPostMethod.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestPostMethod.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
      
      private String getRequestAsString(RequestEntity entity) throws Exception {
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          entity.writeRequest(bos);
          return new String(bos.toByteArray(), "UTF-8");
      }
      
      public void testPostParametersEncoding() throws Exception {
          PostMethod post = new PostMethod();
          post.setRequestBody(new NameValuePair[] { PAIR });
          assertEquals("name=value", getRequestAsString(post.getRequestEntity()));
  
          post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
          assertEquals("name=value&name1=value1&name2=value2", 
              getRequestAsString(post.getRequestEntity()));
  
          post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new 
NameValuePair("hasSpace", "a b c d") });
          assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
              getRequestAsString(post.getRequestEntity()));
  
      }
  
      public void testPostSetRequestBody() throws Exception {
          PostMethod post = new PostMethod("/foo");
          String body = "this+is+the+body";
          post.setRequestEntity(new StringRequestEntity(body));
          assertEquals(body, getRequestAsString(post.getRequestEntity()));
      }
      
  }
  
  
  
  1.7       +7 -7      
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
  
  Index: SimpleHttpServer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SimpleHttpServer.java     11 May 2004 20:43:55 -0000      1.6
  +++ SimpleHttpServer.java     7 Oct 2004 16:14:16 -0000       1.7
  @@ -80,8 +80,8 @@
        */
       public SimpleHttpServer(int port) throws IOException {
           server = new ServerSocket(port);
  -        if(LOG.isInfoEnabled()) {
  -            LOG.info("New SimpleHttpServer on port " + getLocalPort());
  +        if(LOG.isDebugEnabled()) {
  +            LOG.debug("Starting test HTTP server on port " + getLocalPort());
           }
           tg = new ThreadGroup("SimpleHttpServer group");
           t = new Thread(tg, this, "SimpleHttpServer connection handler");
  @@ -137,8 +137,8 @@
           }
   
           stopped = true;
  -        if(LOG.isInfoEnabled()) {
  -            LOG.info("Stopping SimpleHttpServer on port " + getLocalPort());
  +        if(LOG.isDebugEnabled()) {
  +            LOG.debug("Stopping test HTTP server on port " + getLocalPort());
           }
   
           tg.interrupt();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to