olegk 2004/09/28 14:08:48 Modified: httpclient/src/test/org/apache/commons/httpclient SimpleHttpConnection.java TestNoHost.java TestRedirects.java httpclient/src/test/org/apache/commons/httpclient/auth TestBasicAuth.java httpclient/src/test/org/apache/commons/httpclient/cookie TestCookieIgnoreSpec.java httpclient/src/test/org/apache/commons/httpclient/server SimpleResponse.java Removed: httpclient/src/test/org/apache/commons/httpclient TestMethodsRedirectNoHost.java Log: Redirect test cases refactored to take advantage of the new HTTP testing framework based on SimpleHttpServer Contributed by Oleg Kalnichevski Revision Changes Path 1.19 +5 -3 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java Index: SimpleHttpConnection.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- SimpleHttpConnection.java 22 Feb 2004 18:08:49 -0000 1.18 +++ SimpleHttpConnection.java 28 Sep 2004 21:08:48 -0000 1.19 @@ -50,6 +50,8 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a> * @author Michael Becke + * + * @deprecated */ class SimpleHttpConnection extends HttpConnection { 1.40 +4 -5 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.39 retrieving revision 1.40 diff -u -r1.39 -r1.40 --- TestNoHost.java 15 Sep 2004 20:42:17 -0000 1.39 +++ TestNoHost.java 28 Sep 2004 21:08:48 -0000 1.40 @@ -74,7 +74,6 @@ suite.addTest(TestURIUtil.suite()); suite.addTest(TestURIUtil2.suite()); suite.addTest(TestMethodsNoHost.suite()); - suite.addTest(TestMethodsRedirectNoHost.suite()); suite.addTest(TestHttpState.suite()); suite.addTest(TestResponseHeaders.suite()); suite.addTest(TestRequestHeaders.suite()); 1.3 +269 -41 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java Index: TestRedirects.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestRedirects.java 12 Apr 2004 11:16:25 -0000 1.2 +++ TestRedirects.java 28 Sep 2004 21:08:48 -0000 1.3 @@ -36,9 +36,14 @@ import junit.framework.TestSuite; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.params.HttpClientParams; +import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory; +import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.server.HttpService; import org.apache.commons.httpclient.server.RequestLine; +import org.apache.commons.httpclient.server.SimpleHttpServer; import org.apache.commons.httpclient.server.SimpleRequest; import org.apache.commons.httpclient.server.SimpleResponse; @@ -68,9 +73,39 @@ return new TestSuite(TestRedirects.class); } - private class RedirectService implements HttpService { + private class BasicRedirectService implements HttpService { + + private String host = null; + private int port; + + public BasicRedirectService(final String host, int port) { + super(); + this.host = host; + this.port = port; + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + RequestLine reqline = request.getRequestLine(); + HttpVersion ver = reqline.getHttpVersion(); + if (reqline.getUri().equals("/oldlocation/")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.addHeader(new Header("Location", + "http://" + this.host + ":" + this.port + "/newlocation/")); + } else if (reqline.getUri().equals("/newlocation/")) { + response.setStatusLine(ver, HttpStatus.SC_OK); + response.setBodyString("Successful redirect"); + } else { + response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND); + } + return true; + } + } - public RedirectService() { + private class CircularRedirectService implements HttpService { + + public CircularRedirectService() { super(); } @@ -78,22 +113,71 @@ throws IOException { RequestLine reqline = request.getRequestLine(); - if (reqline.getUri().equals("/circular-location1/")) { - response.setStatusLine("HTTP/1.1 302 Object moved"); + HttpVersion ver = reqline.getHttpVersion(); + if (reqline.getUri().equals("/circular-oldlocation/")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); response.addHeader(new Header("Location", "/circular-location2/")); } else if (reqline.getUri().equals("/circular-location2/")) { - response.setStatusLine("HTTP/1.1 302 Object moved"); - response.addHeader(new Header("Location", "/circular-location1/")); + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.addHeader(new Header("Location", "/circular-oldlocation/")); } else { - response.setStatusLine("HTTP/1.1 404 Not Found"); + response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND); } return true; } } + private class RelativeRedirectService implements HttpService { + + public RelativeRedirectService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + RequestLine reqline = request.getRequestLine(); + HttpVersion ver = reqline.getHttpVersion(); + if (reqline.getUri().equals("/oldlocation/")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.addHeader(new Header("Location", "/relativelocation/")); + } else if (reqline.getUri().equals("/relativelocation/")) { + response.setStatusLine(ver, HttpStatus.SC_OK); + response.setBodyString("Successful redirect"); + } else { + response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND); + } + return true; + } + } + + private class BogusRedirectService implements HttpService { + + public BogusRedirectService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + RequestLine reqline = request.getRequestLine(); + HttpVersion ver = reqline.getHttpVersion(); + if (reqline.getUri().equals("/oldlocation/")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.addHeader(new Header("Location", "xxx://bogus")); + } else if (reqline.getUri().equals("/relativelocation/")) { + response.setStatusLine(ver, HttpStatus.SC_OK); + response.setBodyString("Successful redirect"); + } else { + response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND); + } + return true; + } + } + public void testMaxRedirectCheck() throws IOException { - this.server.setHttpService(new RedirectService()); - GetMethod httpget = new GetMethod("/circular-location1/"); + this.server.setHttpService(new CircularRedirectService()); + GetMethod httpget = new GetMethod("/circular-oldlocation/"); try { this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5); @@ -108,8 +192,8 @@ } public void testCircularRedirect() throws IOException { - this.server.setHttpService(new RedirectService()); - GetMethod httpget = new GetMethod("/circular-location1/"); + this.server.setHttpService(new CircularRedirectService()); + GetMethod httpget = new GetMethod("/circular-oldlocation/"); try { this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false); this.client.executeMethod(httpget); @@ -122,47 +206,191 @@ } } - private class RedirectService2 implements HttpService { - - private String host = null; - private int port; + public void testBasicRedirect() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + this.server.setHttpService(new BasicRedirectService(host, port)); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("/newlocation/", httpget.getPath()); + assertEquals(host, httpget.getURI().getHost()); + assertEquals(port, httpget.getURI().getPort()); + assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false), httpget.getURI()); + } finally { + httpget.releaseConnection(); + } + } - public RedirectService2(final String host, int port) { - super(); - this.host = host; - this.port = port; + public void testNoRedirect() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + this.server.setHttpService(new BasicRedirectService(host, port)); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(false); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, httpget.getStatusCode()); + assertEquals("/oldlocation/", httpget.getPath()); + assertEquals(new URI("/oldlocation/", false), httpget.getURI()); + } finally { + httpget.releaseConnection(); } + } - public boolean process(final SimpleRequest request, final SimpleResponse response) - throws IOException - { - RequestLine reqline = request.getRequestLine(); - if (reqline.getUri().equals("/location1/")) { - response.setStatusLine("HTTP/1.1 302 Object moved"); - response.addHeader(new Header("Location", "http://" + this.host + ":" + this.port + "/location2/")); - } else if (reqline.getUri().equals("/location2/")) { - response.setStatusLine("HTTP/1.1 200 OK"); - response.setBodyString("Successful redirect"); - } else { - response.setStatusLine("HTTP/1.1 404 Not Found"); - } - return true; + public void testPostRedirect() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + this.server.setHttpService(new BasicRedirectService(host, port)); + PostMethod httppost = new PostMethod("/oldlocation/"); + httppost.setRequestEntity(new StringRequestEntity("stuff")); + try { + this.client.executeMethod(httppost); + assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, httppost.getStatusCode()); + assertEquals("/oldlocation/", httppost.getPath()); + assertEquals(new URI("/oldlocation/", false), httppost.getURI()); + } finally { + httppost.releaseConnection(); } } - public void testRedirectLocation() throws IOException { + public void testRelativeRedirect() throws IOException { String host = this.server.getLocalAddress(); int port = this.server.getLocalPort(); - this.server.setHttpService(new RedirectService2(host, port)); - GetMethod httpget = new GetMethod("/location1/"); + this.server.setHttpService(new RelativeRedirectService()); + this.client.getParams().setBooleanParameter( + HttpClientParams.REJECT_RELATIVE_REDIRECT, false); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); try { this.client.executeMethod(httpget); + assertEquals("/relativelocation/", httpget.getPath()); assertEquals(host, httpget.getURI().getHost()); assertEquals(port, httpget.getURI().getPort()); - assertEquals(new URI("http://" + host + ":" + port + "/location2/", false), httpget.getURI()); + assertEquals(new URI("http://" + host + ":" + port + "/relativelocation/", false), + httpget.getURI()); } finally { httpget.releaseConnection(); } } + public void testRejectRelativeRedirect() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + this.server.setHttpService(new RelativeRedirectService()); + this.client.getParams().setBooleanParameter( + HttpClientParams.REJECT_RELATIVE_REDIRECT, true); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, httpget.getStatusCode()); + assertEquals("/oldlocation/", httpget.getPath()); + assertEquals(new URI("/oldlocation/", false), httpget.getURI()); + } finally { + httpget.releaseConnection(); + } + } + + public void testRejectBogusRedirectLocation() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + this.server.setHttpService(new BogusRedirectService()); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + fail("BogusRedirectService should have been thrown"); + } catch (IllegalStateException e) { + //expected + } finally { + httpget.releaseConnection(); + } + } + + public void testCrossSiteRedirect() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + + SimpleHttpServer thatserver = new SimpleHttpServer(); + this.server.setHttpService(new BasicRedirectService(host, port)); + thatserver.setHttpService(new BasicRedirectService(host, port)); + + HostConfiguration hostconfig = new HostConfiguration(); + hostconfig.setHost( + thatserver.getLocalAddress(), + thatserver.getLocalPort(), + Protocol.getProtocol("http")); + + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(hostconfig, httpget); + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("/newlocation/", httpget.getPath()); + assertEquals(host, httpget.getURI().getHost()); + assertEquals(port, httpget.getURI().getPort()); + assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false), + httpget.getURI()); + } finally { + httpget.releaseConnection(); + } + thatserver.destroy(); + } + + public void testCrossProtocolRedirect() throws IOException { + + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + + Protocol.registerProtocol("test", new Protocol("test", + new DefaultProtocolSocketFactory(), port)); + + this.server.setHttpService(new BasicRedirectService(host, port)); + GetMethod httpget = new GetMethod("test://" + host + "/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("/newlocation/", httpget.getPath()); + assertEquals(host, httpget.getURI().getHost()); + assertEquals(port, httpget.getURI().getPort()); + assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false), httpget.getURI()); + } finally { + httpget.releaseConnection(); + } + + Protocol.unregisterProtocol("test"); + } + + + public void testRedirectWithCookie() throws IOException { + + client.getState().addCookie(new Cookie("localhost", "name", "value", "/", -1, false)); + + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + + this.server.setHttpService(new BasicRedirectService(host, port)); + GetMethod httpget = new GetMethod("/oldlocation/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("/newlocation/", httpget.getPath()); + + Header[] headers = httpget.getRequestHeaders(); + int cookiecount = 0; + for (int i = 0; i < headers.length; i++) { + if ("cookie".equalsIgnoreCase(headers[i].getName())) { + ++cookiecount; + } + } + assertEquals("There can only be one (cookie)", 1, cookiecount); + } finally { + httpget.releaseConnection(); + } + } } 1.4 +17 -13 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java Index: TestBasicAuth.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TestBasicAuth.java 12 Jun 2004 22:47:23 -0000 1.3 +++ TestBasicAuth.java 28 Sep 2004 21:08:48 -0000 1.4 @@ -40,6 +40,7 @@ import org.apache.commons.httpclient.HttpClientTestBase; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.server.HttpService; @@ -85,9 +86,10 @@ { Header challenge = new Header("WWW-Authenticate", "Basic realm=\"test\""); RequestLine requestLine = request.getRequestLine(); + HttpVersion ver = requestLine.getHttpVersion(); Header auth = request.getFirstHeader("Authorization"); if (auth == null) { - response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED); response.addHeader(challenge); response.setBodyString("Authorization required"); return true; @@ -108,12 +110,12 @@ } } if (!pass) { - response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.setStatusLine(ver, HttpStatus.SC_FORBIDDEN); response.addHeader(challenge); response.setBodyString("Access forbidden"); return true; } - response.setStatusLine("HTTP/1.1 200 OK"); + response.setStatusLine(ver, HttpStatus.SC_OK); response.setBodyString("Authorization successful"); return true; } @@ -130,9 +132,10 @@ { Header challenge = new Header("WWW-Authenticate", "Basic realm=\"test2\""); RequestLine requestLine = request.getRequestLine(); + HttpVersion ver = requestLine.getHttpVersion(); Header auth = request.getFirstHeader("Authorization"); if (auth == null) { - response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED); response.addHeader(challenge); response.setBodyString("Authorization required"); return true; @@ -153,12 +156,12 @@ } } if (!pass) { - response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.setStatusLine(ver, HttpStatus.SC_FORBIDDEN); response.addHeader(challenge); response.setBodyString("Access forbidden"); return true; } - response.setStatusLine("HTTP/1.1 200 OK"); + response.setStatusLine(ver, HttpStatus.SC_OK); response.setBodyString("Authorization successful"); return true; } @@ -175,9 +178,10 @@ { Header challenge = new Header("WwW-AuThEnTiCaTe", "bAsIc ReAlM=\"test\""); RequestLine requestLine = request.getRequestLine(); + HttpVersion ver = requestLine.getHttpVersion(); Header auth = request.getFirstHeader("Authorization"); if (auth == null) { - response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED); response.addHeader(challenge); response.setBodyString("Authorization required"); return true; @@ -198,12 +202,12 @@ } } if (!pass) { - response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.setStatusLine(ver, HttpStatus.SC_FORBIDDEN); response.addHeader(challenge); response.setBodyString("Access forbidden"); return true; } - response.setStatusLine("HTTP/1.1 200 OK"); + response.setStatusLine(ver, HttpStatus.SC_OK); response.setBodyString("Authorization successful"); return true; } 1.4 +8 -5 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java Index: TestCookieIgnoreSpec.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TestCookieIgnoreSpec.java 27 Apr 2004 22:35:21 -0000 1.3 +++ TestCookieIgnoreSpec.java 28 Sep 2004 21:08:48 -0000 1.4 @@ -36,6 +36,8 @@ import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClientTestBase; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.server.HttpService; import org.apache.commons.httpclient.server.SimpleRequest; @@ -72,7 +74,8 @@ public boolean process(final SimpleRequest request, final SimpleResponse response) throws IOException { - response.setStatusLine("HTTP/1.1 200 OK"); + HttpVersion ver = request.getRequestLine().getHttpVersion(); + response.setStatusLine(ver, HttpStatus.SC_OK); response.addHeader(new Header("Connection", "close")); response.addHeader(new Header("Set-Cookie", "custno = 12345; comment=test; version=1," + 1.3 +11 -9 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java Index: SimpleResponse.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SimpleResponse.java 14 Sep 2004 15:50:41 -0000 1.2 +++ SimpleResponse.java 28 Sep 2004 21:08:48 -0000 1.3 @@ -80,11 +80,7 @@ return this.statusLine; } - public void setStatusLine(final String string) { - this.statusLine = string; - } - - public void setStatusLine(final HttpVersion version, int statuscode) { + public void setStatusLine(final HttpVersion version, int statuscode, String statustext) { if (version == null) { throw new IllegalArgumentException("HTTP version may not be null"); } @@ -92,12 +88,18 @@ buffer.append(version); buffer.append(' '); buffer.append(statuscode); - String statustext = HttpStatus.getStatusText(statuscode); + if (statustext == null) { + statustext = HttpStatus.getStatusText(statuscode); + } if (statustext != null) { buffer.append(' '); buffer.append(statustext); } this.statusLine = buffer.toString(); + } + + public void setStatusLine(final HttpVersion version, int statuscode) { + setStatusLine(version, statuscode, null); } public boolean containsHeader(final String name) {
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]