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.core5.testing.nio.extension;
29  
30  import java.util.function.Consumer;
31  
32  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
33  import org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap;
34  import org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy;
35  import org.apache.hc.core5.io.CloseMode;
36  import org.apache.hc.core5.testing.SSLTestContexts;
37  import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
38  import org.apache.hc.core5.testing.nio.LoggingExceptionCallback;
39  import org.apache.hc.core5.testing.nio.LoggingH2StreamListener;
40  import org.apache.hc.core5.testing.nio.LoggingHttp1StreamListener;
41  import org.apache.hc.core5.testing.nio.LoggingIOSessionDecorator;
42  import org.apache.hc.core5.testing.nio.LoggingIOSessionListener;
43  import org.junit.jupiter.api.Assertions;
44  import org.junit.jupiter.api.extension.AfterEachCallback;
45  import org.junit.jupiter.api.extension.BeforeEachCallback;
46  import org.junit.jupiter.api.extension.ExtensionContext;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  public class H2AsyncRequesterResource implements BeforeEachCallback, AfterEachCallback {
51  
52      private static final Logger LOG = LoggerFactory.getLogger(H2AsyncRequesterResource.class);
53  
54      private final Consumer<H2RequesterBootstrap> bootstrapCustomizer;
55  
56      private HttpAsyncRequester requester;
57  
58      public H2AsyncRequesterResource(final Consumer<H2RequesterBootstrap> bootstrapCustomizer) {
59          this.bootstrapCustomizer = bootstrapCustomizer;
60      }
61  
62      @Override
63      public void beforeEach(final ExtensionContext extensionContext) throws Exception {
64          LOG.debug("Starting up test client");
65          final H2RequesterBootstrap bootstrap = H2RequesterBootstrap.bootstrap()
66                  .setTlsStrategy(new H2ClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
67                  .setStreamListener(LoggingHttp1StreamListener.INSTANCE_CLIENT)
68                  .setStreamListener(LoggingH2StreamListener.INSTANCE)
69                  .setConnPoolListener(LoggingConnPoolListener.INSTANCE)
70                  .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
71                  .setExceptionCallback(LoggingExceptionCallback.INSTANCE)
72                  .setIOSessionListener(LoggingIOSessionListener.INSTANCE);
73          bootstrapCustomizer.accept(bootstrap);
74          requester = bootstrap.create();
75      }
76  
77      @Override
78      public void afterEach(final ExtensionContext extensionContext) throws Exception {
79          LOG.debug("Shutting down test client");
80          if (requester != null) {
81              try {
82                  requester.close(CloseMode.GRACEFUL);
83              } catch (final Exception ignore) {
84              }
85          }
86      }
87  
88      public HttpAsyncRequester start() {
89          Assertions.assertNotNull(requester);
90          requester.start();
91          return requester;
92      }
93  
94  }