JAVA-17488 (#14694)
Moved remaining apache-httpclient v4 code from apache-httpclient to apache-httpclient4
This commit is contained in:
parent
d167545325
commit
ac590b3823
@ -16,7 +16,7 @@ public class ApacheHttpClient5UnitTest {
|
|||||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
||||||
HttpGet request = new HttpGet(DUMMY_URL);
|
HttpGet request = new HttpGet(DUMMY_URL);
|
||||||
|
|
||||||
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.httpclient.readresponsebodystring;
|
package com.baeldung.httpclient.readresponsebodystring;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -8,11 +7,13 @@ import java.net.http.HttpClient;
|
|||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
public class HttpClientUnitTest {
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class HttpClientUnitTest {
|
||||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException {
|
void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build();
|
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build();
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.httpclient.readresponsebodystring;
|
package com.baeldung.httpclient.readresponsebodystring;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -10,12 +10,14 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HttpUrlConnectionUnitTest {
|
public class HttpUrlConnectionUnitTest {
|
||||||
|
|
||||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseHttpUrlConnection_thenCorrect() throws IOException {
|
void whenUseHttpUrlConnection_thenCorrect() throws IOException {
|
||||||
HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection();
|
HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection();
|
||||||
|
|
||||||
InputStream inputStream = connection.getInputStream();
|
InputStream inputStream = connection.getInputStream();
|
||||||
@ -28,7 +30,7 @@ public class HttpUrlConnectionUnitTest {
|
|||||||
response.append(currentLine);
|
response.append(currentLine);
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
Assert.assertNotNull(response.toString());
|
assertNotNull(response.toString());
|
||||||
System.out.println("Response -> " + response.toString());
|
System.out.println("Response -> " + response.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.httpclient.readresponsebodystring;
|
package com.baeldung.httpclient.readresponsebodystring;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
public class SpringRestTemplateUnitTest {
|
public class SpringRestTemplateUnitTest {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.httpclient.readresponsebodystring;
|
package com.baeldung.httpclient.readresponsebodystring;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ public class SpringWebClientUnitTest {
|
|||||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseWebClientRetrieve_thenCorrect() {
|
void whenUseWebClientRetrieve_thenCorrect() {
|
||||||
WebClient webClient = WebClient.create(DUMMY_URL);
|
WebClient webClient = WebClient.create(DUMMY_URL);
|
||||||
Mono<String> body = webClient.get().retrieve().bodyToMono(String.class);
|
Mono<String> body = webClient.get().retrieve().bodyToMono(String.class);
|
||||||
String s = body.block();
|
String s = body.block();
|
||||||
|
@ -15,45 +15,6 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- http client -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
<version>${httpclient.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>fluent-hc</artifactId>
|
|
||||||
<version>${httpclient.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpmime</artifactId>
|
|
||||||
<version>${httpclient.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpasyncclient</artifactId>
|
|
||||||
<version>${httpasyncclient.version}</version> <!-- 4.0.2 --> <!-- 4.1-beta1 -->
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.core5</groupId>
|
<groupId>org.apache.httpcomponents.core5</groupId>
|
||||||
<artifactId>httpcore5</artifactId>
|
<artifactId>httpcore5</artifactId>
|
||||||
@ -115,12 +76,8 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- util -->
|
|
||||||
<httpasyncclient.version>4.1.4</httpasyncclient.version>
|
|
||||||
<!-- testing -->
|
|
||||||
<mockserver.version>5.6.1</mockserver.version>
|
<mockserver.version>5.6.1</mockserver.version>
|
||||||
<wiremock.version>2.5.1</wiremock.version>
|
<wiremock.version>2.5.1</wiremock.version>
|
||||||
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
|
||||||
<!-- http client & core 5 -->
|
<!-- http client & core 5 -->
|
||||||
<httpcore5.version>5.2</httpcore5.version>
|
<httpcore5.version>5.2</httpcore5.version>
|
||||||
<httpclient5.version>5.2</httpclient5.version>
|
<httpclient5.version>5.2</httpclient5.version>
|
||||||
|
@ -4,10 +4,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import org.apache.hc.core5.http.ParseException;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||||
import org.apache.hc.client5.http.entity.mime.FileBody;
|
import org.apache.hc.client5.http.entity.mime.FileBody;
|
||||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||||
@ -19,6 +19,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
|||||||
import org.apache.hc.core5.http.ContentType;
|
import org.apache.hc.core5.http.ContentType;
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.HttpStatus;
|
import org.apache.hc.core5.http.HttpStatus;
|
||||||
|
import org.apache.hc.core5.http.ParseException;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -30,9 +31,6 @@ import java.net.URL;
|
|||||||
|
|
||||||
class HttpClientMultipartLiveTest extends GetRequestMockServer {
|
class HttpClientMultipartLiveTest extends GetRequestMockServer {
|
||||||
|
|
||||||
// No longer available
|
|
||||||
// private static final String SERVER = "http://echo.200please.com";
|
|
||||||
|
|
||||||
private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart";
|
private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart";
|
||||||
private static final String TEXTFILENAME = "temp.txt";
|
private static final String TEXTFILENAME = "temp.txt";
|
||||||
private static final String IMAGEFILENAME = "image.jpg";
|
private static final String IMAGEFILENAME = "image.jpg";
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package com.baeldung.httpclient;
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||||
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
|
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
|
||||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
|||||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -29,21 +29,34 @@ import org.apache.hc.core5.http.HttpHeaders;
|
|||||||
import org.apache.hc.core5.http.HttpHost;
|
import org.apache.hc.core5.http.HttpHost;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
import org.apache.hc.core5.http.HttpResponse;
|
||||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HttpClientAdvancedConfigurationIntegrationTest {
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
|
||||||
@Rule
|
class HttpClientAdvancedConfigurationIntegrationTest {
|
||||||
public WireMockRule serviceMock = new WireMockRule(8089);
|
|
||||||
|
|
||||||
@Rule
|
public WireMockServer serviceMock;
|
||||||
public WireMockRule proxyMock = new WireMockRule(8090);
|
public WireMockServer proxyMock;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void before () {
|
||||||
|
serviceMock = new WireMockServer(8089);
|
||||||
|
serviceMock.start();
|
||||||
|
proxyMock = new WireMockServer(8090);
|
||||||
|
proxyMock.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void after () {
|
||||||
|
serviceMock.stop();
|
||||||
|
proxyMock.stop();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
String userAgent = "BaeldungAgent/1.0";
|
String userAgent = "BaeldungAgent/1.0";
|
||||||
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
||||||
@ -59,11 +72,11 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpClient.execute(httpGet);
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getCode(), 200);
|
assertEquals(200, response.getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
String xmlBody = "<xml><id>1</id></xml>";
|
String xmlBody = "<xml><id>1</id></xml>";
|
||||||
serviceMock.stubFor(post(urlEqualTo("/person"))
|
serviceMock.stubFor(post(urlEqualTo("/person"))
|
||||||
@ -82,12 +95,12 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpClient.execute(httpPost);
|
HttpResponse response = httpClient.execute(httpPost);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getCode(), 200);
|
assertEquals(200, response.getCode());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
proxyMock.stubFor(get(urlMatching(".*"))
|
proxyMock.stubFor(get(urlMatching(".*"))
|
||||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
@ -107,7 +120,7 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpclient.execute(httpGet);
|
HttpResponse response = httpclient.execute(httpGet);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getCode(), 200);
|
assertEquals(200, response.getCode());
|
||||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
}
|
}
|
||||||
@ -151,7 +164,7 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpclient.execute(httpGet, context);
|
HttpResponse response = httpclient.execute(httpGet, context);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getCode(), 200);
|
assertEquals(200, response.getCode());
|
||||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
||||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
}
|
}
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
package com.baeldung.httpclient.base;
|
|
||||||
|
|
||||||
import com.baeldung.httpclient.ResponseUtil;
|
|
||||||
import org.apache.http.auth.AuthenticationException;
|
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.impl.auth.BasicScheme;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class HttpClientBasicPostLiveTest {
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests - non-GET
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void whenExecutingPostRequest_thenNoExceptions() throws IOException {
|
|
||||||
instance.execute(new HttpPost(SAMPLE_URL));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException {
|
|
||||||
final HttpPost request = new HttpPost(SAMPLE_URL);
|
|
||||||
request.setEntity(new StringEntity("in the body of the POST"));
|
|
||||||
instance.execute(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
|
|
||||||
final HttpPost request = new HttpPost(SAMPLE_URL);
|
|
||||||
request.setEntity(new StringEntity("in the body of the POST"));
|
|
||||||
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
|
|
||||||
request.addHeader(new BasicScheme().authenticate(creds, request, null));
|
|
||||||
instance.execute(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
package com.baeldung.httpclient.base;
|
|
||||||
|
|
||||||
import com.baeldung.httpclient.ResponseUtil;
|
|
||||||
|
|
||||||
import org.apache.http.Header;
|
|
||||||
import org.apache.http.HttpHeaders;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
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.conn.BasicHttpClientConnectionManager;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.emptyArray;
|
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
public class HttpClientLiveTest {
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests
|
|
||||||
|
|
||||||
@Test(expected = ConnectTimeoutException.class)
|
|
||||||
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
|
||||||
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5).setConnectTimeout(5).setSocketTimeout(2).build();
|
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
|
||||||
request.setConfig(requestConfig);
|
|
||||||
response = instance.execute(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests - configs
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws IOException {
|
|
||||||
instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build();
|
|
||||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws IOException {
|
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
|
||||||
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
|
||||||
response = instance.execute(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
|
||||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
|
||||||
|
|
||||||
final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
|
||||||
assertThat(headers, not(emptyArray()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.httpclient.conn;
|
package com.baeldung.httpclient.conn;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
@ -34,14 +35,13 @@ import org.apache.hc.core5.pool.PoolStats;
|
|||||||
import org.apache.hc.core5.util.Args;
|
import org.apache.hc.core5.util.Args;
|
||||||
import org.apache.hc.core5.util.TimeValue;
|
import org.apache.hc.core5.util.TimeValue;
|
||||||
import org.apache.hc.core5.util.Timeout;
|
import org.apache.hc.core5.util.Timeout;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class HttpClientConnectionManagementLiveTest {
|
class HttpClientConnectionManagementLiveTest {
|
||||||
|
|
||||||
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
|
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
|
||||||
@Test
|
@Test
|
||||||
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException {
|
final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException {
|
||||||
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
|
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
|
||||||
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
|
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
|
||||||
final LeaseRequest connRequest = connMgr.lease("some-id", route, null);
|
final LeaseRequest connRequest = connMgr.lease("some-id", route, null);
|
||||||
@ -51,7 +51,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
|
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
|
||||||
@Test
|
@Test
|
||||||
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws IOException {
|
final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws IOException {
|
||||||
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(poolingConnManager)
|
.setConnectionManager(poolingConnManager)
|
||||||
@ -65,7 +65,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each
|
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each
|
||||||
@Test
|
@Test
|
||||||
public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException, IOException {
|
final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException, IOException {
|
||||||
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
||||||
HttpGet get2 = new HttpGet("https://www.google.com");
|
HttpGet get2 = new HttpGet("https://www.google.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
@ -83,8 +83,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread1.join();
|
thread1.join();
|
||||||
thread2.join();
|
thread2.join();
|
||||||
|
|
||||||
Assert.assertTrue(connManager.getTotalStats()
|
assertEquals(0, connManager.getTotalStats().getLeased());
|
||||||
.getLeased() == 0);
|
|
||||||
client1.close();
|
client1.close();
|
||||||
client2.close();
|
client2.close();
|
||||||
connManager.close();
|
connManager.close();
|
||||||
@ -92,7 +91,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
|
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
|
||||||
@Test
|
@Test
|
||||||
public final void whenIncreasingConnectionPool_thenNoExceptions() {
|
final void whenIncreasingConnectionPool_thenNoExceptions() {
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
connManager.setMaxTotal(5);
|
connManager.setMaxTotal(5);
|
||||||
connManager.setDefaultMaxPerRoute(4);
|
connManager.setDefaultMaxPerRoute(4);
|
||||||
@ -103,7 +102,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 4.2. Using Threads to Execute Connections
|
// Example 4.2. Using Threads to Execute Connections
|
||||||
@Test
|
@Test
|
||||||
public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteRequest() throws InterruptedException, IOException {
|
final void whenExecutingSameRequestsInDifferentThreads_thenExecuteRequest() throws InterruptedException, IOException {
|
||||||
HttpGet get = new HttpGet("http://www.baeldung.com");
|
HttpGet get = new HttpGet("http://www.baeldung.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
@ -133,7 +132,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 5.1. A Custom Keep Alive Strategy
|
// Example 5.1. A Custom Keep Alive Strategy
|
||||||
@Test
|
@Test
|
||||||
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
|
final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
|
||||||
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
|
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
|
public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
|
||||||
@ -162,7 +161,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
//Example 6.1. BasicHttpClientConnectionManager Connection Reuse
|
//Example 6.1. BasicHttpClientConnectionManager Connection Reuse
|
||||||
@Test
|
@Test
|
||||||
public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, TimeoutException, IOException, URISyntaxException {
|
final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, TimeoutException, IOException, URISyntaxException {
|
||||||
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
|
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
|
||||||
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
|
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
|
||||||
final HttpContext context = new BasicHttpContext();
|
final HttpContext context = new BasicHttpContext();
|
||||||
@ -184,7 +183,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
|
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
|
||||||
@Test
|
@Test
|
||||||
public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException, IOException {
|
final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException, IOException {
|
||||||
HttpGet get = new HttpGet("http://www.baeldung.com");
|
HttpGet get = new HttpGet("http://www.baeldung.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
connManager.setDefaultMaxPerRoute(6);
|
connManager.setDefaultMaxPerRoute(6);
|
||||||
@ -208,7 +207,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 7.1. Setting Socket Timeout to 5 Seconds
|
// Example 7.1. Setting Socket Timeout to 5 Seconds
|
||||||
@Test
|
@Test
|
||||||
public final void whenConfiguringTimeOut_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException, IOException {
|
final void whenConfiguringTimeOut_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException, IOException {
|
||||||
final HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
final HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
||||||
final HttpContext context = new BasicHttpContext();
|
final HttpContext context = new BasicHttpContext();
|
||||||
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
@ -227,7 +226,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 8.1. Setting the HttpClient to Check for Stale Connections
|
// Example 8.1. Setting the HttpClient to Check for Stale Connections
|
||||||
@Test
|
@Test
|
||||||
public final void whenEvictIdealConn_thenNoExceptions() throws InterruptedException, IOException {
|
final void whenEvictIdealConn_thenNoExceptions() throws InterruptedException, IOException {
|
||||||
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
connManager.setMaxTotal(100);
|
connManager.setMaxTotal(100);
|
||||||
try (final CloseableHttpClient httpclient = HttpClients.custom()
|
try (final CloseableHttpClient httpclient = HttpClients.custom()
|
||||||
@ -266,7 +265,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 9.1. Closing Connection and Releasing Resources
|
// Example 9.1. Closing Connection and Releasing Resources
|
||||||
@Test
|
@Test
|
||||||
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws IOException {
|
final void whenClosingConnectionsAndManager_thenCloseWithNoExceptions1() throws IOException {
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(connManager)
|
.setConnectionManager(connManager)
|
||||||
@ -282,7 +281,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
// Example 3.2. TESTER VERSION
|
// Example 3.2. TESTER VERSION
|
||||||
public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException, IOException {
|
final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException, IOException {
|
||||||
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
||||||
HttpGet get2 = new HttpGet("https://www.google.com");
|
HttpGet get2 = new HttpGet("https://www.google.com");
|
||||||
|
|
||||||
@ -300,8 +299,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread2.start();
|
thread2.start();
|
||||||
thread1.join();
|
thread1.join();
|
||||||
thread2.join(1000);
|
thread2.join(1000);
|
||||||
Assert.assertTrue(poolingConnManager.getTotalStats()
|
assertEquals(2, poolingConnManager.getTotalStats().getLeased());
|
||||||
.getLeased() == 2);
|
|
||||||
|
|
||||||
client1.close();
|
client1.close();
|
||||||
client2.close();
|
client2.close();
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
package com.baeldung.httpclient.conn;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.apache.http.conn.HttpClientConnectionManager;
|
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
||||||
|
|
||||||
public class IdleConnectionMonitorThread extends Thread {
|
|
||||||
private final HttpClientConnectionManager connMgr;
|
|
||||||
private volatile boolean shutdown;
|
|
||||||
|
|
||||||
IdleConnectionMonitorThread(final PoolingHttpClientConnectionManager connMgr) {
|
|
||||||
super();
|
|
||||||
this.connMgr = connMgr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void run() {
|
|
||||||
try {
|
|
||||||
while (!shutdown) {
|
|
||||||
synchronized (this) {
|
|
||||||
wait(1000);
|
|
||||||
connMgr.closeExpiredConnections();
|
|
||||||
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final InterruptedException ex) {
|
|
||||||
shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void shutdown() {
|
|
||||||
shutdown = true;
|
|
||||||
synchronized (this) {
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -6,7 +6,6 @@ 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.CloseableHttpClient;
|
||||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
|
||||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.httpclient.base;
|
package com.baeldung.client;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.baeldung.httpclient.GetRequestMockServer;
|
|
||||||
import com.baeldung.httpclient.ResponseUtil;
|
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
@ -12,15 +12,16 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.baeldung.GetRequestMockServer;
|
||||||
|
import com.baeldung.httpclient.ResponseUtil;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* NOTE : Need module spring-security-rest-basic-auth to be running
|
* NOTE : Need module spring-security-rest-basic-auth to be running
|
||||||
*/
|
*/
|
||||||
public class HttpClientSandboxLiveTest extends GetRequestMockServer {
|
class HttpClientSandboxLiveTest extends GetRequestMockServer {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
|
final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
|
||||||
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
final AuthScope authscp = new AuthScope("localhost", 8080);
|
final AuthScope authscp = new AuthScope("localhost", 8080);
|
||||||
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
|
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
|
@ -35,7 +35,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
* This test requires a localhost server over HTTPS <br>
|
* This test requires a localhost server over HTTPS <br>
|
||||||
* It should only be manually run, not part of the automated build
|
* It should only be manually run, not part of the automated build
|
||||||
* */
|
* */
|
||||||
public class RestClientV4LiveManualTest {
|
class RestClientV4LiveManualTest {
|
||||||
|
|
||||||
final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1";
|
final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1";
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ public class RestClientV4LiveManualTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
||||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
String urlOverHttps = "https://localhost:8082/httpclient-simple";
|
String urlOverHttps = "https://localhost:8082/httpclient-simple";
|
||||||
HttpGet getMethod = new HttpGet(urlOverHttps);
|
HttpGet getMethod = new HttpGet(urlOverHttps);
|
||||||
|
@ -2,8 +2,6 @@ package com.baeldung.httpclient;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
|
||||||
public final class ClientUtil {
|
public final class ClientUtil {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package com.baeldung.httpclient;
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
@ -9,11 +9,11 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HttpClientCancelRequestV4LiveTest {
|
class HttpClientCancelRequestV4LiveTest {
|
||||||
|
|
||||||
private static final String SAMPLE_URL = "http://www.github.com";
|
private static final String SAMPLE_URL = "http://www.github.com";
|
||||||
|
|
||||||
@ -21,18 +21,18 @@ public class HttpClientCancelRequestV4LiveTest {
|
|||||||
|
|
||||||
private CloseableHttpResponse response;
|
private CloseableHttpResponse response;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public final void before() {
|
public final void before() {
|
||||||
instance = HttpClientBuilder.create().build();
|
instance = HttpClientBuilder.create().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@AfterEach
|
||||||
public final void after() throws IllegalStateException, IOException {
|
public final void after() throws IllegalStateException, IOException {
|
||||||
ResponseUtil.closeResponse(response);
|
ResponseUtil.closeResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
|
final void whenRequestIsCanceled_thenCorrect() throws IOException {
|
||||||
instance = HttpClients.custom().build();
|
instance = HttpClients.custom().build();
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||||
response = instance.execute(request);
|
response = instance.execute(request);
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package com.baeldung.httpclient;
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
|
import org.apache.http.auth.AuthenticationException;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.auth.BasicScheme;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -181,4 +185,20 @@ class HttpClientCookBookV4LiveTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
final void whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException {
|
||||||
|
final HttpPost request = new HttpPost(SAMPLE_POST_URL);
|
||||||
|
request.setEntity(new StringEntity("in the body of the POST"));
|
||||||
|
client.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
|
||||||
|
final HttpPost request = new HttpPost(SAMPLE_POST_URL);
|
||||||
|
request.setEntity(new StringEntity("in the body of the POST"));
|
||||||
|
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
|
||||||
|
request.addHeader(new BasicScheme().authenticate(creds, request, null));
|
||||||
|
client.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package com.baeldung.httpclient;
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
@ -92,6 +92,26 @@ class HttpClientTimeoutV4LiveTest extends GetRequestMockServer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() {
|
||||||
|
final RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(5)
|
||||||
|
.setConnectTimeout(5)
|
||||||
|
.setSocketTimeout(2)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.setDefaultRequestConfig(requestConfig)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
|
assertThrows(ConnectTimeoutException.class, () -> {
|
||||||
|
response = client.execute(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenSecuredRestApiIsConsumed_then200OK() throws IOException {
|
void whenSecuredRestApiIsConsumed_then200OK() throws IOException {
|
||||||
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.httpclient.advancedconfig;
|
package com.baeldung.httpclient.advancedconfig;
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
|
||||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
@ -19,8 +19,10 @@ import org.apache.http.impl.client.BasicAuthCache;
|
|||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -32,18 +34,29 @@ import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
|||||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class HttpClientAdvancedConfigurationIntegrationTest {
|
class HttpClientAdvancedConfigurationIntegrationTest {
|
||||||
|
|
||||||
@Rule
|
public WireMockServer serviceMock;
|
||||||
public WireMockRule serviceMock = new WireMockRule(8089);
|
public WireMockServer proxyMock;
|
||||||
|
|
||||||
@Rule
|
@BeforeEach
|
||||||
public WireMockRule proxyMock = new WireMockRule(8090);
|
public void before () {
|
||||||
|
serviceMock = new WireMockServer(8089);
|
||||||
|
serviceMock.start();
|
||||||
|
proxyMock = new WireMockServer(8090);
|
||||||
|
proxyMock.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void after () {
|
||||||
|
serviceMock.stop();
|
||||||
|
proxyMock.stop();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
String userAgent = "BaeldungAgent/1.0";
|
String userAgent = "BaeldungAgent/1.0";
|
||||||
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
||||||
@ -59,11 +72,11 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpClient.execute(httpGet);
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
String xmlBody = "<xml><id>1</id></xml>";
|
String xmlBody = "<xml><id>1</id></xml>";
|
||||||
serviceMock.stubFor(post(urlEqualTo("/person"))
|
serviceMock.stubFor(post(urlEqualTo("/person"))
|
||||||
@ -82,12 +95,11 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpClient.execute(httpPost);
|
HttpResponse response = httpClient.execute(httpPost);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
||||||
//given
|
//given
|
||||||
proxyMock.stubFor(get(urlMatching(".*"))
|
proxyMock.stubFor(get(urlMatching(".*"))
|
||||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
@ -107,13 +119,13 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpclient.execute(httpGet);
|
HttpResponse response = httpclient.execute(httpGet);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
||||||
//given
|
//given
|
||||||
proxyMock.stubFor(get(urlMatching("/private"))
|
proxyMock.stubFor(get(urlMatching("/private"))
|
||||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
@ -152,7 +164,7 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
|
|||||||
HttpResponse response = httpclient.execute(httpGet, context);
|
HttpResponse response = httpclient.execute(httpGet, context);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
||||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
}
|
}
|
||||||
|
@ -12,34 +12,35 @@ import org.apache.http.client.methods.HttpHead;
|
|||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
public class HttpClientExpandUrlLiveTest {
|
|
||||||
|
class HttpClientExpandUrlLiveTest {
|
||||||
|
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public final void before() {
|
public final void beforeEach() {
|
||||||
client = HttpClientBuilder.create().disableRedirectHandling().build();
|
client = HttpClientBuilder.create().disableRedirectHandling().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
final void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
||||||
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
||||||
final String actualResult = expandSingleLevel("http://bit.ly/3LScTri");
|
final String actualResult = expandSingleLevel("http://bit.ly/3LScTri");
|
||||||
assertThat(actualResult, equalTo(expectedResult));
|
assertThat(actualResult, equalTo(expectedResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
final void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
||||||
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
||||||
final String actualResult = expand("http://t.co/e4rDDbnzmk");
|
final String actualResult = expand("http://t.co/e4rDDbnzmk");
|
||||||
assertThat(actualResult, equalTo(expectedResult));
|
assertThat(actualResult, equalTo(expectedResult));
|
||||||
|
@ -17,7 +17,6 @@ import com.baeldung.GetRequestMockServer;
|
|||||||
|
|
||||||
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.baeldung.httpclient.httpclient.conn;
|
package com.baeldung.httpclient.httpclient.conn;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@ -24,6 +26,7 @@ import org.apache.http.conn.ConnectionPoolTimeoutException;
|
|||||||
import org.apache.http.conn.ConnectionRequest;
|
import org.apache.http.conn.ConnectionRequest;
|
||||||
import org.apache.http.conn.routing.HttpRoute;
|
import org.apache.http.conn.routing.HttpRoute;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
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.client.HttpClients;
|
||||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
@ -33,14 +36,14 @@ import org.apache.http.protocol.HttpContext;
|
|||||||
import org.apache.http.protocol.HttpCoreContext;
|
import org.apache.http.protocol.HttpCoreContext;
|
||||||
import org.apache.http.protocol.HttpRequestExecutor;
|
import org.apache.http.protocol.HttpRequestExecutor;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.junit.Ignore;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HttpClientConnectionManagementLiveTest {
|
class HttpClientConnectionManagementLiveTest {
|
||||||
|
|
||||||
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
|
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
|
||||||
@Test
|
@Test
|
||||||
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException {
|
final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException {
|
||||||
try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) {
|
try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) {
|
||||||
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
||||||
final ConnectionRequest connRequest = connManager.requestConnection(route, null);
|
final ConnectionRequest connRequest = connManager.requestConnection(route, null);
|
||||||
@ -50,20 +53,20 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
|
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
|
||||||
@Test
|
@Test
|
||||||
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException {
|
final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||||
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(poolingConnManager)
|
.setConnectionManager(poolingConnManager)
|
||||||
.build();
|
.build();
|
||||||
client.execute(new HttpGet("https://www.baeldung.com"));
|
client.execute(new HttpGet("https://www.baeldung.com"));
|
||||||
|
|
||||||
assertTrue(poolingConnManager.getTotalStats()
|
assertEquals(1, poolingConnManager.getTotalStats()
|
||||||
.getLeased() == 1);
|
.getLeased());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each
|
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each
|
||||||
@Test
|
@Test
|
||||||
public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException {
|
final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException {
|
||||||
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
||||||
HttpGet get2 = new HttpGet("https://www.google.com");
|
HttpGet get2 = new HttpGet("https://www.google.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
@ -81,13 +84,13 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread1.join();
|
thread1.join();
|
||||||
thread2.join();
|
thread2.join();
|
||||||
|
|
||||||
assertTrue(connManager.getTotalStats()
|
assertEquals(0, connManager.getTotalStats()
|
||||||
.getLeased() == 0);
|
.getLeased());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
|
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
|
||||||
@Test
|
@Test
|
||||||
public final void whenIncreasingConnectionPool_thenNoEceptions() {
|
final void whenIncreasingConnectionPool_thenNoEceptions() {
|
||||||
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
|
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
|
||||||
connManager.setMaxTotal(5);
|
connManager.setMaxTotal(5);
|
||||||
connManager.setDefaultMaxPerRoute(4);
|
connManager.setDefaultMaxPerRoute(4);
|
||||||
@ -98,7 +101,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 4.2. Using Threads to Execute Connections
|
// Example 4.2. Using Threads to Execute Connections
|
||||||
@Test
|
@Test
|
||||||
public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException {
|
final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException {
|
||||||
HttpGet get = new HttpGet("http://www.baeldung.com");
|
HttpGet get = new HttpGet("http://www.baeldung.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
@ -117,7 +120,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 5.1. A Custom Keep Alive Strategy
|
// Example 5.1. A Custom Keep Alive Strategy
|
||||||
@Test
|
@Test
|
||||||
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
|
final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
|
||||||
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
|
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
|
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
|
||||||
@ -148,7 +151,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 6.1. BasicHttpClientConnectionManager Connection Reuse
|
// Example 6.1. BasicHttpClientConnectionManager Connection Reuse
|
||||||
@Test
|
@Test
|
||||||
public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
|
final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
|
||||||
BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager();
|
BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager();
|
||||||
HttpClientContext context = HttpClientContext.create();
|
HttpClientContext context = HttpClientContext.create();
|
||||||
|
|
||||||
@ -175,7 +178,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
|
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
|
||||||
@Test
|
@Test
|
||||||
public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
|
final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
|
||||||
HttpGet get = new HttpGet("http://echo.200please.com");
|
HttpGet get = new HttpGet("http://echo.200please.com");
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
connManager.setDefaultMaxPerRoute(5);
|
connManager.setDefaultMaxPerRoute(5);
|
||||||
@ -197,20 +200,20 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 7.1. Setting Socket Timeout to 5 Seconds
|
// Example 7.1. Setting Socket Timeout to 5 Seconds
|
||||||
@Test
|
@Test
|
||||||
public final void whenConfiguringTimeOut_thenNoExceptions() {
|
final void whenConfiguringTimeOut_thenNoExceptions() {
|
||||||
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
|
||||||
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
|
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
|
||||||
connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom()
|
connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom()
|
||||||
.setSoTimeout(5000)
|
.setSoTimeout(5000)
|
||||||
.build());
|
.build());
|
||||||
assertTrue(connManager.getSocketConfig(route.getTargetHost())
|
assertEquals(5000, connManager.getSocketConfig(route.getTargetHost())
|
||||||
.getSoTimeout() == 5000);
|
.getSoTimeout());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example 8.1. Setting the HttpClient to Check for Stale Connections
|
// Example 8.1. Setting the HttpClient to Check for Stale Connections
|
||||||
@Test
|
@Test
|
||||||
public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
|
final void whenHttpClientChecksStaleConns_thenNoExceptions() {
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
HttpClients.custom()
|
HttpClients.custom()
|
||||||
.setDefaultRequestConfig(RequestConfig.custom()
|
.setDefaultRequestConfig(RequestConfig.custom()
|
||||||
@ -222,7 +225,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
// Example 8.2. Using a Stale Connection Monitor Thread
|
// Example 8.2. Using a Stale Connection Monitor Thread
|
||||||
@Test
|
@Test
|
||||||
public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException {
|
final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException {
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
HttpClients.custom()
|
HttpClients.custom()
|
||||||
.setConnectionManager(connManager)
|
.setConnectionManager(connManager)
|
||||||
@ -233,8 +236,8 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Example 9.1. Closing Connection and Releasing Resources
|
// Example 9.1. Closing Connection and Releasing Resources
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test
|
||||||
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws InterruptedException, ExecutionException, IOException, HttpException {
|
final void whenClosingConnectionsAndManager_thenCloseWithNoExceptions1() throws IOException {
|
||||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(connManager)
|
.setConnectionManager(connManager)
|
||||||
@ -248,14 +251,14 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
connManager.close();
|
connManager.close();
|
||||||
connManager.shutdown();
|
connManager.shutdown();
|
||||||
|
|
||||||
|
assertThrows(IllegalStateException.class, () -> {
|
||||||
client.execute(get);
|
client.execute(get);
|
||||||
|
});
|
||||||
assertTrue(response.getEntity() == null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// Example 3.2. TESTER VERSION
|
// Example 3.2. TESTER VERSION
|
||||||
public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException {
|
final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException {
|
||||||
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
HttpGet get1 = new HttpGet("https://www.baeldung.com");
|
||||||
HttpGet get2 = new HttpGet("https://www.google.com");
|
HttpGet get2 = new HttpGet("https://www.google.com");
|
||||||
|
|
||||||
@ -273,13 +276,13 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread2.start();
|
thread2.start();
|
||||||
thread1.join();
|
thread1.join();
|
||||||
thread2.join(1000);
|
thread2.join(1000);
|
||||||
assertTrue(poolingConnManager.getTotalStats()
|
assertEquals(2, poolingConnManager.getTotalStats()
|
||||||
.getLeased() == 2);
|
.getLeased());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// Example 4.2 Tester Version
|
// Example 4.2 Tester Version
|
||||||
public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException {
|
final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException {
|
||||||
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(poolingConnManager)
|
.setConnectionManager(poolingConnManager)
|
||||||
@ -297,7 +300,7 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
// 6.2 TESTER VERSION
|
// 6.2 TESTER VERSION
|
||||||
public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
|
final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
|
||||||
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||||
poolingConnManager.setDefaultMaxPerRoute(5);
|
poolingConnManager.setDefaultMaxPerRoute(5);
|
||||||
poolingConnManager.setMaxTotal(5);
|
poolingConnManager.setMaxTotal(5);
|
||||||
@ -316,15 +319,15 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread.join(10000);
|
thread.join(10000);
|
||||||
countConnMade++;
|
countConnMade++;
|
||||||
if (countConnMade == 0) {
|
if (countConnMade == 0) {
|
||||||
assertTrue(thread.getLeasedConn() == 5);
|
assertEquals(5, thread.getLeasedConn());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("Very Long Running")
|
@Disabled("Very Long Running")
|
||||||
// 8.2 TESTER VERSION
|
// 8.2 TESTER VERSION
|
||||||
public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException {
|
final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException {
|
||||||
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
CloseableHttpClient client = HttpClients.custom()
|
||||||
.setConnectionManager(poolingConnManager)
|
.setConnectionManager(poolingConnManager)
|
||||||
@ -340,10 +343,10 @@ public class HttpClientConnectionManagementLiveTest {
|
|||||||
thread2.start();
|
thread2.start();
|
||||||
thread2.join();
|
thread2.join();
|
||||||
thread3.start();
|
thread3.start();
|
||||||
assertTrue(poolingConnManager.getTotalStats()
|
assertEquals(1, poolingConnManager.getTotalStats()
|
||||||
.getAvailable() == 1);
|
.getAvailable());
|
||||||
thread3.join(32000);
|
thread3.join(32000);
|
||||||
assertTrue(poolingConnManager.getTotalStats()
|
assertEquals(0, poolingConnManager.getTotalStats()
|
||||||
.getAvailable() == 0);
|
.getAvailable());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package com.baeldung.httpclient.httpclient.conn;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
|
@ -2,7 +2,6 @@ package com.baeldung.httpclient.httpclient.conn;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
|
@ -6,18 +6,19 @@ import org.apache.http.client.methods.HttpGet;
|
|||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.junit.Test;
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ApacheHttpClientUnitTest {
|
class ApacheHttpClientUnitTest {
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
||||||
HttpGet request = new HttpGet(DUMMY_URL);
|
HttpGet request = new HttpGet(DUMMY_URL);
|
||||||
|
|
||||||
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
|
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
|
||||||
|
@ -24,7 +24,7 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ApacheHttpClientRetryLiveTest {
|
class ApacheHttpClientRetryLiveTest {
|
||||||
|
|
||||||
private Integer requestCounter;
|
private Integer requestCounter;
|
||||||
private CloseableHttpClient httpClient;
|
private CloseableHttpClient httpClient;
|
||||||
@ -93,14 +93,14 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConfiguration_whenReceivedIOException_thenRetriesPerformed() {
|
void givenDefaultConfiguration_whenReceivedIOException_thenRetriesPerformed() {
|
||||||
createFailingHttpClient();
|
createFailingHttpClient();
|
||||||
assertThrows(IOException.class, () -> httpClient.execute(new HttpGet("https://httpstat.us/200")));
|
assertThrows(IOException.class, () -> httpClient.execute(new HttpGet("https://httpstat.us/200")));
|
||||||
assertThat(requestCounter).isEqualTo(4);
|
assertThat(requestCounter).isEqualTo(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConfiguration_whenDomainNameNotResolved_thenNoRetryApplied() {
|
void givenDefaultConfiguration_whenDomainNameNotResolved_thenNoRetryApplied() {
|
||||||
createDefaultApacheHttpClient();
|
createDefaultApacheHttpClient();
|
||||||
HttpGet request = new HttpGet(URI.create("http://domain.that.does.not.exist:80/api/v1"));
|
HttpGet request = new HttpGet(URI.create("http://domain.that.does.not.exist:80/api/v1"));
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConfiguration_whenGotInternalServerError_thenNoRetryLogicApplied() throws IOException {
|
void givenDefaultConfiguration_whenGotInternalServerError_thenNoRetryLogicApplied() throws IOException {
|
||||||
createDefaultApacheHttpClient();
|
createDefaultApacheHttpClient();
|
||||||
HttpGet request = new HttpGet(URI.create("https://httpstat.us/500"));
|
HttpGet request = new HttpGet(URI.create("https://httpstat.us/500"));
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConfiguration_whenHttpPatchRequest_thenRetryIsNotApplied() {
|
void givenDefaultConfiguration_whenHttpPatchRequest_thenRetryIsNotApplied() {
|
||||||
createFailingHttpClient();
|
createFailingHttpClient();
|
||||||
HttpPatch request = new HttpPatch(URI.create("https://httpstat.us/500"));
|
HttpPatch request = new HttpPatch(URI.create("https://httpstat.us/500"));
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConfiguration_whenHttpPutRequest_thenRetryIsNotApplied() {
|
void givenDefaultConfiguration_whenHttpPutRequest_thenRetryIsNotApplied() {
|
||||||
createFailingHttpClient();
|
createFailingHttpClient();
|
||||||
HttpPut request = new HttpPut(URI.create("https://httpstat.us/500"));
|
HttpPut request = new HttpPut(URI.create("https://httpstat.us/500"));
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenConfiguredRetryHandler_whenHttpPostRequest_thenRetriesPerformed() {
|
void givenConfiguredRetryHandler_whenHttpPostRequest_thenRetriesPerformed() {
|
||||||
createHttpClientWithRetryHandler();
|
createHttpClientWithRetryHandler();
|
||||||
|
|
||||||
HttpPost request = new HttpPost(URI.create("https://httpstat.us/200"));
|
HttpPost request = new HttpPost(URI.create("https://httpstat.us/200"));
|
||||||
@ -148,7 +148,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCustomRetryHandler_whenUnknownHostException_thenRetryAnyway() {
|
void givenCustomRetryHandler_whenUnknownHostException_thenRetryAnyway() {
|
||||||
createHttpClientWithCustomRetryHandler();
|
createHttpClientWithCustomRetryHandler();
|
||||||
|
|
||||||
HttpGet request = new HttpGet(URI.create("https://domain.that.does.not.exist/200"));
|
HttpGet request = new HttpGet(URI.create("https://domain.that.does.not.exist/200"));
|
||||||
@ -158,7 +158,7 @@ public class ApacheHttpClientRetryLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDisabledRetries_whenExecutedHttpRequestEndUpWithIOException_thenRetryIsNotApplied() {
|
void givenDisabledRetries_whenExecutedHttpRequestEndUpWithIOException_thenRetryIsNotApplied() {
|
||||||
createHttpClientWithRetriesDisabled();
|
createHttpClientWithRetriesDisabled();
|
||||||
HttpGet request = new HttpGet(URI.create("https://httpstat.us/200"));
|
HttpGet request = new HttpGet(URI.create("https://httpstat.us/200"));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user