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