1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.testing.async;
29
30 import java.net.InetSocketAddress;
31 import java.util.concurrent.Future;
32
33 import org.apache.hc.client5.http.config.RequestConfig;
34 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
35 import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
36 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
37 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
38 import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
39 import org.apache.hc.client5.testing.SSLTestContexts;
40 import org.apache.hc.core5.function.Decorator;
41 import org.apache.hc.core5.http.HttpHost;
42 import org.apache.hc.core5.http.URIScheme;
43 import org.apache.hc.core5.http.config.Http1Config;
44 import org.apache.hc.core5.http.impl.HttpProcessors;
45 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
46 import org.apache.hc.core5.http.protocol.HttpProcessor;
47 import org.apache.hc.core5.io.CloseMode;
48 import org.apache.hc.core5.reactor.ListenerEndpoint;
49 import org.junit.Rule;
50 import org.junit.rules.ExternalResource;
51
52 public abstract class AbstractHttp1IntegrationTestBase extends AbstractServerTestBase {
53
54 public AbstractHttp1IntegrationTestBase(final URIScheme scheme) {
55 super(scheme);
56 }
57
58 public AbstractHttp1IntegrationTestBase() {
59 super(URIScheme.HTTP);
60 }
61
62 protected HttpAsyncClientBuilder clientBuilder;
63 protected PoolingAsyncClientConnectionManager connManager;
64 protected CloseableHttpAsyncClient httpclient;
65
66 @Rule
67 public ExternalResource connManagerResource = new ExternalResource() {
68
69 @Override
70 protected void before() throws Throwable {
71 connManager = PoolingAsyncClientConnectionManagerBuilder.create()
72 .setTlsStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
73 .build();
74 }
75
76 @Override
77 protected void after() {
78 if (connManager != null) {
79 connManager.close();
80 connManager = null;
81 }
82 }
83
84 };
85
86 @Rule
87 public ExternalResource clientBuilderResource = new ExternalResource() {
88
89 @Override
90 protected void before() throws Throwable {
91 clientBuilder = HttpAsyncClientBuilder.create()
92 .setDefaultRequestConfig(RequestConfig.custom()
93 .setConnectionRequestTimeout(TIMEOUT)
94 .setConnectTimeout(TIMEOUT)
95 .build())
96 .setConnectionManager(connManager);
97 }
98
99 @Override
100 protected void after() {
101 if (httpclient != null) {
102 httpclient.close(CloseMode.GRACEFUL);
103 httpclient = null;
104 }
105 }
106
107 };
108
109 public HttpHost start(
110 final HttpProcessor httpProcessor,
111 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
112 final Http1Config h1Config) throws Exception {
113 server.start(httpProcessor, exchangeHandlerDecorator, h1Config);
114 final Future<ListenerEndpoint> endpointFuture = server.listen(new InetSocketAddress(0));
115 httpclient = clientBuilder.build();
116 httpclient.start();
117 final ListenerEndpoint endpoint = endpointFuture.get();
118 final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
119 return new HttpHost(scheme.name(), "localhost", address.getPort());
120 }
121
122 public HttpHost start(
123 final HttpProcessor httpProcessor,
124 final Http1Config h1Config) throws Exception {
125 return start(httpProcessor, null, h1Config);
126 }
127
128 public HttpHost start() throws Exception {
129 return start(HttpProcessors.server(), Http1Config.DEFAULT);
130 }
131
132 }