Skip to content

Commit

Permalink
Move integration test to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Sep 11, 2024
1 parent cd5b9bb commit b9b16a4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 93 deletions.
44 changes: 0 additions & 44 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,50 +584,6 @@ public void TestEnableLoginRetryOn404()
}
}


[Test]
public void TestNonRetryableHttpExceptionThrowsError()
{
var handler = new Mock<DelegatingHandler>();
handler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.RequestUri.ToString().Contains("https://authenticationexceptiontest.com/")),
ItExpr.IsAny<CancellationToken>())
.ThrowsAsync(new HttpRequestException("", new AuthenticationException()));

var httpClient = HttpUtil.Instance.GetHttpClient(
new HttpClientConfig(false, "fakeHost", "fakePort", "user", "password", "fakeProxyList", false, false, 7),
handler.Object);

var mockRestRequester = new MockInfiniteTimeout(httpClient);

using (var conn = new MockSnowflakeDbConnection(mockRestRequester))
{
string invalidConnectionString = "host=authenticationexceptiontest.com;"
+ "account=account;user=user;password=password;";
conn.ConnectionString = invalidConnectionString;

Assert.AreEqual(conn.State, ConnectionState.Closed);
try
{
conn.Open();
Assert.Fail();
}
catch (AggregateException e)
{
Assert.IsInstanceOf<HttpRequestException>(e.InnerException);
Assert.IsInstanceOf<AuthenticationException>(e.InnerException.InnerException);
}
catch (Exception unexpected)
{
Assert.Fail($"Unexpected {unexpected.GetType()} exception occurred");
}

Assert.AreEqual(ConnectionState.Closed, conn.State);
}
}

[Test]
public void TestValidateDefaultParameters()
{
Expand Down
49 changes: 0 additions & 49 deletions Snowflake.Data.Tests/Mock/MockInfiniteTimeout.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Snowflake.Data.Tests/UnitTests/HttpUtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,55 @@ namespace Snowflake.Data.Tests.UnitTests
using NUnit.Framework;
using Snowflake.Data.Core;
using RichardSzalay.MockHttp;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System;
using System.Security.Authentication;
using Moq;
using Moq.Protected;

[TestFixture]
class HttpUtilTest
{
[Test]
public async Task TestNonRetryableHttpExceptionThrowsError()
{
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://authenticationexceptiontest.com/"));
// Disable warning as this is the way to be compliant with netstandard2.0
// API reference: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestmessage?view=netstandard-2.0
#pragma warning disable CS0618 // Type or member is obsolete
request.Properties[BaseRestRequest.HTTP_REQUEST_TIMEOUT_KEY] = Timeout.InfiniteTimeSpan;
request.Properties[BaseRestRequest.REST_REQUEST_TIMEOUT_KEY] = Timeout.InfiniteTimeSpan;
#pragma warning restore CS0618 // Type or member is obsolete

var handler = new Mock<DelegatingHandler>();
handler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.RequestUri.ToString().Contains("https://authenticationexceptiontest.com/")),
ItExpr.IsAny<CancellationToken>())
.ThrowsAsync(new HttpRequestException("", new AuthenticationException()));

var httpClient = HttpUtil.Instance.GetHttpClient(
new HttpClientConfig(false, "fakeHost", "fakePort", "user", "password", "fakeProxyList", false, false, 7),
handler.Object);

try
{
await httpClient.SendAsync(request, CancellationToken.None).ConfigureAwait(false);
Assert.Fail();
}
catch (HttpRequestException e)
{
Assert.IsInstanceOf<AuthenticationException>(e.InnerException);
}
catch (Exception unexpected)
{
Assert.Fail($"Unexpected {unexpected.GetType()} exception occurred");
}
}

[Test]
// Parameters: status code, force retry on 404, expected retryable value
[TestCase(HttpStatusCode.OK, false, false)]
Expand Down

0 comments on commit b9b16a4

Please sign in to comment.