hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r582648 - in /jakarta/httpcomponents/httpclient/trunk/module-client/src: main/java/org/apache/http/impl/client/ test/java/org/apache/http/client/protocol/
Date Sun, 07 Oct 2007 16:51:18 GMT
Author: olegk
Date: Sun Oct  7 09:51:11 2007
New Revision: 582648

URL: http://svn.apache.org/viewvc?rev=582648&view=rev
Log:
Ported redirect test cases from the HttpClient 3.x branch

Added:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java
      - copied, changed from r582623, jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestRedirects.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestAllProtocol.java

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?rev=582648&r1=582647&r2=582648&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
Sun Oct  7 09:51:11 2007
@@ -774,15 +774,7 @@
             }
             redirectCount++;
             
-            URI uri;
-            try {
-                uri = this.redirectHandler.getLocationURI(response, context);
-            } catch (ProtocolException ex) {
-                if (LOG.isWarnEnabled()) {
-                    LOG.warn(ex.getMessage());
-                }
-                return null;
-            }
+            URI uri = this.redirectHandler.getLocationURI(response, context);
 
             HttpHost newTarget = new HttpHost(
                     uri.getHost(), 

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestAllProtocol.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestAllProtocol.java?rev=582648&r1=582647&r2=582648&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestAllProtocol.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestAllProtocol.java
Sun Oct  7 09:51:11 2007
@@ -42,9 +42,8 @@
 
     public static Test suite() {
         TestSuite suite = new TestSuite();
-
         suite.addTest(TestCookie2Support.suite());
-
+        suite.addTest(TestRedirects.suite());
         return suite;
     }
 

Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java
(from r582623, jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestRedirects.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java?p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java&p1=jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestRedirects.java&r1=582623&r2=582648&rev=582648&view=diff
==============================================================================
--- jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestRedirects.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java
Sun Oct  7 09:51:11 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v
1.9 2005/01/14 19:40:39 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -27,32 +27,48 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.client.protocol;
 
 import java.io.IOException;
 
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.ProtocolException;
+import org.apache.http.ProtocolVersion;
+import org.apache.http.client.CircularRedirectException;
+import org.apache.http.client.CookieStore;
+import org.apache.http.client.RedirectException;
+import org.apache.http.client.RoutedRequest;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.params.ClientPNames;
+import org.apache.http.cookie.SM;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.cookie.BasicClientCookie;
+import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+
 import junit.framework.Test;
 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.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;
-
 /**
  * Redirection test cases.
  *
  * @author Oleg Kalnichevski
  * 
- * @version $Id$
+ * @version $Revision$
  */
-public class TestRedirects extends HttpClientTestBase {
+public class TestRedirects extends ServerTestBase {
 
     // ------------------------------------------------------------ Constructor
     public TestRedirects(final String testName) throws IOException {
@@ -68,12 +84,11 @@
     // ------------------------------------------------------- TestCase Methods
 
     public static Test suite() {
-        TestSuite suite = new TestSuite(TestRedirects.class);
-        ProxyTestDecorator.addTests(suite);
-        return suite;
+        return new TestSuite(TestRedirects.class);
     }
 
-    private class BasicRedirectService implements HttpService {
+    private class BasicRedirectService implements HttpRequestHandler {
+        
 		private int statuscode = HttpStatus.SC_MOVED_TEMPORARILY;
 		private String host = null;
         private int port;
@@ -91,26 +106,28 @@
             this(host, port, -1);
         }
 
-        public boolean process(final SimpleRequest request, final SimpleResponse response)
-            throws IOException {
-            RequestLine reqline = request.getRequestLine();
-            HttpVersion ver = reqline.getHttpVersion();
-            if (reqline.getUri().equals("/oldlocation/")) {
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            String uri = request.getRequestLine().getUri();
+            if (uri.equals("/oldlocation/")) {
                 response.setStatusLine(ver, this.statuscode);
-                response.addHeader(new Header("Location", 
+                response.addHeader(new BasicHeader("Location", 
                 		"http://" + this.host + ":" + this.port + "/newlocation/"));
-                response.addHeader(new Header("Connection", "close"));
-            } else if (reqline.getUri().equals("/newlocation/")) {
+                response.addHeader(new BasicHeader("Connection", "close"));
+            } else if (uri.equals("/newlocation/")) {
                 response.setStatusLine(ver, HttpStatus.SC_OK);
-                response.setBodyString("Successful redirect");
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
             } else {
                 response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
             }
-            return true;
         }
     }
 
-    private class CircularRedirectService implements HttpService {
+    private class CircularRedirectService implements HttpRequestHandler {
 
         private int invocations = 0;
         
@@ -118,49 +135,50 @@
             super();
         }
         
-        public boolean process(final SimpleRequest request, final SimpleResponse response)
-            throws IOException
-        {
-            RequestLine reqline = request.getRequestLine();
-            HttpVersion ver = reqline.getHttpVersion();
-            if (reqline.getUri().startsWith("/circular-oldlocation")) {
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            String uri = request.getRequestLine().getUri();
+            if (uri.startsWith("/circular-oldlocation")) {
                 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
-                response.addHeader(new Header("Location", "/circular-location2?invk=" + (++this.invocations)));
-            } else if (reqline.getUri().startsWith("/circular-location2")) {
+                response.addHeader(new BasicHeader("Location", "/circular-location2?invk="
+ (++this.invocations)));
+            } else if (uri.startsWith("/circular-location2")) {
                 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
-                response.addHeader(new Header("Location", "/circular-oldlocation?invk=" +
(++this.invocations)));
+                response.addHeader(new BasicHeader("Location", "/circular-oldlocation?invk="
+ (++this.invocations)));
             } else {
                 response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
             }
-            return true;
         }
     }
 
-    private class RelativeRedirectService implements HttpService {
+    private class RelativeRedirectService implements HttpRequestHandler {
         
             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/")) {
+            public void handle(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException, IOException {
+                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+                String uri = request.getRequestLine().getUri();
+                if (uri.equals("/oldlocation/")) {
                     response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
-                    response.addHeader(new Header("Location", "/relativelocation/"));
-                } else if (reqline.getUri().equals("/relativelocation/")) {
+                    response.addHeader(new BasicHeader("Location", "/relativelocation/"));
+                } else if (uri.equals("/relativelocation/")) {
                     response.setStatusLine(ver, HttpStatus.SC_OK);
-                    response.setBodyString("Successful redirect");
+                    StringEntity entity = new StringEntity("Successful redirect");
+                    response.setEntity(entity);
                 } else {
                     response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
                 }
-                return true;
             }
         }
 
-    private class BogusRedirectService implements HttpService {
+    private class BogusRedirectService implements HttpRequestHandler {
         private String url;
         
         public BogusRedirectService(String redirectUrl) {
@@ -168,337 +186,397 @@
             this.url = redirectUrl;
         }
 
-        public boolean process(final SimpleRequest request, final SimpleResponse response)
-            throws IOException {
-            RequestLine reqline = request.getRequestLine();
-            HttpVersion ver = reqline.getHttpVersion();
-            if (reqline.getUri().equals("/oldlocation/")) {
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            String uri = request.getRequestLine().getUri();
+            if (uri.equals("/oldlocation/")) {
                 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
-                response.addHeader(new Header("Location", url));
-            } else if (reqline.getUri().equals("/relativelocation/")) {
+                response.addHeader(new BasicHeader("Location", url));
+            } else if (uri.equals("/relativelocation/")) {
                 response.setStatusLine(ver, HttpStatus.SC_OK);
-                response.setBodyString("Successful redirect");
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
             } else {
                 response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
             }
-            return true;
         }
     }
 
-    public void testBasicRedirect300() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect300() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_MULTIPLE_CHOICES));
-        GetMethod httpget = new GetMethod("/oldlocation/");
-        httpget.setFollowRedirects(false);
-        try {
-            this.client.executeMethod(httpget);
-            assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, httpget.getStatusCode());
-            assertEquals("/oldlocation/", httpget.getPath());
-            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
-        } finally {
-            httpget.releaseConnection();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        
+        assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
+        assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
     }
 
-    public void testBasicRedirect301() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect301() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_MOVED_PERMANENTLY));
-        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();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
     }
 
-    public void testBasicRedirect302() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect302() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_MOVED_TEMPORARILY));
-        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();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
     }
 
-    public void testBasicRedirect303() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect303() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_SEE_OTHER));
-        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();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
     }
 
-    public void testBasicRedirect304() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect304() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_NOT_MODIFIED));
-        GetMethod httpget = new GetMethod("/oldlocation/");
-        httpget.setFollowRedirects(true);
-        try {
-            this.client.executeMethod(httpget);
-            assertEquals(HttpStatus.SC_NOT_MODIFIED, httpget.getStatusCode());
-            assertEquals("/oldlocation/", httpget.getPath());
-            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
-        } finally {
-            httpget.releaseConnection();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        
+        assertEquals(HttpStatus.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
+        assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
     }
 
-    public void testBasicRedirect305() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect305() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_USE_PROXY));
-        GetMethod httpget = new GetMethod("/oldlocation/");
-        httpget.setFollowRedirects(true);
-        try {
-            this.client.executeMethod(httpget);
-            assertEquals(HttpStatus.SC_USE_PROXY, httpget.getStatusCode());
-            assertEquals("/oldlocation/", httpget.getPath());
-            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
-        } finally {
-            httpget.releaseConnection();
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        
+        assertEquals(HttpStatus.SC_USE_PROXY, response.getStatusLine().getStatusCode());
+        assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
     }
 
