Author: olegk
Date: Wed Jun 22 12:46:46 2005
New Revision: 192985

URL: http://svn.apache.org/viewcvs?rev=192985&view=rev
Log:
Ported to the SimpleHttpServer based testing framework

Modified:
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappHeaders.java

Modified: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappHeaders.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappHeaders.java?rev=192985&r1=192984&r2=192985&view=diff
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappHeaders.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappHeaders.java
 Wed Jun 22 12:46:46 2005
@@ -1,5 +1,5 @@
 /*
- * $Header: 
/home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestWebappHeaders.java,v
 1.13 2004/06/13 20:22:19 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -24,41 +24,30 @@
  * 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.IOException;
 import java.net.InetAddress;
+import java.util.Iterator;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 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;
 
 /**
- * This suite of tests depends upon the httpclienttest webapp,
- * which is available in the httpclient/src/test-webapp
- * directory in the CVS tree.
- * <p>
- * The webapp should be deployed in the context "httpclienttest"
- * on a servlet engine running on port 8080 on the localhost
- * (IP 127.0.0.1).
- * <p>
- * You can change the assumed port by setting the
- * "httpclient.test.localPort" property.
- * You can change the assumed host by setting the
- * "httpclient.test.localHost" property.
- * You can change the assumed context by setting the
- * "httpclient.test.webappContext" property.
- *
  * @author Rodney Waldhoff
  * @version $Id$
  */
-public class TestWebappHeaders extends TestWebappBase {
+public class TestWebappHeaders extends HttpClientTestBase {
 
-    public TestWebappHeaders(String testName) {
+    public TestWebappHeaders(String testName) throws Exception {
         super(testName);
     }
 
@@ -74,133 +63,160 @@
 
     // ------------------------------------------------------------------ Tests
 
+    class HeaderDumpService implements HttpService {
+
+        public HeaderDumpService() {
+            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("HeaderSetByServlet", "Yes"));       
     
+
+            StringBuffer buffer = new StringBuffer(); 
+            buffer.append("Request headers: \r\n");
+            for (Iterator i = request.getHeaderIterator(); i.hasNext(); ) {
+                Header header = (Header) i.next();
+                buffer.append("name=\"");
+                buffer.append(header.getName().toLowerCase());
+                buffer.append("\";value=\"");
+                buffer.append(header.getValue());
+                buffer.append("\"\r\n");
+            }
+            response.setBodyString(buffer.toString());
+            return true;
+        }
+    }
+
     /**
      * Test [EMAIL PROTECTED] HttpMethod#addRequestHeader}.
      */
     public void testAddRequestHeader() throws Exception {
-        HttpClient client = createHttpClient();
-        GetMethod method = new GetMethod("/" + getWebappContext() + 
"/headers");
+        this.server.setHttpService(new HeaderDumpService());
+        
+        GetMethod method = new GetMethod("/");
         method.setRequestHeader(new Header("addRequestHeader(Header)","True"));
         method.setRequestHeader("addRequestHeader(String,String)","Also True");
-        
         try {
-            client.executeMethod(method);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
-        }
-        // Tomcat 4 at least converts the header name to lower case
-        
assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(header)\";value=\"True\"<br>")
 >= 0);
-        
assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(string,string)\";value=\"Also
 True\"<br>") >= 0);
