Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1333163 support for default proxy #997

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Snowflake.Data.Tests/SFBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2021 Snowflake Computing Inc. All rights reserved.
*/

Expand Down
81 changes: 66 additions & 15 deletions Snowflake.Data.Tests/UnitTests/HttpUtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
*/

using System.Net.Http;
using NUnit.Framework;
using Snowflake.Data.Core;
using RichardSzalay.MockHttp;
using System.Threading.Tasks;
using System.Net;
using System;

namespace Snowflake.Data.Tests.UnitTests
{
using NUnit.Framework;
using Snowflake.Data.Core;
using RichardSzalay.MockHttp;
using System.Threading.Tasks;
using System.Net;
using System;

[TestFixture]
class HttpUtilTest
{
Expand Down Expand Up @@ -102,48 +101,100 @@ public void TestGetJitter(int seconds)
}

[Test]
public void ShouldCreateHttpClientHandlerWithProxy()
public void TestCreateHttpClientHandlerWithExplicitProxy()
{
// given
var config = new HttpClientConfig(
true,
true,
"snowflake.com",
"123",
"testUser",
"proxyPassword",
"localhost",
"localhost",
false,
false,
7
);

// when
var handler = (HttpClientHandler) HttpUtil.Instance.SetupCustomHttpHandler(config);

// then
Assert.IsTrue(handler.UseProxy);
Assert.IsNotNull(handler.Proxy);
}

[Test]
public void ShouldCreateHttpClientHandlerWithoutProxy()
public void TestCreateHttpClientHandlerWithDefaultProxy()
{
// given
var config = new HttpClientConfig(
true,
true,
null,
null,
null,
null,
null,
false,
false,
7
);

// when
var handler = (HttpClientHandler) HttpUtil.Instance.SetupCustomHttpHandler(config);

// then
Assert.IsTrue(handler.UseProxy);
Assert.IsNull(handler.Proxy);
}

[Test]
public void TestCreateHttpClientHandlerWithoutProxy()
{
// given
var config = new HttpClientConfig(
false,
false,
null,
null,
null,
null,
null,
null,
false,
false,
0
);

// when
var handler = (HttpClientHandler) HttpUtil.Instance.SetupCustomHttpHandler(config);


// then
Assert.IsFalse(handler.UseProxy);
Assert.IsNull(handler.Proxy);
}

[Test]
public void TestIgnoreProxyDetailsIfProxyDisabled()
{
// given
var config = new HttpClientConfig(
true,
false,
"snowflake.com",
"123",
"testUser",
"proxyPassword",
"localhost",
false,
false,
7
);

// when
var handler = (HttpClientHandler) HttpUtil.Instance.SetupCustomHttpHandler(config);

// then
Assert.IsFalse(handler.UseProxy);
Assert.IsNull(handler.Proxy);
Expand Down
53 changes: 53 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFSessionPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Snowflake.Data.Client;
using Snowflake.Data.Core.Authenticator;
using Snowflake.Data.Core.Tools;
using Snowflake.Data.Tests.Util;

namespace Snowflake.Data.Tests.UnitTests
{
Expand Down Expand Up @@ -166,6 +167,58 @@ public void TestResolveConnectionArea(string host, string expectedMessage)
Assert.AreEqual(expectedMessage, message);
}

[Test]
public void TestFailWhenProxyConfiguredWithoutPort()
{
// arrange
var connectionString = "ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;proxyHost=localhost";

// act
var thrown = Assert.Throws<SnowflakeDbException>(() => SFSessionProperties.ParseConnectionString(connectionString, null));

// assert
SnowflakeDbExceptionAssert.HasErrorCode(thrown, SFError.MISSING_CONNECTION_PROPERTY);
Assert.That(thrown.Message, Does.Contain("Required property PROXYPORT is not provided"));
}

[Test]
public void TestFailWhenProxyUserProvidedWithoutProxyPassword()
{
// arrange
var connectionString = "ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;proxyHost=localhost;proxyPort=1234;proxyUser=testUser";

// act
var thrown = Assert.Throws<SnowflakeDbException>(() => SFSessionProperties.ParseConnectionString(connectionString, null));

// assert
SnowflakeDbExceptionAssert.HasErrorCode(thrown, SFError.MISSING_CONNECTION_PROPERTY);
Assert.That(thrown.Message, Does.Contain("Required property PROXYPASSWORD is not provided"));
}

[Test]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;proxyPort=1234;proxyPassword=xyz", SFSessionProperty.PROXYPORT)]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;proxyUser=testUser;proxyPassword=xyz", SFSessionProperty.PROXYUSER)]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;proxyPassword=xyz", SFSessionProperty.PROXYPASSWORD)]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;useProxy=true;nonProxyHosts=xyz", SFSessionProperty.NONPROXYHOSTS)]
public void TestFailWhenConfiguringProxyDetailsWithoutProxyHost(string connectionString, SFSessionProperty unwantedProperty)
{
// act
var thrown = Assert.Throws<SnowflakeDbException>(() => SFSessionProperties.ParseConnectionString(connectionString, null));

// assert
SnowflakeDbExceptionAssert.HasErrorCode(thrown, SFError.INVALID_CONNECTION_STRING);
Assert.That(thrown.Message, Does.Contain($"Proxy property {unwantedProperty.ToString()} provided while PROXYHOST is missing"));
}

