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 org.apache.hc.client5.testing.SSLTestContexts;
31  import org.apache.hc.core5.function.Supplier;
32  import org.apache.hc.core5.http.URIScheme;
33  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
34  import org.apache.hc.core5.reactive.ReactiveServerExchangeHandler;
35  import org.apache.hc.core5.reactor.IOReactorConfig;
36  import org.apache.hc.core5.testing.nio.H2TestServer;
37  import org.apache.hc.core5.testing.reactive.ReactiveEchoProcessor;
38  import org.apache.hc.core5.testing.reactive.ReactiveRandomProcessor;
39  import org.apache.hc.core5.util.TimeValue;
40  import org.apache.hc.core5.util.Timeout;
41  import org.junit.Rule;
42  import org.junit.rules.ExternalResource;
43  
44  public abstract class AbstractServerTestBase {
45  
46      public static final Timeout TIMEOUT = Timeout.ofSeconds(30);
47      public static final Timeout LONG_TIMEOUT = Timeout.ofSeconds(60);
48  
49      protected final URIScheme scheme;
50  
51      public AbstractServerTestBase(final URIScheme scheme) {
52          this.scheme = scheme;
53      }
54  
55      public AbstractServerTestBase() {
56          this(URIScheme.HTTP);
57      }
58  
59      protected H2TestServer server;
60  
61      @Rule
62      public ExternalResource serverResource = new ExternalResource() {
63  
64          @Override
65          protected void before() throws Throwable {
66              server = new H2TestServer(
67                      IOReactorConfig.custom()
68                          .setSoTimeout(TIMEOUT)
69                          .build(),
70                      scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, null, null);
71              server.register("/echo/*", new Supplier<AsyncServerExchangeHandler>() {
72  
73                  @Override
74                  public AsyncServerExchangeHandler get() {
75                      if (isReactive()) {
76                          return new ReactiveServerExchangeHandler(new ReactiveEchoProcessor());
77                      } else {
78                          return new AsyncEchoHandler();
79                      }
80                  }
81  
82              });
83              server.register("/random/*", new Supplier<AsyncServerExchangeHandler>() {
84  
85                  @Override
86                  public AsyncServerExchangeHandler get() {
87                      if (isReactive()) {
88                          return new ReactiveServerExchangeHandler(new ReactiveRandomProcessor());
89                      } else {
90                          return new AsyncRandomHandler();
91                      }
92                  }
93  
94              });
95          }
96  
97          @Override
98          protected void after() {
99              if (server != null) {
100                 server.shutdown(TimeValue.ofSeconds(5));
101                 server = null;
102             }
103         }
104 
105     };
106 
107     protected boolean isReactive() {
108         return false;
109     }
110 }