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.sync.extension;
29  
30  import java.io.IOException;
31  import java.util.function.Consumer;
32  
33  import org.apache.hc.client5.http.config.ConnectionConfig;
34  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
35  import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
36  import org.apache.hc.client5.http.impl.classic.HttpClients;
37  import org.apache.hc.client5.http.impl.classic.MinimalHttpClient;
38  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
39  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
40  import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
41  import org.apache.hc.client5.testing.SSLTestContexts;
42  import org.apache.hc.core5.function.Decorator;
43  import org.apache.hc.core5.http.HttpHost;
44  import org.apache.hc.core5.http.URIScheme;
45  import org.apache.hc.core5.http.config.Http1Config;
46  import org.apache.hc.core5.http.io.HttpServerRequestHandler;
47  import org.apache.hc.core5.http.io.SocketConfig;
48  import org.apache.hc.core5.http.protocol.HttpProcessor;
49  import org.apache.hc.core5.io.CloseMode;
50  import org.apache.hc.core5.testing.classic.ClassicTestServer;
51  import org.apache.hc.core5.util.Timeout;
52  import org.junit.jupiter.api.Assertions;
53  import org.junit.jupiter.api.extension.AfterEachCallback;
54  import org.junit.jupiter.api.extension.BeforeEachCallback;
55  import org.junit.jupiter.api.extension.ExtensionContext;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  public class TestClientResources implements BeforeEachCallback, AfterEachCallback {
60  
61      private static final Logger LOG = LoggerFactory.getLogger(TestClientResources.class);
62  
63      private final URIScheme scheme;
64      private final Timeout timeout;
65  
66      private ClassicTestServer server;
67      private PoolingHttpClientConnectionManager connManager;
68      private CloseableHttpClient client;
69  
70      public TestClientResources(final URIScheme scheme, final Timeout timeout) {
71          this.scheme = scheme;
72          this.timeout = timeout;
73      }
74  
75      @Override
76      public void beforeEach(final ExtensionContext extensionContext) throws Exception {
77          LOG.debug("Starting up test server");
78          server = new ClassicTestServer(
79                  scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
80                  SocketConfig.custom()
81                          .setSoTimeout(timeout)
82                          .build());
83      }
84  
85      @Override
86      public void afterEach(final ExtensionContext extensionContext) throws Exception {
87          LOG.debug("Shutting down test server");
88  
89          if (client != null) {
90              client.close(CloseMode.GRACEFUL);
91          }
92  
93          if (connManager != null) {
94              connManager.close(CloseMode.IMMEDIATE);
95          }
96  
97          if (server != null) {
98              server.shutdown(CloseMode.IMMEDIATE);
99          }
100     }
101 
102     public URIScheme scheme() {
103         return this.scheme;
104     }
105 
106     public ClassicTestServer startServer(
107             final Http1Config http1Config,
108             final HttpProcessor httpProcessor,
109             final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
110         Assertions.assertNotNull(server);
111         server.start(http1Config, httpProcessor, handlerDecorator);
112         return server;
113     }
114 
115     public HttpHost targetHost() {
116         Assertions.assertNotNull(server);
117         return new HttpHost(scheme.id, "localhost", server.getPort());
118     }
119 
120     public CloseableHttpClient startClient(
121             final Consumer<PoolingHttpClientConnectionManagerBuilder> connManagerCustomizer,
122             final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
123         Assertions.assertNull(connManager);
124         Assertions.assertNull(client);
125 
126         final PoolingHttpClientConnectionManagerBuilder connManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
127         connManagerBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLTestContexts.createClientSSLContext()));
128         connManagerBuilder.setDefaultSocketConfig(SocketConfig.custom()
129                 .setSoTimeout(timeout)
130                 .build());
131         connManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
132                 .setConnectTimeout(timeout)
133                 .build());
134         connManagerCustomizer.accept(connManagerBuilder);
135 
136         connManager = connManagerBuilder.build();
137 
138         final HttpClientBuilder clientBuilder = HttpClientBuilder.create()
139                 .setConnectionManager(connManager);
140         clientCustomizer.accept(clientBuilder);
141         client = clientBuilder.build();
142         return client;
143     }
144 
145     public MinimalHttpClient startMinimalClient() throws Exception {
146         Assertions.assertNull(connManager);
147         Assertions.assertNull(client);
148 
149         final PoolingHttpClientConnectionManagerBuilder connManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
150         connManagerBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLTestContexts.createClientSSLContext()));
151         connManagerBuilder.setDefaultSocketConfig(SocketConfig.custom()
152                 .setSoTimeout(timeout)
153                 .build());
154         connManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
155                 .setConnectTimeout(timeout)
156                 .build());
157         connManager = connManagerBuilder.build();
158 
159         final MinimalHttpClient minimalClient = HttpClients.createMinimal(connManager);
160         client = minimalClient;
161         return minimalClient;
162     }
163 
164     public CloseableHttpClient startClient(
165             final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
166         return startClient(b -> {}, clientCustomizer);
167     }
168 
169     public PoolingHttpClientConnectionManager connManager() {
170         Assertions.assertNotNull(connManager);
171         return connManager;
172     }
173 
174 }