hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1558353 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/impl/execchain/MinimalClientExec.java test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java
Date Wed, 15 Jan 2014 12:19:06 GMT
Author: olegk
Date: Wed Jan 15 12:19:06 2014
New Revision: 1558353

URL: http://svn.apache.org/r1558353
Log:
HTTPCLIENT-1447: clients created with HttpClients#createMinimal do not work with absolute
URIs
Contributed by Joseph Walton <joe at kafsemo.org>

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java?rev=1558353&r1=1558352&r2=1558353&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java
Wed Jan 15 12:19:06 2014
@@ -30,6 +30,7 @@ package org.apache.http.impl.execchain;
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 
@@ -42,6 +43,7 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
+import org.apache.http.ProtocolException;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -50,6 +52,7 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.client.protocol.RequestClientConnControl;
+import org.apache.http.client.utils.URIUtils;
 import org.apache.http.conn.ConnectionKeepAliveStrategy;
 import org.apache.http.conn.ConnectionRequest;
 import org.apache.http.conn.HttpClientConnectionManager;
@@ -106,6 +109,25 @@ public class MinimalClientExec implement
         this.keepAliveStrategy  = keepAliveStrategy;
     }
 
+    static void rewriteRequestURI(
+            final HttpRequestWrapper request,
+            final HttpRoute route) throws ProtocolException {
+        try {
+            URI uri = request.getURI();
+            if (uri != null) {
+                // Make sure the request URI is relative
+                if (uri.isAbsolute()) {
+                    uri = URIUtils.rewriteURI(uri, null, true);
+                } else {
+                    uri = URIUtils.rewriteURI(uri);
+                }
+                request.setURI(uri);
+            }
+        } catch (final URISyntaxException ex) {
+            throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(),
ex);
+        }
+    }
+
     public CloseableHttpResponse execute(
             final HttpRoute route,
             final HttpRequestWrapper request,
@@ -115,6 +137,8 @@ public class MinimalClientExec implement
         Args.notNull(request, "HTTP request");
         Args.notNull(context, "HTTP context");
 
+        rewriteRequestURI(request, route);
+
         final ConnectionRequest connRequest = connManager.requestConnection(route, null);
         if (execAware != null) {
             if (execAware.isAborted()) {

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java?rev=1558353&r1=1558352&r2=1558353&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestMinimalClientExec.java
Wed Jan 15 12:19:06 2014
@@ -27,6 +27,7 @@
 package org.apache.http.impl.execchain;
 
 import junit.framework.Assert;
+
 import org.apache.http.ConnectionReuseStrategy;
 import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpException;
@@ -51,10 +52,13 @@ import org.apache.http.message.BasicHttp
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 
+import static org.junit.Assert.assertEquals;
+
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InterruptedIOException;
@@ -339,4 +343,23 @@ public class TestMinimalClientExec {
         }
     }
 
-}
\ No newline at end of file
+    @Test
+    public void absoluteUriIsRewrittenToRelativeBeforeBeingPassedInRequestLine() throws Exception
{
+        final HttpRoute route = new HttpRoute(target);
+        final HttpClientContext context = new HttpClientContext();
+        final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
+
+        final HttpResponse response = Mockito.mock(HttpResponse.class);
+        Mockito.when(requestExecutor.execute(
+                Mockito.<HttpRequest>any(),
+                Mockito.<HttpClientConnection>any(),
+                Mockito.<HttpClientContext>any())).thenReturn(response);
+
+        minimalClientExec.execute(route, request, context, execAware);
+
+        final ArgumentCaptor<HttpRequest> reqCaptor = ArgumentCaptor.forClass(HttpRequest.class);
+        Mockito.verify(requestExecutor).execute(reqCaptor.capture(), Mockito.<HttpClientConnection>any(),
Mockito.<HttpClientContext>any());
+
+        assertEquals("/test", reqCaptor.getValue().getRequestLine().getUri());
+    }
+}



Mime
View raw message