mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-15 23:13:32 +08:00
test(wxapi): 补充服务器推送单元测试
This commit is contained in:
parent
ce29cf383d
commit
cb4f12b536
@ -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":
|
||||
{
|
||||
|
@ -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 = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a test]]></Content><MsgId>1234567890123456</MsgId></xml>";
|
||||
|
||||
var xDoc = XDocument.Parse(xml);
|
||||
string @event = xDoc.Root!.Element("MsgType")!.Value.ToUpper();
|
||||
|
||||
Assert.Equal("TEXT", @event);
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "反序列化 TEXT 事件")]
|
||||
public void DeserializeTextMessageEventTest()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user