Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ public Socket tunnel(
final int status = response.getCode();

if (status > 299) {
EntityUtils.consume(response.getEntity());
conn.close();
try {
EntityUtils.consume(response.getEntity());
} finally {
conn.close();
}
throw new HttpException("Tunnel refused: " + new StatusLine(response));
}
return conn.getSocket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.io.ManagedHttpClientConnection;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
Expand Down Expand Up @@ -110,4 +115,32 @@ void testSuccessfulTunnel() throws IOException, HttpException {
assertEquals(mockSocket, resultSocket, "Expected the mock socket to be returned");
}

}
@Test
void testTunnelClosesConnectionWhenConsumingErrorResponseFails() throws IOException, HttpException {
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory = mock(HttpConnectionFactory.class);
final ManagedHttpClientConnection managedConnection = mock(ManagedHttpClientConnection.class);
when(managedConnection.isOpen()).thenReturn(true);
when(connFactory.createConnection(null)).thenReturn(managedConnection);

final IOException consumeFailure = new IOException("Failed to consume proxy response");
final InputStream content = mock(InputStream.class);
doThrow(consumeFailure).when(content).close();
final HttpEntity entity = mock(HttpEntity.class);
when(entity.isStreaming()).thenReturn(true);
when(entity.getContent()).thenReturn(content);
final ClassicHttpResponse response = mock(ClassicHttpResponse.class);
when(response.getCode()).thenReturn(500);
when(response.getEntity()).thenReturn(entity);
when(managedConnection.receiveResponseHeader()).thenReturn(response);

final ProxyClient client = new ProxyClient(connFactory, null, null, RequestConfig.DEFAULT);
final HttpHost proxy = new HttpHost("proxy.example.com", 8080);
final HttpHost target = new HttpHost("target.example.com", 80);
final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());

assertSame(consumeFailure, assertThrows(IOException.class,
() -> client.tunnel(proxy, target, credentials)));
verify(managedConnection).close();
}

}
Loading