Я подключаюсь к API Github через среду тестирования, а привязка для подключения следующей конечной точки (любая конечная точка в api.github.com) приведет к следующей ошибке:
https://api.github.com/repos/myrepo/Test/git/blobs/929246f65aab4d636cb229c790f966afc332c124
FAILED: testGetBlobWithMandatoryParameters
github {getBlob} integration test with mandatory parameters.
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.
Это соединение работает, если я использую обычный java-клиент, проблема возникает только тогда, когда я подключаюсь через среду тестирования.
Мой код:
@Test(priority = 1, description = "github {getBlob} integration test.")
public void testGetBlobWithMandatoryParameters() throws IOException, JSONException{
String urlStr = "https://api.github.com/repos/myrepo/Test/git/blobs/929246f65aab4d636cb229c790f966afc332c124";
URL url = new URL(urlStr);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("GET");
InputStream responseStream = null;
String responseString = null;
if (httpConnection.getResponseCode() >= 400) {
responseStream = httpConnection.getErrorStream();
} else {
responseStream = httpConnection.getInputStream();
}
if (responseStream != null) {
StringBuilder stringBuilder = new StringBuilder();
byte[] bytes = new byte[1024];
int len;
while ((len = responseStream.read(bytes)) != -1) {
stringBuilder.append(new String(bytes, 0, len));
}
if (!stringBuilder.toString().trim().isEmpty()) {
responseString = stringBuilder.toString();
}
}
System.out.println(responseString);
}
Добавить
System.setProperty("javax.net.ssl.trustStore", "keystore.jks")
System.setProperty("javax.net.ssl.trustStorePassword", "changeit")