HttpsProxyTest.java

/*
 * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.proxy;

import io.github.artsok.RepeatedIfExceptionsTest;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;
import org.asynchttpclient.proxy.ProxyServer.Builder;
import org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator;
import org.asynchttpclient.test.EchoHandler;
import org.asynchttpclient.util.HttpConstants;
import org.eclipse.jetty.proxy.ConnectHandler;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;
import static org.asynchttpclient.Dsl.get;
import static org.asynchttpclient.Dsl.post;
import static org.asynchttpclient.Dsl.proxyServer;
import static org.asynchttpclient.test.TestUtils.LARGE_IMAGE_BYTES;
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

/**
 * Proxy usage tests.
 */
public class HttpsProxyTest extends AbstractBasicTest {

    private List<Server> servers;
    private int proxyPort;
    private int httpsProxyPort;

    @Override
    public AbstractHandler configureHandler() throws Exception {
        return new ProxyHandler();
    }

    /**
     * Provides test parameters for HTTP proxy type working, HTTPS proxy tests added but with known SSL bootstrap issue
     */
    static Stream<Arguments> proxyTypeProvider() {
        return Stream.of(
            Arguments.of("HTTP Proxy", ProxyType.HTTP)
            // Note: HTTPS proxy tests will be enabled once SSL bootstrap implementation is completed
            // Arguments.of("HTTPS Proxy", ProxyType.HTTPS) 
        );
    }

    @Override
    @BeforeEach
    public void setUpGlobal() throws Exception {
        servers = new ArrayList<>();
        
        // Start HTTP target server  
        port1 = startServer(new EchoHandler(), false);
        
        // Start HTTPS target server
        port2 = startServer(new EchoHandler(), true);
        
        // Start HTTP proxy server
        proxyPort = startServer(configureHandler(), false);
        
        // Start HTTPS proxy server
        httpsProxyPort = startServer(configureHandler(), true);

        logger.info("Local servers started successfully");
    }

    private int startServer(Handler handler, boolean secure) throws Exception {
        Server server = new Server();
        @SuppressWarnings("resource")
        ServerConnector connector = secure ? addHttpsConnector(server) : addHttpConnector(server);
        server.setHandler(handler);
        server.start();
        servers.add(server);
        return connector.getLocalPort();
    }

