Skip to content

Commit e2976f0

Browse files
authored
Java 15033 Added apache httpclient v4 test case demonstrating close/release resources (#14295)
1 parent 9e144b0 commit e2976f0

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCookBookV4LiveTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.httpclient;
22

3-
43
import org.junit.jupiter.api.AfterEach;
54
import org.junit.jupiter.api.BeforeEach;
65
import org.junit.jupiter.api.Test;
@@ -33,6 +32,7 @@
3332
import org.apache.http.entity.ContentType;
3433
import org.apache.http.impl.client.CloseableHttpClient;
3534
import org.apache.http.impl.client.HttpClientBuilder;
35+
import org.apache.http.impl.client.HttpClients;
3636
import org.apache.http.message.BasicNameValuePair;
3737
import org.apache.http.util.EntityUtils;
3838

@@ -163,4 +163,22 @@ void givenStreamIsClosed_thenCloseResponse() throws IOException {
163163
response.close();
164164
}
165165
}
166+
167+
@Test
168+
void givenAutoClosableClient_thenCorrect() throws IOException {
169+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
170+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
171+
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
172+
// handle response;
173+
HttpEntity entity = response.getEntity();
174+
if (entity != null) {
175+
try (InputStream instream = entity.getContent()) {
176+
// Process the input stream if needed
177+
}
178+
}
179+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
180+
}
181+
}
182+
}
183+
166184
}

0 commit comments

Comments
 (0)