1414import org .apache .hc .client5 .http .auth .BearerToken ;
1515import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
1616import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
17- import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
18- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
19- import org .apache .hc .client5 .http .impl .classic .MinimalHttpClient ;
17+ import org .apache .hc .client5 .http .impl .io .DefaultHttpClientConnectionOperator ;
18+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
2019import org .apache .hc .client5 .http .impl .routing .DefaultProxyRoutePlanner ;
2120import org .apache .hc .core5 .http .HttpHost ;
21+ import org .apache .hc .core5 .http .config .Registry ;
2222import org .awaitility .Awaitility ;
2323import org .junit .Assert ;
2424import org .junit .Test ;
3434import java .lang .reflect .Method ;
3535import java .lang .reflect .Modifier ;
3636import java .net .URISyntaxException ;
37+ import java .util .HashMap ;
3738import java .util .concurrent .ConcurrentHashMap ;
3839import java .util .concurrent .TimeUnit ;
3940
@@ -98,12 +99,12 @@ public void testFactoryInstantiationIntegrationsConfig() throws Exception {
9899 }
99100
100101 @ Test
101- public void testFactoryInstantiationWithProxy () throws Exception {
102+ public void testFactoryInstantiationWithProxyCredentials () throws Exception {
102103 SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
103104 .enableDebug ()
104105 .impressionsMode (ImpressionsManager .Mode .DEBUG )
105106 .impressionsRefreshRate (1 )
106- .endpoint (ENDPOINT ,EVENTS_ENDPOINT )
107+ .endpoint (ENDPOINT , EVENTS_ENDPOINT )
107108 .telemetryURL (SplitClientConfig .TELEMETRY_ENDPOINT )
108109 .authServiceURL (AUTH_SERVICE )
109110 .setBlockUntilReadyTimeout (1000 )
@@ -147,11 +148,16 @@ public void testFactoryInstantiationWithProxy() throws Exception {
147148 Assert .assertEquals ("test" , credMap .entrySet ().stream ().iterator ().next ().getValue ().getUserName ());
148149 assertNotNull (credMap .entrySet ().stream ().iterator ().next ().getValue ().getUserPassword ());
149150
150- splitClientConfig = SplitClientConfig .builder ()
151+ splitFactory .destroy ();
152+ }
153+
154+ @ Test
155+ public void testFactoryInstantiationWithProxyToken () throws Exception {
156+ SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
151157 .enableDebug ()
152158 .impressionsMode (ImpressionsManager .Mode .DEBUG )
153159 .impressionsRefreshRate (1 )
154- .endpoint (ENDPOINT ,EVENTS_ENDPOINT )
160+ .endpoint (ENDPOINT , EVENTS_ENDPOINT )
155161 .telemetryURL (SplitClientConfig .TELEMETRY_ENDPOINT )
156162 .authServiceURL (AUTH_SERVICE )
157163 .setBlockUntilReadyTimeout (1000 )
@@ -171,7 +177,7 @@ public void testFactoryInstantiationWithProxy() throws Exception {
171177 httpClientField2 .setAccessible (true );
172178 Class <?> InternalHttp2 = Class .forName ("org.apache.hc.client5.http.impl.classic.InternalHttpClient" );
173179
174- Field credentialsProviderField2 = InternalHttp .getDeclaredField ("credentialsProvider" );
180+ Field credentialsProviderField2 = InternalHttp2 .getDeclaredField ("credentialsProvider" );
175181 credentialsProviderField2 .setAccessible (true );
176182 BasicCredentialsProvider credentialsProvider2 = (BasicCredentialsProvider ) credentialsProviderField2 .get (InternalHttp2 .cast (httpClientField2 .get (client2 )));
177183
@@ -181,7 +187,12 @@ public void testFactoryInstantiationWithProxy() throws Exception {
181187
182188 Assert .assertEquals ("123456789" , credMap2 .entrySet ().stream ().iterator ().next ().getValue ().getToken ());
183189
184- splitClientConfig = SplitClientConfig .builder ()
190+ splitFactory2 .destroy ();
191+ }
192+
193+ @ Test
194+ public void testFactoryInstantiationWithProxyMtls () throws Exception {
195+ SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
185196 .enableDebug ()
186197 .impressionsMode (ImpressionsManager .Mode .DEBUG )
187198 .impressionsRefreshRate (1 )
@@ -191,15 +202,61 @@ public void testFactoryInstantiationWithProxy() throws Exception {
191202 .setBlockUntilReadyTimeout (1000 )
192203 .proxyPort (6060 )
193204 .proxyScheme ("https" )
194- .proxyMtlsAuth (new ProxyMTLSAuth .Builder ().proxyP12File ("file " ).proxyP12FilePassKey ("pass " ).build ())
205+ .proxyMtlsAuth (new ProxyMTLSAuth .Builder ().proxyP12File ("src/test/resources/keyStore.p12 " ).proxyP12FilePassKey ("split " ).build ())
195206 .proxyHost (ENDPOINT )
196207 .build ();
197208 SplitFactoryImpl splitFactory3 = new SplitFactoryImpl (API_KEY , splitClientConfig );
198209 assertNotNull (splitFactory3 .client ());
199210 assertNotNull (splitFactory3 .manager ());
200211
201- splitFactory .destroy ();
202- splitFactory2 .destroy ();
212+ Field splitHttpClientField3 = SplitFactoryImpl .class .getDeclaredField ("_splitHttpClient" );
213+ splitHttpClientField3 .setAccessible (true );
214+ SplitHttpClientImpl client3 = (SplitHttpClientImpl ) splitHttpClientField3 .get (splitFactory3 );
215+
216+ Field httpClientField3 = SplitHttpClientImpl .class .getDeclaredField ("_client" );
217+ httpClientField3 .setAccessible (true );
218+ Class <?> InternalHttp3 = Class .forName ("org.apache.hc.client5.http.impl.classic.InternalHttpClient" );
219+
220+ Field connManagerField = InternalHttp3 .getDeclaredField ("connManager" );
221+ connManagerField .setAccessible (true );
222+ PoolingHttpClientConnectionManager connManager = (PoolingHttpClientConnectionManager ) connManagerField .get (InternalHttp3 .cast (httpClientField3 .get (client3 )));
223+
224+ Field connectionOperatorField = PoolingHttpClientConnectionManager .class .getDeclaredField ("connectionOperator" );
225+ connectionOperatorField .setAccessible (true );
226+ DefaultHttpClientConnectionOperator connectionOperator = (DefaultHttpClientConnectionOperator ) connectionOperatorField .get (connManager );
227+
228+ Field tlsSocketStrategyLookupField = DefaultHttpClientConnectionOperator .class .getDeclaredField ("tlsSocketStrategyLookup" );
229+ tlsSocketStrategyLookupField .setAccessible (true );
230+ Registry tlsSocketStrategyLookup = (Registry ) tlsSocketStrategyLookupField .get (connectionOperator );
231+
232+ Field mapField = Registry .class .getDeclaredField ("map" );
233+ mapField .setAccessible (true );
234+ Class <?> map = mapField .get (tlsSocketStrategyLookup ).getClass ();
235+
236+ Class <?> value = ((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ).getClass ();
237+
238+ Field arg1Field = value .getDeclaredField ("arg$1" );
239+ arg1Field .setAccessible (true );
240+ Class <?> sslConnectionSocketFactory = arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" )).getClass ();
241+
242+ Field socketFactoryField = sslConnectionSocketFactory .getDeclaredField ("socketFactory" );
243+ socketFactoryField .setAccessible (true );
244+ Class <?> socketFactory = socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))).getClass ();
245+
246+ Field contextField = socketFactory .getDeclaredField ("context" );
247+ contextField .setAccessible (true );
248+ Class <?> context = Class .forName ("sun.security.ssl.SSLContextImpl" );
249+
250+ Field keyManagerField = context .getDeclaredField ("keyManager" );
251+ keyManagerField .setAccessible (true );
252+ Class <?> keyManager = keyManagerField .get (contextField .get (socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))))).getClass ();
253+
254+ Field credentialsMapField = keyManager .getDeclaredField ("credentialsMap" );
255+ credentialsMapField .setAccessible (true );
256+ HashMap <String ,Object > credentialsMap = (HashMap ) credentialsMapField .get (keyManagerField .get (contextField .get (socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))))));
257+
258+ assertNotNull (credentialsMap .get ("1" ));
259+
203260 splitFactory3 .destroy ();
204261 }
205262
0 commit comments