Skip to content

Commit de257ee

Browse files
committed
[BAEL-16045] - Check Article Code Matches GitHub for https://www.baeldung.com/httpclient-ssl article
1 parent f3dd83f commit de257ee

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

httpclient-simple/src/test/java/com/baeldung/client/RestClientLiveManualTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import java.io.IOException;
88
import java.security.GeneralSecurityException;
9-
import java.security.cert.X509Certificate;
109

1110
import javax.net.ssl.SSLContext;
11+
import javax.net.ssl.SSLPeerUnverifiedException;
1212

1313
import org.apache.http.HttpResponse;
1414
import org.apache.http.client.ClientProtocolException;
@@ -46,7 +46,7 @@ public class RestClientLiveManualTest {
4646
// old httpClient will throw UnsupportedOperationException
4747
@Ignore
4848
@Test
49-
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_1() throws GeneralSecurityException {
49+
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException {
5050
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
5151
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
5252

@@ -92,7 +92,7 @@ public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumed_t
9292
}
9393

9494
@Test
95-
public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException {
95+
public final void givenAcceptingAllCertificatesUsing4_4_whenUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException {
9696
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
9797
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
9898
requestFactory.setHttpClient(httpClient);
@@ -101,4 +101,12 @@ public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUs
101101
assertThat(response.getStatusCode().value(), equalTo(200));
102102
}
103103

104+
@Test(expected = SSLPeerUnverifiedException.class)
105+
public void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
106+
CloseableHttpClient httpClient = HttpClients.createDefault();
107+
String urlOverHttps = "https://localhost:8082/httpclient-simple";
108+
HttpGet getMethod = new HttpGet(urlOverHttps);
109+
HttpResponse response = httpClient.execute(getMethod);
110+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
111+
}
104112
}

0 commit comments

Comments
 (0)