From cb4f12b536ee97278e45cbb7e1a5c45bba2e68bf Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 2 Aug 2021 12:43:01 +0800 Subject: [PATCH] =?UTF-8?q?test(wxapi):=20=E8=A1=A5=E5=85=85=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=8E=A8=E9=80=81=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WechatNotifyController.cs | 6 +++--- .../WechatApiEventDeserializationTests.cs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatNotifyController.cs b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatNotifyController.cs index 2419963b..465039c9 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatNotifyController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatNotifyController.cs @@ -65,17 +65,17 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers [FromRoute(Name = "app_id")] string appId) { // 接收服务器推送 - // 文档:https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html + // 文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html using var reader = new StreamReader(Request.Body, Encoding.UTF8); string content = await reader.ReadToEndAsync(); _logger.LogInformation("接收到微信推送的数据:{0}", content); var xDoc = XDocument.Parse(content); - string @event = xDoc.Root!.Element("Event")!.Value.ToUpper(); + string msgType = xDoc.Root!.Element("MsgType")!.Value.ToUpper(); var client = _wechatApiHttpClientFactory.Create(appId); - switch (@event) + switch (msgType) { case "TEXT": { diff --git a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/WechatApiEventDeserializationTests.cs b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/WechatApiEventDeserializationTests.cs index 2a3178cb..70f49b4e 100644 --- a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/WechatApiEventDeserializationTests.cs +++ b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/WechatApiEventDeserializationTests.cs @@ -3,12 +3,24 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Xml.Linq; using Xunit; namespace SKIT.FlurlHttpClient.Wechat.Api.UnitTests { public class WechatApiEventDeserializationTests { + [Fact(DisplayName = "获取事件消息类型")] + public void GetEventMessageTypeTest() + { + string xml = "13488318601234567890123456"; + + var xDoc = XDocument.Parse(xml); + string @event = xDoc.Root!.Element("MsgType")!.Value.ToUpper(); + + Assert.Equal("TEXT", @event); + } + [Fact(DisplayName = "反序列化 TEXT 事件")] public void DeserializeTextMessageEventTest() {