|
| 1 | +package org.jfrog.build.client; |
| 2 | + |
| 3 | +import org.apache.http.auth.AuthScope; |
| 4 | +import org.apache.http.auth.Credentials; |
| 5 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 6 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 7 | +import org.jfrog.build.api.util.NullLog; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import static org.testng.Assert.assertEquals; |
| 11 | +import static org.testng.Assert.assertNotNull; |
| 12 | + |
| 13 | +@Test |
| 14 | +public class PreemptiveHttpClientBuilderTest { |
| 15 | + public void testCredentialsMaintainedWithProxy() { |
| 16 | + String rtUser = "rt-user"; |
| 17 | + String rtPassword = "rt-password"; |
| 18 | + |
| 19 | + String proxyHost = "127.0.0.1"; |
| 20 | + int proxyPort = 8000; |
| 21 | + String proxyUser = "proxy-user"; |
| 22 | + String proxyPassword = "proxy-password"; |
| 23 | + |
| 24 | + PreemptiveHttpClientBuilder clientBuilder = new PreemptiveHttpClientBuilder() |
| 25 | + .setConnectionRetries(3) |
| 26 | + .setInsecureTls(false) |
| 27 | + .setTimeout(300) |
| 28 | + .setLog(new NullLog()) |
| 29 | + .setProxyConfiguration(createProxyConfiguration(proxyHost, proxyPort, proxyUser, proxyPassword)) |
| 30 | + .setUserName(rtUser) |
| 31 | + .setPassword(rtPassword); |
| 32 | + PreemptiveHttpClient deployClient = clientBuilder.build(); |
| 33 | + |
| 34 | + // Assert both Artifactory and proxy credentials exist in the credentials provider. |
| 35 | + BasicCredentialsProvider credentialsProvider = deployClient.basicCredentialsProvider; |
| 36 | + Credentials rtCredentials = credentialsProvider.getCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)); |
| 37 | + assertNotNull(rtCredentials); |
| 38 | + assertEquals(rtCredentials, new UsernamePasswordCredentials(rtUser, rtPassword)); |
| 39 | + |
| 40 | + Credentials portCredentials = credentialsProvider.getCredentials(new AuthScope(proxyHost, proxyPort)); |
| 41 | + assertNotNull(portCredentials); |
| 42 | + assertEquals(portCredentials, new UsernamePasswordCredentials(proxyUser, proxyPassword)); |
| 43 | + } |
| 44 | + |
| 45 | + private ProxyConfiguration createProxyConfiguration(String host, int port, String proxyUser, String proxyPassword) { |
| 46 | + ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); |
| 47 | + proxyConfiguration.host = host; |
| 48 | + proxyConfiguration.port = port; |
| 49 | + proxyConfiguration.username = proxyUser; |
| 50 | + proxyConfiguration.password = proxyPassword; |
| 51 | + return proxyConfiguration; |
| 52 | + } |
| 53 | +} |
0 commit comments