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;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.client5.http.config.RequestConfig;
33  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
34  import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
35  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
36  import org.apache.hc.client5.testing.SSLTestContexts;
37  import org.apache.hc.client5.testing.classic.EchoHandler;
38  import org.apache.hc.client5.testing.classic.RandomHandler;
39  import org.apache.hc.core5.function.Decorator;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.URIScheme;
42  import org.apache.hc.core5.http.config.Http1Config;
43  import org.apache.hc.core5.http.io.HttpServerRequestHandler;
44  import org.apache.hc.core5.http.io.SocketConfig;
45  import org.apache.hc.core5.http.protocol.HttpProcessor;
46  import org.apache.hc.core5.io.CloseMode;
47  import org.apache.hc.core5.io.Closer;
48  import org.apache.hc.core5.testing.classic.ClassicTestServer;
49  import org.apache.hc.core5.util.Timeout;
50  import org.junit.Rule;
51  import org.junit.rules.ExternalResource;
52  
53  /**
54   * Base class for tests using local test server. The server will not be started per default.
55   */
56  public abstract class LocalServerTestBase {
57  
58      public static final Timeout TIMEOUT = Timeout.ofSeconds(30);
59      public static final Timeout LONG_TIMEOUT = Timeout.ofSeconds(60);
60  
61      public LocalServerTestBase(final URIScheme scheme) {
62          this.scheme = scheme;
63      }
64  
65      public LocalServerTestBase() {
66          this(URIScheme.HTTP);
67      }
68  
69      protected final URIScheme scheme;
70  
71      protected ClassicTestServer server;
72  
73      @Rule
74      public ExternalResource serverResource = new ExternalResource() {
75  
76          @Override
77          protected void before() throws Throwable {
78              server = new ClassicTestServer(
79                      scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
80                      SocketConfig.custom()
81                              .setSoTimeout(TIMEOUT)
82                              .build());
83              server.registerHandler("/echo/*", new EchoHandler());
84              server.registerHandler("/random/*", new RandomHandler());
85          }
86  
87          @Override
88          protected void after() {
89              if (server != null) {
90                  try {
91                      server.shutdown(CloseMode.IMMEDIATE);
92                      server = null;
93                  } catch (final Exception ignore) {
94                  }
95              }
96          }
97  
98      };
99  
100     protected PoolingHttpClientConnectionManager connManager;
101     protected HttpClientBuilder clientBuilder;
102     protected CloseableHttpClient httpclient;
103 
104     @Rule
105     public ExternalResource clientResource = new ExternalResource() {
106 
107         @Override
108         protected void before() throws Throwable {
109             connManager = new PoolingHttpClientConnectionManager();
110             connManager.setDefaultSocketConfig(SocketConfig.custom()
111                     .setSoTimeout(TIMEOUT)
112                     .build());
113             clientBuilder = HttpClientBuilder.create()
114                     .setDefaultRequestConfig(RequestConfig.custom()
115                             .setConnectionRequestTimeout(TIMEOUT)
116                             .setConnectTimeout(TIMEOUT)
117                             .build())
118                     .setConnectionManager(connManager);
119         }
120 
121         @Override
122         protected void after() {
123             Closer.closeQuietly(httpclient);
124             httpclient = null;
125         }
126 
127     };
128 
129     public HttpHost start(
130             final Http1Config http1Config,
131             final HttpProcessor httpProcessor,
132             final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
133         this.server.start(http1Config, httpProcessor, handlerDecorator);
134 
135         if (this.httpclient == null) {
136             this.httpclient = this.clientBuilder.build();
137         }
138 
139         return new HttpHost(this.scheme.name(), "localhost", this.server.getPort());
140     }
141 
142     public HttpHost start(
143             final HttpProcessor httpProcessor,
144             final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
145         return start(null, httpProcessor, handlerDecorator);
146     }
147 
148     public HttpHost start() throws Exception {
149         return start(null, null, null);
150     }
151 
152 }