[Test]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;proxyHost=localhost")]
[TestCase("ACCOUNT=account;USER=test;PASSWORD=test;useProxy=false;proxyHost=localhost;proxyPort=1234;proxyUser=testUser")]
public void TestProxyValidationsOnlyWhenProxyEnabledAndProxyHostConfigured(string connectionString)
{
// act
Assert.DoesNotThrow(() => SFSessionProperties.ParseConnectionString(connectionString, null));
}

public static IEnumerable<TestCase> ConnectionStringTestCases()
{
string defAccount = "testaccount";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Snowflake.Data.Tests.UnitTests.Session
public class SFHttpClientProxyPropertiesTest
{
[Test, TestCaseSource(nameof(ProxyPropertiesProvider))]
public void ShouldExtractProxyProperties(ProxyPropertiesTestCase testCase)
public void TestExtractProxyProperties(ProxyPropertiesTestCase testCase)
{
// given
var extractor = new SFSessionHttpClientProxyProperties.Extractor();
Expand All @@ -23,6 +23,7 @@ public void ShouldExtractProxyProperties(ProxyPropertiesTestCase testCase)
var proxyProperties = extractor.ExtractProperties(properties);

// then
Assert.AreEqual(testCase.expectedProperties.useProxy, proxyProperties.useProxy);
Assert.AreEqual(testCase.expectedProperties.proxyHost, proxyProperties.proxyHost);
Assert.AreEqual(testCase.expectedProperties.proxyPort, proxyProperties.proxyPort);
Assert.AreEqual(testCase.expectedProperties.nonProxyHosts, proxyProperties.nonProxyHosts);
Expand All @@ -37,6 +38,7 @@ public static IEnumerable<ProxyPropertiesTestCase> ProxyPropertiesProvider()
conectionString = "account=test;user=test;password=test",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = false,
proxyHost = null,
proxyPort = null,
nonProxyHosts = null,
Expand All @@ -49,6 +51,20 @@ public static IEnumerable<ProxyPropertiesTestCase> ProxyPropertiesProvider()
conectionString = "account=test;user=test;password=test;useProxy=false;proxyHost=snowflake.com;proxyPort=123;nonProxyHosts=localhost;proxyPassword=proxyPassword;proxyUser=Chris",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = false,
proxyHost = null,
proxyPort = null,
nonProxyHosts = null,
proxyPassword = null,
proxyUser = null
}
};
var proxyPropertiesConfiguredButDisabledCase2 = new ProxyPropertiesTestCase()
{
conectionString = "account=test;user=test;password=test;proxyHost=snowflake.com;proxyPort=123;nonProxyHosts=localhost;proxyPassword=proxyPassword;proxyUser=Chris",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = false,
proxyHost = null,
proxyPort = null,
nonProxyHosts = null,
Expand All @@ -58,11 +74,12 @@ public static IEnumerable<ProxyPropertiesTestCase> ProxyPropertiesProvider()
};
var proxyPropertiesConfiguredAndEnabledCase = new ProxyPropertiesTestCase()
{
conectionString = "account=test;user=test;password=test;useProxy=true;proxyHost=snowflake.com",
conectionString = "account=test;user=test;password=test;useProxy=true;proxyHost=snowflake.com;proxyPort=1234",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = true,
proxyHost = "snowflake.com",
proxyPort = null,
proxyPort = "1234",
nonProxyHosts = null,
proxyPassword = null,
proxyUser = null
Expand All @@ -74,19 +91,35 @@ public static IEnumerable<ProxyPropertiesTestCase> ProxyPropertiesProvider()
"account=test;user=test;password=test;useProxy=true;proxyHost=snowflake.com;proxyPort=123;nonProxyHosts=localhost;proxyPassword=proxyPassword;proxyUser=Chris",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = true,
proxyHost = "snowflake.com",
proxyPort = "123",
nonProxyHosts = "localhost",
proxyPassword = "proxyPassword",
proxyUser = "Chris"
}
};
var defaultProxyEnabled = new ProxyPropertiesTestCase()
{
conectionString = "account=test;user=test;password=test;useProxy=true;",
expectedProperties = new SFSessionHttpClientProxyProperties()
{
useProxy = true,
proxyHost = null,
proxyPort = null,
nonProxyHosts = null,
proxyPassword = null,
proxyUser = null
}
};
return new []
{
noProxyPropertiesCase,
proxyPropertiesConfiguredButDisabledCase,
proxyPropertiesConfiguredButDisabledCase2,
proxyPropertiesConfiguredAndEnabledCase,
proxyPropertiesAllConfiguredAndEnabled
proxyPropertiesAllConfiguredAndEnabled,
defaultProxyEnabled
};
}

Expand Down
Loading
Loading