Skip to content

Commit 863f6e4

Browse files
author
eugenp
committed
new tests for ssl
1 parent 0693cca commit 863f6e4

2 files changed

Lines changed: 83 additions & 7 deletions

File tree

‎spring-security-rest-basic-auth/src/test/java/org/baeldung/client/ClientLiveTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package org.baeldung.client;
22

3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.io.IOException;
7+
import java.security.GeneralSecurityException;
8+
import java.security.cert.X509Certificate;
9+
310
import org.apache.http.auth.AuthScope;
411
import org.apache.http.auth.UsernamePasswordCredentials;
12+
import org.apache.http.conn.scheme.Scheme;
13+
import org.apache.http.conn.ssl.SSLSocketFactory;
14+
import org.apache.http.conn.ssl.TrustStrategy;
515
import org.apache.http.impl.client.DefaultHttpClient;
616
import org.baeldung.client.spring.ClientConfig;
717
import org.baeldung.web.dto.Foo;
@@ -14,6 +24,7 @@
1424
import org.springframework.test.context.ContextConfiguration;
1525
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1626
import org.springframework.test.context.support.AnnotationConfigContextLoader;
27+
import org.springframework.web.client.ResourceAccessException;
1728
import org.springframework.web.client.RestTemplate;
1829

1930
@RunWith(SpringJUnit4ClassRunner.class)
@@ -35,4 +46,32 @@ public final void whenSecuredRestApiIsConsumed_then200OK() {
3546
System.out.println(responseEntity.getStatusCode());
3647
}
3748

49+
@Test(expected = ResourceAccessException.class)
50+
public final void whenHttpsUrlIsConsumed_thenException() {
51+
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
52+
final ResponseEntity<String> response = new RestTemplate().exchange(urlOverHttps, HttpMethod.GET, null, String.class);
53+
assertThat(response.getStatusCode().value(), equalTo(200));
54+
}
55+
56+
@Test
57+
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws IOException, GeneralSecurityException {
58+
final RestTemplate newRestTemplate = new RestTemplate();
59+
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
60+
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
61+
62+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
63+
@Override
64+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
65+
return true;
66+
}
67+
};
68+
69+
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
70+
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
71+
72+
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
73+
final ResponseEntity<String> response = newRestTemplate.exchange(urlOverHttps, HttpMethod.GET, null, String.class);
74+
assertThat(response.getStatusCode().value(), equalTo(200));
75+
}
76+
3877
}

‎spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
package org.baeldung.client;
22

3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
36
import java.io.IOException;
7+
import java.security.GeneralSecurityException;
8+
import java.security.cert.X509Certificate;
49
import java.util.Timer;
510
import java.util.TimerTask;
611

12+
import javax.net.ssl.SSLPeerUnverifiedException;
13+
714
import org.apache.http.HttpResponse;
815
import org.apache.http.client.ClientProtocolException;
916
import org.apache.http.client.methods.HttpGet;
1017
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;
1123
import org.apache.http.impl.client.DefaultHttpClient;
24+
import org.apache.http.impl.conn.PoolingClientConnectionManager;
1225
import org.apache.http.params.CoreConnectionPNames;
1326
import org.apache.http.params.HttpConnectionParams;
1427
import org.apache.http.params.HttpParams;
15-
import org.baeldung.client.spring.ClientConfig;
1628
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;
2129

22-
@RunWith(SpringJUnit4ClassRunner.class)
23-
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
2430
public class RawClientLiveTest {
2531

2632
// tests
@@ -51,6 +57,37 @@ public void run() {
5157
System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode());
5258
}
5359

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+
5491
// util
5592

5693
final void configureViaHighLevelApi(final int timeout, final HttpParams httpParams) {

0 commit comments

Comments
 (0)