From 5b9e023fd3516895fe5b80380ce19e22af9af99b Mon Sep 17 00:00:00 2001 From: panos-kakos <102670093+panos-kakos@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:18:26 +0300 Subject: [PATCH] [JAVA-15031] Upgraded code for apache httpclient + moved old version to apache-httpclient4 module. (#13830) --- .../HttpClientCancelRequestLiveTest.java | 40 +++++++++++++ .../httpclient/base/HttpClientLiveTest.java | 29 +--------- apache-httpclient4/README.md | 1 + .../HttpClientCancelRequestV4LiveTest.java | 57 +++++++++++++++++++ 4 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestLiveTest.java create mode 100644 apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestLiveTest.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestLiveTest.java new file mode 100644 index 0000000000..d19e0e1d86 --- /dev/null +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestLiveTest.java @@ -0,0 +1,40 @@ +package com.baeldung.httpclient; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpStatus; +import org.junit.jupiter.api.Test; + +class HttpClientCancelRequestLiveTest { + + private static final String SAMPLE_URL = "http://www.github.com"; + + @Test + void whenRequestIsCanceled_thenCorrect() throws IOException { + HttpGet request = new HttpGet(SAMPLE_URL); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + httpClient.execute(request, response -> { + HttpEntity entity = response.getEntity(); + + System.out.println("----------------------------------------"); + System.out.println(response.getCode()); + if (entity != null) { + System.out.println("Response content length: " + entity.getContentLength()); + } + System.out.println("----------------------------------------"); + // Do not feel like reading the response body + // Call abort on the request object + request.abort(); + + assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK); + return response; + }); + } + } +} diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientLiveTest.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientLiveTest.java index ef12c37412..4173909f7d 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientLiveTest.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientLiveTest.java @@ -1,8 +1,8 @@ package com.baeldung.httpclient.base; import com.baeldung.httpclient.ResponseUtil; + import org.apache.http.Header; -import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; @@ -10,7 +10,6 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ConnectTimeoutException; 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.impl.conn.BasicHttpClientConnectionManager; import org.junit.After; import org.junit.Before; @@ -73,30 +72,4 @@ public class HttpClientLiveTest { assertThat(headers, not(emptyArray())); } - // tests - cancel request - - @Test - public final void whenRequestIsCanceled_thenCorrect() throws IOException { - instance = HttpClients.custom().build(); - final HttpGet request = new HttpGet(SAMPLE_URL); - response = instance.execute(request); - - try { - final HttpEntity entity = response.getEntity(); - - System.out.println("----------------------------------------"); - System.out.println(response.getStatusLine()); - if (entity != null) { - System.out.println("Response content length: " + entity.getContentLength()); - } - System.out.println("----------------------------------------"); - - // Do not feel like reading the response body - // Call abort on the request object - request.abort(); - } finally { - response.close(); - } - } - } diff --git a/apache-httpclient4/README.md b/apache-httpclient4/README.md index 9c18e06243..22b0391e6b 100644 --- a/apache-httpclient4/README.md +++ b/apache-httpclient4/README.md @@ -4,6 +4,7 @@ This module contains articles about Apache HttpClient 4.5 ### Relevant Articles +- [Apache HttpClient – Cancel Request](https://www.baeldung.com/httpclient-cancel-request) - [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl) - [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout) - [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header) diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java new file mode 100644 index 0000000000..226a7b8cf7 --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java @@ -0,0 +1,57 @@ +package com.baeldung.httpclient; + +import java.io.IOException; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HttpClientCancelRequestV4LiveTest { + + private static final String SAMPLE_URL = "http://www.github.com"; + + private CloseableHttpClient instance; + + private CloseableHttpResponse response; + + @Before + public final void before() { + instance = HttpClientBuilder.create().build(); + } + + @After + public final void after() throws IllegalStateException, IOException { + ResponseUtil.closeResponse(response); + } + + @Test + public final void whenRequestIsCanceled_thenCorrect() throws IOException { + instance = HttpClients.custom().build(); + final HttpGet request = new HttpGet(SAMPLE_URL); + response = instance.execute(request); + + try { + final HttpEntity entity = response.getEntity(); + + System.out.println("----------------------------------------"); + System.out.println(response.getStatusLine()); + if (entity != null) { + System.out.println("Response content length: " + entity.getContentLength()); + } + System.out.println("----------------------------------------"); + + // Do not feel like reading the response body + // Call abort on the request object + request.abort(); + } finally { + response.close(); + } + } +}