mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-28 05:39:07 +00:00
Use try-with-resources to try and avoid any leaks.
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1783811 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
97f1e35a00
commit
a9df4780c1
@ -98,15 +98,17 @@ public void testBasicSSL() throws Exception {
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -119,15 +121,17 @@ public void testBasicDefaultHostnameVerifier() throws Exception {
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier());
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientAuthSSL() throws Exception {
|
||||
@ -140,16 +144,18 @@ public void testClientAuthSSL() throws Exception {
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=IOException.class)
|
||||
public void testClientAuthSSLFailure() throws Exception {
|
||||
@ -170,16 +176,18 @@ public void initialize(final SSLServerSocket socket) throws SSLException {
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=SSLException.class)
|
||||
public void testSSLTrustVerification() throws Exception {
|
||||
@ -195,12 +203,15 @@ public void testSSLTrustVerification() throws Exception {
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
sslSocket.close();
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSSLTrustVerificationOverrideWithCustsom() throws Exception {
|
||||
@ -242,11 +253,14 @@ private void testSSLTrustVerificationOverride(final TrustStrategy trustStrategy)
|
||||
sslcontext,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
sslSocket.close();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -267,13 +281,16 @@ public void initialize(final SSLServerSocket socket) throws SSLException {
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext());
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=IOException.class)
|
||||
public void testSSLDisabledByDefault() throws Exception {
|
||||
@ -293,9 +310,10 @@ public void initialize(final SSLServerSocket socket) throws SSLException {
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext());
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user