File tree Expand file tree Collapse file tree
apache-httpclient4/src/test/java/com/baeldung/httpclient Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .baeldung .httpclient ;
22
3-
43import org .junit .jupiter .api .AfterEach ;
54import org .junit .jupiter .api .BeforeEach ;
65import org .junit .jupiter .api .Test ;
3332import org .apache .http .entity .ContentType ;
3433import org .apache .http .impl .client .CloseableHttpClient ;
3534import org .apache .http .impl .client .HttpClientBuilder ;
35+ import org .apache .http .impl .client .HttpClients ;
3636import org .apache .http .message .BasicNameValuePair ;
3737import 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}
You can’t perform that action at this time.
0 commit comments