Author: olegk Date: Sun Jun 26 06:26:59 2005 New Revision: 201849 URL: http://svn.apache.org/viewcvs?rev=201849&view=rev Log: Remaining TestWebApp based test cases ported to the SimpleHttpServer based test framework
Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebapp.java jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappParameters.java Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java?rev=201849&r1=201848&r2=201849&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java Sun Jun 26 06:26:59 2005 @@ -40,9 +40,13 @@ import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; +import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.server.AuthRequestHandler; import org.apache.commons.httpclient.server.HttpRequestHandlerChain; +import org.apache.commons.httpclient.server.HttpService; import org.apache.commons.httpclient.server.HttpServiceHandler; +import org.apache.commons.httpclient.server.SimpleRequest; +import org.apache.commons.httpclient.server.SimpleResponse; /** * Tests specific to entity enclosing methods. @@ -268,5 +272,84 @@ method.releaseConnection(); } } + + class RequestBodyStatsService implements HttpService { + + public RequestBodyStatsService() { + 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")); + + StringBuffer buffer = new StringBuffer(); + buffer.append("Request bosy stats:\r\n"); + buffer.append("===================\r\n"); + long l = request.getContentLength(); + if (l >= 0) { + buffer.append("Content-Length: "); + buffer.append(l); + buffer.append("\r\n"); + } + Header te = request.getFirstHeader("Transfer-Encoding"); + if (te != null) { + buffer.append("Content-Length: "); + buffer.append(te.getValue()); + buffer.append("\r\n"); + } + byte[] b = request.getBodyBytes(); + if (b.length <= 0) { + buffer.append("No body submitted\r\n"); + } + response.setBodyString(buffer.toString()); + return true; + } + } + + public void testEmptyPostMethod() throws Exception { + this.server.setHttpService(new RequestBodyStatsService()); + + PostMethod method = new PostMethod("/"); + method.setRequestHeader("Content-Type", "text/plain"); + this.client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + String response = method.getResponseBodyAsString(); + assertNotNull(method.getRequestHeader("Content-Length")); + assertTrue(response.indexOf("No body submitted") >= 0); + + method = new PostMethod("/"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestEntity(new StringRequestEntity("")); + this.client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + assertNotNull(method.getRequestHeader("Content-Length")); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method = new PostMethod("/"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setContentChunked(true); + this.client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + assertNotNull(method.getRequestHeader("Content-Length")); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method = new PostMethod("/"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestEntity(new StringRequestEntity("")); + method.setContentChunked(true); + this.client.executeMethod(method); + assertNull(method.getRequestHeader("Content-Length")); + assertNotNull(method.getRequestHeader("Transfer-Encoding")); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + } + } Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebapp.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebapp.java?rev=201849&r1=201848&r2=201849&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebapp.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebapp.java Sun Jun 26 06:26:59 2005 @@ -61,7 +61,6 @@ public static Test suite() { TestSuite suite = new TestSuite(); - suite.addTest(TestWebappMethods.suite()); suite.addTest(TestWebappParameters.suite()); return suite; } Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappParameters.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappParameters.java?rev=201849&r1=201848&r2=201849&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappParameters.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestWebappParameters.java Sun Jun 26 06:26:59 2005 @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestWebappParameters.java,v 1.11 2004/02/22 18:08:50 olegk Exp $ + * $HeadURL$ * $Revision$ * $Date$ * ==================================================================== @@ -24,37 +24,25 @@ * 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 junit.framework.*; import org.apache.commons.httpclient.methods.*; +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 TestWebappParameters extends TestWebappBase { +public class TestWebappParameters extends HttpClientTestBase { - public TestWebappParameters(String testName) { + public TestWebappParameters(String testName) throws Exception { super(testName); } @@ -70,24 +58,46 @@ // ------------------------------------------------------------------ Tests + class QueryInfoService implements HttpService { + + public QueryInfoService() { + 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")); + + URI uri = new URI(request.getRequestLine().getUri(), true); + + StringBuffer buffer = new StringBuffer(); + buffer.append("QueryString=\""); + buffer.append(uri.getQuery()); + buffer.append("\"\r\n"); + response.setBodyString(buffer.toString()); + return true; + } + } + /** * Test that [EMAIL PROTECTED] GetMethod#setQueryString(java.lang.String)} * can include a leading question mark. */ public void testGetMethodQueryString() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString("?hadQuestionMark=true"); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=true\"</p>") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"hadQuestionMark=true\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -95,19 +105,17 @@ * doesn't have to include a leading question mark. */ public void testGetMethodQueryString2() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString("hadQuestionMark=false"); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=false\"</p>") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"hadQuestionMark=false\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -115,19 +123,17 @@ * values get added to the query string. */ public void testGetMethodParameters() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value") }); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-one=param-value\"</p>") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"param-one=param-value\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -135,25 +141,21 @@ * works with multiple parameters. */ public void testGetMethodMultiParameters() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value"), new NameValuePair("param-two","param-value2"), new NameValuePair("special-chars",":/?~.") }); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"special-chars\";value=\":/?~.\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-one\";value=\"param-value\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-two\";value=\"param-value2\"") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"param-one=param-value¶m-two=param-value2&special-chars=:/?~.\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -161,19 +163,17 @@ * works with a parameter name but no value. */ public void testGetMethodParameterWithoutValue() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); - method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value",null) }); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-without-value=\"</p>") >= 0); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); + method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value", null) }); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"param-without-value=\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -181,45 +181,39 @@ * works with a parameter name that occurs more than once. */ public void testGetMethodParameterAppearsTwice() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString(new NameValuePair[] { new NameValuePair("foo","one"), new NameValuePair("foo","two") }); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"one\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"two\"") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"foo=one&foo=two\"") >= 0); + } finally { + method.releaseConnection(); + } } public void testGetMethodOverwriteQueryString() throws Exception { - HttpClient client = createHttpClient(); - GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + GetMethod method = new GetMethod("/"); method.setQueryString("query=string"); method.setQueryString(new NameValuePair[] { new NameValuePair("param","eter"), new NameValuePair("para","meter") }); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"query\";value=\"string\"") == -1); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertFalse(response.indexOf("QueryString=\"query=string\"") >= 0); + assertTrue(response.indexOf("QueryString=\"param=eter¶=meter\"") >= 0); + } finally { + method.releaseConnection(); + } } /** @@ -228,24 +222,21 @@ * properly. */ public void testPostMethodParameterAndQueryString() throws Exception { - HttpClient client = createHttpClient(); - PostMethod method = new PostMethod("/" + getWebappContext() + "/params"); + this.server.setHttpService(new QueryInfoService()); + PostMethod method = new PostMethod("/"); method.setQueryString("query=string"); method.setRequestBody(new NameValuePair[] { new NameValuePair("param","eter"), new NameValuePair("para","meter") } ); - - try { - client.executeMethod(method); - } catch (Throwable t) { - t.printStackTrace(); - fail("Unable to execute method : " + t.toString()); - } - assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0); - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"query=string\"</p>") >= 0); - assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); + try { + this.client.executeMethod(method); + assertEquals(200, method.getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0); + assertFalse(response.indexOf("QueryString=\"param=eter¶=meter\"") >= 0); + } finally { + method.releaseConnection(); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]