View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.testing.extension.sync;
29  
30  import org.apache.hc.client5.http.config.ConnectionConfig;
31  import org.apache.hc.client5.http.impl.classic.HttpClients;
32  import org.apache.hc.client5.http.impl.classic.MinimalHttpClient;
33  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
34  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
35  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
36  import org.apache.hc.client5.testing.SSLTestContexts;
37  import org.apache.hc.core5.http.io.SocketConfig;
38  import org.apache.hc.core5.util.Timeout;
39  
40  final class MinimalTestClientBuilder implements TestClientBuilder {
41  
42      private Timeout timeout;
43  
44      private HttpClientConnectionManager connectionManager;
45  
46      public MinimalTestClientBuilder() {
47      }
48  
49      @Override
50      public ClientProtocolLevel getProtocolLevel() {
51          return ClientProtocolLevel.MINIMAL;
52      }
53  
54      @Override
55      public TestClientBuilder setTimeout(final Timeout timeout) {
56          this.timeout = timeout;
57          return this;
58      }
59  
60      @Override
61      public TestClientBuilder setConnectionManager(final HttpClientConnectionManager connectionManager) {
62          this.connectionManager = connectionManager;
63          return this;
64      }
65  
66      @Override
67      public TestClient build() throws Exception {
68          final HttpClientConnectionManager connectionManagerCopy = connectionManager != null ? connectionManager :
69                  PoolingHttpClientConnectionManagerBuilder.create()
70                          .setTlsSocketStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
71                          .setDefaultSocketConfig(SocketConfig.custom()
72                                  .setSoTimeout(timeout)
73                                  .build())
74                          .setDefaultConnectionConfig(ConnectionConfig.custom()
75                                  .setConnectTimeout(timeout)
76                                  .build())
77                          .build();
78  
79          final MinimalHttpClient minimalClient = HttpClients.createMinimal(connectionManagerCopy);
80          return new TestClient(minimalClient, connectionManagerCopy);
81      }
82  
83  }