Skip to content

Commit 5b416e6

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 13f7e2a + 72279b8 commit 5b416e6

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

httpclient/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
<mockito.version>1.10.8</mockito.version>
174174

175175
<httpcore.version>4.3.3</httpcore.version>
176-
<httpclient.version>4.3.4</httpclient.version>
176+
<httpclient.version>4.3.6</httpclient.version>
177177

178178
<rest-assured.version>2.4.0</rest-assured.version>
179179

httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java renamed to httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.security.GeneralSecurityException;
88
import java.security.cert.X509Certificate;
99

10-
import javax.net.ssl.SSLPeerUnverifiedException;
10+
import javax.net.ssl.SSLException;
1111

1212
import org.apache.http.HttpResponse;
1313
import org.apache.http.client.ClientProtocolException;
@@ -25,24 +25,25 @@
2525
import org.apache.http.impl.client.HttpClientBuilder;
2626
import org.apache.http.impl.client.HttpClients;
2727
import org.apache.http.impl.conn.PoolingClientConnectionManager;
28-
import org.junit.Ignore;
2928
import org.junit.Test;
3029

3130
/**
3231
* This test requires a localhost server over HTTPS <br>
3332
* It should only be manually run, not part of the automated build
3433
* */
35-
public class HttpsClientLiveManualTest {
34+
public class HttpsClientSslLiveTest {
35+
36+
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
37+
// "https://mms.nw.ru/" // hosted
38+
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
3639

3740
// tests
3841

39-
@Test(expected = SSLPeerUnverifiedException.class)
40-
@Ignore("Only for a server that has HTTPS enabled (on 8443)")
42+
@Test(expected = SSLException.class)
4143
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
4244
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
4345

44-
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
45-
final HttpGet getMethod = new HttpGet(urlOverHttps);
46+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
4647
final HttpResponse response = httpClient.execute(getMethod);
4748
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
4849
}
@@ -58,13 +59,34 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
5859
};
5960
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
6061
final SchemeRegistry registry = new SchemeRegistry();
61-
registry.register(new Scheme("https", 8443, sf));
62+
registry.register(new Scheme("https", 443, sf));
63+
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
64+
65+
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
66+
67+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
68+
final HttpResponse response = httpClient.execute(getMethod);
69+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
70+
71+
httpClient.close();
72+
}
73+
74+
@Test
75+
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
76+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
77+
@Override
78+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
79+
return true;
80+
}
81+
};
82+
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
83+
final SchemeRegistry registry = new SchemeRegistry();
84+
registry.register(new Scheme("https", 443, sf));
6285
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
6386

6487
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
6588

66-
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
67-
final HttpGet getMethod = new HttpGet(urlOverHttps);
89+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
6890
final HttpResponse response = httpClient.execute(getMethod);
6991
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
7092

@@ -80,8 +102,7 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
80102

81103
// new
82104

83-
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
84-
final HttpGet getMethod = new HttpGet(urlOverHttps);
105+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
85106
final HttpResponse response = httpClient.execute(getMethod);
86107
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
87108
}

0 commit comments

Comments
 (0)