[JAVA-15021] Upgraded to apache httpclient 5.2 (#13309)
Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
parent
aa53dde127
commit
edd27fda3b
@ -112,6 +112,18 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-fluent</artifactId>
|
||||
<version>${httpclient5-fluent.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
@ -308,6 +320,7 @@
|
||||
<!-- http client & core 5 -->
|
||||
<httpcore5.version>5.2</httpcore5.version>
|
||||
<httpclient5.version>5.2</httpclient5.version>
|
||||
<httpclient5-fluent.version>5.2</httpclient5-fluent.version>
|
||||
<!-- maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
@ -1,74 +1,88 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.auth.AuthenticationException;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.fluent.Form;
|
||||
import org.apache.http.client.fluent.Request;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.junit.Test;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.fluent.Form;
|
||||
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
|
||||
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.HttpClients;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.apache.hc.core5.http.NameValuePair;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import com.baeldung.handler.CustomHttpClientResponseHandler;
|
||||
|
||||
/*
|
||||
* NOTE : Need module spring-rest to be running
|
||||
*/
|
||||
public class HttpClientPostingLiveTest {
|
||||
class HttpClientPostingLiveTest {
|
||||
private static final String SAMPLE_URL = "http://www.example.com";
|
||||
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
|
||||
private static final String DEFAULT_USER = "test";
|
||||
private static final String DEFAULT_PASS = "test";
|
||||
|
||||
@Test
|
||||
public void whenSendPostRequestUsingHttpClient_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
void whenSendPostRequestUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
params.add(new BasicNameValuePair("username", DEFAULT_USER));
|
||||
params.add(new BasicNameValuePair("password", DEFAULT_PASS));
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(params));
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect() throws IOException, AuthenticationException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(URL_SECURED_BY_BASIC_AUTHENTICATION);
|
||||
|
||||
httpPost.setEntity(new StringEntity("test post"));
|
||||
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
|
||||
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
final UsernamePasswordCredentials credentials =
|
||||
new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS.toCharArray());
|
||||
|
||||
credsProvider.setCredentials(new AuthScope(URL_SECURED_BY_BASIC_AUTHENTICATION, 80) ,credentials);
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.custom()
|
||||
.setDefaultCredentialsProvider(credsProvider)
|
||||
.build();
|
||||
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostJsonUsingHttpClient_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
void whenPostJsonUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final String json = "{\"id\":1,\"name\":\"John\"}";
|
||||
@ -77,68 +91,91 @@ public class HttpClientPostingLiveTest {
|
||||
httpPost.setHeader("Accept", "application/json");
|
||||
httpPost.setHeader("Content-type", "application/json");
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostFormUsingHttpClientFluentAPI_thenCorrect() throws IOException {
|
||||
final HttpResponse response = Request.Post(SAMPLE_URL).bodyForm(Form.form().add("username", DEFAULT_USER).add("password", DEFAULT_PASS).build()).execute().returnResponse();
|
||||
void whenPostFormUsingHttpClientFluentAPI_thenCorrect() throws IOException {
|
||||
Request request = Request.post(SAMPLE_URL)
|
||||
.bodyForm(Form.form()
|
||||
.add("username", DEFAULT_USER)
|
||||
.add("password", DEFAULT_PASS)
|
||||
.build());
|
||||
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
HttpResponse response = request.execute()
|
||||
.returnResponse();
|
||||
assertThat(response.getCode(), equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMultipartRequestUsingHttpClient_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
void whenSendMultipartRequestUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addTextBody("username", DEFAULT_USER);
|
||||
builder.addTextBody("password", DEFAULT_PASS);
|
||||
builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
builder.addBinaryBody(
|
||||
"file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
|
||||
final HttpEntity multipart = builder.build();
|
||||
httpPost.setEntity(multipart);
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUploadFileUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addBinaryBody(
|
||||
"file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
final HttpEntity multipart = builder.build();
|
||||
|
||||
httpPost.setEntity(multipart);
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUploadFileUsingHttpClient_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
void whenGetUploadFileProgressUsingHttpClient_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
builder.addBinaryBody(
|
||||
"file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
final HttpEntity multipart = builder.build();
|
||||
|
||||
httpPost.setEntity(multipart);
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetUploadFileProgressUsingHttpClient_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.createDefault();
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
|
||||
|
||||
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
|
||||
final HttpEntity multipart = builder.build();
|
||||
|
||||
final ProgressEntityWrapper.ProgressListener pListener = percentage -> assertFalse(Float.compare(percentage, 100) > 0);
|
||||
final ProgressEntityWrapper.ProgressListener pListener =
|
||||
percentage -> assertFalse(Float.compare(percentage, 100) > 0);
|
||||
|
||||
httpPost.setEntity(new ProgressEntityWrapper(multipart, pListener));
|
||||
|
||||
final CloseableHttpResponse response = client.execute(httpPost);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
client.close();
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client
|
||||
.execute(httpPost, new CustomHttpClientResponseHandler())) {
|
||||
|
||||
final int statusCode = response.getCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,8 +4,8 @@ import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.entity.HttpEntityWrapper;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
|
||||
|
||||
public class ProgressEntityWrapper extends HttpEntityWrapper {
|
||||
private final ProgressListener listener;
|
||||
|
Loading…
x
Reference in New Issue
Block a user