-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from snowflakedb/mock-session-renew
Add mock tests to test session renew
- Loading branch information
Showing
8 changed files
with
319 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Net.Http; | ||
|
||
namespace Snowflake.Data.Tests.Mock | ||
{ | ||
using Snowflake.Data.Core; | ||
|
||
class MockRestSessionExpired : IRestRequest | ||
{ | ||
static private readonly String EXPIRED_SESSION_TOKEN="session_expired_token"; | ||
|
||
static private readonly String TOKEN_FMT = "Snowflake Token=\"{0}\""; | ||
|
||
static private readonly int SESSION_EXPIRED_CODE = 390112; | ||
|
||
public MockRestSessionExpired() { } | ||
|
||
public Task<T> PostAsync<T>(SFRestRequest postRequest, CancellationToken cancellationToken) | ||
{ | ||
if (postRequest.jsonBody is AuthnRequest) | ||
{ | ||
AuthnResponse authnResponse = new AuthnResponse | ||
{ | ||
data = new AuthnResponseData() | ||
{ | ||
token = EXPIRED_SESSION_TOKEN, | ||
masterToken = "master_token", | ||
authResponseSessionInfo = new SessionInfo(), | ||
nameValueParameter = new List<NameValueParameter>() | ||
}, | ||
success = true | ||
}; | ||
|
||
// login request return success | ||
return Task.FromResult<T>((T)(object)authnResponse); | ||
} | ||
else if (postRequest.jsonBody is QueryRequest) | ||
{ | ||
if (postRequest.authorizationToken.Equals(String.Format(TOKEN_FMT, EXPIRED_SESSION_TOKEN))) | ||
{ | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = SESSION_EXPIRED_CODE | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
else if (postRequest.authorizationToken.Equals(String.Format(TOKEN_FMT, "new_session_token"))) | ||
{ | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = true, | ||
data = new QueryExecResponseData | ||
{ | ||
rowSet = new string[,] { { "1" } }, | ||
rowType = new List<ExecResponseRowType>() | ||
{ | ||
new ExecResponseRowType | ||
{ | ||
name = "colone", | ||
type = "FIXED" | ||
} | ||
}, | ||
parameters = new List<NameValueParameter>() | ||
} | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
else | ||
{ | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = 1 | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
} | ||
else if (postRequest.jsonBody is RenewSessionRequest) | ||
{ | ||
return Task.FromResult<T>((T)(object)new RenewSessionResponse | ||
{ | ||
success = true, | ||
data = new RenewSessionResponseData() | ||
{ | ||
sessionToken = "new_session_token" | ||
} | ||
}); | ||
} | ||
else | ||
{ | ||
return Task.FromResult<T>((T)(object)null); | ||
} | ||
} | ||
|
||
public T Post<T>(SFRestRequest postRequest) | ||
{ | ||
return Task.Run(async () => await PostAsync<T>(postRequest, CancellationToken.None)).Result; | ||
} | ||
|
||
public T Get<T>(SFRestRequest request) | ||
{ | ||
return Task.Run(async () => await GetAsync<T>(request, CancellationToken.None)).Result; | ||
} | ||
|
||
public Task<T> GetAsync<T>(SFRestRequest request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<T>((T)(object)null); | ||
} | ||
|
||
public Task<HttpResponseMessage> GetAsync(S3DownloadRequest request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<HttpResponseMessage>(null); | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
Snowflake.Data.Tests/Mock/MockRestSessionExpiredInQueryExec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
|
||
namespace Snowflake.Data.Tests.Mock | ||
{ | ||
using Snowflake.Data.Core; | ||
|
||
class MockRestSessionExpiredInQueryExec : IRestRequest | ||
{ | ||
static private readonly int QUERY_IN_EXEC_CODE = 333333; | ||
|
||
static private readonly int SESSION_EXPIRED_CODE = 390112; | ||
|
||
private int getResultCallCount = 0; | ||
|
||
public MockRestSessionExpiredInQueryExec() { } | ||
|
||
public Task<T> PostAsync<T>(SFRestRequest postRequest, CancellationToken cancellationToken) | ||
{ | ||
if (postRequest.jsonBody is AuthnRequest) | ||
{ | ||
AuthnResponse authnResponse = new AuthnResponse | ||
{ | ||
data = new AuthnResponseData() | ||
{ | ||
token = "session_token", | ||
masterToken = "master_token", | ||
authResponseSessionInfo = new SessionInfo(), | ||
nameValueParameter = new List<NameValueParameter>() | ||
}, | ||
success = true | ||
}; | ||
|
||
// login request return success | ||
return Task.FromResult<T>((T)(object)authnResponse); | ||
} | ||
else if (postRequest.jsonBody is QueryRequest) | ||
{ | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = QUERY_IN_EXEC_CODE | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
|
||
} | ||
else if (postRequest.jsonBody is RenewSessionRequest) | ||
{ | ||
return Task.FromResult<T>((T)(object)new RenewSessionResponse | ||
{ | ||
success = true, | ||
data = new RenewSessionResponseData() | ||
{ | ||
sessionToken = "new_session_token" | ||
} | ||
}); | ||
} | ||
else | ||
{ | ||
return Task.FromResult<T>((T)(object)null); | ||
} | ||
} | ||
|
||
public T Post<T>(SFRestRequest postRequest) | ||
{ | ||
return Task.Run(async () => await PostAsync<T>(postRequest, CancellationToken.None)).Result; | ||
} | ||
|
||
public T Get<T>(SFRestRequest request) | ||
{ | ||
return Task.Run(async () => await GetAsync<T>(request, CancellationToken.None)).Result; | ||
} | ||
|
||
public Task<T> GetAsync<T>(SFRestRequest request, CancellationToken cancellationToken) | ||
{ | ||
if (getResultCallCount == 0) | ||
{ | ||
getResultCallCount++; | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = QUERY_IN_EXEC_CODE | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
else if (getResultCallCount == 1) | ||
{ | ||
getResultCallCount++; | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = SESSION_EXPIRED_CODE | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
else if (getResultCallCount == 2 && | ||
request.authorizationToken.Equals("Snowflake Token=\"new_session_token\"")) | ||
{ | ||
getResultCallCount++; | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = true, | ||
data = new QueryExecResponseData | ||
{ | ||
rowSet = new string[,] { { "1" } }, | ||
rowType = new List<ExecResponseRowType>() | ||
{ | ||
new ExecResponseRowType | ||
{ | ||
name = "colone", | ||
type = "FIXED" | ||
} | ||
}, | ||
parameters = new List<NameValueParameter>() | ||
} | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
else | ||
{ | ||
QueryExecResponse queryExecResponse = new QueryExecResponse | ||
{ | ||
success = false, | ||
code = 1 | ||
}; | ||
return Task.FromResult<T>((T)(object)queryExecResponse); | ||
} | ||
} | ||
|
||
public Task<HttpResponseMessage> GetAsync(S3DownloadRequest request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<HttpResponseMessage>(null); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. | ||
*/ | ||
|
||
namespace Snowflake.Data.Tests | ||
{ | ||
using Snowflake.Data.Core; | ||
using NUnit.Framework; | ||
using System; | ||
|
||
/** | ||
* Mock rest request to test session renew | ||
*/ | ||
[TestFixture] | ||
class SFStatementTest | ||
{ | ||
[Test] | ||
public void TestSessionRenew() | ||
{ | ||
Mock.MockRestSessionExpired rest = new Mock.MockRestSessionExpired(); | ||
SFSession sfSession = new SFSession("account=test;user=test;password=test", null, rest); | ||
sfSession.Open(); | ||
SFStatement statement = new SFStatement(sfSession, rest); | ||
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false); | ||
Assert.AreEqual(true, resultSet.Next()); | ||
Assert.AreEqual("1", resultSet.GetString(0)); | ||
} | ||
|
||
[Test] | ||
public void TestSessionRenewDuringQueyrExec() | ||
{ | ||
Mock.MockRestSessionExpiredInQueryExec rest = new Mock.MockRestSessionExpiredInQueryExec(); | ||
SFSession sfSession = new SFSession("account=test;user=test;password=test", null, rest); | ||
sfSession.Open(); | ||
SFStatement statement = new SFStatement(sfSession, rest); | ||
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false); | ||
Assert.AreEqual(true, resultSet.Next()); | ||
Assert.AreEqual("1", resultSet.GetString(0)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
using System.Runtime.CompilerServices; | ||
[assembly: InternalsVisibleTo("Snowflake.Data.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters