You need to use a HttpClient ie override the BasicHttpClient and set proxies to it as below-
Step-1)
CredentialsProvider credsProvider = createProxyCredentialsProvider(proxyHost, proxyPort, proxyUserName, proxyPassword);
Step-2)
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
Step-3) method Step-1
private CredentialsProvider createProxyCredentialsProvider(String proxyHost, int proxyPort, String proxyUserName, String proxyPassword) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(proxyUserName, proxyPassword));
return credsProvider;
}
Step-4)
Set the values from Step-1 and Step-2
HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setDefaultCookieStore(cookieStore)
.setDefaultCredentialsProvider(credsProvider) //<------------- From Step-1
.setProxy(proxy) //<------------- From Step-2
.build();
You can inject the Corporate Proxy details to the custom {{HttpClient}} li below from a config file simply by annotating the key names from the host config file which is used by the runner for mentioning host and port.
e.g.:
See an example here(Client)- https://github.com/authorjapps/zerocode/blob/master/src/main/java/org/jsmart/zerocode/core/httpclient/soap/SoapCorporateProxySslHttpClient.java
Usage example here(Test): https://github.com/authorjapps/zerocode/blob/master/src/test/java/org/jsmart/zerocode/core/soap/SoapCorpProxySslHttpClientTest.java
How to use?
@UseHttpClient(SoapCorporateProxySslHttpClient.class)
@TargetEnv("soap_host_with_corp_proxy.properties")
@RunWith(ZeroCodeUnitRunner.class)
public class SoapCorpProxySslHttpClientTest {
@Ignore
@Test
@Scenario("foo/bar/soap_test_case_file.json")
public void testSoapWithCorpProxyEnabled() throws Exception {
}
}
Explanation:
@TargetEnv("hello_world_host.properties")
@RunWith(ZeroCodeUnitRunner.class)
public class HelloWorldTest {
// @Test
// tests here
}
soap_host_with_corp_proxy.properties
---------------------------
# Web Server host and port
web.application.endpoint.host=https://soap-server-host/ServiceName
web.application.endpoint.port=443
# Web Service context; Leave it blank in case you do not have a common context
web.application.endpoint.context=
#sample test purpose - if you remove this from ehre, then make sure to remove from Java file
corporate.proxy.host=http://exam.corporate-proxy-host.co.uk
corporate.proxy.port=80
corporate.proxy.username=HAVYSTARUSER
corporate.proxy.password=i#am#here#for#soap#
Your HttpClient:
----------------
See-
https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
public class YourHttpClient {
@Inject
@Named("corporate.proxy.host")
private String proxyHost;
@Inject
@Named("corporate.proxy.port")
private String proxyPort;
@Inject
@Named("corporate.proxy.username")
private String proxyUserName;
@Inject
@Named("corporate.proxy.password")
private String proxyPassword;
// Build the client using these.
}