Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

package tests.integration.com.microsoft.azure.sdk.iot.helpers;

import com.github.monkeywie.proxyee.server.HttpProxyServer;

import java.util.concurrent.TimeUnit;

/**
* Helpers for the local HTTP proxy servers that the proxy related integration tests run their traffic through.
*/
public class ProxyServerTools
{
private static final int PROXY_START_TIMEOUT_SECONDS = 30;

/**
* Start the provided proxy server on the provided port and block until it is actually listening on that port.
*
* {@link HttpProxyServer#startAsync(int)} only initiates the bind, so tests that don't wait on the returned future
* can start sending traffic to the proxy before it is listening. When that happens, the client under test gets a
* "Connection refused" instead of a working proxy.
*
* @param proxyServer the proxy server to start.
* @param port the port for the proxy server to listen on.
* @throws Exception if the proxy server could not be started within {@link #PROXY_START_TIMEOUT_SECONDS} seconds.
*/
public static void startProxyServer(HttpProxyServer proxyServer, int port) throws Exception
{
proxyServer.startAsync(port).toCompletableFuture().get(PROXY_START_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.runners.Parameterized;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.FlakeyTest;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.ProxyServerTools;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.TestConstants;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity;
import tests.integration.com.microsoft.azure.sdk.iot.helpers.Tools;
Expand Down Expand Up @@ -147,12 +148,12 @@ public FileUploadState()
}

@BeforeClass
public static void startProxy()
public static void startProxy() throws Exception
{
HttpProxyServerConfig config = new HttpProxyServerConfig();
config.setHandleSsl(false);
proxyServer = new HttpProxyServer().serverConfig(config);
proxyServer.startAsync(testProxyPort);
ProxyServerTools.startProxyServer(proxyServer, testProxyPort);
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ public void tearDownTest()
}

@BeforeClass
public static void startProxy()
public static void startProxy() throws Exception
{
HttpProxyServerConfig config = new HttpProxyServerConfig();
config.setHandleSsl(false);
proxyServer = new HttpProxyServer().serverConfig(config);
proxyServer.startAsync(testProxyPort);
ProxyServerTools.startProxyServer(proxyServer, testProxyPort);
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public static void setUp()
}

@BeforeClass
public static void startProxy()
public static void startProxy() throws Exception
{
HttpProxyServerConfig config = new HttpProxyServerConfig();
config.setHandleSsl(false);
proxyServer = new HttpProxyServer().serverConfig(config);
proxyServer.startAsync(testProxyPort);
ProxyServerTools.startProxyServer(proxyServer, testProxyPort);
}

@AfterClass
Expand Down
Loading
Loading