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.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 }