From d3a1171524a62dca8210e97d6a80c607296be16c Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 13 Jun 2015 16:30:56 +0000 Subject: [PATCH] HTTPCLIENT-1658: fixed regression in RequestBuilder#copy git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1685284 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/methods/RequestBuilder.java | 21 ++----------------- .../client/methods/TestRequestBuilder.java | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java index 93da2d16e..7cbfc0001 100644 --- a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java +++ b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java @@ -293,26 +293,9 @@ public class RequestBuilder { final URI originalUri; if (request instanceof HttpUriRequest) { - originalUri = ((HttpUriRequest) request).getURI(); + uri = ((HttpUriRequest) request).getURI(); } else { - originalUri = URI.create(request.getRequestLine().getUri()); - } - - final URIBuilder uriBuilder = new URIBuilder(originalUri); - if (parameters == null) { - final List queryParams = uriBuilder.getQueryParams(); - if (!queryParams.isEmpty()) { - parameters = queryParams; - uriBuilder.clearParameters(); - } else { - parameters = null; - } - } - try { - uri = uriBuilder.build(); - } catch (URISyntaxException ex) { - // Should never happen - uri = originalUri; + uri = URI.create(request.getRequestLine().getUri()); } if (request instanceof Configurable) { diff --git a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java index f83926898..84fea770d 100644 --- a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java +++ b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java @@ -33,6 +33,7 @@ import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.HttpRequest; import org.apache.http.HttpVersion; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; @@ -168,10 +169,8 @@ public class TestRequestBuilder { final RequestBuilder builder = RequestBuilder.copy(get); final List parameters = builder.getParameters(); Assert.assertNotNull(parameters); - Assert.assertEquals(2, parameters.size()); - Assert.assertEquals(new BasicNameValuePair("p1", "this"), parameters.get(0)); - Assert.assertEquals(new BasicNameValuePair("p2", "that"), parameters.get(1)); - Assert.assertEquals(new URI("/stuff"), builder.getUri()); + Assert.assertEquals(0, parameters.size()); + Assert.assertEquals(new URI("/stuff?p1=this&p2=that"), builder.getUri()); } @Test @@ -196,12 +195,20 @@ public class TestRequestBuilder { final RequestBuilder builder = RequestBuilder.copy(post); final List parameters = builder.getParameters(); Assert.assertNotNull(parameters); - Assert.assertEquals(1, parameters.size()); - Assert.assertEquals(new BasicNameValuePair("p1", "wtf"), parameters.get(0)); - Assert.assertEquals(new URI("/stuff"), builder.getUri()); + Assert.assertEquals(0, parameters.size()); + Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri()); Assert.assertSame(entity, builder.getEntity()); } + @Test + public void testCopyAndSetUri() throws Exception { + final URI uri1 = URI.create("http://host1.com/path?param=something"); + final URI uri2 = URI.create("http://host2.com/path?param=somethingdifferent"); + final HttpRequest request1 = new HttpGet(uri1); + final HttpUriRequest request2 = RequestBuilder.copy(request1).setUri(uri2).build(); + Assert.assertEquals(request2.getURI(), uri2); + } + @Test public void testGettersAndMutators() throws Exception { final HttpEntity entity = new StringEntity("stuff");