using System; using System.Xml.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Senparc.Weixin.MP.Entities; using Senparc.Weixin.MP.Test.NetCore3.MessageHandlers.TestEntities; using Senparc.WeixinTests; namespace Senparc.Weixin.MP.Test.MessageHandlers { public partial class MessageHandlersTest : BaseTest { /// /// 事件测试 /// /// /// /// /// private CustomMessageHandlers VerifyEventTest(string xml, Event eventType) where T : RequestMessageEventBase { var messageHandlers = new CustomMessageHandlers(XDocument.Parse(xml)); messageHandlers.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod; Assert.IsNotNull(messageHandlers.RequestDocument); //messageHandlers.DefaultMessageHandlerAsyncEvent = NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.SelfSynicMethod; messageHandlers.Execute(); Assert.IsNotNull(messageHandlers.TextResponseMessage); var requestMessage = messageHandlers.RequestMessage as T; Console.WriteLine(messageHandlers.RequestMessage.GetType()); Assert.IsNotNull(requestMessage); Assert.AreEqual(eventType, requestMessage.Event); return messageHandlers; } #region 微信订阅消息 [TestMethod] public void SubscribeTest() { var xml = @" subscribe Event 637109853628154800 1575361182 "; var messageHandler = VerifyEventTest(xml, Event.subscribe); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_Subscribe; Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText)); Assert.AreEqual("欢迎关注", (messageHandler.ResponseMessage as ResponseMessageText).Content); } #endregion #region 微信认证事件推送 [TestMethod] public void QualificationVerifySuccessTest() { var xml = @" 1442401156 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.qualification_verify_success); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_QualificationVerifySuccess; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.ExpiredTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("success", messageHandler.TextResponseMessage); } [TestMethod] public void QualificationVerifyFailTest() { var xml = @" 1442401156 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.qualification_verify_fail); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_QualificationVerifyFail; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.FailTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("by time", requestMessage.FailReason); Assert.AreEqual("success", messageHandler.TextResponseMessage); } [TestMethod] public void NamingVerifySuccessTest() { var xml = @" 1442401093 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.naming_verify_success); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_NamingVerifySuccess; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.ExpiredTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("success", messageHandler.TextResponseMessage); } [TestMethod] public void NamingVerifyFailTest() { var xml = @" 1442401061 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.naming_verify_fail); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_NamingVerifyFail; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.FailTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("by time 2", requestMessage.FailReason); Assert.AreEqual("success", messageHandler.TextResponseMessage); } [TestMethod] public void AnnualRenewTest() { var xml = @" 1442401004 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.annual_renew); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_AnnualRenew; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.ExpiredTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("success", messageHandler.TextResponseMessage); } [TestMethod] public void VerifyExpiredTest() { var xml = @" 1442400900 1442401156 "; var messageHandler = VerifyEventTest(xml, Event.verify_expired); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_VerifyExpired; Assert.AreEqual("2015-09-16 18:59:16", requestMessage.ExpiredTime.ToString("yyyy-MM-dd HH:mm:ss")); Assert.AreEqual("success", messageHandler.TextResponseMessage); } #endregion #region 微信小程序打开 [TestMethod] public void ViewMiniProgramTest() { var xml = @" 123456789 MENUID "; var messageHandler = VerifyEventTest(xml, Event.view_miniprogram); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_View_Miniprogram; Assert.IsNotNull(requestMessage); Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText)); Assert.AreEqual("小程序被访问:MENUID - pages/index/index", (messageHandler.ResponseMessage as ResponseMessageText).Content); } #endregion #region 卡券回调 [TestMethod] public void GiftCard_Pay_DoneTest() { var xml = @" 1472631550 "; var messageHandler = VerifyEventTest(xml, Event.giftcard_pay_done); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_GiftCard_Pay_Done; Assert.IsInstanceOfType(requestMessage, typeof(RequestMessageEvent_GiftCard_Pay_Done)); Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText)); Assert.AreEqual("这里是 OnEvent_GiftCard_Pay_DoneRequest", (messageHandler.ResponseMessage as ResponseMessageText).Content); } [TestMethod] public void GiftCard_Send_To_FriendTest() { var xml = @" 1472631550 true "; var messageHandler = VerifyEventTest(xml, Event.giftcard_send_to_friend); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_GiftCard_Send_To_Friend; Assert.IsInstanceOfType(requestMessage, typeof(RequestMessageEvent_GiftCard_Send_To_Friend)); Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText)); Assert.AreEqual("这里是 OnEvent_GiftCard_Send_To_FriendRequest", (messageHandler.ResponseMessage as ResponseMessageText).Content); } [TestMethod] public void GiftCard_User_AcceptdTest() { var xml = @" 1472631800 true "; var messageHandler = VerifyEventTest(xml, Event.giftcard_user_accept); var requestMessage = messageHandler.RequestMessage as RequestMessageEvent_GiftCard_User_Accept; Assert.IsInstanceOfType(requestMessage, typeof(RequestMessageEvent_GiftCard_User_Accept)); Assert.IsInstanceOfType(messageHandler.ResponseMessage, typeof(ResponseMessageText)); Assert.AreEqual("这里是 OnEvent_GiftCard_User_AcceptRequest", (messageHandler.ResponseMessage as ResponseMessageText).Content); } #endregion } }