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  
31  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
32  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
33  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
34  import org.apache.hc.client5.http.auth.AuthScope;
35  import org.apache.hc.client5.http.auth.CredentialsProvider;
36  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
37  import org.apache.hc.client5.http.protocol.HttpClientContext;
38  import org.apache.hc.client5.testing.BasicTestAuthenticator;
39  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
40  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
41  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
42  import org.apache.hc.core5.http.HeaderElements;
43  import org.apache.hc.core5.http.HttpHeaders;
44  import org.apache.hc.core5.http.HttpHost;
45  import org.apache.hc.core5.http.HttpResponse;
46  import org.apache.hc.core5.http.HttpStatus;
47  import org.apache.hc.core5.http.URIScheme;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  import org.mockito.Mockito;
51  
52  public abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuthenticationTest {
53  
54      public TestHttp1ClientAuthentication(final URIScheme scheme) {
55          super(scheme, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD);
56      }
57  
58      @Test
59      public void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
60          final HttpHost target = startServer();
61          configureServer(bootstrap -> bootstrap
62                  .register("*", AsyncEchoHandler::new)
63                  .setExchangeHandlerDecorator(exchangeHandler ->
64                          new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) {
65  
66                              @Override
67                              protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
68                                  unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
69                              }
70                          }));
71  
72          final TestAsyncClient client = startClient();
73  
74          final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);
75          Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
76                  .thenReturn(new UsernamePasswordCredentials("test", "test".toCharArray()));
77          final HttpClientContext context = HttpClientContext.create();
78          context.setCredentialsProvider(credsProvider);
79  
80          final SimpleHttpRequest request = SimpleRequestBuilder.get()
81                  .setHttpHost(target)
82                  .setPath("/")
83                  .build();
84          final Future<SimpleHttpResponse> future = client.execute(request, context, null);
85          final HttpResponse response = future.get();
86  
87          Assertions.assertNotNull(response);
88          Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
89          Mockito.verify(credsProvider).getCredentials(
90                  Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any());
91      }
92  
93  }