mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2026-04-07 10:11:10 +00:00
Compare commits
6 Commits
CryptoExch
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93034e8af8 | ||
|
|
cdd0bd83ab | ||
|
|
61d371682c | ||
|
|
8aae769e54 | ||
|
|
1b4f6926df | ||
|
|
acca3468f3 |
@ -1,11 +1,15 @@
|
|||||||
using CryptoExchange.Net.Attributes;
|
using CryptoExchange.Net.Attributes;
|
||||||
|
using CryptoExchange.Net.Converters;
|
||||||
using CryptoExchange.Net.Converters.SystemTextJson;
|
using CryptoExchange.Net.Converters.SystemTextJson;
|
||||||
using System.Text.Json;
|
using CryptoExchange.Net.Objects;
|
||||||
|
using CryptoExchange.Net.SharedApis;
|
||||||
|
using CryptoExchange.Net.Testing;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using CryptoExchange.Net.Converters;
|
|
||||||
using CryptoExchange.Net.SharedApis;
|
|
||||||
|
|
||||||
namespace CryptoExchange.Net.UnitTests
|
namespace CryptoExchange.Net.UnitTests
|
||||||
{
|
{
|
||||||
@ -185,6 +189,31 @@ namespace CryptoExchange.Net.UnitTests
|
|||||||
Assert.That(result == expected);
|
Assert.That(result == expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestEnumConverterParseNullOnNonNullableOnlyLogsOnce()
|
||||||
|
{
|
||||||
|
LibraryHelpers.StaticLogger = new TraceLogger();
|
||||||
|
var listener = new EnumValueTraceListener();
|
||||||
|
Trace.Listeners.Add(listener);
|
||||||
|
EnumConverter<TestEnum>.Reset();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Assert.Throws<Exception>(() =>
|
||||||
|
{
|
||||||
|
var result = JsonSerializer.Deserialize<NotNullableSTJEnumObject>("{\"Value\": null}", SerializerOptions.WithConverters(new SerializationContext()));
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.DoesNotThrow(() =>
|
||||||
|
{
|
||||||
|
var result2 = JsonSerializer.Deserialize<NotNullableSTJEnumObject>("{\"Value\": null}", SerializerOptions.WithConverters(new SerializationContext()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Trace.Listeners.Remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[TestCase("1", true)]
|
[TestCase("1", true)]
|
||||||
[TestCase("true", true)]
|
[TestCase("true", true)]
|
||||||
[TestCase("yes", true)]
|
[TestCase("yes", true)]
|
||||||
|
|||||||
@ -120,13 +120,14 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
var t = ReadNullable(ref reader, typeToConvert, options, out var isEmptyString);
|
var t = ReadNullable(ref reader, typeToConvert, options, out var isEmptyStringOrNull);
|
||||||
if (t != null)
|
if (t != null)
|
||||||
return t.Value;
|
return t.Value;
|
||||||
|
|
||||||
if (isEmptyString && !_unknownValuesWarned.Contains(null))
|
if (isEmptyStringOrNull && !_unknownValuesWarned.Contains(null))
|
||||||
{
|
{
|
||||||
// We received an empty string and have no mapping for it, and the property isn't nullable
|
// We received an empty string and have no mapping for it, and the property isn't nullable
|
||||||
|
_unknownValuesWarned.Add(null!);
|
||||||
LibraryHelpers.StaticLogger?.LogWarning($"Received null or empty enum value, but property type is not a nullable enum. EnumType: {typeof(T).FullName}. If you think {typeof(T).FullName} should be nullable please open an issue on the Github repo");
|
LibraryHelpers.StaticLogger?.LogWarning($"Received null or empty enum value, but property type is not a nullable enum. EnumType: {typeof(T).FullName}. If you think {typeof(T).FullName} should be nullable please open an issue on the Github repo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +150,9 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
|||||||
return (T)_undefinedEnumValue;
|
return (T)_undefinedEnumValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private T? ReadNullable(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out bool isEmptyString)
|
private T? ReadNullable(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out bool isEmptyStringOrNull)
|
||||||
{
|
{
|
||||||
isEmptyString = false;
|
isEmptyStringOrNull = false;
|
||||||
var enumType = typeof(T);
|
var enumType = typeof(T);
|
||||||
if (_mappingToEnum == null)
|
if (_mappingToEnum == null)
|
||||||
CreateMapping();
|
CreateMapping();
|
||||||
@ -167,13 +168,16 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (stringValue is null)
|
if (stringValue is null)
|
||||||
|
{
|
||||||
|
isEmptyStringOrNull = true;
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!GetValue(enumType, stringValue, out var result))
|
if (!GetValue(enumType, stringValue, out var result))
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(stringValue))
|
if (string.IsNullOrWhiteSpace(stringValue))
|
||||||
{
|
{
|
||||||
isEmptyString = true;
|
isEmptyStringOrNull = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -305,6 +309,12 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static void Reset()
|
||||||
|
{
|
||||||
|
_undefinedEnumValue = null;
|
||||||
|
_unknownValuesWarned = new ConcurrentBag<string>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the string value for an enum value using the MapAttribute mapping. When multiple values are mapped for a enum entry the first value will be returned
|
/// Get the string value for an enum value using the MapAttribute mapping. When multiple values are mapped for a enum entry the first value will be returned
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -6,9 +6,9 @@
|
|||||||
<PackageId>CryptoExchange.Net</PackageId>
|
<PackageId>CryptoExchange.Net</PackageId>
|
||||||
<Authors>JKorf</Authors>
|
<Authors>JKorf</Authors>
|
||||||
<Description>CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.</Description>
|
<Description>CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.</Description>
|
||||||
<PackageVersion>11.0.2</PackageVersion>
|
<PackageVersion>11.0.3</PackageVersion>
|
||||||
<AssemblyVersion>11.0.2</AssemblyVersion>
|
<AssemblyVersion>11.0.3</AssemblyVersion>
|
||||||
<FileVersion>11.0.2</FileVersion>
|
<FileVersion>11.0.3</FileVersion>
|
||||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||||
<PackageTags>OKX;OKX.Net;Mexc;Mexc.Net;Kucoin;Kucoin.Net;Kraken;Kraken.Net;Huobi;Huobi.Net;CoinEx;CoinEx.Net;Bybit;Bybit.Net;Bitget;Bitget.Net;Bitfinex;Bitfinex.Net;Binance;Binance.Net;CryptoCurrency;CryptoCurrency Exchange;CryptoExchange.Net</PackageTags>
|
<PackageTags>OKX;OKX.Net;Mexc;Mexc.Net;Kucoin;Kucoin.Net;Kraken;Kraken.Net;Huobi;Huobi.Net;CoinEx;CoinEx.Net;Bybit;Bybit.Net;Bitget;Bitget.Net;Bitfinex;Bitfinex.Net;Binance;Binance.Net;CryptoCurrency;CryptoCurrency Exchange;CryptoExchange.Net</PackageTags>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
|
|||||||
@ -154,6 +154,9 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public CallResult AsDataless()
|
public CallResult AsDataless()
|
||||||
{
|
{
|
||||||
|
if (Error != null )
|
||||||
|
return new CallResult(Error);
|
||||||
|
|
||||||
return SuccessResult;
|
return SuccessResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ using CryptoExchange.Net.RateLimiting.Interfaces;
|
|||||||
using CryptoExchange.Net.RateLimiting.Trackers;
|
using CryptoExchange.Net.RateLimiting.Trackers;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace CryptoExchange.Net.RateLimiting.Guards
|
namespace CryptoExchange.Net.RateLimiting.Guards
|
||||||
{
|
{
|
||||||
@ -36,6 +37,7 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
private readonly double? _decayRate;
|
private readonly double? _decayRate;
|
||||||
private readonly int? _connectionWeight;
|
private readonly int? _connectionWeight;
|
||||||
private readonly Func<RequestDefinition, string, string?, string> _keySelector;
|
private readonly Func<RequestDefinition, string, string?, string> _keySelector;
|
||||||
|
private readonly SemaphoreSlim? _sharedGuardSemaphore;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Name => "RateLimitGuard";
|
public string Name => "RateLimitGuard";
|
||||||
@ -52,6 +54,11 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public TimeSpan TimeSpan { get; }
|
public TimeSpan TimeSpan { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether this guard is shared between multiple gates
|
||||||
|
/// </summary>
|
||||||
|
public bool SharedGuard { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -62,8 +69,9 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
/// <param name="windowType">Type of rate limit window</param>
|
/// <param name="windowType">Type of rate limit window</param>
|
||||||
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
|
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
|
||||||
/// <param name="connectionWeight">The weight of a new connection</param>
|
/// <param name="connectionWeight">The weight of a new connection</param>
|
||||||
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IGuardFilter filter, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null)
|
/// <param name="shared">Whether this guard is shared between multiple gates</param>
|
||||||
: this(keySelector, new[] { filter }, limit, timeSpan, windowType, decayPerTimeSpan, connectionWeight)
|
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IGuardFilter filter, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null, bool shared = false)
|
||||||
|
: this(keySelector, new[] { filter }, limit, timeSpan, windowType, decayPerTimeSpan, connectionWeight, shared)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,16 +85,21 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
/// <param name="windowType">Type of rate limit window</param>
|
/// <param name="windowType">Type of rate limit window</param>
|
||||||
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
|
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
|
||||||
/// <param name="connectionWeight">The weight of a new connection</param>
|
/// <param name="connectionWeight">The weight of a new connection</param>
|
||||||
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IEnumerable<IGuardFilter> filters, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null)
|
/// <param name="shared">Whether this guard is shared between multiple gates</param>
|
||||||
|
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IEnumerable<IGuardFilter> filters, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null, bool shared = false)
|
||||||
{
|
{
|
||||||
_filters = filters;
|
_filters = filters;
|
||||||
_trackers = new Dictionary<string, IWindowTracker>();
|
_trackers = new Dictionary<string, IWindowTracker>();
|
||||||
_windowType = windowType;
|
_windowType = windowType;
|
||||||
Limit = limit;
|
Limit = limit;
|
||||||
TimeSpan = timeSpan;
|
TimeSpan = timeSpan;
|
||||||
|
SharedGuard = shared;
|
||||||
_keySelector = keySelector;
|
_keySelector = keySelector;
|
||||||
_decayRate = decayPerTimeSpan;
|
_decayRate = decayPerTimeSpan;
|
||||||
_connectionWeight = connectionWeight;
|
_connectionWeight = connectionWeight;
|
||||||
|
|
||||||
|
if (SharedGuard)
|
||||||
|
_sharedGuardSemaphore = new SemaphoreSlim(1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -101,6 +114,11 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
if (type == RateLimitItemType.Connection)
|
if (type == RateLimitItemType.Connection)
|
||||||
requestWeight = _connectionWeight ?? requestWeight;
|
requestWeight = _connectionWeight ?? requestWeight;
|
||||||
|
|
||||||
|
if (SharedGuard)
|
||||||
|
_sharedGuardSemaphore!.Wait();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var key = _keySelector(definition, host, apiKey) + keySuffix;
|
var key = _keySelector(definition, host, apiKey) + keySuffix;
|
||||||
if (!_trackers.TryGetValue(key, out var tracker))
|
if (!_trackers.TryGetValue(key, out var tracker))
|
||||||
{
|
{
|
||||||
@ -108,12 +126,19 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
_trackers.Add(key, tracker);
|
_trackers.Add(key, tracker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var delay = tracker.GetWaitTime(requestWeight);
|
var delay = tracker.GetWaitTime(requestWeight);
|
||||||
if (delay == default)
|
if (delay == default)
|
||||||
return LimitCheck.NotNeeded(Limit, TimeSpan, tracker.Current);
|
return LimitCheck.NotNeeded(Limit, TimeSpan, tracker.Current);
|
||||||
|
|
||||||
return LimitCheck.Needed(delay, Limit, TimeSpan, tracker.Current);
|
return LimitCheck.Needed(delay, Limit, TimeSpan, tracker.Current);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (SharedGuard)
|
||||||
|
_sharedGuardSemaphore!.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, string? apiKey, int requestWeight, string? keySuffix)
|
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, string? apiKey, int requestWeight, string? keySuffix)
|
||||||
@ -127,9 +152,23 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
if (type == RateLimitItemType.Connection)
|
if (type == RateLimitItemType.Connection)
|
||||||
requestWeight = _connectionWeight ?? requestWeight;
|
requestWeight = _connectionWeight ?? requestWeight;
|
||||||
|
|
||||||
|
|
||||||
var key = _keySelector(definition, host, apiKey) + keySuffix;
|
var key = _keySelector(definition, host, apiKey) + keySuffix;
|
||||||
var tracker = _trackers[key];
|
var tracker = _trackers[key];
|
||||||
|
|
||||||
|
if (SharedGuard)
|
||||||
|
_sharedGuardSemaphore!.Wait();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
tracker.ApplyWeight(requestWeight);
|
tracker.ApplyWeight(requestWeight);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (SharedGuard)
|
||||||
|
_sharedGuardSemaphore!.Release();
|
||||||
|
}
|
||||||
|
|
||||||
return RateLimitState.Applied(Limit, TimeSpan, tracker.Current);
|
return RateLimitState.Applied(Limit, TimeSpan, tracker.Current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@ namespace CryptoExchange.Net.Testing
|
|||||||
if (message.Contains("Cannot map"))
|
if (message.Contains("Cannot map"))
|
||||||
throw new Exception("Enum value error: " + message);
|
throw new Exception("Enum value error: " + message);
|
||||||
|
|
||||||
if (message.Contains("Received null enum value"))
|
if (message.Contains("Received null or empty enum value"))
|
||||||
throw new Exception("Enum null error: " + message);
|
throw new Exception("Enum null error: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ namespace CryptoExchange.Net.Testing
|
|||||||
if (message.Contains("Cannot map"))
|
if (message.Contains("Cannot map"))
|
||||||
throw new Exception("Enum value error: " + message);
|
throw new Exception("Enum value error: " + message);
|
||||||
|
|
||||||
if (message.Contains("Received null enum value"))
|
if (message.Contains("Received null or empty enum value"))
|
||||||
throw new Exception("Enum null error: " + message);
|
throw new Exception("Enum null error: " + message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
126
CryptoExchange.Net/Testing/SharedRestRequestValidator.cs
Normal file
126
CryptoExchange.Net/Testing/SharedRestRequestValidator.cs
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
using CryptoExchange.Net.Clients;
|
||||||
|
using CryptoExchange.Net.Objects;
|
||||||
|
using CryptoExchange.Net.SharedApis;
|
||||||
|
using CryptoExchange.Net.Testing.Comparers;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net.Testing
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Validator for REST requests, comparing path, http method, authentication and response parsing
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TClient">The Rest client</typeparam>
|
||||||
|
public class SharedRestRequestValidator<TClient> where TClient : BaseRestClient
|
||||||
|
{
|
||||||
|
private readonly TClient _client;
|
||||||
|
private readonly Func<WebCallResult, bool> _isAuthenticated;
|
||||||
|
private readonly string _folder;
|
||||||
|
private readonly string _baseAddress;
|
||||||
|
private readonly string? _nestedPropertyForCompare;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ctor
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">Client to test</param>
|
||||||
|
/// <param name="folder">Folder for json test values</param>
|
||||||
|
/// <param name="baseAddress">The base address that is expected</param>
|
||||||
|
/// <param name="isAuthenticated">Func for checking if the request is authenticated</param>
|
||||||
|
/// <param name="nestedPropertyForCompare">Property to use for compare</param>
|
||||||
|
public SharedRestRequestValidator(TClient client, string folder, string baseAddress, Func<WebCallResult, bool> isAuthenticated, string? nestedPropertyForCompare = null)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
_folder = folder;
|
||||||
|
_baseAddress = baseAddress;
|
||||||
|
_nestedPropertyForCompare = nestedPropertyForCompare;
|
||||||
|
_isAuthenticated = isAuthenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate a request
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">Expected response type</typeparam>
|
||||||
|
/// <param name="methodInvoke">Method invocation</param>
|
||||||
|
/// <param name="name">Method name for looking up json test values</param>
|
||||||
|
/// <param name="endpointOptions">Request options</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public Task ValidateAsync<TResponse>(
|
||||||
|
Func<TClient, Task<ExchangeWebResult<TResponse>>> methodInvoke,
|
||||||
|
string name,
|
||||||
|
EndpointOptions endpointOptions,
|
||||||
|
params Func<TResponse, bool>[] validation)
|
||||||
|
=> ValidateAsync<TResponse, TResponse>(methodInvoke, name, endpointOptions, validation);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate a request
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">Expected response type</typeparam>
|
||||||
|
/// <typeparam name="TActualResponse">The concrete response type</typeparam>
|
||||||
|
/// <param name="methodInvoke">Method invocation</param>
|
||||||
|
/// <param name="name">Method name for looking up json test values</param>
|
||||||
|
/// <param name="endpointOptions">Request options</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public async Task ValidateAsync<TResponse, TActualResponse>(
|
||||||
|
Func<TClient, Task<ExchangeWebResult<TResponse>>> methodInvoke,
|
||||||
|
string name,
|
||||||
|
EndpointOptions endpointOptions,
|
||||||
|
params Func<TResponse, bool>[] validation) where TActualResponse : TResponse
|
||||||
|
{
|
||||||
|
var listener = new EnumValueTraceListener();
|
||||||
|
Trace.Listeners.Add(listener);
|
||||||
|
|
||||||
|
var path = Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName;
|
||||||
|
FileStream file;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
file = File.OpenRead(Path.Combine(path, _folder, $"{name}.txt"));
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
throw new Exception($"Response file not found for {name}: {path}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer = new byte[file.Length];
|
||||||
|
await file.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
||||||
|
file.Close();
|
||||||
|
|
||||||
|
var data = Encoding.UTF8.GetString(buffer);
|
||||||
|
using var reader = new StringReader(data);
|
||||||
|
var expectedMethod = reader.ReadLine();
|
||||||
|
var expectedPath = reader.ReadLine();
|
||||||
|
var expectedAuth = bool.Parse(reader.ReadLine()!);
|
||||||
|
var response = reader.ReadToEnd();
|
||||||
|
|
||||||
|
TestHelpers.ConfigureRestClient(_client, response, System.Net.HttpStatusCode.OK);
|
||||||
|
var result = await methodInvoke(_client).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Check request/response properties
|
||||||
|
if (result.Error != null)
|
||||||
|
throw new Exception(name + " returned error " + result.Error);
|
||||||
|
if (endpointOptions.NeedsAuthentication != expectedAuth)
|
||||||
|
throw new Exception(name + $" authentication not matched. Expected: {expectedAuth}, Actual: {_isAuthenticated(result.AsDataless())}");
|
||||||
|
if (result.RequestMethod != new HttpMethod(expectedMethod!))
|
||||||
|
throw new Exception(name + $" http method not matched. Expected {expectedMethod}, Actual: {result.RequestMethod}");
|
||||||
|
if (expectedPath != result.RequestUrl!.Replace(_baseAddress, "").Split(new char[] { '?' })[0])
|
||||||
|
throw new Exception(name + $" path not matched. Expected: {expectedPath}, Actual: {result.RequestUrl!.Replace(_baseAddress, "").Split(new char[] { '?' })[0]}");
|
||||||
|
|
||||||
|
var index = 0;
|
||||||
|
foreach(var validate in validation)
|
||||||
|
{
|
||||||
|
if (!validate(result.Data!))
|
||||||
|
throw new Exception(name + $" response validation #{index} failed");
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Trace.Listeners.Remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -68,6 +68,9 @@ Make a one time donation in a crypto currency of your choice. If you prefer to d
|
|||||||
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
|
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
|
||||||
|
|
||||||
## Release notes
|
## Release notes
|
||||||
|
* Version 11.0.3 - 30 Mar 2026
|
||||||
|
* Updated Enum converter to only warn once per type for null/empty value for non-nullable enum property
|
||||||
|
|
||||||
* Version 11.0.2 - 26 Mar 2026
|
* Version 11.0.2 - 26 Mar 2026
|
||||||
* Updated SetOptions logic to allow calling on client without credentials
|
* Updated SetOptions logic to allow calling on client without credentials
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user