Skip to content

Commit 0b198c0

Browse files
author
Eugen
committed
Merge pull request eugenp#166 from Doha2012/master
add httpClient 4.4 test
2 parents 68f567c + 009b057 commit 0b198c0

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
import static org.hamcrest.Matchers.equalTo;
55
import static org.junit.Assert.assertThat;
66

7+
import java.io.IOException;
78
import java.security.GeneralSecurityException;
89
import java.security.cert.X509Certificate;
910

11+
import org.apache.http.HttpResponse;
12+
import org.apache.http.client.ClientProtocolException;
13+
import org.apache.http.client.methods.HttpGet;
1014
import org.apache.http.conn.scheme.Scheme;
15+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
1116
import org.apache.http.conn.ssl.SSLSocketFactory;
1217
import org.apache.http.conn.ssl.TrustStrategy;
1318
import org.apache.http.impl.client.CloseableHttpClient;
19+
import org.apache.http.impl.client.HttpClients;
1420
import org.junit.Test;
1521
import org.springframework.http.HttpMethod;
1622
import org.springframework.http.ResponseEntity;
@@ -23,6 +29,9 @@
2329
* */
2430
public class RestClientLiveManualTest {
2531

32+
final String urlOverHttps = "http://localhost:8080/spring-security-rest-basic-auth/api/bars/1";
33+
34+
2635
// tests
2736

2837
@Test
@@ -39,9 +48,28 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
3948
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
4049
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
4150

42-
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
51+
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
52+
assertThat(response.getStatusCode().value(), equalTo(200));
53+
}
54+
55+
@Test
56+
public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException {
57+
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
58+
59+
final HttpGet getMethod = new HttpGet(urlOverHttps);
60+
final HttpResponse response = httpClient.execute(getMethod);
61+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
62+
}
63+
64+
@Test
65+
public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException {
66+
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
67+
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
68+
requestFactory.setHttpClient(httpClient);
69+
4370
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
4471
assertThat(response.getStatusCode().value(), equalTo(200));
4572
}
4673

4774
}
75+

0 commit comments

Comments
 (0)