Author: olegk
Date: Thu Apr 14 11:21:03 2005
New Revision: 161333

URL: http://svn.apache.org/viewcvs?view=rev&rev=161333
Log:
HTTP POST test cases ported to the SimpleHTTPServer based testing tramework

Contributed by Oleg Kalnichevski

Added:
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java
      - copied, changed from r161164, 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostMethod.java
Removed:
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostMethod.java
Modified:
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestLocalHostBase.java
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoncompliant.java
    
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java

Modified: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestLocalHostBase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestLocalHostBase.java?view=diff&r1=161332&r2=161333
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestLocalHostBase.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestLocalHostBase.java
 Thu Apr 14 11:21:03 2005
@@ -36,6 +36,8 @@
  * web server.
  * 
  * @author Michael Becke
+ * 
+ * @deprecated
  */
 public abstract class TestLocalHostBase extends TestCase {
 

Modified: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java?view=diff&r1=161332&r2=161333
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
 Thu Apr 14 11:21:03 2005
@@ -94,7 +94,8 @@
         suite.addTest(TestURIUtil.suite());
         suite.addTest(TestURIUtil2.suite());
         // Method specific
-        suite.addTest(TestPostMethod.suite());
+        suite.addTest(TestEntityEnclosingMethod.suite());
+        suite.addTest(TestPostParameterEncoding.suite());
         suite.addTest(TestPartsNoHost.suite());
         suite.addTest(TestMultipartPost.suite());
         // Non compliant behaviour

Modified: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoncompliant.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoncompliant.java?view=diff&r1=161332&r2=161333
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoncompliant.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoncompliant.java
 Thu Apr 14 11:21:03 2005
@@ -178,6 +178,7 @@
         this.server.setRequestHandler(new HttpRequestHandler() {
             public boolean processRequest(SimpleHttpServerConnection conn,
                     SimpleRequest request) throws IOException {
+                conn.setSocketTimeout(20000);
                 ResponseWriter out = conn.getWriter();
                 out.println("HTTP/1.1 304 OK");
                 out.println("Connection: keep-alive");
@@ -185,7 +186,6 @@
                 out.println();
                 out.flush();
                 conn.setKeepAlive(true);
-                conn.setSocketTimeout(20000);
                 return true;
             }
         });
@@ -201,6 +201,7 @@
         this.server.setRequestHandler(new HttpRequestHandler() {
             public boolean processRequest(SimpleHttpServerConnection conn,
                     SimpleRequest request) throws IOException {
+                conn.setSocketTimeout(20000);
                 ResponseWriter out = conn.getWriter();
                 out.println("HTTP/1.1 204 OK");
                 out.println("Connection: close");
@@ -208,7 +209,6 @@
                 out.println();
                 out.flush();
                 conn.setKeepAlive(true);
-                conn.setSocketTimeout(20000);
                 return true;
             }
         });

Copied: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java
 (from r161164, 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostMethod.java)
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java?view=diff&rev=161333&p1=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostMethod.java&r1=161164&p2=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java&r2=161333
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostMethod.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestPostParameterEncoding.java
 Thu Apr 14 11:21:03 2005
@@ -47,7 +47,7 @@
  * 
  * @version $Id$
  */
-public class TestPostMethod extends TestCase {
+public class TestPostParameterEncoding extends TestCase {
 
     static final String NAME = "name", VALUE = "value";
     static final String NAME0 = "name0", VALUE0 = "value0";
@@ -59,16 +59,16 @@
     static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
     static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
 
-    public TestPostMethod(final String testName) throws IOException {
+    public TestPostParameterEncoding(final String testName) throws IOException 
{
         super(testName);
     }
 
     public static Test suite() {
-        return new TestSuite(TestPostMethod.class);
+        return new TestSuite(TestPostParameterEncoding.class);
     }
 
     public static void main(String args[]) {
-        String[] testCaseName = { TestPostMethod.class.getName() };
+        String[] testCaseName = { TestPostParameterEncoding.class.getName() };
         junit.textui.TestRunner.main(testCaseName);
     }
     

Modified: 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java?view=diff&r1=161332&r2=161333
==============================================================================
--- 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java
 (original)
+++ 
jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java
 Thu Apr 14 11:21:03 2005
@@ -25,8 +25,6 @@
  * 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;
@@ -41,13 +39,9 @@
  * @author <a href="[EMAIL PROTECTED]">Jeff Dever</a>
  * @version $Id$
  */
-public class TestWebappPostMethod extends TestWebappBase {
-
-    HttpClient httpClient; 
-    final String paramsPath = "/" + getWebappContext() + "/params";
-    final String bodyPath = "/" + getWebappContext() + "/body";
+public class TestWebappPostMethod extends HttpClientTestBase {
 
-    public TestWebappPostMethod(String testName) {
+    public TestWebappPostMethod(String testName) throws IOException {
         super(testName);
     }
 
@@ -61,115 +55,117 @@
         junit.textui.TestRunner.main(testCaseName);
     }
 
-    public void setUp() {
-        httpClient = createHttpClient();
-    }
-
-    /**
-     * Helper method for performing a routine test.
-     */
-    private void verifyBody(PostMethod method) throws Exception {
-        httpClient.executeMethod(method);
-
-        assertEquals(200,method.getStatusCode());
-        String body = method.getResponseBodyAsString();
-        //System.out.println(body);
-        assertTrue(body.indexOf("Body Servlet: POST") >= 0);
-        assertTrue(body.indexOf("pname1=pvalue1&pname2=pvalue2") >= 0);
-    }
-
-
-    /**
-     * Helper method for performing a routine test.
-     */
-    private void verifyParams(PostMethod method) throws Exception {
-        httpClient.executeMethod(method);
-
-        assertEquals(200,method.getStatusCode());
-        String body = method.getResponseBodyAsString();
-        //System.out.println(body);
-        assertTrue(body.indexOf("Param Servlet: POST") >= 0);
-        assertTrue(body.indexOf("QueryString=null") >= 0);
-        assertTrue(body.indexOf("name=\"pname1\";value=\"pvalue1\"") >= 0);
-        assertTrue(body.indexOf("name=\"pname2\";value=\"pvalue2\"") >= 0);
-    }
-
-
     // ------------------------------------------------------------------ Tests
     
     /**
-     * Test that the body can be set as a array or parameters the param 
servlet.
+     * Test that the body can be set as a array of parameters
      */
     public void testParametersBodyToParamServlet() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
         NameValuePair[] parametersBody =  new NameValuePair[] { 
             new NameValuePair("pname1","pvalue1"),
             new NameValuePair("pname2","pvalue2") 
         };
-
         method.setRequestBody(parametersBody);
-
-        verifyParams(method);
+        this.server.setHttpService(new EchoService());
+        try {
+            this.client.executeMethod(method);
+            assertEquals(200, method.getStatusCode());
+            String body = method.getResponseBodyAsString();
+            assertEquals("pname1=pvalue1&pname2=pvalue2", body);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
-     * Test that the body can be set as a String to the param servlet.
+     * Test that the body can be set as a String
      */
     public void testStringBodyToParamServlet() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
         String stringBody = "pname1=pvalue1&pname2=pvalue2";
-
         method.setRequestEntity(
             new StringRequestEntity(stringBody, 
PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
-        
-        verifyParams(method);
+        this.server.setHttpService(new EchoService());
+        try {
+            this.client.executeMethod(method);
+            assertEquals(200, method.getStatusCode());
+            String body = method.getResponseBodyAsString();
+            assertEquals("pname1=pvalue1&pname2=pvalue2", body);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
-     * Test that the body can be set as a String to the body servlet.
+     * Test that the body can be set as a String without an explict 
+     * content type
      */
     public void testStringBodyToBodyServlet() throws Exception {
-        PostMethod method = new PostMethod(bodyPath);
+        PostMethod method = new PostMethod("/");
         String stringBody = "pname1=pvalue1&pname2=pvalue2";
 
         method.setRequestEntity(new StringRequestEntity(stringBody));
-        
-        verifyBody(method);
+        this.server.setHttpService(new EchoService());
+        try {
+            this.client.executeMethod(method);
+            assertEquals(200, method.getStatusCode());
+            String body = method.getResponseBodyAsString();
+            assertEquals("pname1=pvalue1&pname2=pvalue2", body);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
      * Test that parameters can be added.
      */
     public void testAddParametersToParamServlet() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
 
         method.addParameter(new NameValuePair("pname1","pvalue1"));
         method.addParameter(new NameValuePair("pname2","pvalue2"));
 
-        verifyParams(method);
+        this.server.setHttpService(new EchoService());
+        try {
+            this.client.executeMethod(method);
+            assertEquals(200, method.getStatusCode());
+            String body = method.getResponseBodyAsString();
+            assertEquals("pname1=pvalue1&pname2=pvalue2", body);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
      * Test that parameters can be added and removed.
      */
     public void testAddRemoveParametersToParamServlet() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
 
         method.addParameter(new NameValuePair("pname0","pvalue0"));
         method.addParameter(new NameValuePair("pname1","pvalue1"));
         method.addParameter(new NameValuePair("pname2","pvalue2"));
         method.addParameter(new NameValuePair("pname3","pvalue3"));
         method.removeParameter("pname0");
-        method.removeParameter("pname4");
+        method.removeParameter("pname3");
 
-        verifyParams(method);
+        this.server.setHttpService(new EchoService());
+        try {
+            this.client.executeMethod(method);
+            assertEquals(200, method.getStatusCode());
+            String body = method.getResponseBodyAsString();
+            assertEquals("pname1=pvalue1&pname2=pvalue2", body);
+        } finally {
+            method.releaseConnection();
+        }
     }
 
     /**
      * Test the return value of the PostMethod#removeParameter.
      */
     public void testRemoveParameterReturnValue() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
 
         method.addParameter("param", "whatever");
         assertTrue("Return value of the method is expected to be true", 
method.removeParameter("param"));
@@ -186,7 +182,7 @@
      * Test if setParameter overwrites existing parameter values.
      */
     public void testAddParameterFollowedBySetParameter() throws Exception {
-        PostMethod method = new PostMethod(paramsPath);
+        PostMethod method = new PostMethod("/");
 
         method.addParameter("param", "a");
         method.addParameter("param", "b");



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

Reply via email to