diff --git a/src/main/java/com/ljunggren/ups/api/UpsApi.java b/src/main/java/com/ljunggren/ups/api/UpsApi.java index e22daab..088b88f 100644 --- a/src/main/java/com/ljunggren/ups/api/UpsApi.java +++ b/src/main/java/com/ljunggren/ups/api/UpsApi.java @@ -26,18 +26,11 @@ private String password; private String accessKey; private UpsProperties properties; - private CloseableHttpClient httpClient = HttpClients.custom().build(); public static final List KNOWN_REQUEST_CODES = Arrays.asList(new Integer[] { 200, 400, 401, 404, 405, 500, 503 }); - // package private for unit testing - UpsApi(UpsEnvironment environment, String username, String password, String accessKey, CloseableHttpClient httpClient) { - this(environment, username, password, accessKey); - this.httpClient = httpClient; - } - public UpsApi(UpsEnvironment environment, String username, String password, String accessKey) { this.username = username; this.password = password; @@ -45,7 +38,12 @@ this.properties = new UpsProperties(environment); } - public UpsResponse track(String trackingNumber) { + public UpsResponse track(String trackingNumber) throws IOException { + return track(trackingNumber, HttpClients.createDefault()); + } + + // package private for unit testing + UpsResponse track(String trackingNumber, CloseableHttpClient httpClient) throws IOException { String url = properties.getTrackingUrl().replace("{inquiryNumber}", trackingNumber); HttpGet get = new HttpGet(url); Header[] headers = new Header[] { @@ -67,6 +65,8 @@ return generateErrorReponse("Tracking Error", String.format("Unknown response code %d", responseCode)); } catch (Exception e) { return generateErrorReponse("Tracking Error", e.getMessage()); + } finally { + httpClient.close(); } } diff --git a/src/test/java/com/ljunggren/ups/api/UpsApiTest.java b/src/test/java/com/ljunggren/ups/api/UpsApiTest.java index fbaf3c2..8ff4b9e 100644 --- a/src/test/java/com/ljunggren/ups/api/UpsApiTest.java +++ b/src/test/java/com/ljunggren/ups/api/UpsApiTest.java @@ -55,8 +55,8 @@ String json = readFromResources("/trackingResponse.json"); setup(json, 200); UpsEnvironment environment = UpsEnvironment.CIE; - UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); - UpsResponse upsResponse = upsApi.track("12345"); + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey"); + UpsResponse upsResponse = upsApi.track("12345", httpClient); assertNotNull(upsResponse.getTrackingResponse()); assertNull(upsResponse.getResponse()); } @@ -66,8 +66,8 @@ String json = "notRealResponse"; setup(json, 407); UpsEnvironment environment = UpsEnvironment.CIE; - UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); - UpsResponse upsResponse = upsApi.track("12345"); + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey"); + UpsResponse upsResponse = upsApi.track("12345", httpClient); assertNull(upsResponse.getTrackingResponse()); assertEquals("Unknown response code 407", upsResponse.getResponse().getErrors().get(0).getMessage()); } @@ -77,8 +77,8 @@ String json = "notRealResponse"; setup(json, 200); UpsEnvironment environment = UpsEnvironment.CIE; - UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); - UpsResponse upsResponse = upsApi.track("12345"); + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey"); + UpsResponse upsResponse = upsApi.track("12345", httpClient); assertNull(upsResponse.getTrackingResponse()); assertEquals("Unable to parse response", upsResponse.getResponse().getErrors().get(0).getMessage()); }