hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1100199 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src: main/java/org/apache/http/impl/nio/conn/ test/java/org/apache/http/impl/nio/client/
Date Fri, 06 May 2011 13:14:12 GMT
Author: olegk
Date: Fri May  6 13:14:11 2011
New Revision: 1100199

URL: http://svn.apache.org/viewvc?rev=1100199&view=rev
Log:
Added redirect handling test cases

Added:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
  (with props)
Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultHttpAsyncRoutePlanner.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultHttpAsyncRoutePlanner.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultHttpAsyncRoutePlanner.java?rev=1100199&r1=1100198&r2=1100199&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultHttpAsyncRoutePlanner.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultHttpAsyncRoutePlanner.java
Fri May  6 13:14:11 2011
@@ -35,6 +35,7 @@ import org.apache.http.HttpRequest;
 import org.apache.http.nio.conn.scheme.LayeringStrategy;
 import org.apache.http.nio.conn.scheme.Scheme;
 import org.apache.http.nio.conn.scheme.SchemeRegistry;
+import org.apache.http.params.HttpProtocolParamBean;
 import org.apache.http.protocol.HttpContext;
 
 import org.apache.http.conn.routing.HttpRoute;
@@ -67,7 +68,12 @@ public class DefaultHttpAsyncRoutePlanne
         }
         InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
         HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams());
