Skip to content

Commit

Permalink
"Encourage" usage of the client builder to get an instance of the sma…
Browse files Browse the repository at this point in the history
…rty client. This will ensure the maxtimeout is passed through to the OKHTTP client.
  • Loading branch information
RyanLCox1 committed Mar 4, 2024
1 parent ba9fd95 commit 2e83278
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
23 changes: 11 additions & 12 deletions src/main/java/com/smartystreets/api/SmartySender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
import java.util.logging.Logger;

public class SmartySender implements Sender {
private int maxTimeOut;
private OkHttpClient client;
private final int maxTimeOut;
private final OkHttpClient client;

public SmartySender() {
this.maxTimeOut = 10000;
this.client = new OkHttpClient();
}

public SmartySender(int maxTimeout) {
this();
this.maxTimeOut = maxTimeout;
SmartySender(int maxTimeOut) {
this.maxTimeOut = maxTimeOut;
this.client = new OkHttpClient.Builder()
.writeTimeout(this.maxTimeOut, TimeUnit.MILLISECONDS)
.readTimeout(this.maxTimeOut, TimeUnit.MILLISECONDS)
.connectTimeout(this.maxTimeOut, TimeUnit.MILLISECONDS)
.build();
}

SmartySender(int maxTimeOut, Proxy proxy) {
Expand All @@ -38,8 +37,8 @@ public SmartySender(int maxTimeout) {
.build();
}

SmartySender(OkHttpClient client) {
this();
SmartySender(int maxTimeOut, OkHttpClient client) {
this.maxTimeOut = maxTimeOut;
this.client = client;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/smartystreets/api/SmartyApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void setupCredentials() throws Exception {
@Test
public void testInternationalAutocomplete() throws Exception {
ArrayList<String> licenses = new ArrayList<>();
licenses.add("international-autocomplete-cloud");
licenses.add("international-autocomplete-v2-cloud");
com.smartystreets.api.international_autocomplete.Client client = new ClientBuilder(credentials).withLicenses(licenses).buildInternationalAutcompleteApiClient();

com.smartystreets.api.international_autocomplete.Lookup lookup = new com.smartystreets.api.international_autocomplete.Lookup("Louis");
Expand Down
30 changes: 8 additions & 22 deletions src/test/java/com/smartystreets/api/SmartySenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

public class SmartySenderTest {

final static int MAX_TIMEOUT = 1;

@Test
public void testHttpRequestContainsCorrectHeaders() throws Exception {
SmartySender sender = new SmartySender(mockHttpClient("{\"key\": \"value\"}", 200));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient("{\"key\": \"value\"}", 200));
Request request = new Request();
request.setUrlPrefix("http://localhost");
request.putHeader("X-name1", "value1");
Expand All @@ -32,7 +34,7 @@ public void testHttpRequestContainsCorrectHeaders() throws Exception {
@Test
public void testHttpRequestContainsGetWhenAppropriate() throws Exception {
String responseString = "This is a GET response.";
SmartySender sender = new SmartySender(mockHttpClient(responseString, 200));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient(responseString, 200));
Request request = new Request();
request.setUrlPrefix("http://localhost");

Expand All @@ -44,7 +46,7 @@ public void testHttpRequestContainsGetWhenAppropriate() throws Exception {
@Test
public void testHttpRequestContainsPostWhenAppropriate() throws Exception {
String responseString = "This is a POST response.";
SmartySender sender = new SmartySender(mockHttpClient(responseString, 200));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient(responseString, 200));
Request request = new Request();
request.setUrlPrefix("http://localhost");

Expand All @@ -54,26 +56,10 @@ public void testHttpRequestContainsPostWhenAppropriate() throws Exception {
assertArrayEquals(responseString.getBytes(), response.getPayload());
}

// @Test
// public void testHttpRequestContainsCorrectContent() throws Exception {
// SmartySender sender = new SmartySender(this.getMockClient(200));
// Request request = new Request();
// request.setUrlPrefix("http://localhost");
//
// request.setPayload("This is the test content.".getBytes());
// sender.send(request);
//
// assertEquals("This is the test content.", this.httpRequest.bodyPublisher().toString());
// }

//endregion

//region [ Response Packaging ]

@Test
public void testResponseContainsCorrectPayload() throws Exception {
String responseBody = "{\"key\": \"value\"}";
SmartySender sender = new SmartySender(mockHttpClient(responseBody, 200));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient(responseBody, 200));
Request request = new Request();
request.setUrlPrefix("http://localhost");

Expand All @@ -85,7 +71,7 @@ public void testResponseContainsCorrectPayload() throws Exception {
@Test
public void testResponseContainsStatusCode200OnSuccess() throws Exception {
String responseBody = "{\"key\": \"value\"}";
SmartySender sender = new SmartySender(mockHttpClient(responseBody, 200));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient(responseBody, 200));
Request request = new Request();
request.setUrlPrefix("http://localhost");

Expand All @@ -97,7 +83,7 @@ public void testResponseContainsStatusCode200OnSuccess() throws Exception {
@Test
public void testResponseContainsStatusCode400WhenA400IsThrown() throws Exception {
String responseBody = "{\"key\": \"value\"}";
SmartySender sender = new SmartySender(mockHttpClient(responseBody, 400));
SmartySender sender = new SmartySender(MAX_TIMEOUT, mockHttpClient(responseBody, 400));
Request request = new Request();
request.setUrlPrefix("http://localhost");

Expand Down

0 comments on commit 2e83278

Please sign in to comment.