-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for subscription confirmation (#191)
* Add tests for subscription confirmation
- Loading branch information
Showing
4 changed files
with
159 additions
and
6 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
45 changes: 45 additions & 0 deletions
45
CryptoExchange.Net.UnitTests/TestImplementations/Sockets/TestChannelQuery.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,45 @@ | ||
using CryptoExchange.Net.Objects; | ||
using CryptoExchange.Net.Objects.Sockets; | ||
using CryptoExchange.Net.Sockets; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets | ||
{ | ||
internal class SubResponse | ||
{ | ||
[JsonProperty("channel")] | ||
public string Channel { get; set; } = null!; | ||
|
||
[JsonProperty("status")] | ||
public string Status { get; set; } = null!; | ||
} | ||
|
||
internal class UnsubResponse | ||
{ | ||
[JsonProperty("status")] | ||
public string Status { get; set; } = null!; | ||
} | ||
|
||
internal class TestChannelQuery : Query<SubResponse> | ||
{ | ||
public override HashSet<string> ListenerIdentifiers { get; set; } | ||
|
||
public TestChannelQuery(string channel, string request, bool authenticated, int weight = 1) : base(request, authenticated, weight) | ||
{ | ||
ListenerIdentifiers = new HashSet<string> { channel }; | ||
} | ||
|
||
public override Task<CallResult<SubResponse>> HandleMessageAsync(SocketConnection connection, DataEvent<SubResponse> message) | ||
{ | ||
if (!message.Data.Status.Equals("confirmed", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return Task.FromResult(new CallResult<SubResponse>(new ServerError(message.Data.Status))); | ||
} | ||
|
||
return base.HandleMessageAsync(connection, message); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...toExchange.Net.UnitTests/TestImplementations/Sockets/TestSubscriptionWithResponseCheck.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,38 @@ | ||
using CryptoExchange.Net.Objects; | ||
using CryptoExchange.Net.Objects.Sockets; | ||
using CryptoExchange.Net.Sockets; | ||
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets | ||
{ | ||
internal class TestSubscriptionWithResponseCheck<T> : Subscription<SubResponse, UnsubResponse> | ||
{ | ||
private readonly Action<DataEvent<T>> _handler; | ||
private readonly string _channel; | ||
|
||
public override HashSet<string> ListenerIdentifiers { get; set; } | ||
|
||
public TestSubscriptionWithResponseCheck(string channel, Action<DataEvent<T>> handler) : base(Mock.Of<ILogger>(), false) | ||
{ | ||
ListenerIdentifiers = new HashSet<string>() { channel }; | ||
_handler = handler; | ||
_channel = channel; | ||
} | ||
|
||
public override Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<object> message) | ||
{ | ||
var data = (T)message.Data; | ||
_handler.Invoke(message.As(data)); | ||
return Task.FromResult(new CallResult(null)); | ||
} | ||
|
||
public override Type GetMessageType(IMessageAccessor message) => typeof(T); | ||
public override Query GetSubQuery(SocketConnection connection) => new TestChannelQuery(_channel, "subscribe", false, 1); | ||
public override Query GetUnsubQuery() => new TestChannelQuery(_channel, "unsubscribe", false, 1); | ||
} | ||
} |
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