Skip to content

Commit

Permalink
[aws-xray-recorder-sdk-apache-http] Don't return a null target to avo…
Browse files Browse the repository at this point in the history
…id possible NPE (aws#338)
  • Loading branch information
srprash authored Jun 20, 2022
1 parent 9b09ed5 commit e3436c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit e3436c1

Please sign in to comment.