diff --git a/src/main/java/com/ljunggren/usps/api/UspsApi.java b/src/main/java/com/ljunggren/usps/api/UspsApi.java index 09f5b31..d0922c6 100644 --- a/src/main/java/com/ljunggren/usps/api/UspsApi.java +++ b/src/main/java/com/ljunggren/usps/api/UspsApi.java @@ -26,20 +26,18 @@ private String username; private UspsProperties properties; - private CloseableHttpClient httpClient = HttpClients.custom().build(); - - // package private for unit testing - UspsApi(UspsEnvironment environment, String username, CloseableHttpClient httpClient) { - this(environment, username); - this.httpClient = httpClient; - } public UspsApi(UspsEnvironment environment, String username) { this.username = username; this.properties = new UspsProperties(environment); } - public TrackingResponse track(String trackingNumber) { + public TrackingResponse track(String trackingNumber) throws IOException { + return track(trackingNumber, HttpClients.createDefault()); + } + + // package private for unit testing + TrackingResponse track(String trackingNumber, CloseableHttpClient httpClient) throws IOException { String url = properties.getTrackingUrl(); HttpPost post = new HttpPost(url); try { @@ -58,6 +56,8 @@ return new TrackingResponse(null, String.format("Response code %d", responseCode)); } catch (Exception e) { return new TrackingResponse(null, e.getMessage()); + } finally { + httpClient.close(); } } diff --git a/src/test/java/com/ljunggren/usps/api/UspsApiTest.java b/src/test/java/com/ljunggren/usps/api/UspsApiTest.java index 6dcf98f..6dab13a 100644 --- a/src/test/java/com/ljunggren/usps/api/UspsApiTest.java +++ b/src/test/java/com/ljunggren/usps/api/UspsApiTest.java @@ -59,8 +59,8 @@ String xml = readFromResources("/trackingResponse.xml"); setup(xml, 200); UspsEnvironment environment = UspsEnvironment.TESTING; - UspsApi uspsApi = new UspsApi(environment, xml, httpClient); - TrackingResponse trackingResponse = uspsApi.track("12345"); + UspsApi uspsApi = new UspsApi(environment, xml); + TrackingResponse trackingResponse = uspsApi.track("12345", httpClient); assertNotNull(trackingResponse.getTrackInfo()); assertNull(trackingResponse.getError()); } @@ -71,8 +71,8 @@ setup(xml, 200); ErrorResponse errorResponse = XmlUtils.xmlToObject(xml, ErrorResponse.class); UspsEnvironment environment = UspsEnvironment.TESTING; - UspsApi uspsApi = new UspsApi(environment, xml, httpClient); - TrackingResponse trackingResponse = uspsApi.track("12345"); + UspsApi uspsApi = new UspsApi(environment, xml); + TrackingResponse trackingResponse = uspsApi.track("12345", httpClient); assertEquals(errorResponse.getDescription(), trackingResponse.getError()); assertNull(trackingResponse.getTrackInfo()); } @@ -82,8 +82,8 @@ String xml = "notRealResponse"; setup(xml, 407); UspsEnvironment environment = UspsEnvironment.TESTING; - UspsApi uspsApi = new UspsApi(environment, xml, httpClient); - TrackingResponse trackingResponse = uspsApi.track("12345"); + UspsApi uspsApi = new UspsApi(environment, xml); + TrackingResponse trackingResponse = uspsApi.track("12345", httpClient); assertEquals("Response code 407", trackingResponse.getError()); assertNull(trackingResponse.getTrackInfo()); } @@ -93,8 +93,8 @@ String xml = "notRealResponse"; setup(xml, 200); UspsEnvironment environment = UspsEnvironment.TESTING; - UspsApi uspsApi = new UspsApi(environment, xml, httpClient); - TrackingResponse trackingResponse = uspsApi.track("12345"); + UspsApi uspsApi = new UspsApi(environment, xml); + TrackingResponse trackingResponse = uspsApi.track("12345", httpClient); assertEquals("Unable to parse response", trackingResponse.getError()); assertNull(trackingResponse.getTrackInfo()); }