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.extension.async;
29
30 import java.util.Collection;
31
32 import org.apache.hc.client5.http.AuthenticationStrategy;
33 import org.apache.hc.client5.http.HttpRequestRetryStrategy;
34 import org.apache.hc.client5.http.UserTokenHandler;
35 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
36 import org.apache.hc.client5.http.config.ConnectionConfig;
37 import org.apache.hc.client5.http.config.TlsConfig;
38 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
39 import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
40 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
41 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
42 import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
43 import org.apache.hc.client5.testing.SSLTestContexts;
44 import org.apache.hc.core5.http.Header;
45 import org.apache.hc.core5.http.HttpRequestInterceptor;
46 import org.apache.hc.core5.http.HttpResponseInterceptor;
47 import org.apache.hc.core5.http.config.Http1Config;
48 import org.apache.hc.core5.http.config.Lookup;
49 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
50 import org.apache.hc.core5.http2.config.H2Config;
51 import org.apache.hc.core5.reactor.IOReactorConfig;
52 import org.apache.hc.core5.util.Timeout;
53
54 final class StandardTestClientBuilder implements TestAsyncClientBuilder {
55
56 private final PoolingAsyncClientConnectionManagerBuilder connectionManagerBuilder;
57 private final HttpAsyncClientBuilder clientBuilder;
58
59 private Timeout timeout;
60 private TlsStrategy tlsStrategy;
61
62 public StandardTestClientBuilder() {
63 this.connectionManagerBuilder = PoolingAsyncClientConnectionManagerBuilder.create();
64 this.clientBuilder = HttpAsyncClientBuilder.create();
65 }
66
67 @Override
68 public ClientProtocolLevel getProtocolLevel() {
69 return ClientProtocolLevel.STANDARD;
70 }
71
72 @Override
73 public TestAsyncClientBuilder setTimeout(final Timeout timeout) {
74 this.timeout = timeout;
75 return this;
76 }
77
78 @Override
79 public TestAsyncClientBuilder addResponseInterceptorFirst(final HttpResponseInterceptor interceptor) {
80 this.clientBuilder.addResponseInterceptorFirst(interceptor);
81 return this;
82 }
83
84 @Override
85 public TestAsyncClientBuilder addResponseInterceptorLast(final HttpResponseInterceptor interceptor) {
86 this.clientBuilder.addResponseInterceptorLast(interceptor);
87 return this;
88 }
89
90 @Override
91 public TestAsyncClientBuilder addRequestInterceptorFirst(final HttpRequestInterceptor interceptor) {
92 this.clientBuilder.addRequestInterceptorFirst(interceptor);
93 return this;
94 }
95
96 @Override
97 public TestAsyncClientBuilder addRequestInterceptorLast(final HttpRequestInterceptor interceptor) {
98 this.clientBuilder.addRequestInterceptorLast(interceptor);
99 return this;
100 }
101
102 @Override
103 public TestAsyncClientBuilder setTlsStrategy(final TlsStrategy tlsStrategy) {
104 this.tlsStrategy = tlsStrategy;
105 return this;
106 }
107
108 @Override
109 public TestAsyncClientBuilder setDefaultTlsConfig(final TlsConfig tlsConfig) {
110 this.connectionManagerBuilder.setDefaultTlsConfig(tlsConfig);
111 return this;
112 }
113
114 @Override
115 public TestAsyncClientBuilder setHttp1Config(final Http1Config http1Config) {
116 this.clientBuilder.setHttp1Config(http1Config);
117 return this;
118 }
119
120 @Override
121 public TestAsyncClientBuilder setH2Config(final H2Config h2Config) {
122 this.clientBuilder.setH2Config(h2Config);
123 return this;
124 }
125
126 @Override
127 public TestAsyncClientBuilder setUserTokenHandler(final UserTokenHandler userTokenHandler) {
128 this.clientBuilder.setUserTokenHandler(userTokenHandler);
129 return this;
130 }
131
132 @Override
133 public TestAsyncClientBuilder setDefaultHeaders(final Collection<? extends Header> defaultHeaders) {
134 this.clientBuilder.setDefaultHeaders(defaultHeaders);
135 return this;
136 }
137
138 @Override
139 public TestAsyncClientBuilder setRetryStrategy(final HttpRequestRetryStrategy retryStrategy) {
140 this.clientBuilder.setRetryStrategy(retryStrategy);
141 return this;
142 }
143
144 @Override
145 public TestAsyncClientBuilder setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
146 this.clientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy);
147 return this;
148 }
149
150 @Override
151 public TestAsyncClientBuilder setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
152 this.clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
153 return this;
154 }
155
156 @Override
157 public TestAsyncClient build() throws Exception {
158 final PoolingAsyncClientConnectionManager connectionManager = connectionManagerBuilder
159 .setTlsStrategy(tlsStrategy != null ? tlsStrategy : new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
160 .setDefaultConnectionConfig(ConnectionConfig.custom()
161 .setSocketTimeout(timeout)
162 .setConnectTimeout(timeout)
163 .build())
164 .build();
165 final CloseableHttpAsyncClient client = clientBuilder
166 .setIOReactorConfig(IOReactorConfig.custom()
167 .setSoTimeout(timeout)
168 .build())
169 .setConnectionManager(connectionManager)
170 .build();
171 return new TestAsyncClient(client, connectionManager);
172 }
173
174 }