Skip to content

Commit b018c6c

Browse files
nboseckerpivovarit
authored andcommitted
Fixed http ssl apache 4.4 test, moved from Sandbox to Live class (eugenp#764)
* Moved project to core-java from eclipse folder * Fixed http ssl test, moved from Sandbox to Live.
1 parent 366914a commit b018c6c

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.baeldung.httpclient;
22

3-
import static org.hamcrest.Matchers.equalTo;
43
import static org.junit.Assert.assertThat;
54

65
import java.io.IOException;
76
import java.security.GeneralSecurityException;
7+
import java.security.KeyManagementException;
8+
import java.security.KeyStoreException;
9+
import java.security.NoSuchAlgorithmException;
810
import java.security.cert.X509Certificate;
911

1012
import javax.net.ssl.SSLContext;
@@ -16,6 +18,7 @@
1618
import org.apache.http.conn.ClientConnectionManager;
1719
import org.apache.http.conn.scheme.Scheme;
1820
import org.apache.http.conn.scheme.SchemeRegistry;
21+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
1922
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2023
import org.apache.http.conn.ssl.SSLContextBuilder;
2124
import org.apache.http.conn.ssl.SSLContexts;
@@ -108,4 +111,30 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
108111
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
109112
}
110113

114+
@Test
115+
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException {
116+
117+
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
118+
@Override
119+
public boolean isTrusted(X509Certificate[] certificate, String authType) {
120+
return true;
121+
}
122+
};
123+
124+
SSLContext sslContext = null;
125+
try {
126+
sslContext = new SSLContextBuilder().loadTrustMaterial(null, acceptingTrustStrategy).build();
127+
128+
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
129+
e.printStackTrace();
130+
}
131+
132+
CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
133+
final HttpGet httpGet = new HttpGet("https://sesar3.geoinfogeochem.org/sample/igsn/ODP000002");
134+
httpGet.setHeader("Accept", "application/xml");
135+
136+
final HttpResponse response = client.execute(httpGet);
137+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
138+
}
139+
111140
}

httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientSandboxLiveTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,4 @@ public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectSt
5959

6060
System.out.println(response.getStatusLine());
6161
}
62-
63-
@Test
64-
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException {
65-
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
66-
67-
final HttpGet httpGet = new HttpGet("https://sesar3.geoinfogeochem.org/sample/igsn/ODP000002");
68-
httpGet.setHeader("Accept", "application/xml");
69-
70-
response = httpClient.execute(httpGet);
71-
}
72-
7362
}

0 commit comments

Comments
 (0)