Skip to content

Commit 7ae97c1

Browse files
committed
Add default protected constructor for HttpClientBuilder to prevent builder with null recorder from being created (#89)
1 parent 6d0d569 commit 7ae97c1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

aws-xray-recorder-sdk-apache-http/src/main/java/com/amazonaws/xray/proxies/apache/http/HttpClientBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class HttpClientBuilder extends org.apache.http.impl.client.HttpClientBui
1616

1717
private AWSXRayRecorder recorder;
1818

19+
protected HttpClientBuilder() {
20+
super();
21+
}
22+
1923
public static HttpClientBuilder create() {
2024
HttpClientBuilder newBuilder = new HttpClientBuilder();
2125
newBuilder.setRecorder(AWSXRay.getGlobalRecorder());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.amazonaws.xray.proxies.apache.http;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Modifier;
5+
6+
import org.junit.Before;
7+
import org.junit.FixMethodOrder;
8+
import org.junit.Test;
9+
import org.junit.runners.MethodSorters;
10+
import org.mockito.Mockito;
11+
12+
import com.amazonaws.xray.AWSXRay;
13+
import com.amazonaws.xray.AWSXRayRecorderBuilder;
14+
import com.amazonaws.xray.emitters.Emitter;
15+
import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder;
16+
17+
import static org.junit.Assert.assertEquals;
18+
19+
@FixMethodOrder(MethodSorters.JVM)
20+
public class HttpClientBuilderTest {
21+
22+
@Before
23+
public void setupAWSXRay() {
24+
// Prevent accidental publish to Daemon
25+
Emitter blankEmitter = Mockito.mock(Emitter.class);
26+
Mockito.doReturn(true).when(blankEmitter).sendSegment(Mockito.anyObject());
27+
Mockito.doReturn(true).when(blankEmitter).sendSubsegment(Mockito.anyObject());
28+
AWSXRay.setGlobalRecorder(AWSXRayRecorderBuilder.standard().withEmitter(blankEmitter).build());
29+
AWSXRay.clearTraceEntity();
30+
}
31+
32+
@Test
33+
public void testConstructorProtected() throws NoSuchMethodException {
34+
// Since the constructor is protected and this is in the same package, we have to test this using reflection.
35+
Constructor clientBuilderConstructor = HttpClientBuilder.class.getDeclaredConstructor();
36+
assertEquals(Modifier.PROTECTED, clientBuilderConstructor.getModifiers()); // PROTECTED = 4;
37+
}
38+
}

0 commit comments

Comments
 (0)