Here you go:
The rest
api i was trying to access uses keystore, trust store and user name
password. I haven't been able to figur eout how to specify user name
password in rest-assured.
package testPkg;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class TestClient {
public static void main(String[] args) throws Exception {
testAuth();
}
public static String testAuth() {
String KEY_STORE_PATH = give keystore file path here
String KEY_STORE_PASSWORD = give key store password here
String TRUST_STORE_PATH = give truststore path here
String TRUST_STORE_PASSWORD = give trust store password here
String SERVICE_USER = givelogin id here
String SERVICE_ACCT_PASSWORD = give password here
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(SERVICE_USER, SERVICE_ACCT_PASSWORD);
provider.setCredentials(AuthScope.ANY, (Credentials) credentials);
try {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreInput = new FileInputStream(KEY_STORE_PATH);
keystore.load(keystoreInput, KEY_STORE_PASSWORD.toCharArray());
System.out.println("Keystore has " + keystore.size() + " keys");
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream truststoreInput = new FileInputStream(TRUST_STORE_PATH);
truststore.load(truststoreInput, TRUST_STORE_PASSWORD.toCharArray());
System.out.println("Truststore has " + truststore.size() + " keys");
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(keystore, KEY_STORE_PASSWORD, truststore);
HttpClient httpClient = HttpClientBuilder.create()
.setSSLSocketFactory(sslSocketFactory)
.setDefaultCredentialsProvider(provider).build();
HttpGet httpGet = new HttpGet(GIVE REST API URL HERE);
HttpResponse httpResponse = httpClient.execute(httpGet);
String response = EntityUtils.toString(httpResponse.getEntity());
System.out.println("response::" + response);
System.out.println("responseCode " + httpResponse.getStatusLine().getStatusCode());
return response;
} catch (Exception e) {
System.out.println(e.getCause());
e.printStackTrace();
}
return null;
}
}
Do let me know if you are able to get the code working in rest-assured.