mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-14 01:42:55 +00:00
Unit tests updated
This commit is contained in:
@@ -22,9 +22,10 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
|
||||
public TestRestClient(ClientOptions exchangeOptions) : base(exchangeOptions, exchangeOptions.ApiCredentials == null ? null : new TestAuthProvider(exchangeOptions.ApiCredentials))
|
||||
{
|
||||
RequestFactory = new Mock<IRequestFactory>().Object;
|
||||
}
|
||||
|
||||
public void SetResponse(string responseData)
|
||||
public void SetResponse(string responseData, Stream requestStream = null)
|
||||
{
|
||||
var expectedBytes = Encoding.UTF8.GetBytes(responseData);
|
||||
var responseStream = new MemoryStream();
|
||||
@@ -37,6 +38,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
var request = new Mock<IRequest>();
|
||||
request.Setup(c => c.Headers).Returns(new WebHeaderCollection());
|
||||
request.Setup(c => c.Uri).Returns(new Uri("http://www.test.com"));
|
||||
request.Setup(c => c.GetRequestStream()).Returns(Task.FromResult(requestStream));
|
||||
request.Setup(c => c.GetResponse()).Returns(Task.FromResult(response.Object));
|
||||
|
||||
var factory = Mock.Get(RequestFactory);
|
||||
@@ -84,9 +86,9 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
.Returns(request.Object);
|
||||
}
|
||||
|
||||
public async Task<CallResult<T>> Request<T>() where T:class
|
||||
public async Task<CallResult<T>> Request<T>(string method = "GET") where T:class
|
||||
{
|
||||
return await ExecuteRequest<T>(new Uri("http://www.test.com"));
|
||||
return await ExecuteRequest<T>(new Uri("http://www.test.com"), method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Authentication;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
using WebSocket4Net;
|
||||
|
||||
namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
{
|
||||
public class TestSocket: IWebsocket
|
||||
{
|
||||
public bool CanConnect { get; set; }
|
||||
public bool Connected { get; set; }
|
||||
|
||||
public event Action OnClose;
|
||||
public event Action<string> OnMessage;
|
||||
public event Action<Exception> OnError;
|
||||
public event Action OnOpen;
|
||||
|
||||
public int Id { get; }
|
||||
public bool ShouldReconnect { get; set; }
|
||||
public Func<byte[], string> DataInterpreter { get; set; }
|
||||
public DateTime? DisconnectTime { get; set; }
|
||||
public string Url { get; }
|
||||
public WebSocketState SocketState { get; }
|
||||
public bool IsClosed => !Connected;
|
||||
public bool IsOpen => Connected;
|
||||
public bool PingConnection { get; set; }
|
||||
public TimeSpan PingInterval { get; set; }
|
||||
public SslProtocols SSLProtocols { get; set; }
|
||||
|
||||
public Task<bool> Connect()
|
||||
{
|
||||
Connected = CanConnect;
|
||||
return Task.FromResult(CanConnect);
|
||||
}
|
||||
|
||||
public void Send(string data)
|
||||
{
|
||||
if(!Connected)
|
||||
throw new Exception("Socket not connected");
|
||||
}
|
||||
|
||||
public Task Close()
|
||||
{
|
||||
Connected = false;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public void SetProxy(string host, int port)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public void InvokeClose()
|
||||
{
|
||||
Connected = false;
|
||||
OnClose?.Invoke();
|
||||
}
|
||||
|
||||
public void InvokeOpen()
|
||||
{
|
||||
OnOpen?.Invoke();
|
||||
}
|
||||
|
||||
public void InvokeMessage(string data)
|
||||
{
|
||||
OnMessage?.Invoke(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
using CryptoExchange.Net.Logging;
|
||||
using CryptoExchange.Net.Objects;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
using Moq;
|
||||
|
||||
namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
{
|
||||
public class TestSocketClient: SocketClient
|
||||
{
|
||||
public Action OnReconnect { get; set; }
|
||||
|
||||
public TestSocketClient() : this(new SocketClientOptions())
|
||||
{
|
||||
}
|
||||
|
||||
public TestSocketClient(SocketClientOptions exchangeOptions) : base(exchangeOptions, exchangeOptions.ApiCredentials == null ? null : new TestAuthProvider(exchangeOptions.ApiCredentials))
|
||||
{
|
||||
SocketFactory = new Mock<IWebsocketFactory>().Object;
|
||||
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<string>())).Returns(new TestSocket());
|
||||
}
|
||||
|
||||
public TestSocket CreateSocket()
|
||||
{
|
||||
return (TestSocket)CreateSocket(BaseAddress);
|
||||
}
|
||||
|
||||
public CallResult<bool> ConnectSocketSub(SocketSubscription sub)
|
||||
{
|
||||
return ConnectSocket(sub).Result;
|
||||
}
|
||||
|
||||
protected override bool SocketReconnect(SocketSubscription subscription, TimeSpan disconnectedTime)
|
||||
{
|
||||
OnReconnect?.Invoke();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user