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  package org.apache.hc.client5.testing.async;
28  
29  import java.util.Arrays;
30  import java.util.Collection;
31  
32  import org.apache.hc.client5.http.AuthenticationStrategy;
33  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
34  import org.apache.hc.client5.http.config.RequestConfig;
35  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
36  import org.apache.hc.client5.http.impl.async.H2AsyncClientBuilder;
37  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
38  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
39  import org.apache.hc.client5.testing.SSLTestContexts;
40  import org.apache.hc.core5.http.HttpVersion;
41  import org.apache.hc.core5.http.URIScheme;
42  import org.apache.hc.core5.http.config.Lookup;
43  import org.junit.Rule;
44  import org.junit.rules.ExternalResource;
45  import org.junit.runner.RunWith;
46  import org.junit.runners.Parameterized;
47  
48  @RunWith(Parameterized.class)
49  public class TestH2ClientAuthentication extends AbstractHttpAsyncClientAuthentication<CloseableHttpAsyncClient> {
50  
51      @Parameterized.Parameters(name = "HTTP/2 {0}")
52      public static Collection<Object[]> protocols() {
53          return Arrays.asList(new Object[][]{
54                  {URIScheme.HTTP},
55                  {URIScheme.HTTPS},
56          });
57      }
58  
59      protected H2AsyncClientBuilder clientBuilder;
60      protected PoolingAsyncClientConnectionManager connManager;
61  
62      @Rule
63      public ExternalResource clientResource = new ExternalResource() {
64  
65          @Override
66          protected void before() throws Throwable {
67              clientBuilder = H2AsyncClientBuilder.create()
68                      .setDefaultRequestConfig(RequestConfig.custom()
69                              .setConnectionRequestTimeout(TIMEOUT)
70                              .setConnectTimeout(TIMEOUT)
71                              .build())
72                      .setTlsStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()));
73          }
74  
75      };
76  
77      public TestH2ClientAuthentication(final URIScheme scheme) {
78          super(scheme, HttpVersion.HTTP_2);
79      }
80  
81      @Override
82      void setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
83          clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
84      }
85  
86      @Override
87      void setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
88          clientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy);
89      }
90  
91      @Override
92      protected CloseableHttpAsyncClient createClient() throws Exception {
93          return clientBuilder.build();
94      }
95  
96  }