From 5c84df1a61a1978a16bd2b7fdfcebf75581d64c1 Mon Sep 17 00:00:00 2001 From: panos-kakos <102670093+panos-kakos@users.noreply.github.com> Date: Mon, 26 Jun 2023 14:39:00 +0300 Subject: [PATCH] Java 15033 Added apache httpclient v4 test case demonstrating close/release resources (#14295) --- .../HttpClientCookBookV4LiveTest.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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)); + } + } + } + }