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.function.Consumer;
30  
31  import org.apache.hc.client5.http.AuthenticationStrategy;
32  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
33  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
34  import org.apache.hc.client5.http.impl.async.H2AsyncClientBuilder;
35  import org.apache.hc.core5.function.Decorator;
36  import org.apache.hc.core5.http.HttpRequestInterceptor;
37  import org.apache.hc.core5.http.HttpResponseInterceptor;
38  import org.apache.hc.core5.http.URIScheme;
39  import org.apache.hc.core5.http.config.Lookup;
40  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
41  import org.apache.hc.core5.http2.config.H2Config;
42  import org.apache.hc.core5.testing.nio.H2TestServer;
43  
44  public abstract class TestH2ClientAuthentication extends AbstractHttpAsyncClientAuthenticationTest<CloseableHttpAsyncClient> {
45  
46      public TestH2ClientAuthentication(final URIScheme scheme) {
47          super(scheme);
48      }
49  
50      @Override
51      protected H2TestServer startServer(final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator) throws Exception {
52          return startServer(H2Config.DEFAULT, null, exchangeHandlerDecorator);
53      }
54  
55      @Override
56      protected CloseableHttpAsyncClient startClientCustom(final Consumer<TestClientBuilder> clientCustomizer) throws Exception {
57  
58          return startH2Client(new Consumer<H2AsyncClientBuilder>() {
59  
60              @Override
61              public void accept(final H2AsyncClientBuilder builder) {
62  
63                  clientCustomizer.accept(new TestClientBuilder() {
64  
65                      @Override
66                      public TestClientBuilder setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
67                          builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
68                          return this;
69                      }
70  
71                      @Override
72                      public TestClientBuilder setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
73                          builder.setTargetAuthenticationStrategy(targetAuthStrategy);
74                          return this;
75                      }
76  
77                      @Override
78                      public TestClientBuilder addResponseInterceptor(final HttpResponseInterceptor responseInterceptor) {
79                          builder.addResponseInterceptorLast(responseInterceptor);
80                          return this;
81                      }
82  
83                      @Override
84                      public TestClientBuilder addRequestInterceptor(final HttpRequestInterceptor requestInterceptor) {
85                          builder.addRequestInterceptorFirst(requestInterceptor);
86                          return this;
87                      }
88  
89                  });
90  
91              }
92  
93          });
94      }
95  
96  }