Skip to content

Commit c17010d

Browse files
author
eugenp
committed
testing work
1 parent 80c3ca1 commit c17010d

3 files changed

Lines changed: 25 additions & 57 deletions

File tree

‎spring-security-rest-basic-auth/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
<configuration>
253253
<excludes>
254254
<exclude>**/*LiveTest.java</exclude>
255+
<exclude>**/*ManualTest.java</exclude>
255256
</excludes>
256257
<systemPropertyVariables>
257258
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
package org.baeldung.client;
22

3-
import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
43
import static org.hamcrest.Matchers.equalTo;
54
import static org.hamcrest.Matchers.is;
65
import static org.junit.Assert.assertThat;
76

8-
import java.security.GeneralSecurityException;
9-
import java.security.cert.X509Certificate;
10-
11-
import org.apache.http.conn.scheme.Scheme;
12-
import org.apache.http.conn.ssl.SSLSocketFactory;
13-
import org.apache.http.conn.ssl.TrustStrategy;
14-
import org.apache.http.impl.client.CloseableHttpClient;
157
import org.baeldung.client.spring.ClientConfig;
168
import org.baeldung.web.dto.Foo;
17-
import org.junit.Ignore;
189
import org.junit.Test;
1910
import org.junit.runner.RunWith;
2011
import org.springframework.beans.factory.annotation.Autowired;
2112
import org.springframework.http.HttpMethod;
2213
import org.springframework.http.ResponseEntity;
23-
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
2414
import org.springframework.test.context.ContextConfiguration;
2515
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2616
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@@ -54,24 +44,4 @@ public final void whenHttpsUrlIsConsumed_thenException() {
5444
assertThat(response.getStatusCode().value(), equalTo(200));
5545
}
5646

57-
@Test
58-
@Ignore("Only to run against a Server with HTTPS enabled (on 8443)")
59-
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
60-
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
61-
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
62-
63-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
64-
@Override
65-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
66-
return true;
67-
}
68-
};
69-
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, 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 = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
74-
assertThat(response.getStatusCode().value(), equalTo(200));
75-
}
76-
7747
}

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

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

3+
import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
34
import static org.hamcrest.Matchers.equalTo;
45
import static org.junit.Assert.assertThat;
56

67
import java.io.IOException;
78
import java.security.GeneralSecurityException;
89
import java.security.cert.X509Certificate;
9-
import java.util.Timer;
10-
import java.util.TimerTask;
1110

1211
import javax.net.ssl.SSLPeerUnverifiedException;
1312

1413
import org.apache.http.HttpResponse;
1514
import org.apache.http.client.ClientProtocolException;
16-
import org.apache.http.client.config.RequestConfig;
1715
import org.apache.http.client.methods.HttpGet;
1816
import org.apache.http.conn.ClientConnectionManager;
1917
import org.apache.http.conn.scheme.Scheme;
@@ -30,6 +28,10 @@
3028
import org.apache.http.impl.conn.PoolingClientConnectionManager;
3129
import org.junit.Ignore;
3230
import org.junit.Test;
31+
import org.springframework.http.HttpMethod;
32+
import org.springframework.http.ResponseEntity;
33+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
34+
import org.springframework.web.client.RestTemplate;
3335

3436
/**
3537
* This test requires a localhost server over HTTPS <br>
@@ -39,30 +41,6 @@ public class HttpsClientLiveManualTest {
3941

4042
// tests
4143

42-
@Test
43-
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
44-
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
45-
46-
final int timeout = 30; // seconds
47-
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
48-
final HttpGet getMethod = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/bars/1");
49-
getMethod.setConfig(requestConfig);
50-
51-
final int hardTimeout = 10; // seconds
52-
final TimerTask task = new TimerTask() {
53-
@Override
54-
public void run() {
55-
if (getMethod != null) {
56-
getMethod.abort();
57-
}
58-
}
59-
};
60-
new Timer(true).schedule(task, hardTimeout * 1000);
61-
62-
final HttpResponse response = httpClient.execute(getMethod);
63-
System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode());
64-
}
65-
6644
@Test(expected = SSLPeerUnverifiedException.class)
6745
@Ignore("Only for a server that has HTTPS enabled (on 8443)")
6846
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
@@ -98,6 +76,25 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
9876
httpClient.close();
9977
}
10078

79+
@Test
80+
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
81+
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
82+
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
83+
84+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
85+
@Override
86+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
87+
return true;
88+
}
89+
};
90+
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
91+
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
92+
93+
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
94+
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
95+
assertThat(response.getStatusCode().value(), equalTo(200));
96+
}
97+
10198
@Test
10299
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
103100
final SSLContextBuilder builder = new SSLContextBuilder();

0 commit comments

Comments
 (0)