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.Arrays;
30 import java.util.Collection;
31 import java.util.concurrent.Future;
32
33 import org.apache.hc.client5.http.AuthenticationStrategy;
34 import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
35 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
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.UsernamePasswordCredentials;
39 import org.apache.hc.client5.http.config.RequestConfig;
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.impl.nio.PoolingAsyncClientConnectionManager;
43 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
44 import org.apache.hc.client5.http.protocol.HttpClientContext;
45 import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
46 import org.apache.hc.client5.testing.BasicTestAuthenticator;
47 import org.apache.hc.client5.testing.SSLTestContexts;
48 import org.apache.hc.core5.function.Decorator;
49 import org.apache.hc.core5.function.Supplier;
50 import org.apache.hc.core5.http.HeaderElements;
51 import org.apache.hc.core5.http.HttpHeaders;
52 import org.apache.hc.core5.http.HttpHost;
53 import org.apache.hc.core5.http.HttpResponse;
54 import org.apache.hc.core5.http.HttpStatus;
55 import org.apache.hc.core5.http.HttpVersion;
56 import org.apache.hc.core5.http.URIScheme;
57 import org.apache.hc.core5.http.config.Http1Config;
58 import org.apache.hc.core5.http.config.Lookup;
59 import org.apache.hc.core5.http.impl.HttpProcessors;
60 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
61 import org.junit.Assert;
62 import org.junit.Rule;
63 import org.junit.Test;
64 import org.junit.rules.ExternalResource;
65 import org.junit.runner.RunWith;
66 import org.junit.runners.Parameterized;
67
68 @RunWith(Parameterized.class)
69 public class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuthentication<CloseableHttpAsyncClient> {
70
71 @Parameterized.Parameters(name = "HTTP/1.1 {0}")
72 public static Collection<Object[]> protocols() {
73 return Arrays.asList(new Object[][]{
74 {URIScheme.HTTP},
75 {URIScheme.HTTPS},
76 });
77 }
78
79 protected HttpAsyncClientBuilder clientBuilder;
80 protected PoolingAsyncClientConnectionManager connManager;
81
82 @Rule
83 public ExternalResource connManagerResource = new ExternalResource() {
84
85 @Override
86 protected void before() throws Throwable {
87 connManager = PoolingAsyncClientConnectionManagerBuilder.create()
88 .setTlsStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
89 .build();
90 }
91
92 @Override
93 protected void after() {
94 if (connManager != null) {
95 connManager.close();
96 connManager = null;
97 }
98 }
99
100 };
101
102 @Rule
103 public ExternalResource clientBuilderResource = new ExternalResource() {
104
105 @Override
106 protected void before() throws Throwable {
107 clientBuilder = HttpAsyncClientBuilder.create()
108 .setDefaultRequestConfig(RequestConfig.custom()
109 .setConnectionRequestTimeout(TIMEOUT)
110 .setConnectTimeout(TIMEOUT)
111 .build())
112 .setConnectionManager(connManager);
113 }
114
115 };
116
117 public TestHttp1ClientAuthentication(final URIScheme scheme) {
118 super(scheme, HttpVersion.HTTP_1_1);
119 }
120
121 @Override
122 void setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
123 clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
124 }
125
126 @Override
127 void setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
128 clientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy);
129 }
130
131 @Override
132 protected CloseableHttpAsyncClient createClient() throws Exception {
133 return clientBuilder.build();
134 }
135
136 @Test
137 public void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
138 server.register("*", new Supplier<AsyncServerExchangeHandler>() {
139
140 @Override
141 public AsyncServerExchangeHandler get() {
142 return new AsyncEchoHandler();
143 }
144
145 });
146 final HttpHost target = start(
147 HttpProcessors.server(),
148 new Decorator<AsyncServerExchangeHandler>() {
149
150 @Override
151 public AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler exchangeHandler) {
152 return new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) {
153
154 @Override
155 protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
156 unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
157 }
158 };
159 }
160
161 },
162 Http1Config.DEFAULT);
163
164 final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
165 new UsernamePasswordCredentials("test", "test".toCharArray()));
166 final HttpClientContext context = HttpClientContext.create();
167 context.setCredentialsProvider(credsProvider);
168
169 final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
170 final HttpResponse response = future.get();
171
172 Assert.assertNotNull(response);
173 Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
174 final AuthScope authscope = credsProvider.getAuthScope();
175 Assert.assertNotNull(authscope);
176 Assert.assertEquals("test realm", authscope.getRealm());
177 }
178
179 }