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
This commit is contained in:
Oleg Kalnichevski 2015-06-13 16:30:56 +00:00
parent 5d1871a8a0
commit d3a1171524
2 changed files with 16 additions and 26 deletions

View File

@ -293,26 +293,9 @@ public class RequestBuilder {
final URI originalUri; final URI originalUri;
if (request instanceof HttpUriRequest) { if (request instanceof HttpUriRequest) {
originalUri = ((HttpUriRequest) request).getURI(); uri = ((HttpUriRequest) request).getURI();
} else { } else {
originalUri = URI.create(request.getRequestLine().getUri()); uri = URI.create(request.getRequestLine().getUri());
}
final URIBuilder uriBuilder = new URIBuilder(originalUri);
if (parameters == null) {
final List<NameValuePair> 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;
} }
if (request instanceof Configurable) { if (request instanceof Configurable) {

View File

@ -33,6 +33,7 @@ import java.util.List;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
@ -168,10 +169,8 @@ public class TestRequestBuilder {
final RequestBuilder builder = RequestBuilder.copy(get); final RequestBuilder builder = RequestBuilder.copy(get);
final List<NameValuePair> parameters = builder.getParameters(); final List<NameValuePair> parameters = builder.getParameters();
Assert.assertNotNull(parameters); Assert.assertNotNull(parameters);
Assert.assertEquals(2, parameters.size()); Assert.assertEquals(0, parameters.size());
Assert.assertEquals(new BasicNameValuePair("p1", "this"), parameters.get(0)); Assert.assertEquals(new URI("/stuff?p1=this&p2=that"), builder.getUri());
Assert.assertEquals(new BasicNameValuePair("p2", "that"), parameters.get(1));
Assert.assertEquals(new URI("/stuff"), builder.getUri());
} }
@Test @Test
@ -196,12 +195,20 @@ public class TestRequestBuilder {
final RequestBuilder builder = RequestBuilder.copy(post); final RequestBuilder builder = RequestBuilder.copy(post);
final List<NameValuePair> parameters = builder.getParameters(); final List<NameValuePair> parameters = builder.getParameters();
Assert.assertNotNull(parameters); Assert.assertNotNull(parameters);
Assert.assertEquals(1, parameters.size()); Assert.assertEquals(0, parameters.size());
Assert.assertEquals(new BasicNameValuePair("p1", "wtf"), parameters.get(0)); Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
Assert.assertEquals(new URI("/stuff"), builder.getUri());
Assert.assertSame(entity, builder.getEntity()); 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 @Test
public void testGettersAndMutators() throws Exception { public void testGettersAndMutators() throws Exception {
final HttpEntity entity = new StringEntity("stuff"); final HttpEntity entity = new StringEntity("stuff");