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.concurrent.Future;
30  import java.util.function.Consumer;
31  
32  import org.apache.hc.client5.http.AuthenticationStrategy;
33  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
34  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
35  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
36  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
37  import org.apache.hc.client5.http.auth.AuthScope;
38  import org.apache.hc.client5.http.auth.CredentialsProvider;
39  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
40  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
41  import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
42  import org.apache.hc.client5.http.protocol.HttpClientContext;
43  import org.apache.hc.client5.testing.BasicTestAuthenticator;
44  import org.apache.hc.core5.function.Decorator;
45  import org.apache.hc.core5.http.HeaderElements;
46  import org.apache.hc.core5.http.HttpHeaders;
47  import org.apache.hc.core5.http.HttpHost;
48  import org.apache.hc.core5.http.HttpRequestInterceptor;
49  import org.apache.hc.core5.http.HttpResponse;
50  import org.apache.hc.core5.http.HttpResponseInterceptor;
51  import org.apache.hc.core5.http.HttpStatus;
52  import org.apache.hc.core5.http.URIScheme;
53  import org.apache.hc.core5.http.config.Http1Config;
54  import org.apache.hc.core5.http.config.Lookup;
55  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
56  import org.apache.hc.core5.testing.nio.H2TestServer;
57  import org.junit.jupiter.api.Assertions;
58  import org.junit.jupiter.api.Test;
59  import org.mockito.Mockito;
60  
61  public abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuthenticationTest<CloseableHttpAsyncClient> {
62  
63      public TestHttp1ClientAuthentication(final URIScheme scheme) {
64          super(scheme);
65      }
66  
67      @Override
68      protected H2TestServer startServer(final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator) throws Exception {
69          return startServer(Http1Config.DEFAULT, null, exchangeHandlerDecorator);
70      }
71  
72      @Override
73      protected CloseableHttpAsyncClient startClientCustom(final Consumer<TestClientBuilder> clientCustomizer) throws Exception {
74  
75          return startClient(new Consumer<HttpAsyncClientBuilder>() {
76  
77              @Override
78              public void accept(final HttpAsyncClientBuilder builder) {
79  
80                  clientCustomizer.accept(new TestClientBuilder() {
81  
82                      @Override
83                      public TestClientBuilder setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
84                          builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
85                          return this;
86                      }
87  
88                      @Override
89                      public TestClientBuilder setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
90                          builder.setTargetAuthenticationStrategy(targetAuthStrategy);
91                          return this;
92                      }
93  
94                      @Override
95                      public TestClientBuilder addResponseInterceptor(final HttpResponseInterceptor responseInterceptor) {
96                          builder.addResponseInterceptorLast(responseInterceptor);
97                          return this;
98                      }
99  
100                     @Override
101                     public TestClientBuilder addRequestInterceptor(final HttpRequestInterceptor requestInterceptor) {
102                         builder.addRequestInterceptorFirst(requestInterceptor);
103                         return this;
104                     }
105 
106                 });
107 
108             }
109 
110         });
111     }
112 
113     @Test
114     public void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
115         final H2TestServer server = startServer(exchangeHandler ->
116                 new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) {
117 
118                     @Override
119                     protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
120                         unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
121                     }
122                 });
123         server.register("*", AsyncEchoHandler::new);
124         final HttpHost target = targetHost();
125 
126         final CloseableHttpAsyncClient client = startClient();
127 
128         final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);
129         Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
130                 .thenReturn(new UsernamePasswordCredentials("test", "test".toCharArray()));
131         final HttpClientContext context = HttpClientContext.create();
132         context.setCredentialsProvider(credsProvider);
133 
134         final SimpleHttpRequest request = SimpleRequestBuilder.get()
135                 .setHttpHost(target)
136                 .setPath("/")
137                 .build();
138         final Future<SimpleHttpResponse> future = client.execute(request, context, null);
139         final HttpResponse response = future.get();
140 
141         Assertions.assertNotNull(response);
142         Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
143         Mockito.verify(credsProvider).getCredentials(
144                 Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any());
145     }
146 
147 }