-    public void testBasicRedirect307() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(
+    public void testBasicRedirect307() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", 
                 new BasicRedirectService(host, port, HttpStatus.SC_TEMPORARY_REDIRECT));
-        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();
-        }
-    }
+        
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
 
-    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();
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
     }
 
-    public void testMaxRedirectCheck() throws IOException {
-        this.server.setHttpService(new CircularRedirectService());
-        GetMethod httpget = new GetMethod("/circular-oldlocation/");
+    public void testMaxRedirectCheck() throws Exception {
+        this.localServer.register("*", new CircularRedirectService());
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
+        client.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 5);
+
+        HttpGet httpget = new HttpGet("/circular-oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
         try {
-            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
true);
-            this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5);
-            this.client.executeMethod(httpget);
+            client.execute(request);
             fail("RedirectException exception should have been thrown");
-        }
-        catch (RedirectException e) {
+        } catch (RedirectException e) {
             // expected
-        } finally {
-            httpget.releaseConnection();
         }
     }
 
-    public void testCircularRedirect() throws IOException {
-        this.server.setHttpService(new CircularRedirectService());
-        GetMethod httpget = new GetMethod("/circular-oldlocation/");
+    public void testCircularRedirect() throws Exception {
+        this.localServer.register("*", new CircularRedirectService());
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);
+
+        HttpGet httpget = new HttpGet("/circular-oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
         try {
-            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
false);
-            this.client.executeMethod(httpget);
+            client.execute(request);
             fail("CircularRedirectException exception should have been thrown");
-        } catch (CircularRedirectException expected) {
-        } finally {
-            httpget.releaseConnection();
+        } catch (CircularRedirectException e) {
+            // expected
         }
     }
 