-        Scheme scheme = this.schemeRegistry.getScheme(target);
+        Scheme scheme;
+        try {
+            scheme = this.schemeRegistry.getScheme(target);
+        } catch (IllegalStateException ex) {
+            throw new HttpException(ex.getMessage());
+        }
         LayeringStrategy layeringStrategy = scheme.getLayeringStrategy();
         boolean secure = layeringStrategy != null && layeringStrategy.isSecure();
         if (proxy == null) {

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java?rev=1100199&r1=1100198&r2=1100199&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java
Fri May  6 13:14:11 2011
@@ -29,7 +29,6 @@ import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
-import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
@@ -63,7 +62,6 @@ import org.apache.http.protocol.Response
 import org.apache.http.protocol.ResponseContent;
 import org.apache.http.protocol.ResponseDate;
 import org.apache.http.protocol.ResponseServer;
-import org.apache.http.util.EntityUtils;
 import org.junit.Assert;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -191,10 +189,8 @@ public class TestClientAuthentication ex
         HttpGet httpget = new HttpGet("/");
         Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
         HttpResponse response = future.get();
-        HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(response);
         Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
-        Assert.assertNotNull(entity);
-        EntityUtils.consume(entity);
         AuthScope authscope = credsProvider.getAuthScope();
         Assert.assertNotNull(authscope);
         Assert.assertEquals("test realm", authscope.getRealm());
@@ -209,10 +205,8 @@ public class TestClientAuthentication ex
         HttpGet httpget = new HttpGet("/");
         Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
         HttpResponse response = future.get();
-        HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(response);
         Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
-        Assert.assertNotNull(entity);
-        EntityUtils.consume(entity);
         AuthScope authscope = credsProvider.getAuthScope();
         Assert.assertNotNull(authscope);
         Assert.assertEquals("test realm", authscope.getRealm());
@@ -227,10 +221,8 @@ public class TestClientAuthentication ex
         HttpGet httpget = new HttpGet("/");
         Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
         HttpResponse response = future.get();
-        HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(response);
         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
-        Assert.assertNotNull(entity);
-        EntityUtils.consume(entity);
         AuthScope authscope = credsProvider.getAuthScope();
         Assert.assertNotNull(authscope);
         Assert.assertEquals("test realm", authscope.getRealm());
@@ -261,7 +253,7 @@ public class TestClientAuthentication ex
 
         HttpPut httpput = new HttpPut("/");
 
-        NByteArrayEntity requestEntity = new NByteArrayEntity(new byte[] { 1, 2, 3, 4, 5,
6, 7, 8, 9 }) {
+        NByteArrayEntity entity = new NByteArrayEntity(new byte[] { 1, 2, 3, 4, 5, 6, 7,
8, 9 }) {
 
             @Override
             public boolean isRepeatable() {
@@ -270,15 +262,13 @@ public class TestClientAuthentication ex
 
         };
 
-        httpput.setEntity(requestEntity);
+        httpput.setEntity(entity);
         httpput.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
 
         Future<HttpResponse> future = this.httpclient.execute(this.target, httpput,
null);
         HttpResponse response = future.get();
         Assert.assertNotNull(response);
-        HttpEntity responseEntity = response.getEntity();
         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
-        Assert.assertNotNull(responseEntity);
     }
 
     @Test(expected=ExecutionException.class) @Ignore
@@ -326,9 +316,7 @@ public class TestClientAuthentication ex
         Future<HttpResponse> future = this.httpclient.execute(this.target, httppost,
null);
         HttpResponse response = future.get();
         Assert.assertNotNull(response);
-        HttpEntity entity = response.getEntity();
         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
-        Assert.assertNotNull(entity);
         AuthScope authscope = credsProvider.getAuthScope();
         Assert.assertNotNull(authscope);
         Assert.assertEquals("test realm", authscope.getRealm());

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java?rev=1100199&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
(added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
Fri May  6 13:14:11 2011
@@ -0,0 +1,664 @@
+/*
+ * ====================================================================
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.nio.client;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import org.apache.http.Header;
+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.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.cookie.BasicClientCookie;
+import org.apache.http.localserver.AsyncHttpTestBase;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Redirection test cases.
+ */
+public class TestRedirects extends AsyncHttpTestBase {
+
+    private static class BasicRedirectService implements HttpRequestHandler {
+
+        private int statuscode = HttpStatus.SC_MOVED_TEMPORARILY;
+        private String host = null;
+        private int port;
+
+        public BasicRedirectService(final String host, int port, int statuscode) {
+            super();
+            this.host = host;
+            this.port = port;
+            if (statuscode > 0) {
+                this.statuscode = statuscode;
+            }
+        }
+
+        public BasicRedirectService(final String host, int port) {
+            this(host, port, -1);
+        }
+
+        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 BasicHeader("Location",
+                        "http://" + this.host + ":" + this.port + "/newlocation/"));
+                response.addHeader(new BasicHeader("Connection", "close"));
+            } else if (uri.equals("/newlocation/")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
+            }
+        }
+    }
+
+    private static class CircularRedirectService implements HttpRequestHandler {
+
+        public CircularRedirectService() {
+            super();
+        }
+
+        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 BasicHeader("Location", "/circular-location2"));
+            } else if (uri.startsWith("/circular-location2")) {
+                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
+                response.addHeader(new BasicHeader("Location", "/circular-oldlocation"));
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
+            }
+        }
+    }
+
+    private static class RelativeRedirectService implements HttpRequestHandler {
+
+        public RelativeRedirectService() {
+            super();
+        }
+
+        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 BasicHeader("Location", "/relativelocation/"));
+            } else if (uri.equals("/relativelocation/")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
+            }
+        }
+    }
+
+    private static class RelativeRedirectService2 implements HttpRequestHandler {
+
+        public RelativeRedirectService2() {
+            super();
+        }
+
+        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("/test/oldlocation")) {
+                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
+                response.addHeader(new BasicHeader("Location", "relativelocation"));
+            } else if (uri.equals("/test/relativelocation")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
+            }
+        }
+    }
+
+    private static class BogusRedirectService implements HttpRequestHandler {
+        private String url;
+
+        public BogusRedirectService(String redirectUrl) {
+            super();
+            this.url = redirectUrl;
+        }
+
+        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 BasicHeader("Location", url));
+            } else if (uri.equals("/relativelocation/")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
+            }
+        }
+    }
+
+    @Test
+    public void testBasicRedirect300() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_MULTIPLE_CHOICES));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
+    }
+
+    @Test
+    public void testBasicRedirect301() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_MOVED_PERMANENTLY));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test
+    public void testBasicRedirect302() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_MOVED_TEMPORARILY));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test
+    public void testBasicRedirect302NoLocation() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*", new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request,
+                    final HttpResponse response,
+                    final HttpContext context) throws HttpException, IOException {
+                response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
+            }
+
+        });
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test
+    public void testBasicRedirect303() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_SEE_OTHER));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test
+    public void testBasicRedirect304() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_NOT_MODIFIED));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
+    }
+
+    @Test
+    public void testBasicRedirect305() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_USE_PROXY));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_USE_PROXY, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
+    }
+
+    @Test
+    public void testBasicRedirect307() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port, HttpStatus.SC_TEMPORARY_REDIRECT));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test(expected=ExecutionException.class)
+    public void testMaxRedirectCheck() throws Exception {
+        this.localServer.register("*", new CircularRedirectService());
+
+        this.httpclient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,
true);
+        this.httpclient.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 5);
+
+        HttpGet httpget = new HttpGet("/circular-oldlocation/");
+        try {
+            Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
+            future.get();
+        } catch (ExecutionException e) {
+            Assert.assertTrue(e.getCause() instanceof RedirectException);
+            throw e;
+        }
+    }
+
+    @Test(expected=ExecutionException.class)
+    public void testCircularRedirect() throws Exception {
+        this.localServer.register("*", new CircularRedirectService());
+
+        this.httpclient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,
false);
+
+        HttpGet httpget = new HttpGet("/circular-oldlocation/");
+
+        try {
+            Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
+            future.get();
+        } catch (ExecutionException e) {
+            Assert.assertTrue(e.getCause() instanceof CircularRedirectException);
+            throw e;
+        }
+    }
+
+    @Test
+    public void testPostNoRedirect() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*", new BasicRedirectService(host, port));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpPost httppost = new HttpPost("/oldlocation/");
+        httppost.setEntity(new NStringEntity("stuff"));
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httppost,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals("POST", reqWrapper.getRequestLine().getMethod());
+    }
+
+    @Test
+    public void testPostRedirectSeeOther() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*", new BasicRedirectService(host, port,
+                HttpStatus.SC_SEE_OTHER));
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpPost httppost = new HttpPost("/oldlocation/");
+        httppost.setEntity(new NStringEntity("stuff"));
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httppost,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals("GET", reqWrapper.getRequestLine().getMethod());
+    }
+
+    @Test
+    public void testRelativeRedirect() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*", new RelativeRedirectService());
+
+        HttpContext context = new BasicHttpContext();
+
+        this.httpclient.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, false);
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/relativelocation/", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test
+    public void testRelativeRedirect2() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*", new RelativeRedirectService2());
+
+        HttpContext context = new BasicHttpContext();
+
+        this.httpclient.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, false);
+        HttpGet httpget = new HttpGet("/test/oldlocation");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/test/relativelocation", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, targetHost.getHostName());
+        Assert.assertEquals(port, targetHost.getPort());
+    }
+
+    @Test(expected=ExecutionException.class)
+    public void testRejectRelativeRedirect() throws Exception {
+        this.localServer.register("*", new RelativeRedirectService());
+
+        this.httpclient.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, true);
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        try {
+            Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
+            future.get();
+        } catch (ExecutionException e) {
+            Assert.assertTrue(e.getCause() instanceof ProtocolException);
+            throw e;
+        }
+    }
+
+    @Test(expected=ExecutionException.class)
+    public void testRejectBogusRedirectLocation() throws Exception {
+        this.localServer.register("*", new BogusRedirectService("xxx://bogus"));
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        try {
+            Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
+            future.get();
+        } catch (ExecutionException e) {
+            Assert.assertTrue(e.getCause() instanceof HttpException);
+            throw e;
+        }
+    }
+
+    @Test(expected=ExecutionException.class)
+    public void testRejectInvalidRedirectLocation() throws Exception {
+        InetSocketAddress address = this.localServer.getServiceAddress();
+        int port = address.getPort();
+        String host = address.getHostName();
+        this.localServer.register("*",
+                new BogusRedirectService("http://"+ host +":"+ port +"/newlocation/?p=I have
spaces"));
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+        try {
+            Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
null);
+            future.get();
+        } catch (ExecutionException e) {
+            Assert.assertTrue(e.getCause() instanceof ProtocolException);
+            throw e;
+        }
+    }
+
+    @Test
+    public void testRedirectWithCookie() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port));
+
+        CookieStore cookieStore = new BasicCookieStore();
+        this.httpclient.setCookieStore(cookieStore);
+
+        BasicClientCookie cookie = new BasicClientCookie("name", "value");
+        cookie.setDomain(host);
+        cookie.setPath("/");
+
+        cookieStore.addCookie(cookie);
+
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+
+        Header[] headers = reqWrapper.getHeaders(SM.COOKIE);
+        Assert.assertEquals("There can only be one (cookie)", 1, headers.length);
+    }
+
+    @Test
+    public void testDefaultHeadersRedirect() throws Exception {
+        String host = this.target.getHostName();
+        int port = this.target.getPort();
+        this.localServer.register("*",
+                new BasicRedirectService(host, port));
+
+        HttpContext context = new BasicHttpContext();
+
+        List<Header> defaultHeaders = new ArrayList<Header>(1);
+        defaultHeaders.add(new BasicHeader(HTTP.USER_AGENT, "my-test-client"));
+
+        this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);
+
+        HttpGet httpget = new HttpGet("/oldlocation/");
+
+        Future<HttpResponse> future = this.httpclient.execute(this.target, httpget,
context, null);
+        HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
+
+        Header header = reqWrapper.getFirstHeader(HTTP.USER_AGENT);
+        Assert.assertEquals("my-test-client", header.getValue());
+    }
+
+}

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message