RestFunctions { protected static HttpClientContext getHttpClientContext(RestConfig restConfig, HttpHost target, Optional<HttpHost> proxy) throws IOException { HttpClientContext httpClientContext = HttpClientContext.create(); boolean credentialsAdded = false; CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (restConfig.getBasicAuthUser() != null && restConfig.getBasicAuthPasswordPath() != null) { String password = new String(readBytes(new Path(restConfig.getBasicAuthPasswordPath())), StandardCharsets.UTF_8); credentialsProvider.setCredentials( new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), password)); credentialsAdded = true; } if (proxy.isPresent() && restConfig.getProxyBasicAuthUser() != null && restConfig.getProxyBasicAuthPasswordPath() != null) { String password = new String(readBytes(new Path(restConfig.getProxyBasicAuthPasswordPath())), StandardCharsets.UTF_8); credentialsProvider.setCredentials( new AuthScope(proxy.get()), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), password)); credentialsAdded = true; } if (credentialsAdded) { httpClientContext.setCredentialsProvider(credentialsProvider); } return httpClientContext; }  static T getArg(int index, Class<T> clazz, List<Object> args);  }
@Test public void restGetShouldGetHttpClientContext() throws Exception { HttpHost target = new HttpHost("localhost", 8080); HttpHost proxy = new HttpHost("localhost", 3128); { RestConfig restConfig = new RestConfig(); HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty()); assertNull(actual.getCredentialsProvider()); } { RestConfig restConfig = new RestConfig(); restConfig.put(BASIC_AUTH_USER, "user"); restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath()); HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty()); HttpClientContext expected = HttpClientContext.create(); CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider(); expectedCredentialsProvider.setCredentials( new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword)); expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy))); } { RestConfig restConfig = new RestConfig(); restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser"); restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath()); HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy)); HttpClientContext expected = HttpClientContext.create(); CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider(); expectedCredentialsProvider.setCredentials( new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword)); expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy))); } { RestConfig restConfig = new RestConfig(); restConfig.put(BASIC_AUTH_USER, "user"); restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath()); restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser"); restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath()); HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy)); HttpClientContext expected = HttpClientContext.create(); CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider(); expectedCredentialsProvider.setCredentials( new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword)); expectedCredentialsProvider.setCredentials( new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword)); expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy))); } }