Skip to content

Commit 4ea3b0f

Browse files
committed
Add class HttpAsyncClientTest
1 parent 9e7d330 commit 4ea3b0f

2 files changed

Lines changed: 184 additions & 0 deletions

File tree

‎httpclient/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
<version>1.9</version>
6161
</dependency>
6262

63+
<dependency>
64+
<groupId>org.apache.httpcomponents</groupId>
65+
<artifactId>httpasyncclient</artifactId>
66+
<version>4.1-beta1</version>
67+
</dependency>
68+
6369
<!-- logging -->
6470

6571
<dependency>
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package org.baeldung.httpclient;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.security.cert.X509Certificate;
7+
import java.util.concurrent.Future;
8+
9+
import javax.net.ssl.SSLContext;
10+
11+
import org.apache.http.HttpHost;
12+
import org.apache.http.HttpResponse;
13+
import org.apache.http.auth.AuthScope;
14+
import org.apache.http.auth.UsernamePasswordCredentials;
15+
import org.apache.http.client.CredentialsProvider;
16+
import org.apache.http.client.config.RequestConfig;
17+
import org.apache.http.client.methods.HttpGet;
18+
import org.apache.http.client.protocol.HttpClientContext;
19+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
20+
import org.apache.http.conn.ssl.SSLContexts;
21+
import org.apache.http.conn.ssl.TrustStrategy;
22+
import org.apache.http.impl.client.BasicCookieStore;
23+
import org.apache.http.impl.client.BasicCredentialsProvider;
24+
import org.apache.http.impl.cookie.BasicClientCookie;
25+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
26+
import org.apache.http.impl.nio.client.HttpAsyncClients;
27+
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
28+
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
29+
import org.apache.http.nio.reactor.ConnectingIOReactor;
30+
import org.apache.http.protocol.BasicHttpContext;
31+
import org.apache.http.protocol.HttpContext;
32+
import org.junit.Test;
33+
34+
public class HttpAsyncClientTest {
35+
36+
private static final String HOST = "http://www.google.com";
37+
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
38+
private static final String HOST_WITH_PROXY = "https://issues.apache.org/";
39+
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";// "http://localhost:8080/spring-security-rest-basic-auth/api/foos/1";
40+
private static final String DEFAULT_USER = "test";// "user1";
41+
private static final String DEFAULT_PASS = "test";// "user1Pass";
42+
43+
private static final String HOST_WITH_COOKIE = "http://yuilibrary.com/yui/docs/cookie/cookie-simple-example.html"; // "http://github.com";
44+
private static final String COOKIE_DOMAIN = ".yuilibrary.com"; // ".github.com";
45+
private static final String COOKIE_NAME = "example"; // "JSESSIONID";
46+
47+
48+
@Test
49+
public void whenUseHttpAsyncClient_thenCorrect() throws Exception {
50+
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
51+
client.start();
52+
final HttpGet request = new HttpGet(HOST);
53+
final Future<HttpResponse> future = client.execute(request, null);
54+
final HttpResponse response = future.get();
55+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
56+
client.close();
57+
}
58+
59+
@Test
60+
public void whenUseMultipleHttpAsyncClient_thenCorrect() throws Exception {
61+
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
62+
final PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
63+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(cm).build();
64+
client.start();
65+
final String[] toGet = { "http://www.google.com/", "http://www.apache.org/", "http://www.bing.com/" };
66+
67+
final GetThread[] threads = new GetThread[toGet.length];
68+
for (int i = 0; i < threads.length; i++) {
69+
final HttpGet request = new HttpGet(toGet[i]);
70+
threads[i] = new GetThread(client, request);
71+
}
72+
73+
for (final GetThread thread : threads) {
74+
thread.start();
75+
}
76+
77+
for (final GetThread thread : threads) {
78+
thread.join();
79+
}
80+
81+
}
82+
83+
@Test
84+
public void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
85+
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
86+
client.start();
87+
final HttpHost proxy = new HttpHost("74.50.126.248", 3127);
88+
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
89+
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
90+
request.setConfig(config);
91+
final Future<HttpResponse> future = client.execute(request, null);
92+
final HttpResponse response = future.get();
93+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
94+
client.close();
95+
}
96+
97+
@Test
98+
public void whenUseSSLWithHttpAsyncClient_thenCorrect() throws Exception {
99+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
100+
@Override
101+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
102+
return true;
103+
}
104+
};
105+
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
106+
107+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslContext).build();
108+
109+
client.start();
110+
final HttpGet request = new HttpGet(HOST_WITH_SSL);
111+
final Future<HttpResponse> future = client.execute(request, null);
112+
final HttpResponse response = future.get();
113+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
114+
client.close();
115+
}
116+
117+
118+
@Test
119+
public void whenUseCookiesWithHttpAsyncClient_thenCorrect() throws Exception {
120+
final BasicCookieStore cookieStore = new BasicCookieStore();
121+
final BasicClientCookie cookie = new BasicClientCookie(COOKIE_NAME, "1234");
122+
cookie.setDomain(COOKIE_DOMAIN);
123+
cookie.setPath("/");
124+
cookieStore.addCookie(cookie);
125+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
126+
client.start();
127+
final HttpGet request = new HttpGet(HOST_WITH_COOKIE);
128+
129+
final HttpContext localContext = new BasicHttpContext();
130+
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
131+
132+
final Future<HttpResponse> future = client.execute(request, localContext, null);
133+
final HttpResponse response = future.get();
134+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
135+
client.close();
136+
}
137+
138+
@Test
139+
public void whenUseAuthenticationWithHttpAsyncClient_thenCorrect() throws Exception {
140+
final CredentialsProvider provider = new BasicCredentialsProvider();
141+
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
142+
provider.setCredentials(AuthScope.ANY, credentials);
143+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setDefaultCredentialsProvider(provider).build();
144+
145+
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
146+
client.start();
147+
final Future<HttpResponse> future = client.execute(request, null);
148+
final HttpResponse response = future.get();
149+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
150+
client.close();
151+
}
152+
153+
static class GetThread extends Thread {
154+
155+
private final CloseableHttpAsyncClient client;
156+
private final HttpContext context;
157+
private final HttpGet request;
158+
159+
public GetThread(final CloseableHttpAsyncClient client, final HttpGet request) {
160+
this.client = client;
161+
context = HttpClientContext.create();
162+
this.request = request;
163+
}
164+
165+
@Override
166+
public void run() {
167+
try {
168+
final Future<HttpResponse> future = client.execute(request, context, null);
169+
final HttpResponse response = future.get();
170+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
171+
} catch (final Exception ex) {
172+
System.out.println(ex.getLocalizedMessage());
173+
}
174+
}
175+
176+
}
177+
}
178+

0 commit comments

Comments
 (0)