From e797d34331dd7327171322b1e08fef58337f3c31 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Wed, 1 Dec 2021 21:17:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(tenpayv3):=20`CertificateEntry`=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20JSON=20=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E6=88=96=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Settings/CertificateEntry.cs | 2 ++ .../WechatTenpayCertificateManagerTests.cs | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/WechatTenpayCertificateManagerTests.cs diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs index 607f8e17..ff1ac3d4 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs @@ -28,6 +28,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings /// public DateTimeOffset ExpireTime { get; } + [Newtonsoft.Json.JsonConstructor] + [System.Text.Json.Serialization.JsonConstructor] public CertificateEntry(string serialNumber, string certificate, DateTimeOffset effectiveTime, DateTimeOffset expireTime) { if (string.IsNullOrEmpty(serialNumber)) diff --git a/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/WechatTenpayCertificateManagerTests.cs b/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/WechatTenpayCertificateManagerTests.cs new file mode 100644 index 00000000..c1bf5eec --- /dev/null +++ b/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/WechatTenpayCertificateManagerTests.cs @@ -0,0 +1,30 @@ +using System; +using Xunit; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests +{ + public class WechatTenpayCertificateManagerTests + { + [Fact(DisplayName = "序列化与反序列化 `CertificateEntry`")] + public void CertificateEntrySerializationTest() + { + var entry = new Settings.CertificateEntry("SN", "-----BEGIN CERTIFICATE-----END CERTIFICATE-----", DateTimeOffset.Now, DateTimeOffset.Now); + + var serialized1 = Newtonsoft.Json.JsonConvert.SerializeObject(entry); + var deserialized1 = Newtonsoft.Json.JsonConvert.DeserializeObject(serialized1); + + Assert.Equal(entry.SerialNumber, deserialized1.SerialNumber); + Assert.Equal(entry.Certificate, deserialized1.Certificate); + Assert.Equal(entry.EffectiveTime, deserialized1.EffectiveTime); + Assert.Equal(entry.ExpireTime, deserialized1.ExpireTime); + + var serialized2 = System.Text.Json.JsonSerializer.Serialize(entry); + var deserialized2 = System.Text.Json.JsonSerializer.Deserialize(serialized2); + + Assert.Equal(entry.SerialNumber, deserialized2.SerialNumber); + Assert.Equal(entry.Certificate, deserialized2.Certificate); + Assert.Equal(entry.EffectiveTime, deserialized2.EffectiveTime); + Assert.Equal(entry.ExpireTime, deserialized2.ExpireTime); + } + } +}