#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Senparc.NeuChar.Context;
using Senparc.CO2NET.Extensions;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.MessageHandlers;
using Senparc.NeuChar.Entities;
using Senparc.NeuChar.Helpers;
using Senparc.WeixinTests;
using Senparc.NeuChar.Entities.Request;
using Senparc.Weixin.MP.MessageContexts;
using System.Threading.Tasks;
using Senparc.Weixin.MP.Test.NetCore3.MessageHandlers.TestEntities;
//TODO:分布式向下文升级,部分方法需要修改后重启测试 —— Jeffrey 2019.9.15
namespace Senparc.Weixin.MP.Test.MessageHandlers
{
[TestClass]
public partial class MessageHandlersTest : BaseTest
{
string xmlText = @"
1357986928
5832509444155992350
";
string xmlLocation = @"
1358061152
31.285774
120.597610
19
5832828233808572154
";
[TestMethod]
public void TextMessageRequestTest()
{
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xmlText));
//启用同步方法做替补
messageHandlers.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod;
Assert.IsNotNull(messageHandlers.RequestDocument);
messageHandlers.Execute();
Assert.IsNotNull(messageHandlers.ResponseMessage);
Assert.IsNotNull(messageHandlers.ResponseDocument);
Assert.IsFalse(messageHandlers.UsingEncryptMessage);//没有使用加密模式
Assert.IsFalse(messageHandlers.UsingCompatibilityModelEncryptMessage);//没有加密模式,所以也没有兼容模式
Console.Write(messageHandlers.ResponseDocument.ToString());
Assert.AreEqual("gh_a96a4a619366", messageHandlers.ResponseMessage.FromUserName);
var responseMessage = messageHandlers.ResponseMessage as ResponseMessageText;
Assert.IsNotNull(responseMessage);
Assert.AreEqual("文字信息", responseMessage.Content);
}
[TestMethod]
public void Event_LocationSelectTest()
{
var requestXML = @"
ToUserName
FromUserName
1444293582
";
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(requestXML));
Assert.IsNotNull(messageHandlers.RequestDocument);
Assert.IsInstanceOfType(messageHandlers.RequestMessage, typeof(RequestMessageEvent_Location_Select));
Assert.AreEqual("ZBZXC", ((RequestMessageEvent_Location_Select)messageHandlers.RequestMessage).EventKey);
messageHandlers.Execute();
Assert.IsNotNull(messageHandlers.ResponseMessage);
Assert.IsNotNull(messageHandlers.ResponseDocument);
Assert.IsFalse(messageHandlers.UsingEncryptMessage);//没有使用加密模式
Assert.IsFalse(messageHandlers.UsingCompatibilityModelEncryptMessage);//没有加密模式,所以也没有兼容模式
Console.WriteLine(messageHandlers.ResponseDocument.ToString());
Assert.AreEqual("ToUserName", messageHandlers.ResponseMessage.FromUserName);
Assert.IsInstanceOfType(messageHandlers.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("OnEvent_LocationSelectRequest", ((ResponseMessageText)messageHandlers.ResponseMessage).Content);
}
[TestMethod]
public void OnSubscribeTest()
{
var requestXML = @"
1516545128
subscribe
";
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(requestXML));
Assert.IsNotNull(messageHandlers.RequestDocument);
Assert.IsInstanceOfType(messageHandlers.RequestMessage, typeof(RequestMessageEvent_Subscribe));
Assert.AreEqual("", ((RequestMessageEvent_Subscribe)messageHandlers.RequestMessage).EventKey);//EventKey为空
messageHandlers.Execute();
Assert.IsNotNull(messageHandlers.ResponseMessage);
Assert.IsNotNull(messageHandlers.ResponseDocument);
Assert.IsInstanceOfType(messageHandlers.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("欢迎关注", ((ResponseMessageText)messageHandlers.ResponseMessage).Content);
Console.WriteLine(messageHandlers.FinalResponseDocument);
}
[TestMethod]
public void CompatibilityModelEncryptMessageRequestTest()
{
//兼容模式测试
var ecryptXml = @"
1414387151
6074746557628292943
";
//signature=e3203b6433eb554dd2fcba78fa48cb948fcb4801×tamp=1414387151&nonce=917222494&encrypt_type=aes&msg_signature=ae70d4e343d946fc0477a5c760b95be0947fddbb
var postModel = new PostModel()
{
Msg_Signature = "ae70d4e343d946fc0477a5c760b95be0947fddbb",
Timestamp = "1414387151",
Nonce = "917222494",
Token = "weixin",
EncodingAESKey = "mNnY5GekpChwqhy2c4NBH90g3hND6GeI4gii2YCvKLY",
AppId = "wx669ef95216eef885"
};
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(ecryptXml), postModel);
Assert.IsNotNull(messageHandlers.RequestDocument);
Assert.IsNotNull(messageHandlers.RequestMessage);
Assert.IsNotNull(messageHandlers.RequestMessage.Encrypt);
Assert.IsNotNull(messageHandlers.RequestMessage.FromUserName);
Assert.IsNotNull(messageHandlers.EcryptRequestDocument);
Assert.IsTrue(messageHandlers.UsingEncryptMessage);
Assert.IsTrue(messageHandlers.UsingCompatibilityModelEncryptMessage);
}
[TestMethod]
public async Task PureEncryptMessageRequestTest()
{
//纯安全模式测试
var ecryptXml = @"
";
var postModel = new PostModel()
{
Msg_Signature = "20f4a1263d198b696e6958e0d65e928aa68f7d96",
Timestamp = "1570032739",
Nonce = "2068872452",
Token = "weixin",
EncodingAESKey = "mNnY5GekpChwqhy2c4NBH90g3hND6GeI4gii2YCvKLY",
AppId = "wx669ef95216eef885"
};
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(ecryptXml), postModel);
messageHandlers.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod;
messageHandlers.OmitRepeatedMessage = false;//不去重
Assert.IsNotNull(messageHandlers.RequestDocument);
Assert.IsNotNull(messageHandlers.RequestMessage);
Assert.IsNotNull(messageHandlers.RequestMessage.Encrypt);
Assert.IsNotNull(messageHandlers.RequestMessage.FromUserName);
Assert.IsNotNull(messageHandlers.EcryptRequestDocument);
Assert.IsTrue(messageHandlers.UsingEncryptMessage);
Assert.IsFalse(messageHandlers.UsingCompatibilityModelEncryptMessage);
Console.WriteLine("RequestMessage:");
Console.WriteLine(messageHandlers.RequestMessage.ToJson(true));
await messageHandlers.ExecuteAsync(new CancellationToken());
Console.WriteLine("ResponseMessage:");
Console.WriteLine(messageHandlers.ResponseMessage.ToJson(true));
}
[TestMethod]
public void SyncTest()
{
//测试缓存同步
var messageHandlers1 = new CustomMessageHandlers(XDocument.Parse(xmlText));
var messageHandlers2 = new CustomMessageHandlers(XDocument.Parse(xmlText));
messageHandlers1.Execute();
Assert.AreEqual(messageHandlers1.GlobalMessageContext.GetHashCode(), messageHandlers2.GlobalMessageContext.GetHashCode());
}
[TestMethod]
public void RegexTest()
{
string regexXmlText = @"
1357986928
5832509444155992351
";
var messageHandler = new CustomMessageHandlers(XDocument.Parse(regexXmlText));
messageHandler.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod;
messageHandler.ExecuteAsync(new CancellationToken()).GetAwaiter().GetResult();
Assert.IsNotNull(messageHandler.ResponseMessage);
Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText));
Console.WriteLine(messageHandler.ResponseMessage.ToJson(true));
}
[TestMethod]
public void ContextTest()
{
//var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xmlText));
//messageHandlers.Execute();
//var messageContext = messageHandlers.GlobalMessageContext.GetMessageContext(messageHandlers.RequestMessage);
//Assert.IsTrue(messageContext.RequestMessages.Count > 0);
//Assert.IsNotNull(messageHandlers.CurrentMessageContext);
//Assert.AreEqual("olPjZjsXuQPJoV0HlruZkNzKc91E", messageHandlers.CurrentMessageContext.UserName);
//messageHandlers.GlobalMessageContext.ExpireMinutes = 0;//马上过期
//messageHandlers.Execute();
//messageContext = messageHandlers.GlobalMessageContext.GetMessageContext(messageHandlers.RequestMessage);
//Assert.AreEqual(0, messageContext.RequestMessages.Count);
}
private class TestContext
{
public static int FinishCount = 0;
public static string RequestXmlFormat = @"
1357986928
5832509444155992350
";
public void Run()
{
for (int i = 0; i < 10; i++)
{
//模拟10个不同用户名
var userName = Thread.CurrentThread.Name + "_" + i;
var xml = string.Format(RequestXmlFormat, userName);
for (int j = 0; j < 2; j++)
{
//每个用户请求2次
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xml));
messageHandlers.Execute();
}
Thread.Sleep(5);
}
TestContext.FinishCount++;
}
}
[TestMethod]
public void RestoreTest()
{
//var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xmlText));
//messageHandlers.Execute();
//Assert.IsTrue(messageHandlers.GlobalMessageContext..MessageCollection.Count > 0);
//messageHandlers.GlobalMessageContext.Restore();
//Assert.AreEqual(0, messageHandlers.GlobalMessageContext.MessageCollection.Count);
}
[TestMethod]
public void MutipleThreadsTest()
{
////
//var weixinContext = MessageHandler.GlobalWeixinContext;//全局共享的WeixinContext上下文对象
//weixinContext.Restore();
////多线程并发写入测试
//List threadList = new List();
//for (int i = 0; i < 200; i++)
//{
// var testContext = new TestContext();
// var thread = new Thread(testContext.Run);
// thread.Name = i.ToString();
// threadList.Add(thread);
//}
//threadList.ForEach(z => z.Start()); //开始所有线程
//while (TestContext.FinishCount < 200)
//{
//}
//Assert.AreEqual(200 * 10, weixinContext.MessageCollection.Count); //用户数量
////判断消息上下是否自动移到底部
//{
// var userName = "3_4";
// var xml = string.Format(TestContext.RequestXmlFormat, userName);
// var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xml));
// messageHandlers.Execute();
// var lastQueueMessage = weixinContext.MessageQueue.Last();
// Assert.AreEqual(userName, lastQueueMessage.UserName);
//}
////判断超时信息是否被及时删除
//{
// weixinContext.ExpireMinutes = 0.001; //设置过期时间(0.06秒)
// Thread.Sleep(100);
// weixinContext.GetLastRequestMessage("new"); //触发过期判断
// Assert.AreEqual(1, weixinContext.MessageCollection.Count); //只删除剩下当前这一个
//}
}
[TestMethod]
public void OnEvent_MassSendJobFinishRequestTest()
{
var xml = @"
1394524295
1988
100
80
75
5
";
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xml));
messageHandlers.Execute();
Assert.IsNotNull(messageHandlers.ResponseMessage);
Console.Write(messageHandlers.ResponseDocument);
}
[TestMethod]
public void DefaultResponseMessageTest()
{
var messageHandler = new CustomMessageHandlers(XDocument.Parse(xmlLocation));
messageHandler.Execute();
//TestMessageHandlers中没有处理坐标信息的重写方法,将返回默认消息
Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("您发送的消息类型暂未被识别。RequestMessage Type:RequestMessageLocation", ((ResponseMessageText)messageHandler.ResponseMessage).Content);
}
[TestMethod]
public void UnknowTypeMessageTest()
{
var requestXmlFormat = @"
1357986928
5832509444155992350
";
var types = new[] { "unknown1", "unknown2", "unknown3" };
foreach (var type in types)
{
var fileXml = requestXmlFormat.FormatWith("JeffreySu", type);
var messageHandler = new CustomMessageHandlers(XDocument.Parse(fileXml));
messageHandler.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod;
messageHandler.OmitRepeatedMessage = false;//禁用去重机制
messageHandler.Execute();
Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("未知消息类型:{0}".FormatWith(type), ((ResponseMessageText)messageHandler.ResponseMessage).Content);
}
}
///
/// 专为测试用的构造函数测试
///
[TestMethod]
public void TestConstructorTest()
{
var requestMessage = new RequestMessageText()
{
Content = "Hi",
CreateTime = SystemTime.Now,
FromUserName = "FromeUserName",
ToUserName = "ToUserName",
MsgId = 123,
};
var messageHandler = new CustomMessageHandlers(requestMessage);
messageHandler.Execute();
//TestMessageHandlers中没有处理坐标信息的重写方法,将返回默认消息
Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("文字信息", ((ResponseMessageText)messageHandler.ResponseMessage).Content);
}
#region 文件类型测试
private string testFileXml = @"
1519483978
222
6526133992738756795
";
//
//
//
[TestMethod]
public void FileTest()
{
//数据不全,未开始正式测试
var postModel = new PostModel()
{
AppId = "wx43899fc5fd7ab4dc",
Msg_Signature = "a61aaa287b63782f8e5d801075e20e75bfe37af4",
Timestamp = "1519483978",
Nonce = "1122185331",
Token = "moZ05vVoeueufLTuCqv",
EncodingAESKey = "68JptSBfCdTa12VRKa3Ztv4cDRbqlor8Kgy6c9l6bKK",
};
var messageHandler = new CustomMessageHandlers(XDocument.Parse(testFileXml), postModel, 10);
messageHandler.Execute();
Assert.IsInstanceOfType(messageHandler.RequestMessage, typeof(RequestMessageFile));
Console.WriteLine(messageHandler.RequestDocument);
Console.WriteLine(messageHandler.ResponseDocument);
Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText));
Assert.AreEqual("95d98d3bf1b251a9e4a40f3bd88eef29", ((ResponseMessageText)messageHandler.ResponseMessage).Content);
}
#endregion
#region NeuChar 测试
[TestMethod]
public void NeuCharDefineTest()
{
string xmlText = @"
Senparc
NeuChar
1537966819
NeuChar
636735924194707412
SaveConfig
";
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xmlText));
Assert.IsNotNull(messageHandlers.RequestDocument);
messageHandlers.Execute();
Assert.IsNull(messageHandlers.ResponseMessage);
Assert.IsNull(messageHandlers.ResponseDocument);
Assert.IsFalse(messageHandlers.UsingEncryptMessage);//没有使用加密模式
Assert.IsFalse(messageHandlers.UsingCompatibilityModelEncryptMessage);//没有加密模式,所以也没有兼容模式
Console.Write(messageHandlers.TextResponseMessage);
}
[TestMethod]
public void NeuCharReplyTest()
{
string xmlText = @"
1357986928
5832509444155992350
";
var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xmlText));
Assert.IsNotNull(messageHandlers.RequestDocument);
messageHandlers.Execute();
Assert.IsNotNull(messageHandlers.ResponseMessage);
Assert.IsNotNull(messageHandlers.ResponseDocument);
Assert.IsFalse(messageHandlers.UsingEncryptMessage);//没有使用加密模式
Assert.IsFalse(messageHandlers.UsingCompatibilityModelEncryptMessage);//没有加密模式,所以也没有兼容模式
Console.Write(messageHandlers.ResponseDocument.ToString());
}
#endregion
}
}