Index: org/apache/commons/httpclient/TestExternalHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestExternalHost.java,v retrieving revision 1.2 diff -u -w -r1.2 TestExternalHost.java --- org/apache/commons/httpclient/TestExternalHost.java 4 Oct 2001 17:49:13 -0000 1.2 +++ org/apache/commons/httpclient/TestExternalHost.java 23 Jul 2002 07:00:18 -0000 @@ -68,7 +68,17 @@ * A suite composed of only those tests which * require an external Internet connection. * + * Optionally this test can be run using a proxy. If the proxy is not + * authenticating do not set user / password. + * + * System properties: + * httpclient.test.proxyHost - proxy host name + * httpclient.test.proxyPort - proxy port + * httpclient.test.proxyUser - proxy auth username + * httpclient.test.proxyPass - proxy auth password + * * @author Rodney Waldhoff + * @author Ortwin Glück * @version $Id: TestExternalHost.java,v 1.2 2001/10/04 17:49:13 rwaldhoff Exp $ */ public class TestExternalHost extends TestCase { Index: org/apache/commons/httpclient/TestHttps.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttps.java,v retrieving revision 1.4 diff -u -w -r1.4 TestHttps.java --- org/apache/commons/httpclient/TestHttps.java 4 Feb 2002 15:26:43 -0000 1.4 +++ org/apache/commons/httpclient/TestHttps.java 23 Jul 2002 07:00:19 -0000 @@ -78,6 +78,7 @@ * (see build.xml) * * @author Rodney Waldhoff + * @author Ortwin Glück * @version $Id: TestHttps.java,v 1.4 2002/02/04 15:26:43 dion Exp $ */ public class TestHttps extends TestCase { @@ -85,6 +86,10 @@ // ---------------------------------------------------------------- Members private URL _urlWithPort = null; private URL _urlWithoutPort = null; + private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost"); + private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort"); + private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser"); + private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass"); // ------------------------------------------------------------ Constructor public TestHttps(String testName) { @@ -109,7 +114,19 @@ public void testHttpsGet() { HttpClient client = new HttpClient(); + if (PROXY_HOST == null) { client.startSession(_urlWithPort); + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(_urlWithPort.getHost(), _urlWithPort.getPort(), + PROXY_HOST, + Integer.parseInt(PROXY_PORT), + true); + } GetMethod method = new GetMethod("/"); method.setUseDisk(false); try { @@ -131,7 +148,19 @@ public void testHttpsGetNoPort() { HttpClient client = new HttpClient(); + if (PROXY_HOST == null) { client.startSession(_urlWithoutPort); + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(_urlWithoutPort.getHost(), 443, + PROXY_HOST, + Integer.parseInt(PROXY_PORT), + true); + } GetMethod method = new GetMethod("/"); method.setUseDisk(false); try { Index: org/apache/commons/httpclient/TestMethodsExternalHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java,v retrieving revision 1.3 diff -u -w -r1.3 TestMethodsExternalHost.java --- org/apache/commons/httpclient/TestMethodsExternalHost.java 4 Feb 2002 15:26:43 -0000 1.3 +++ org/apache/commons/httpclient/TestMethodsExternalHost.java 23 Jul 2002 07:00:19 -0000 @@ -75,6 +75,7 @@ * * @author Remy Maucherat * @author Rodney Waldhoff + * @author Ortwin Glück * @version $Id: TestMethodsExternalHost.java,v 1.3 2002/02/04 15:26:43 dion Exp $ */ public class TestMethodsExternalHost extends TestCase { @@ -85,6 +86,10 @@ private static final String externalHost = "java.sun.com"; private static final int externalPort = 80; private static final String externalPath = "/index.html"; + private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost"); + private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort"); + private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser"); + private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass"); // ------------------------------------------------------------ Constructor @@ -113,6 +118,17 @@ try { client.startSession(externalHost, externalPort); + if (PROXY_HOST == null) { + client.startSession(externalHost, externalPort); + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(externalHost, externalPort, + PROXY_HOST, Integer.parseInt(PROXY_PORT), false); + } client.executeMethod(method); } catch (Throwable t) { t.printStackTrace(); @@ -133,8 +149,17 @@ public void testMethodsGetExternal() { HttpClient client = new HttpClient(); + if (PROXY_HOST == null) { client.startSession(externalHost, externalPort); - + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(externalHost, externalPort, + PROXY_HOST, Integer.parseInt(PROXY_PORT), false); + } GetMethod method = new GetMethod("/"); method.setUseDisk(false); @@ -184,8 +209,17 @@ public void testMethodsHeadExternal() { HttpClient client = new HttpClient(); + if (PROXY_HOST == null) { client.startSession(externalHost, externalPort); - + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(externalHost, externalPort, + PROXY_HOST, Integer.parseInt(PROXY_PORT), false); + } HeadMethod method = new HeadMethod(externalPath); try { @@ -207,12 +241,24 @@ public void testIOException() { HttpClient client = new HttpClient(); - client.startSession("http://whaturl.com.org", externalPort); - + String host = "whaturl.com.org"; + int port = 80; + if (PROXY_HOST == null) { + client.startSession(host, port); + } else { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(null, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.startSession(host, port, + PROXY_HOST, Integer.parseInt(PROXY_PORT), false); + } GetMethod method = new GetMethod(""); try { client.executeMethod(method); + if ((PROXY_HOST != null) && (method.getStatusCode() >= 400)) return; } catch (IOException e) { return; // good } catch (HttpException e) { Index: org/apache/commons/httpclient/TestMethodsNoHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v retrieving revision 1.3 diff -u -w -r1.3 TestMethodsNoHost.java --- org/apache/commons/httpclient/TestMethodsNoHost.java 10 Jul 2002 11:33:40 -0000 1.3 +++ org/apache/commons/httpclient/TestMethodsNoHost.java 23 Jul 2002 07:00:19 -0000 @@ -185,13 +185,13 @@ public void testPostParametersEncoding(){ PostMethod post = new PostMethod(); post.addParameter(PAIR); - assertEquals("name=value", post.getRequestBody()); + assertEquals("name=value", post.getRequestBody().toString()); post.addParameters(new NameValuePair[]{ PAIR1, PAIR2 }); - assertEquals("name=value&name1=value1&name2=value2", post.getRequestBody()); + assertEquals("name=value&name1=value1&name2=value2", post.getRequestBody().toString()); post.addParameter("hasSpace", "a b c d"); - assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d", post.getRequestBody()); + assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d", post.getRequestBody().toString()); } Index: org/apache/commons/httpclient/TestWebappMethods.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v retrieving revision 1.4 diff -u -w -r1.4 TestWebappMethods.java --- org/apache/commons/httpclient/TestWebappMethods.java 24 Apr 2002 14:44:50 -0000 1.4 +++ org/apache/commons/httpclient/TestWebappMethods.java 23 Jul 2002 07:00:21 -0000 @@ -62,6 +62,7 @@ package org.apache.commons.httpclient; +import java.io.ByteArrayInputStream; import junit.framework.*; import org.apache.commons.httpclient.methods.*; @@ -82,6 +83,7 @@ * "httpclient.test.webappContext" property. * * @author Rodney Waldhoff + * @author Ortwin Glück * @version $Id: TestWebappMethods.java,v 1.4 2002/04/24 14:44:50 rwaldhoff Exp $ */ public class TestWebappMethods extends TestWebappBase { @@ -322,6 +324,66 @@ assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.") >= 0); assertEquals(200,method.getStatusCode()); } + + + public void testPostBodyCustomLength() throws Exception { + HttpClient client = new HttpClient(); + client.startSession(host, port); + PostMethod method = new PostMethod("/" + context + "/body"); + method.setUseDisk(false); + String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; + method.setRequestBody(new ByteArrayInputStream(body.getBytes())); + method.setRequestContentLength(body.length()); + try { + client.executeMethod(method); + } catch (Throwable t) { + t.printStackTrace(); + fail("Unable to execute method : " + t.toString()); + } + assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.") >= 0); + assertEquals(200,method.getStatusCode()); + } + + + public void testPostBodyAutoLength() throws Exception { + HttpClient client = new HttpClient(); + client.startSession(host, port); + PostMethod method = new PostMethod("/" + context + "/body"); + method.setUseDisk(false); + String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; + method.setRequestBody(new ByteArrayInputStream(body.getBytes())); + method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO); + try { + client.executeMethod(method); + } catch (Throwable t) { + t.printStackTrace(); + fail("Unable to execute method : " + t.toString()); + } + assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.") >= 0); + assertEquals(200,method.getStatusCode()); + } + + /* + //enable this when chunked requests are properly implemented + //note: only few servers support this + public void testPostBodyChunked() throws Exception { + HttpClient client = new HttpClient(); + client.startSession(host, port); + PostMethod method = new PostMethod("/" + context + "/body"); + method.setUseDisk(false); + String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; + method.setRequestBody(new ByteArrayInputStream(body.getBytes())); + method.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED); + try { + client.executeMethod(method); + } catch (Throwable t) { + t.printStackTrace(); + fail("Unable to execute method : " + t.toString()); + } + assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.") >= 0); + assertEquals(200,method.getStatusCode()); + } + */ public void testPutBody() throws Exception { HttpClient client = new HttpClient(); Index: org/apache/commons/httpclient/TestWebappRedirect.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappRedirect.java,v retrieving revision 1.4 diff -u -w -r1.4 TestWebappRedirect.java --- org/apache/commons/httpclient/TestWebappRedirect.java 4 Feb 2002 15:26:43 -0000 1.4 +++ org/apache/commons/httpclient/TestWebappRedirect.java 23 Jul 2002 07:00:22 -0000 @@ -206,11 +206,7 @@ t.printStackTrace(); fail("Unable to execute method : " + t.toString()); } - assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("Param Servlet: POST") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("

QueryString=\"foo=bar&bar=foo\"

") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); - assertTrue(method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); + assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode()); } public void testPutRedirect() throws Exception { @@ -226,8 +222,7 @@ t.printStackTrace(); fail("Unable to execute method : " + t.toString()); } - assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("This is data to be sent in the body of an HTTP PUT.") >= 0); - assertEquals(200,method.getStatusCode()); + assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode()); } }