-    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", null, null));
-        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 testPostRedirect() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", new BasicRedirectService(host, port));
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+        
+        HttpPost httppost = new HttpPost("/oldlocation/");
+        httppost.setEntity(new StringEntity("stuff"));
+
+        RoutedRequest request = new RoutedRequest.Impl(httppost, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
-    }
+        
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
 
-    public void testRelativeRedirect() 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, 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 + "/relativelocation/", false),

-            		httpget.getURI());
-        } finally {
-            httpget.releaseConnection();
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals("GET", reqWrapper.getRequestLine().getMethod());
+    }
+
+    public void testRelativeRedirect() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", new RelativeRedirectService());
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        client.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, false);
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+        
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/relativelocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
     }
 
-    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);
+    public void testRejectRelativeRedirect() throws Exception {
+        this.localServer.register("*", new RelativeRedirectService());
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+
+        client.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, true);
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
         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();
+            client.execute(request);
+            fail("ProtocolException exception should have been thrown");
+        } catch (ProtocolException e) {
+            // expected
         }
     }
 
-    public void testRejectBogusRedirectLocation() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(new BogusRedirectService("xxx://bogus"));
-        GetMethod httpget = new GetMethod("/oldlocation/");
-        httpget.setFollowRedirects(true);
+    public void testRejectBogusRedirectLocation() throws Exception {
+        this.localServer.register("*", new BogusRedirectService("xxx://bogus"));
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
         try {
-            this.client.executeMethod(httpget);
-            fail("BogusRedirectService should have been thrown");
+            client.execute(request);
+            fail("IllegalStateException should have been thrown");
         } catch (IllegalStateException e) {
-        	//expected
-        } finally {
-            httpget.releaseConnection();
+            // expected
         }
     }
 
-    public void testRejectInvalidRedirectLocation() throws IOException {
-        String host = this.server.getLocalAddress();
-        int port = this.server.getLocalPort();
-        this.server.setHttpService(new BogusRedirectService("http://"+ host +":"+ port +"/newlocation/?p=I
have spaces"));
-        GetMethod httpget = new GetMethod("/oldlocation/");
-        httpget.setFollowRedirects(true);
+    public void testRejectInvalidRedirectLocation() throws Exception {
+        String host = "localhost";
+        int port = this.localServer.getServicePort();
+        this.localServer.register("*", 
+                new BogusRedirectService("http://"+ host +":"+ port +"/newlocation/?p=I have
spaces"));
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
         try {
-            this.client.executeMethod(httpget);
-            fail("InvalidRedirectLocationException should have been thrown");
-        } catch (InvalidRedirectLocationException e) {
-            //expected a protocol exception
-        } finally {
-            httpget.releaseConnection();
+            client.execute(request);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException e) {
+            // expected
         }
     }
 
-    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));
-        thatserver.setTestname(getName());
-        
-        HostConfiguration hostconfig = new HostConfiguration();
-        hostconfig.setHost(
-                thatserver.getLocalAddress(), 
-                thatserver.getLocalPort(),
-                Protocol.getProtocol("http"));
+    public void testRedirectWithCookie() throws Exception {
+        String host = "localhost";
+        int port = this.localServer.getServicePort();
 
-        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 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();
+        this.localServer.register("*", 
+                new BasicRedirectService(host, port));
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        
+        CookieStore cookieStore = new BasicCookieStore();
+        client.setCookieStore(cookieStore);
+        
+        BasicClientCookie cookie = new BasicClientCookie("name", "value");
+        cookie.setDomain("localhost");
+        cookie.setPath("/");
+        
+        cookieStore.addCookie(cookie);
+
+        HttpContext context = client.getDefaultContext();
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute());
+        
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
         }
+        
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+
+        Header[] headers = reqWrapper.getHeaders(SM.COOKIE);
+        assertEquals("There can only be one (cookie)", 1, headers.length);            
     }
+
 }



Mime
View raw message