View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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          // Mock dependencies
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); // Invalid port
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          // Mock dependencies
84          final HttpConnectionFactory<ManagedHttpClientConnection> connFactory = mock(HttpConnectionFactory.class);
85  
86          final ManagedHttpClientConnection managedConnection = mock(ManagedHttpClientConnection.class);
87          when(managedConnection.isOpen()).thenReturn(true); // Always return true for isOpen()
88          when(connFactory.createConnection(null)).thenReturn(managedConnection);
89  
90          final ClassicHttpResponse mockResponse = mock(ClassicHttpResponse.class);
91          when(mockResponse.getCode()).thenReturn(200); // Successful response
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); // Valid port
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 }