1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }