[HTTPCLIENT-2124] NullPointerException in MinimalHttpClient.doExecute(HttpHost, ClassicHttpRequest, HttpContext) (#261)

This commit is contained in:
Gary Gregory 2020-10-25 16:29:31 -04:00 committed by Oleg Kalnichevski
parent 1026a1e558
commit 65c6c25070
2 changed files with 26 additions and 9 deletions

View File

@ -32,6 +32,7 @@ import java.util.Locale;
import java.util.Set;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
@ -71,7 +72,7 @@ public class TestMinimalClientRequestExecution extends LocalServerTestBase {
}
@Test
public void testNonCompliantURI() throws Exception {
public void testNonCompliantURIWithContext() throws Exception {
this.server.registerHandler("*", new SimpleService());
this.httpclient = HttpClients.createMinimal();
final HttpHost target = start();
@ -79,9 +80,10 @@ public class TestMinimalClientRequestExecution extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create();
for (int i = 0; i < 10; i++) {
final HttpGet request = new HttpGet("/");
final ClassicHttpResponse response = this.httpclient.execute(target, request, context);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
try (final CloseableHttpResponse response = this.httpclient.execute(target, request, context)) {
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
}
final HttpRequest reqWrapper = context.getRequest();
Assert.assertNotNull(reqWrapper);
@ -98,4 +100,19 @@ public class TestMinimalClientRequestExecution extends LocalServerTestBase {
}
}
@Test
public void testNonCompliantURIWithoutContext() throws Exception {
this.server.registerHandler("*", new SimpleService());
this.httpclient = HttpClients.createMinimal();
final HttpHost target = start();
for (int i = 0; i < 10; i++) {
final HttpGet request = new HttpGet("/");
try (final CloseableHttpResponse response = this.httpclient.execute(target, request)) {
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
}
}
}
}

View File

@ -142,14 +142,14 @@ public class MinimalHttpClient extends CloseableHttpClient {
execRuntime.connectEndpoint(clientContext);
}
context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
clientContext.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
clientContext.setAttribute(HttpClientContext.HTTP_ROUTE, route);
httpProcessor.process(request, request.getEntity(), context);
httpProcessor.process(request, request.getEntity(), clientContext);
final ClassicHttpResponse response = execRuntime.execute(exchangeId, request, clientContext);
httpProcessor.process(response, response.getEntity(), context);
httpProcessor.process(response, response.getEntity(), clientContext);
if (reuseStrategy.keepAlive(request, response, context)) {
if (reuseStrategy.keepAlive(request, response, clientContext)) {
execRuntime.markConnectionReusable(null, TimeValue.NEG_ONE_MILLISECOND);
} else {
execRuntime.markConnectionNonReusable();