+            this.client.executeMethod(method);
+            String s = method.getResponseBodyAsString();
+            
assertTrue(s.indexOf("name=\"addrequestheader(header)\";value=\"True\"") >= 0);
+            
assertTrue(s.indexOf("name=\"addrequestheader(string,string)\";value=\"Also 
True\"") >= 0);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
      * Test [EMAIL PROTECTED] HttpMethod#removeRequestHeader}.
      */
     public void testRemoveRequestHeader() throws Exception {
-        HttpClient client = createHttpClient();
-        GetMethod method = new GetMethod("/" + getWebappContext() + 
"/headers");
+        this.server.setHttpService(new HeaderDumpService());
+        
+        GetMethod method = new GetMethod("/");
         method.setRequestHeader(new Header("XXX-A-HEADER","true"));
         method.removeRequestHeader("XXX-A-HEADER");
         
         try {
-            client.executeMethod(method);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
+            this.client.executeMethod(method);
+            String s = method.getResponseBodyAsString();
+            assertTrue(!(s.indexOf("xxx-a-header") >= 0));
+        } finally {
+            method.releaseConnection();
         }
-        // Tomcat 4 at least converts the header name to lower case
-        assertTrue(!(method.getResponseBodyAsString().indexOf("xxx-a-header") 
>= 0));
     }
 
     /**
-     * Test [EMAIL PROTECTED] HttpMethod#addRequestHeader}.
+     * Test [EMAIL PROTECTED] HttpMethod#setRequestHeader}.
      */
     public void testOverwriteRequestHeader() throws Exception {
-        HttpClient client = createHttpClient();
-        GetMethod method = new GetMethod("/" + getWebappContext() + 
"/headers");
+        this.server.setHttpService(new HeaderDumpService());
+        
+        GetMethod method = new GetMethod("/");
         method.setRequestHeader(new Header("xxx-a-header","one"));
         method.setRequestHeader("XXX-A-HEADER","two");
         
         try {
-            client.executeMethod(method);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
+            this.client.executeMethod(method);
+            String s = method.getResponseBodyAsString();
+            assertTrue(s.indexOf("name=\"xxx-a-header\";value=\"two\"") >= 0);
+        } finally {
+            method.releaseConnection();
         }
-        // Tomcat 4 at least converts the header name to lower case
-        
assertTrue(method.getResponseBodyAsString().indexOf("name=\"xxx-a-header\";value=\"two\"<br>")
 >= 0);
     }
 
     /**
      * Test [EMAIL PROTECTED] HttpMethod#getResponseHeader}.
      */
     public void testGetResponseHeader() throws Exception {
-        HttpClient client = createHttpClient();
-        GetMethod method = new GetMethod("/" + getWebappContext() + 
"/headers");
+        this.server.setHttpService(new HeaderDumpService());
         
+        GetMethod method = new GetMethod("/");
         try {
-            client.executeMethod(method);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
+            this.client.executeMethod(method);
+            Header h = new Header("HeaderSetByServlet","Yes");
+            assertEquals(h, method.getResponseHeader("headersetbyservlet"));
+        } finally {
+            method.releaseConnection();
         }
-        Header h = new Header("HeaderSetByServlet","Yes");
-        assertEquals(h,method.getResponseHeader("headersetbyservlet"));
     }
 
     /**
      * Test [EMAIL PROTECTED] HttpMethodBase.addHostRequestHeader}.
      */
     public void testHostRequestHeader() throws Exception {
-        InetAddress addr = InetAddress.getByName(getHost());
+        this.server.setHttpService(new HeaderDumpService());
+
+        String hostname = this.server.getLocalAddress();
+        int port = this.server.getLocalPort();
+        
+        InetAddress addr = InetAddress.getByName(hostname);
         String ip = addr.getHostAddress();
-        String hostname = addr.getHostName();
 
-        HttpClient client = new HttpClient();
-        GetMethod get = new GetMethod("/" + getWebappContext() + "/");
+        GetMethod get = new GetMethod("/");
 
         // Open connection using IP.  Host header should be sent
         // Note: RFC 2616 is somewhat unclear on whether a host should
         // be sent in this context - however, both Mozilla and IE send
         // the header for an IP address, instead of sending blank.
-        client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
+        this.client.getHostConfiguration().setHost(ip, port);
         try {
-            client.executeMethod(get);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
-        }
-        Header hostHeader = get.getRequestHeader("Host");
-        assertTrue(hostHeader != null);
-        if (getPort() == 80) {
-            // no port information should be in the value
-            assertTrue(hostHeader.getValue().equals(ip));
-        } else {
-            assertTrue(hostHeader.getValue().equals(ip + ":" + getPort()));
+            this.client.executeMethod(get);
+            Header hostHeader = get.getRequestHeader("Host");
+            assertTrue(hostHeader != null);
+            if (port == Protocol.getProtocol("http").getDefaultPort()) {
+                // no port information should be in the value
+                assertTrue(hostHeader.getValue().equals(ip));
+            } else {
+                assertTrue(hostHeader.getValue().equals(ip + ":" + port));
+            }
+        } finally {
+            get.releaseConnection();
         }
 
-        get = new GetMethod("/" + getWebappContext() + "/");
-
+        get = new GetMethod("/");
         // Open connection using Host.  Host header should
         // contain this value (this test will fail if DNS
         // is not available. Additionally, if the port is
         // something other that 80, then the port value
         // should also be present in the header.
-        client.getHostConfiguration().setHost(hostname, getPort(), 
getProtocol());
+        this.client.getHostConfiguration().setHost(hostname, port);
         try {
-            client.executeMethod(get);
-        } catch (Throwable t) {
-            t.printStackTrace();
-            fail("Unable to execute method : " + t.toString());
-        }
-        hostHeader = get.getRequestHeader("Host");
-        assertTrue(hostHeader != null);
-        if (getPort() == 80) {
-            // no port information should be in the value
-            assertTrue(hostHeader.getValue().equals(hostname));
-        } else {
-            assertTrue(hostHeader.getValue().equals(hostname + ":" + 
getPort()));
+            this.client.executeMethod(get);
+            Header hostHeader = get.getRequestHeader("Host");
+            assertTrue(hostHeader != null);
+            if (port == Protocol.getProtocol("http").getDefaultPort()) {
+                // no port information should be in the value
+                assertTrue(hostHeader.getValue().equals(hostname));
+            } else {
+                assertTrue(hostHeader.getValue().equals(hostname + ":" + 
port));
+            }
+        } finally {
+            get.releaseConnection();
         }
     }
 }



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

Reply via email to