#region Apache License Version 2.0 /*---------------------------------------------------------------- Copyright 2022 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.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Senparc.Weixin.Work.Entities; namespace Senparc.Weixin.Work.Test { [TestClass] public class RequestMessageFactoryTest { [TestMethod] public void GetEncryptPostDataTest() { var xml = @" "; var encryptPostData = RequestMessageFactory.GetEncryptPostData(xml); Assert.IsNotNull(encryptPostData); Assert.AreEqual("wx7618c0a6d9358622", encryptPostData.ToUserName); Assert.AreEqual("P/xe9JLq4RJd3AFnjwRsttpyBrTVAGZ49JspjVI65EL7ra73u5TNn3EXHngx1TQ1gnfuFpbRovJdVZ8WrqZ4y0PI9ncA9GR95TboQWK/RGbDpW/Rkq0il1lpw+c/NTk1abwH1C/2siKATSJTbGQ4mWSyhOME7vINBHeW7EjmGEZSaPxC60z1qcLYgMYAiEL/xrU484V6X6BG/jV2uF76+C7HWGMLVmu4DOHVW+UfqQo9SnpAqZx0KRcvT/8XxGUsGwgNWhYyuzUHxu1VuZK16IiHS494tjWrXs08dKQzcpwyID7dthqQDTdIVe0tiOwPAlXvv7jQ5iMtYoQlT32HOjNTn5o/hz9wFZNnC6TFi2Y0ocEWxEMNwDHsyK85ytryTZzL+OmZ7heB72ABNhx9uGhrLoA5M68/ZXwlmfJVx8M=", encryptPostData.Encrypt); Assert.AreEqual(2, encryptPostData.AgentID); } private string xml_Text = @" 1348831860 1234567890123456 1 "; private string xml_Image = @" 1348831860 1234567890123456 1 "; private string xml_Voice = @" 1357290913 1234567890123456 1 "; private string xml_Video = @" 1357290913 1234567890123456 1 "; private string xml_Location = @" 1351776360 23.134521 113.358803 20 1234567890123456 1 "; private string xmlEvent_Location = @" 123456789 23.104105 113.320107 65.000000 1 "; private string xmlEvent_ShortVideo = @" 1357290913 1234567890123456 1 "; private string xmlEvent_Click = @" 123456789 1 "; private string xmlEvent_View = @" 123456789 1 "; private string xmlEvent_Scancode_Push = @" 1408090502 1 "; private string xmlEvent_Scancode_Waitmsg = @" 1408090606 1 "; private string xmlEvent_Pic_Sysphoto = @" 1408090651 1 1 "; private string xmlEvent_Pic_Photo_Or_Album = @" 1408090816 1 1 "; private string xmlEvent_Pic_Weixin = @" 1408090816 1 1 "; private string xmlEvent_Location_Select = @" 1408091189 1 "; private string xmlEvent_Enter_Agent = @" 1408091189 1 "; private string xml_Suite_Ticket = @" 1403610513 "; private string xml_Change_Auth = @" 1403610513 "; private string xml_Cancel_Auth = @" 1403610513 "; private string xml_Batch_Job_Result = @" 1425284517 0 "; [TestMethod] public void GetRequestEntityTest() { { //Text var doc = XDocument.Parse(xml_Text); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageText; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("this is a test", result.Content); Assert.AreEqual(1, result.AgentID); } { //Image var doc = XDocument.Parse(xml_Image); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageImage; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("this is a url", result.PicUrl); Assert.AreEqual(1, result.AgentID); } { //Voice var doc = XDocument.Parse(xml_Voice); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageVoice; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("media_id", result.MediaId); Assert.AreEqual(1, result.AgentID); } { //Video var doc = XDocument.Parse(xml_Video); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageVideo; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("media_id", result.MediaId); Assert.AreEqual(1, result.AgentID); } { //Location var doc = XDocument.Parse(xml_Location); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageLocation; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual(113.358803, result.Location_Y); Assert.AreEqual(1, result.AgentID); } { //ShortVideo var doc = XDocument.Parse(xmlEvent_ShortVideo); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageShortVideo; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("media_id", result.MediaId); Assert.AreEqual(1, result.AgentID); } { //Event_Location var doc = XDocument.Parse(xmlEvent_Location); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Location; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual(113.320107, result.Longitude); Assert.AreEqual(1, result.AgentID); } { //Event_Click var doc = XDocument.Parse(xmlEvent_Click); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Click; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("EVENTKEY", result.EventKey); Assert.AreEqual(1, result.AgentID); } { //Event_View var doc = XDocument.Parse(xmlEvent_View); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_View; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("www.qq.com", result.EventKey); Assert.AreEqual(1, result.AgentID); } { //Event_Scancode_Push var doc = XDocument.Parse(xmlEvent_Scancode_Push); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Scancode_Push; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("qrcode", result.ScanCodeInfo.ScanType); Assert.AreEqual(1, result.AgentID); } { //Event_Scancode_Waitmsg var doc = XDocument.Parse(xmlEvent_Scancode_Waitmsg); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Scancode_Waitmsg; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("qrcode", result.ScanCodeInfo.ScanType); Assert.AreEqual(1, result.AgentID); } { //Event_Pic_Sysphoto var doc = XDocument.Parse(xmlEvent_Pic_Sysphoto); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Pic_Sysphoto; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("1", result.SendPicsInfo.Count); Assert.AreEqual(1, result.AgentID); } { //Event_Pic_Photo_Or_Album var doc = XDocument.Parse(xmlEvent_Pic_Photo_Or_Album); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Pic_Photo_Or_Album; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("5a75aaca956d97be686719218f275c6b", result.SendPicsInfo.PicList[0].item.PicMd5Sum); Assert.AreEqual(1, result.AgentID); } { //Event_Pic_Weixin var doc = XDocument.Parse(xmlEvent_Pic_Weixin); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Pic_Weixin; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual("5a75aaca956d97be686719218f275c6b", result.SendPicsInfo.PicList[0].item.PicMd5Sum); Assert.AreEqual(1, result.AgentID); } { //Event_Location_Select var doc = XDocument.Parse(xmlEvent_Location_Select); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Location_Select; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual(" 广州市海珠区客村艺苑路 106号", result.SendLocationInfo.Label); Assert.AreEqual(1, result.AgentID); } { //Event_Enter_Agent var doc = XDocument.Parse(xmlEvent_Enter_Agent); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Enter_Agent; Assert.IsNotNull(result); Assert.AreEqual("toUser", result.ToUserName); Assert.AreEqual(1, result.AgentID); } { //Suite_Ticket var doc = XDocument.Parse(xml_Suite_Ticket); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageInfo_Suite_Ticket; Assert.IsNotNull(result); Assert.AreEqual("wxfc918a2d200c9a4c", result.SuiteId); Assert.AreEqual("asdfasfdasdfasdf", result.SuiteTicket); } { //Change_Auth var doc = XDocument.Parse(xml_Change_Auth); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageInfo_Change_Auth; Assert.IsNotNull(result); Assert.AreEqual("wxfc918a2d200c9a4c", result.SuiteId); Assert.AreEqual("wxf8b4f85f3a794e77", result.AuthCorpId); } { //Cancel_Auth var doc = XDocument.Parse(xml_Cancel_Auth); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageInfo_Cancel_Auth; Assert.IsNotNull(result); Assert.AreEqual("wxfc918a2d200c9a4c", result.SuiteId); Assert.AreEqual("wxf8b4f85f3a794e77", result.AuthCorpId); } { //Batch_Job_Result var doc = XDocument.Parse(xml_Batch_Job_Result); var result = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultWorkMessageContext(), doc) as RequestMessageEvent_Batch_Job_Result; Assert.IsNotNull(result); Assert.AreEqual("wx28dbb14e37208abe", result.ToUserName); Assert.AreEqual("ok", result.BatchJob.ErrMsg); Assert.AreEqual(0, result.BatchJob.ErrCode); } } } }