Upgrade to HttpClient 5 (#13026)

* [JAVA-14911] Added HttpClient 5.2 dependencies

* [JAVA-14911] Clean up

* [JAVA-14911] Clean up

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos 2022-11-24 19:06:29 +00:00 committed by GitHub
parent 94c33ceeaa
commit d5a255da3e
2 changed files with 54 additions and 33 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>httpclient-simple</artifactId>
<version>0.1-SNAPSHOT</version>
@ -101,6 +101,18 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>${httpcore5.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
@ -130,6 +142,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
@ -283,6 +306,9 @@
<wiremock.version>2.5.1</wiremock.version>
<httpcore.version>4.4.11</httpcore.version>
<httpclient.version>4.5.8</httpclient.version>
<!-- http client & core 5 -->
<httpcore5.version>5.2</httpcore5.version>
<httpclient5.version>5.2</httpclient5.version>
<!-- maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties>

View File

@ -10,17 +10,17 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.net.URIBuilder;
import org.junit.Before;
import org.junit.Test;
@ -36,7 +36,7 @@ public class HttpClientParamsLiveTest {
public void setUp() {
client = HttpClientBuilder.create()
.build();
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs = new ArrayList<>();
NameValuePair param1NameValuePair = new BasicNameValuePair("param1", "value1");
NameValuePair param2NameValuePair = new BasicNameValuePair("param2", "value2");
nameValuePairs.add(param1NameValuePair);
@ -46,54 +46,50 @@ public class HttpClientParamsLiveTest {
@Test
public void givenStringNameValuePairParams_whenGetRequest_thenResponseOk() throws URISyntaxException, ClientProtocolException, IOException {
HttpGet httpGet = new HttpGet("https://postman-echo.com/get");
URI uri = new URIBuilder(httpGet.getURI()).addParameter("param1", "value1")
URI uri = new URIBuilder(httpGet.getUri()).addParameter("param1", "value1")
.addParameter("param2", "value2")
.build();
((HttpRequestBase) httpGet).setURI(uri);
response = client.execute(httpGet);
httpGet.setUri(uri);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
response = client.execute(httpGet);
assertThat(response.getCode(), equalTo(200));
client.close();
}
@Test
public void givenStringNameValuePairParams_whenPostRequest_thenResponseOk() throws URISyntaxException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost("https://postman-echo.com/post");
URI uri = new URIBuilder(httpPost.getURI()).addParameter("param1", "value1")
URI uri = new URIBuilder(httpPost.getUri()).addParameter("param1", "value1")
.addParameter("param2", "value2")
.build();
((HttpRequestBase) httpPost).setURI(uri);
httpPost.setUri(uri);
response = client.execute(httpPost);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
assertThat(response.getCode(), equalTo(200));
client.close();
}
@Test
public void givenNameValuePairParams_whenGetRequest_thenResponseOk() throws URISyntaxException, ClientProtocolException, IOException {
HttpGet httpGet = new HttpGet("https://postman-echo.com/get");
URI uri = new URIBuilder(httpGet.getURI()).addParameters(nameValuePairs)
URI uri = new URIBuilder(httpGet.getUri()).addParameters(nameValuePairs)
.build();
((HttpRequestBase) httpGet).setURI(uri);
httpGet.setUri(uri);
response = client.execute(httpGet);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
assertThat(response.getCode(), equalTo(200));
client.close();
}
@Test
public void givenNameValuePairsParams_whenPostRequest_thenResponseOk() throws URISyntaxException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost("https://postman-echo.com/post");
URI uri = new URIBuilder(httpPost.getURI()).addParameters(nameValuePairs)
URI uri = new URIBuilder(httpPost.getUri()).addParameters(nameValuePairs)
.build();
((HttpRequestBase) httpPost).setURI(uri);
httpPost.setUri(uri);
response = client.execute(httpPost);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
assertThat(response.getCode(), equalTo(200));
client.close();
}
@ -103,8 +99,7 @@ public class HttpClientParamsLiveTest {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
response = client.execute(httpPost);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
assertThat(response.getCode(), equalTo(200));
client.close();
}