hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1492242 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src: main/java/org/apache/http/impl/nio/client/MainClientExec.java test/java/org/apache/http/nio/client/integration/TestRedirects.java
Date Wed, 12 Jun 2013 15:31:54 GMT
Author: olegk
Date: Wed Jun 12 15:31:53 2013
New Revision: 1492242

URL: http://svn.apache.org/r1492242
Log:
HTTPCLIENT-1359: repeated requests using the same context fail if they redirect
Contributed by James Leigh <james at 3roundstones.com>

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MainClientExec.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MainClientExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MainClientExec.java?rev=1492242&r1=1492241&r2=1492242&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MainClientExec.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MainClientExec.java
Wed Jun 12 15:31:53 2013
@@ -53,6 +53,7 @@ import org.apache.http.client.Credential
 import org.apache.http.client.NonRepeatableRequestException;
 import org.apache.http.client.RedirectException;
 import org.apache.http.client.RedirectStrategy;
+import org.apache.http.client.URICollection;
 import org.apache.http.client.UserTokenHandler;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.Configurable;
@@ -138,6 +139,12 @@ class MainClientExec implements Internal
                 localContext.setRequestConfig(config);
             }
         }
+
+        final URICollection redirectLocations = localContext.getRedirectLocations();
+        if (redirectLocations != null) {
+            redirectLocations.clear();
+        }
+
         prepareRequest(state);
     }
 

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java?rev=1492242&r1=1492241&r2=1492242&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
Wed Jun 12 15:31:53 2013
@@ -284,6 +284,28 @@ public class TestRedirects extends HttpA
         }
     }
 
+    private static class RomeRedirectService implements HttpRequestHandler {
+
+        public RomeRedirectService() {
+            super();
+        }
+
+        public void handle(
+                final HttpRequest request,
+                final HttpResponse response,
+                final HttpContext context) throws HttpException, IOException {
+            final String uri = request.getRequestLine().getUri();
+            if (uri.equals("/rome")) {
+                response.setStatusCode(HttpStatus.SC_OK);
+                final StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
+                response.addHeader(new BasicHeader("Location", "/rome"));
+            }
+        }
+    }
+
     @Test
     public void testBasicRedirect300() throws Exception {
         final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
@@ -809,4 +831,97 @@ public class TestRedirects extends HttpA
         }
     }
 
+    @Test
+    public void testRepeatRequest() throws Exception {
+        final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+        registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+        final HttpHost target = start(registry, null);
+
+        final HttpClientContext context = HttpClientContext.create();
+
+        final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
+        final HttpGet first = new HttpGet("/rome");
+        first.setConfig(config);
+
+        final Future<HttpResponse> future1 = this.httpclient.execute(target, first,
context, null);
+        final HttpResponse response1 = future1.get();
+        Assert.assertNotNull(response1);
+
+        final HttpGet second = new HttpGet("/rome");
+        second.setConfig(config);
+
+        final Future<HttpResponse> future2 = this.httpclient.execute(target, second,
context, null);
+        final HttpResponse response2 = future2.get();
+        Assert.assertNotNull(response2);
+
+        final HttpRequest reqWrapper = context.getRequest();
+        final HttpHost host = context.getTargetHost();
+
+        Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
+        Assert.assertEquals("/rome", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, target);
+    }
+
+    @Test
+    public void testRepeatRequestRedirect() throws Exception {
+        final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+        registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+        final HttpHost target = start(registry, null);
+
+        final HttpClientContext context = HttpClientContext.create();
+
+        final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
+        final HttpGet first = new HttpGet("/lille");
+        first.setConfig(config);
+
+        final Future<HttpResponse> future1 = this.httpclient.execute(target, first,
context, null);
+        final HttpResponse response1 = future1.get();
+        Assert.assertNotNull(response1);
+
+        final HttpGet second = new HttpGet("/lille");
+        second.setConfig(config);
+
+        final Future<HttpResponse> future2 = this.httpclient.execute(target, second,
context, null);
+        final HttpResponse response2 = future2.get();
+        Assert.assertNotNull(response2);
+
+        final HttpRequest reqWrapper = context.getRequest();
+        final HttpHost host = context.getTargetHost();
+
+        Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
+        Assert.assertEquals("/rome", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, target);
+    }
+
+    @Test
+    public void testDifferentRequestSameRedirect() throws Exception {
+        final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+        registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+        final HttpHost target = start(registry, null);
+
+        final HttpClientContext context = HttpClientContext.create();
+
+        final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
+        final HttpGet first = new HttpGet("/alian");
+        first.setConfig(config);
+
+        final Future<HttpResponse> future1 = this.httpclient.execute(target, first,
context, null);
+        final HttpResponse response1 = future1.get();
+        Assert.assertNotNull(response1);
+
+        final HttpGet second = new HttpGet("/lille");
+        second.setConfig(config);
+
+        final Future<HttpResponse> future2 = this.httpclient.execute(target, second,
context, null);
+        final HttpResponse response2 = future2.get();
+        Assert.assertNotNull(response2);
+
+        final HttpRequest reqWrapper = context.getRequest();
+        final HttpHost host = context.getTargetHost();
+
+        Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
+        Assert.assertEquals("/rome", reqWrapper.getRequestLine().getUri());
+        Assert.assertEquals(host, target);
+    }
+
 }
\ No newline at end of file



Mime
View raw message