Java 15033 Added apache httpclient v4 test case demonstrating close/release resources (#14295)

This commit is contained in:
panos-kakos 2023-06-26 14:39:00 +03:00 committed by GitHub
parent 9e144b0994
commit e2976f0686
1 changed files with 19 additions and 1 deletions

View File

@ -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));
}
}
}
}