From fb36ed2caed103f8d69a872969a78e6037f74816 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 20 Mar 2020 14:07:42 +0900 Subject: [PATCH] Fix tests --- BTCPayServer.Client/BTCPayServerClient.cs | 11 ++--------- BTCPayServer.Client/Serializer.cs | 15 +++++++++++++++ BTCPayServer.Tests/ApiKeysTests.cs | 2 +- BTCPayServer.Tests/GreenfieldAPITests.cs | 1 - 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/BTCPayServer.Client/BTCPayServerClient.cs b/BTCPayServer.Client/BTCPayServerClient.cs index d3780411c..4dfad35db 100644 --- a/BTCPayServer.Client/BTCPayServerClient.cs +++ b/BTCPayServer.Client/BTCPayServerClient.cs @@ -12,19 +12,12 @@ namespace BTCPayServer.Client { public partial class BTCPayServerClient { - static BTCPayServerClient() - { - _GlobalSerializer = new JsonSerializerSettings(); - Serializer.RegisterConverters(_GlobalSerializer); - } private readonly string _apiKey; private readonly Uri _btcpayHost; private readonly HttpClient _httpClient; public string APIKey => _apiKey; - private static readonly JsonSerializerSettings _GlobalSerializer = new JsonSerializerSettings(); - public BTCPayServerClient(Uri btcpayHost, HttpClient httpClient = null) { if (btcpayHost == null) @@ -47,7 +40,7 @@ namespace BTCPayServer.Client protected async Task HandleResponse(HttpResponseMessage message) { HandleResponse(message); - return JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync(), _GlobalSerializer); + return JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync(), Serializer.GlobalSerializerSettings); } protected virtual HttpRequestMessage CreateHttpRequest(string path, @@ -75,7 +68,7 @@ namespace BTCPayServer.Client var request = CreateHttpRequest(path, queryPayload, method); if (typeof(T).IsPrimitive || !EqualityComparer.Default.Equals(bodyPayload, default(T))) { - request.Content = new StringContent(JsonConvert.SerializeObject(bodyPayload, _GlobalSerializer), Encoding.UTF8, "application/json"); + request.Content = new StringContent(JsonConvert.SerializeObject(bodyPayload, Serializer.GlobalSerializerSettings), Encoding.UTF8, "application/json"); } return request; diff --git a/BTCPayServer.Client/Serializer.cs b/BTCPayServer.Client/Serializer.cs index 7ab7b43b4..91803026e 100644 --- a/BTCPayServer.Client/Serializer.cs +++ b/BTCPayServer.Client/Serializer.cs @@ -8,6 +8,21 @@ namespace BTCPayServer.Client { public class Serializer { + + private static JsonSerializerSettings _GlobalSerializerSettings; + public static JsonSerializerSettings GlobalSerializerSettings + { + get + { + if (_GlobalSerializerSettings is null) + { + var serializer = new JsonSerializerSettings(); + RegisterConverters(serializer); + _GlobalSerializerSettings = serializer; + } + return _GlobalSerializerSettings; + } + } public static void RegisterConverters(JsonSerializerSettings settings) { if (settings == null) diff --git a/BTCPayServer.Tests/ApiKeysTests.cs b/BTCPayServer.Tests/ApiKeysTests.cs index fde2e5433..5dcc68dc9 100644 --- a/BTCPayServer.Tests/ApiKeysTests.cs +++ b/BTCPayServer.Tests/ApiKeysTests.cs @@ -311,7 +311,7 @@ namespace BTCPayServer.Tests return (T)Convert.ChangeType(rawJson, typeof(T)); } - return JsonConvert.DeserializeObject(rawJson); + return JsonConvert.DeserializeObject(rawJson, BTCPayServer.Client.Serializer.GlobalSerializerSettings); } } } diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index b3569dcbe..b32e6e828 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using BTCPayServer.Client; using BTCPayServer.Client.Models; using BTCPayServer.Controllers; -using BTCPayServer.Controllers.RestApi.Users; using BTCPayServer.Services; using BTCPayServer.Tests.Logging; using Microsoft.AspNet.SignalR.Client;