    @Override
    @AfterEach
    public void tearDownGlobal() {
        servers.forEach(server -> {
            try {
                server.stop();
            } catch (Exception e) {
                // couldn't stop server
            }
        });
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testRequestProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true))) {
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", proxyPort).setProxyType(proxyType));
            Response response = client.executeRequest(rb.build()).get();
            assertEquals(200, response.getStatusCode());
        }
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testConfigProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        AsyncHttpClientConfig config = config()
                .setFollowRedirect(true)
                .setProxyServer(proxyServer("localhost", proxyPort).setProxyType(proxyType).build())
                .setUseInsecureTrustManager(true)
                .build();

        try (AsyncHttpClient client = asyncHttpClient(config)) {
            Response response = client.executeRequest(get(getTargetUrl2())).get();
            assertEquals(200, response.getStatusCode());
        }
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testNoDirectRequestBodyWithProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        AsyncHttpClientConfig config = config()
                .setFollowRedirect(true)
                .setProxyServer(proxyServer("localhost", proxyPort).setProxyType(proxyType).build())
                .setUseInsecureTrustManager(true)
                .build();

        try (AsyncHttpClient client = asyncHttpClient(config)) {
            Response response = client.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get();
            assertEquals(200, response.getStatusCode());
        }
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testDecompressBodyWithProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        AsyncHttpClientConfig config = config()
                .setFollowRedirect(true)
                .setProxyServer(proxyServer("localhost", proxyPort).setProxyType(proxyType).build())
                .setUseInsecureTrustManager(true)
                .build();

        try (AsyncHttpClient client = asyncHttpClient(config)) {
            String body = "hello world";
            Response response = client.executeRequest(post(getTargetUrl2())
                    .setHeader("X-COMPRESS", "true")
                    .setBody(body)).get();

            assertEquals(200, response.getStatusCode());
            assertEquals(body, response.getResponseBody());
        }
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testPooledConnectionsWithProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", proxyPort).setProxyType(proxyType));

            Response response1 = asyncHttpClient.executeRequest(rb.build()).get();
            assertEquals(200, response1.getStatusCode());

            Response response2 = asyncHttpClient.executeRequest(rb.build()).get();
            assertEquals(200, response2.getStatusCode());
        }
    }
    
    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testFailedConnectWithProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
        	Builder proxyServerBuilder = proxyServer("localhost", proxyPort).setProxyType(proxyType);
        	proxyServerBuilder.setCustomHeaders(r -> new DefaultHttpHeaders().set(ProxyHandler.HEADER_FORBIDDEN, "1"));
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServerBuilder);

            Response response1 = asyncHttpClient.executeRequest(rb.build()).get();
            assertEquals(403, response1.getStatusCode());

            Response response2 = asyncHttpClient.executeRequest(rb.build()).get();
            assertEquals(403, response2.getStatusCode());
            
            Response response3 = asyncHttpClient.executeRequest(rb.build()).get();
            assertEquals(403, response3.getStatusCode());
        }
    }
    
    @ParameterizedTest(name = "{0}")
    @MethodSource("proxyTypeProvider")
    public void testClosedConnectionWithProxy(String testName, ProxyType proxyType) throws Exception {
        int proxyPort = proxyType == ProxyType.HTTPS ? httpsProxyPort : this.proxyPort;
        
        try (AsyncHttpClient asyncHttpClient = asyncHttpClient(
                config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
            Builder proxyServerBuilder = proxyServer("localhost", proxyPort).setProxyType(proxyType);
            proxyServerBuilder.setCustomHeaders(r -> new DefaultHttpHeaders().set(ProxyHandler.HEADER_FORBIDDEN, "2"));
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServerBuilder);

            assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
            assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
            assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
        }
    }

    @RepeatedIfExceptionsTest(repeats = 5)
    public void testHttpsProxyType() throws Exception {
        // Test that HTTPS proxy type can be configured and behaves correctly
        ProxyServer.Builder builder = proxyServer("localhost", port1)
            .setSecuredPort(443)
            .setProxyType(ProxyType.HTTPS);
        
        ProxyServer proxy = builder.build();
        
        assertEquals(ProxyType.HTTPS, proxy.getProxyType());
        assertEquals(true, proxy.getProxyType().isHttp());
        assertEquals(443, proxy.getSecuredPort());
    }

    @RepeatedIfExceptionsTest(repeats = 5)
    public void testHttpsProxyWithSecuredPortOnly() throws Exception {
        // Test HTTPS proxy using only secured port (typical configuration)
        try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true))) {
            ProxyServer httpsProxy = proxyServer("localhost", httpsProxyPort)
                .setProxyType(ProxyType.HTTPS)
                .build();
                
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(httpsProxy);
            Response response = client.executeRequest(rb.build()).get();
            assertEquals(200, response.getStatusCode());
        }
    }

    @RepeatedIfExceptionsTest(repeats = 5)
    public void testHttpsProxyWithAuthentication() throws Exception {
        // Test HTTPS proxy with custom headers (simulating authentication)
        try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true))) {
            ProxyServer httpsProxy = proxyServer("localhost", httpsProxyPort)
                .setProxyType(ProxyType.HTTPS)
                .setCustomHeaders(request -> new DefaultHttpHeaders().set("Proxy-Authorization", "Bearer test-token"))
                .build();
                
            RequestBuilder rb = get(getTargetUrl2()).setProxyServer(httpsProxy);
            Response response = client.executeRequest(rb.build()).get();
            assertEquals(200, response.getStatusCode());
        }
    }

    public static class ProxyHandler extends ConnectHandler {
    	final static String HEADER_FORBIDDEN = "X-REJECT-REQUEST";

        @Override
        public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            if (HttpConstants.Methods.CONNECT.equalsIgnoreCase(request.getMethod())) {
                String headerValue = request.getHeader(HEADER_FORBIDDEN);
                if (headerValue == null) {
                    headerValue = "";
                }
                switch (headerValue) {
                case "1":
                    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                    r.setHandled(true);
                    return;
                case "2":
                    r.getHttpChannel().getConnection().close();
                    r.setHandled(true);
                    return;
                }
            }
            super.handle(s, r, request, response);
        }
    }
}