From e3436c198b85d29eba2dad249c19ab7d5471002d Mon Sep 17 00:00:00 2001 From: Prashant Srivastava <50466688+srprash@users.noreply.github.com> Date: Mon, 20 Jun 2022 10:58:37 -0700 Subject: [PATCH] [aws-xray-recorder-sdk-apache-http] Don't return a null target to avoid possible NPE (#338) --- .../proxies/apache/http/TracedHttpClient.java | 14 ++++---------- .../apache/http/TracedHttpClientTest.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/aws-xray-recorder-sdk-apache-http/src/main/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClient.java b/aws-xray-recorder-sdk-apache-http/src/main/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClient.java index bd65d51c..2407155d 100644 --- a/aws-xray-recorder-sdk-apache-http/src/main/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClient.java +++ b/aws-xray-recorder-sdk-apache-http/src/main/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClient.java @@ -72,17 +72,11 @@ public TracedHttpClient( } public static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { - // A null target may be acceptable if there is a default target. - // Otherwise, the null target is detected in the director. - HttpHost target = null; - final URI requestUri = request.getURI(); - if (requestUri.isAbsolute()) { - target = URIUtils.extractHost(requestUri); - if (target == null) { - throw new ClientProtocolException("URI does not specify a valid host name: " - + requestUri); - } + HttpHost target = URIUtils.extractHost(requestUri); + if (target == null) { + throw new ClientProtocolException("URI does not specify a valid host name: " + + requestUri); } return target; } diff --git a/aws-xray-recorder-sdk-apache-http/src/test/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClientTest.java b/aws-xray-recorder-sdk-apache-http/src/test/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClientTest.java index a3fc3043..1fab361a 100644 --- a/aws-xray-recorder-sdk-apache-http/src/test/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClientTest.java +++ b/aws-xray-recorder-sdk-apache-http/src/test/java/com/amazonaws/xray/proxies/apache/http/TracedHttpClientTest.java @@ -26,6 +26,8 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import com.amazonaws.xray.AWSXRay; import com.amazonaws.xray.entities.Segment; @@ -34,6 +36,7 @@ import com.amazonaws.xray.entities.TraceID; import com.github.tomakehurst.wiremock.junit.WireMockClassRule; import org.apache.http.HttpHost; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; @@ -113,4 +116,20 @@ public void unsampledButForcedPropagation() throws Exception { .withHeader(TraceHeader.HEADER_KEY, equalTo("Root=1-67891233-abcdef012345678912345678;Sampled=0"))); } + + @Test + public void testExceptionOnRelativeUrl() throws Exception { + stubFor(any(anyUrl()).willReturn(ok())); + + TraceID traceID = TraceID.fromString("1-67891233-abcdef012345678912345678"); + AWSXRay.beginSegment("test", traceID, null); + Exception exception = assertThrows(ClientProtocolException.class, () -> { + client.execute(new HttpGet("/hello/world/")); + }); + AWSXRay.endSegment(); + + String expectedMessage = "URI does not specify a valid host name:"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } }