diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCookBookV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCookBookV4LiveTest.java index e82120a105..8b83419ba1 100644 --- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCookBookV4LiveTest.java +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCookBookV4LiveTest.java @@ -1,6 +1,5 @@ package com.baeldung.httpclient; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -33,6 +32,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; @@ -163,4 +163,22 @@ class HttpClientCookBookV4LiveTest { response.close(); } } + + @Test + void givenAutoClosableClient_thenCorrect() throws IOException { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpGet httpGet = new HttpGet(SAMPLE_GET_URL); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + // handle response; + HttpEntity entity = response.getEntity(); + if (entity != null) { + try (InputStream instream = entity.getContent()) { + // Process the input stream if needed + } + } + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + } + } + } + }