1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.classic;
29
30 import static org.junit.jupiter.api.Assertions.assertEquals;
31 import static org.junit.jupiter.api.Assertions.assertNotNull;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.fail;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37
38 import java.io.IOException;
39 import java.net.Socket;
40
41 import org.apache.hc.client5.http.auth.Credentials;
42 import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
43 import org.apache.hc.client5.http.config.RequestConfig;
44 import org.apache.hc.client5.http.io.ManagedHttpClientConnection;
45 import org.apache.hc.core5.http.ClassicHttpResponse;
46 import org.apache.hc.core5.http.HttpException;
47 import org.apache.hc.core5.http.HttpHost;
48 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
49 import org.apache.hc.core5.http.io.HttpConnectionFactory;
50 import org.junit.jupiter.api.Test;
51
52 class TestProxyClient {
53
54 @Test
55 void testTunnelWithInvalidPort() throws IOException {
56
57 final HttpConnectionFactory<ManagedHttpClientConnection> connFactory = mock(HttpConnectionFactory.class);
58 final ManagedHttpClientConnection managedConnection = mock(ManagedHttpClientConnection.class);
59 when(connFactory.createConnection(null)).thenReturn(managedConnection);
60
61 final HttpRequestExecutor requestExecutor = mock(HttpRequestExecutor.class);
62 final ClassicHttpResponse response = mock(ClassicHttpResponse.class);
63 when(response.getCode()).thenReturn(200);
64 try {
65 when(requestExecutor.execute(any(), any(), any())).thenReturn(response);
66 } catch (final IOException | HttpException e) {
67 fail("Shouldn't fail");
68 }
69
70 final RequestConfig requestConfig = RequestConfig.DEFAULT;
71
72 final ProxyClient client = new ProxyClient(connFactory, null, null, requestConfig);
73
74 final HttpHost proxy = new HttpHost("proxy.example.com", 8080);
75 final HttpHost target = new HttpHost("target.example.com", -1);
76 final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());
77
78 assertThrows(IllegalArgumentException.class, () -> client.tunnel(proxy, target, credentials));
79 }
80
81 @Test
82 void testSuccessfulTunnel() throws IOException, HttpException {
83
84 final HttpConnectionFactory<ManagedHttpClientConnection> connFactory = mock(HttpConnectionFactory.class);
85
86 final ManagedHttpClientConnection managedConnection = mock(ManagedHttpClientConnection.class);
87 when(managedConnection.isOpen()).thenReturn(true);
88 when(connFactory.createConnection(null)).thenReturn(managedConnection);
89
90 final ClassicHttpResponse mockResponse = mock(ClassicHttpResponse.class);
91 when(mockResponse.getCode()).thenReturn(200);
92 when(managedConnection.receiveResponseHeader()).thenReturn(mockResponse);
93
94 final HttpRequestExecutor mockRequestExecutor = mock(HttpRequestExecutor.class);
95 when(mockRequestExecutor.execute(any(), any(), any())).thenReturn(mockResponse);
96
97 final Socket mockSocket = mock(Socket.class);
98 when(managedConnection.getSocket()).thenReturn(mockSocket);
99
100 final RequestConfig requestConfig = RequestConfig.DEFAULT;
101
102 final ProxyClient client = new ProxyClient(connFactory, null, null, requestConfig);
103
104 final HttpHost proxy = new HttpHost("proxy.example.com", 8080);
105 final HttpHost target = new HttpHost("target.example.com", 80);
106 final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());
107
108 final Socket resultSocket = client.tunnel(proxy, target, credentials);
109 assertNotNull(resultSocket, "Expected a valid socket object");
110 assertEquals(mockSocket, resultSocket, "Expected the mock socket to be returned");
111 }
112
113 }