Skip to content

Commit

Permalink
CryptoExchange.Net testing update (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored May 1, 2024
1 parent de61d53 commit 7ceb019
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
65 changes: 43 additions & 22 deletions Mexc.Net.UnitTests/MexcClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
using Mexc.Net.Clients;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using Mexc.Net.Clients;
using Mexc.Net.Objects.Models;
using Mexc.Net.UnitTests.Helpers;
using Newtonsoft.Json;
using NUnit.Framework.Legacy;
using System.Diagnostics;
using System.Reflection;
using CryptoExchange.Net.Converters.JsonNet;

namespace Mexc.Net.UnitTests
{
[TestFixture]
internal class MexcClientTests
{
[Test]
public void CheckRestInterfaces()
{
var assembly = Assembly.GetAssembly(typeof(MexcRestClient));
var ignore = new string[] { "IMexcRestClientSpotApi" };
var clientInterfaces = assembly.GetTypes().Where(t => t.Name.StartsWith("IMexcRestClient") && !ignore.Contains(t.Name));

foreach (var clientInterface in clientInterfaces)
{
var implementation = assembly.GetTypes().Single(t => t.IsAssignableTo(clientInterface) && t != clientInterface);
int methods = 0;
foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task))))
{
var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());
ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}");
methods++;
}
Debug.WriteLine($"{clientInterface.Name} {methods} methods validated");
}
}

[TestCase()]
public async Task ReceivingHttpErrorWithNoJson_Should_ReturnErrorAndNotSuccess()
{
Expand Down Expand Up @@ -69,5 +51,44 @@ public async Task ReceivingHttpErrorWithJsonError_Should_ReturnErrorAndNotSucces
Assert.That(result.Error!.Code == 400001);
Assert.That(result.Error.Message == "Error occured");
}

[Test]
public void CheckSignatureExample()
{
var authProvider = new MexcAuthenticationProvider(
new ApiCredentials("mx0aBYs33eIilxBWC5", "45d0b3c26f2644f19bfb98b07741b2f5")
);
var client = (RestApiClient)new MexcRestClient().SpotApi;

CryptoExchange.Net.Testing.TestHelpers.CheckSignature(
client,
authProvider,
HttpMethod.Post,
"/api/v3/order",
(uriParams, bodyParams, headers) =>
{
return uriParams["signature"].ToString();
},
"fd3e4e8543c5188531eb7279d68ae7d26a573d0fc5ab0d18eb692451654d837a",
new Dictionary<string, object>
{
{ "symbol", "BTCUSDT" },
{ "side", "BUY" },
{ "type", "LIMIT" },
{ "quantity", "1" },
{ "price", "11" },
{ "recvWindow", "5000" },
},
time: DateTimeConverter.ConvertFromMilliseconds(1644489390087),
disableOrdering: true,
compareCase: false);
}

[Test]
public void CheckInterfaces()
{
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<MexcRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<MexcSocketClient>();
}
}
}
2 changes: 1 addition & 1 deletion Mexc.Net/Mexc.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.4.0" />
</ItemGroup>
</Project>
20 changes: 14 additions & 6 deletions Mexc.Net/MexcAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@ public MexcAuthenticationProvider(ApiCredentials credentials) : base(credentials
{
}

public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary<string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat, out SortedDictionary<string, object> uriParameters, out SortedDictionary<string, object> bodyParameters, out Dictionary<string, string> headers)
public override void AuthenticateRequest(
RestApiClient apiClient,
Uri uri,
HttpMethod method,
IDictionary<string, object> uriParams,
IDictionary<string, object> bodyParams,
Dictionary<string, string> headers,
bool auth,
ArrayParametersSerialization arraySerialization,
HttpMethodParameterPosition parameterPosition,
RequestBodyFormat bodyFormat)
{
uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary<string, object>(providedParameters) : new SortedDictionary<string, object>();
bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary<string, object>(providedParameters) : new SortedDictionary<string, object>();
headers = new Dictionary<string, string>() { { "X-MEXC-APIKEY", _credentials.Key!.GetString() } };
headers.Add("X-MEXC-APIKEY", _credentials.Key!.GetString());

if (!auth)
return;

var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParameters : bodyParameters;
var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParams : bodyParams;
var timestamp = GetMillisecondTimestamp(apiClient);
parameters.Add("timestamp", timestamp);

if (_credentials.CredentialType == ApiCredentialsType.Hmac)
{
uri = uri.SetParameters(uriParameters, arraySerialization);
uri = uri.SetParameters(uriParams, arraySerialization);
parameters.Add("signature", SignHMACSHA256(parameterPosition == HttpMethodParameterPosition.InUri ? uri.Query.Replace("?", "") : parameters.ToFormData()));
}
else
Expand Down

0 comments on commit 7ceb019

Please sign in to comment.