|
1 | 1 | package org.baeldung.client; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
3 | 6 | import java.io.IOException; |
| 7 | +import java.security.GeneralSecurityException; |
| 8 | +import java.security.cert.X509Certificate; |
4 | 9 | import java.util.Timer; |
5 | 10 | import java.util.TimerTask; |
6 | 11 |
|
| 12 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 13 | + |
7 | 14 | import org.apache.http.HttpResponse; |
8 | 15 | import org.apache.http.client.ClientProtocolException; |
9 | 16 | import org.apache.http.client.methods.HttpGet; |
10 | 17 | import org.apache.http.client.params.ClientPNames; |
| 18 | +import org.apache.http.conn.ClientConnectionManager; |
| 19 | +import org.apache.http.conn.scheme.Scheme; |
| 20 | +import org.apache.http.conn.scheme.SchemeRegistry; |
| 21 | +import org.apache.http.conn.ssl.SSLSocketFactory; |
| 22 | +import org.apache.http.conn.ssl.TrustStrategy; |
11 | 23 | import org.apache.http.impl.client.DefaultHttpClient; |
| 24 | +import org.apache.http.impl.conn.PoolingClientConnectionManager; |
12 | 25 | import org.apache.http.params.CoreConnectionPNames; |
13 | 26 | import org.apache.http.params.HttpConnectionParams; |
14 | 27 | import org.apache.http.params.HttpParams; |
15 | | -import org.baeldung.client.spring.ClientConfig; |
16 | 28 | import org.junit.Test; |
17 | | -import org.junit.runner.RunWith; |
18 | | -import org.springframework.test.context.ContextConfiguration; |
19 | | -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
20 | | -import org.springframework.test.context.support.AnnotationConfigContextLoader; |
21 | 29 |
|
22 | | -@RunWith(SpringJUnit4ClassRunner.class) |
23 | | -@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class) |
24 | 30 | public class RawClientLiveTest { |
25 | 31 |
|
26 | 32 | // tests |
@@ -51,6 +57,37 @@ public void run() { |
51 | 57 | System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); |
52 | 58 | } |
53 | 59 |
|
| 60 | + @Test(expected = SSLPeerUnverifiedException.class) |
| 61 | + public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { |
| 62 | + final DefaultHttpClient httpClient = new DefaultHttpClient(); |
| 63 | + |
| 64 | + final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; |
| 65 | + final HttpGet getMethod = new HttpGet(urlOverHttps); |
| 66 | + final HttpResponse response = httpClient.execute(getMethod); |
| 67 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws IOException, GeneralSecurityException { |
| 72 | + final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
| 73 | + @Override |
| 74 | + public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + }; |
| 78 | + final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
| 79 | + final SchemeRegistry registry = new SchemeRegistry(); |
| 80 | + registry.register(new Scheme("https", 8443, sf)); |
| 81 | + final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); |
| 82 | + |
| 83 | + final DefaultHttpClient httpClient = new DefaultHttpClient(ccm); |
| 84 | + |
| 85 | + final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; |
| 86 | + final HttpGet getMethod = new HttpGet(urlOverHttps); |
| 87 | + final HttpResponse response = httpClient.execute(getMethod); |
| 88 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 89 | + } |
| 90 | + |
54 | 91 | // util |
55 | 92 |
|
56 | 93 | final void configureViaHighLevelApi(final int timeout, final HttpParams httpParams) { |
|
0 commit comments