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.impl.async.CloseableHttpAsyncClient;
34 import org.apache.hc.core5.function.Decorator;
35 import org.apache.hc.core5.http.HttpHost;
36 import org.apache.hc.core5.http.URIScheme;
37 import org.apache.hc.core5.http.config.Http1Config;
38 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
39 import org.apache.hc.core5.http.protocol.HttpProcessor;
40 import org.apache.hc.core5.http2.config.H2Config;
41 import org.apache.hc.core5.io.CloseMode;
42 import org.apache.hc.core5.reactor.ListenerEndpoint;
43 import org.junit.Rule;
44 import org.junit.rules.ExternalResource;
45
46 public abstract class AbstractIntegrationTestBase<T extends CloseableHttpAsyncClient> extends AbstractServerTestBase {
47
48 public AbstractIntegrationTestBase(final URIScheme scheme) {
49 super(scheme);
50 }
51
52 public AbstractIntegrationTestBase() {
53 super(URIScheme.HTTP);
54 }
55
56 protected T httpclient;
57
58 protected abstract T createClient() throws Exception;
59
60 @Rule
61 public ExternalResource clientResource = new ExternalResource() {
62
63 @Override
64 protected void after() {
65 if (httpclient != null) {
66 httpclient.close(CloseMode.GRACEFUL);
67 httpclient = null;
68 }
69 }
70
71 };
72
73 public abstract HttpHost start() throws Exception;
74
75 public final HttpHost start(
76 final HttpProcessor httpProcessor,
77 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
78 final Http1Config h1Config) throws Exception {
79 server.start(httpProcessor, exchangeHandlerDecorator, h1Config);
80 final Future<ListenerEndpoint> endpointFuture = server.listen(new InetSocketAddress(0));
81 httpclient = createClient();
82 httpclient.start();
83 final ListenerEndpoint endpoint = endpointFuture.get();
84 final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
85 return new HttpHost(scheme.name(), "localhost", address.getPort());
86 }
87
88 public final HttpHost start(
89 final HttpProcessor httpProcessor,
90 final Http1Config h1Config) throws Exception {
91 return start(httpProcessor, null, h1Config);
92 }
93
94 public final HttpHost start(
95 final HttpProcessor httpProcessor,
96 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
97 final H2Config h2Config) throws Exception {
98 server.start(httpProcessor, exchangeHandlerDecorator, h2Config);
99 final Future<ListenerEndpoint> endpointFuture = server.listen(new InetSocketAddress(0));
100 httpclient = createClient();
101 httpclient.start();
102 final ListenerEndpoint endpoint = endpointFuture.get();
103 final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
104 return new HttpHost(scheme.name(), "localhost", address.getPort());
105 }
106
107
108 public final HttpHost start(
109 final HttpProcessor httpProcessor,
110 final H2Config h2Config) throws Exception {
111 return start(httpProcessor, null, h2Config);
112 }
113
114 }