Skip to content

Commit bc5bdbf

Browse files
authored
Merge pull request #56 from MosheElisha/feature/54-allow-custom-http-client-builder
#54 Allow configuration of HttpClient by making HttpClientRestClient extensible
2 parents 2354251 + 2cedddf commit bc5bdbf

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/main/java/org/sourcelab/kafka/connect/apiclient/rest/HttpClientRestClient.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void init(final Configuration configuration) {
107107
final HttpsContextBuilder httpsContextBuilder = new HttpsContextBuilder(configuration);
108108

109109
// Setup client builder
110-
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
110+
final HttpClientBuilder clientBuilder = createHttpClientBuilder();
111111
clientBuilder
112112
// Define timeout
113113
.setConnectionTimeToLive(configuration.getConnectionTimeToLiveInSeconds(), TimeUnit.SECONDS)
@@ -206,6 +206,15 @@ public void close() {
206206
httpClient = null;
207207
}
208208

209+
/**
210+
* Create the HttpClientBuilder which is used to create the HttpClient.
211+
* This method allows users to extend this class and use a custom builder if needed.
212+
* @return The HttpClientBuilder to use for creating the HttpClient.
213+
*/
214+
protected HttpClientBuilder createHttpClientBuilder() {
215+
return HttpClientBuilder.create();
216+
}
217+
209218
/**
210219
* Make a request against the Pardot API.
211220
* @param request The request to submit.

src/test/java/org/sourcelab/kafka/connect/apiclient/rest/HttpClientRestClientTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient.rest;
1919

20+
import org.apache.http.impl.client.HttpClientBuilder;
2021
import org.junit.BeforeClass;
2122
import org.junit.Test;
2223
import org.sourcelab.kafka.connect.apiclient.Configuration;
@@ -27,6 +28,8 @@
2728
import java.io.File;
2829

2930
import static org.junit.Assert.assertEquals;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
3033

3134
public class HttpClientRestClientTest {
3235

@@ -77,6 +80,29 @@ public void doHttpTest() throws Exception {
7780
}
7881
}
7982

83+
/**
84+
* Test that the HttpClientRestClient actually uses the builder returned from the
85+
* {@link HttpClientRestClient#createHttpClientBuilder()} method.
86+
*/
87+
@Test
88+
public void doHttp_withCustomHttpClientBuilder() {
89+
// Create a mock builder and a rest client that uses the mock builder
90+
final HttpClientBuilder builderMock = mock(HttpClientBuilder.class);
91+
HttpClientRestClient restClient = new HttpClientRestClient() {
92+
@Override
93+
protected HttpClientBuilder createHttpClientBuilder() {
94+
return builderMock;
95+
}
96+
};
97+
98+
// Init the rest client
99+
final Configuration configuration = new Configuration("http://localhost:" + HTTP_PORT);
100+
restClient.init(configuration);
101+
102+
// Verify the mock was used to create the HttpClient
103+
verify(builderMock).build();
104+
}
105+
80106
/**
81107
* Test against Https server.
82108
*/
@@ -186,4 +212,4 @@ public Object parseResponse(final String responseStr) {
186212
return responseStr;
187213
}
188214
}
189-
}
215+
}

0 commit comments

Comments
 (0)