Skip to content

Commit b693287

Browse files
author
tienvn
committed
Merge branch 'master' into BAEL-6541-convert-relative-path
2 parents b13c7c6 + 2546a2e commit b693287

529 files changed

Lines changed: 8365 additions & 1965 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ jmeter/src/main/resources/*-JMeter*.csv
7070
jmeter/src/main/resources/*ReportsDashboard*.csv
7171
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
7272
jmeter/src/main/resources/*FileExtractionExample.csv
73+
jmeter/src/main/resources/*_Summary-Report.csv
7374

7475
ninja/devDb.mv.db
7576

@@ -120,4 +121,7 @@ libraries-2/src/test/resources/crawler4j/**
120121
devDb*.db
121122

122123
#jaxb
123-
*.xjb
124+
*.xjb
125+
126+
#neo4j
127+
persistence-modules/neo4j/data/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.httpclient.httpclient;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
6+
7+
public final class ClientUtil {
8+
9+
private ClientUtil(){}
10+
11+
public static void closeClient(CloseableHttpClient client) throws IOException {
12+
if (client == null) {
13+
return;
14+
}
15+
16+
client.close();
17+
}
18+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package com.baeldung.httpclient.httpclient;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.hamcrest.Matchers.notNullValue;
5+
import static org.hamcrest.Matchers.emptyArray;
6+
import static org.hamcrest.Matchers.not;
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import org.apache.hc.client5.http.classic.methods.HttpPost;
15+
import org.apache.hc.client5.http.config.ConnectionConfig;
16+
import org.apache.hc.client5.http.config.RequestConfig;
17+
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
18+
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
19+
import org.apache.hc.client5.http.classic.methods.HttpGet;
20+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
21+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
22+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
23+
import org.apache.hc.client5.http.impl.classic.HttpClients;
24+
import org.apache.hc.core5.http.ContentType;
25+
import org.apache.hc.core5.http.Header;
26+
import org.apache.hc.core5.http.HttpHeaders;
27+
import org.apache.hc.core5.http.NameValuePair;
28+
import org.apache.hc.core5.http.io.entity.EntityUtils;
29+
import org.apache.hc.core5.http.message.BasicNameValuePair;
30+
import org.apache.hc.core5.util.Timeout;
31+
32+
import java.io.IOException;
33+
import java.net.SocketTimeoutException;
34+
import java.nio.charset.Charset;
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
import java.util.concurrent.TimeUnit;
38+
39+
class HttpClientCookBookLiveTest {
40+
41+
private static final String SAMPLE_GET_URL = "http://www.google.com";
42+
private static final String SAMPLE_POST_URL = "http://www.github.com";
43+
44+
private CloseableHttpClient httpClient;
45+
46+
private CloseableHttpResponse response;
47+
48+
@BeforeEach
49+
public final void before() {
50+
httpClient = HttpClientBuilder.create().build();
51+
}
52+
53+
@AfterEach
54+
public final void after() throws IOException {
55+
ClientUtil.closeClient(httpClient);
56+
}
57+
58+
@Test
59+
void givenGetRequestExecuted_thenCorrectStatusCode() throws IOException {
60+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
61+
httpClient.execute(httpGet,
62+
response -> {
63+
assertThat(response.getCode()).isEqualTo(200);
64+
return response;
65+
}
66+
);
67+
}
68+
69+
@Test
70+
void givenGetRequestExecuted_thenCorrectContentMimeType() throws IOException {
71+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
72+
httpClient.execute(httpGet,
73+
response -> {
74+
final String contentMimeType = ContentType.parse(response.getEntity().getContentType()).getMimeType();
75+
assertThat(contentMimeType).isEqualTo(ContentType.TEXT_HTML.getMimeType());
76+
return response;
77+
}
78+
);
79+
}
80+
81+
@Test
82+
void givenGetRequestExecuted_thenCorrectResponse() throws IOException {
83+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
84+
httpClient.execute(httpGet,
85+
response -> {
86+
String bodyAsString = EntityUtils.toString(response.getEntity());
87+
assertThat(bodyAsString, notNullValue());
88+
return response;
89+
}
90+
);
91+
}
92+
93+
@Test
94+
void whenConfigureTimeoutOnRequest_thenCorrectResponse() throws IOException {
95+
RequestConfig requestConfig = RequestConfig.custom()
96+
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
97+
.build();
98+
99+
CloseableHttpClient httpClient = HttpClientBuilder.create()
100+
.setDefaultRequestConfig(requestConfig)
101+
.build();
102+
103+
HttpGet request = new HttpGet(SAMPLE_GET_URL);
104+
request.setConfig(requestConfig);
105+
106+
httpClient.execute(request,
107+
response -> {
108+
assertThat(response.getCode()).isEqualTo(200);
109+
return response;
110+
}
111+
);
112+
}
113+
114+
@Test
115+
void givenLowSocketTimeOut_whenExecutingRequestWithTimeout_thenException() throws IOException {
116+
ConnectionConfig connConfig = ConnectionConfig.custom()
117+
.setConnectTimeout(1000, TimeUnit.MILLISECONDS)
118+
.setSocketTimeout(20, TimeUnit.MILLISECONDS)
119+
.build();
120+
121+
RequestConfig requestConfig = RequestConfig.custom()
122+
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
123+
.build();
124+
125+
BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager();
126+
cm.setConnectionConfig(connConfig);
127+
128+
CloseableHttpClient httpClient = HttpClientBuilder.create()
129+
.setDefaultRequestConfig(requestConfig)
130+
.setConnectionManager(cm)
131+
.build();
132+
133+
HttpGet request = new HttpGet(SAMPLE_GET_URL);
134+
135+
assertThrows(SocketTimeoutException.class, () -> {
136+
httpClient.execute(request, resp -> resp);
137+
});
138+
}
139+
140+
@Test
141+
void whenExecutingPostRequest_thenNoExceptions() throws IOException {
142+
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
143+
httpClient.execute(httpPost,
144+
response -> {
145+
assertThat(response.getCode()).isEqualTo(200);
146+
return response;
147+
}
148+
);
149+
}
150+
151+
@Test
152+
void givenParametersAddedToRequest_thenCorrect() throws IOException {
153+
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
154+
final List<NameValuePair> params = new ArrayList<NameValuePair>();
155+
params.add(new BasicNameValuePair("key1", "value1"));
156+
params.add(new BasicNameValuePair("key2", "value2"));
157+
httpPost.setEntity(new UrlEncodedFormEntity(params, Charset.defaultCharset()));
158+
159+
httpClient.execute(httpPost, response -> {
160+
assertThat(response.getCode()).isEqualTo(200);
161+
return response;
162+
});
163+
}
164+
165+
@Test
166+
void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException {
167+
CloseableHttpClient client = HttpClientBuilder.create()
168+
.disableRedirectHandling()
169+
.build();
170+
client.execute(new HttpGet("http://t.co/I5YYd9tddw"), response -> {
171+
assertThat(response.getCode()).isEqualTo(301);
172+
return response;
173+
});
174+
}
175+
176+
@Test
177+
void givenHeadersAddedToRequest_thenCorrect() throws IOException {
178+
HttpGet request = new HttpGet(SAMPLE_GET_URL);
179+
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
180+
httpClient.execute(request,
181+
response -> {
182+
assertThat(response.getCode()).isEqualTo(200);
183+
return response;
184+
}
185+
);
186+
}
187+
188+
@Test
189+
void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
190+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
191+
httpClient.execute(httpGet,
192+
response -> {
193+
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
194+
assertThat(headers, not(emptyArray()));
195+
return response;
196+
}
197+
);
198+
}
199+
200+
@Test
201+
void givenAutoClosableClient_thenCorrect() throws IOException {
202+
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
203+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
204+
httpClient.execute(httpGet, resp -> {
205+
assertThat(resp.getCode()).isEqualTo(200);
206+
return resp;
207+
});
208+
}
209+
}
210+
211+
}

apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,22 @@
99

1010
import javax.net.ssl.SSLContext;
1111

12-
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
13-
import org.junit.jupiter.api.Test;
14-
1512
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
1613
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
1714
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
1815
import org.apache.hc.client5.http.auth.AuthScope;
1916
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
2017
import org.apache.hc.client5.http.classic.methods.HttpGet;
21-
import org.apache.hc.client5.http.config.RequestConfig;
2218
import org.apache.hc.client5.http.cookie.BasicCookieStore;
2319
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2420
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
2521
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
2622
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
2723
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
2824
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
25+
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
2926
import org.apache.hc.client5.http.protocol.HttpClientContext;
3027
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
31-
import org.apache.hc.core5.concurrent.FutureCallback;
3228
import org.apache.hc.core5.http.HttpHost;
3329
import org.apache.hc.core5.http.HttpResponse;
3430
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
@@ -37,6 +33,8 @@
3733
import org.apache.hc.core5.reactor.IOReactorConfig;
3834
import org.apache.hc.core5.ssl.SSLContexts;
3935
import org.apache.hc.core5.ssl.TrustStrategy;
36+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
37+
import org.junit.jupiter.api.Test;
4038

4139

4240
class HttpAsyncClientLiveTest extends GetRequestMockServer {
@@ -56,12 +54,10 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
5654

5755
@Test
5856
void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
59-
final HttpHost target = new HttpHost(HOST_WITH_COOKIE);
60-
final SimpleHttpRequest request = SimpleRequestBuilder.get()
61-
.setHttpHost(target)
57+
final SimpleHttpRequest request = SimpleRequestBuilder.get(HOST_WITH_COOKIE)
58+
.build();
59+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
6260
.build();
63-
64-
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
6561
client.start();
6662

6763

@@ -126,6 +122,7 @@ void whenUseSSLWithHttpAsyncClient_thenCorrect() throws Exception {
126122
.build();
127123

128124
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
125+
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
129126
.setSslContext(sslContext)
130127
.build();
131128

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.httpclient;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.client.methods.CloseableHttpResponse;
7+
import org.apache.http.impl.client.CloseableHttpClient;
8+
9+
public final class ClientUtil {
10+
11+
private ClientUtil(){}
12+
13+
public static void closeClient(CloseableHttpClient client) throws IOException {
14+
if (client == null) {
15+
return;
16+
}
17+
18+
client.close();
19+
}
20+
}

0 commit comments

Comments
 (0)