Exemplo n.º 1
0
        public async void TestInterpreting()
        {
            var moqChannel = new Mock <ILubalaChannel>();

            moqChannel.SetupGet(c => c.Resolver).Returns(TypeResolver.Resolver);

            var builder = new HubBuilder(moqChannel.Object);

            var moqPassive = new Mock <IPassiveMessage>();

            moqPassive
            .Setup(x => x.SerializeTo(It.IsAny <Stream>(), It.IsAny <HubContext>()))
            .Returns(new Task(() => { }));
            var handler = new Mock <IMessageHandler>();

            handler
            .Setup(x => x.HandleMessage(It.IsAny <WechatPushingMessage>(), It.IsAny <MessageContext>()))
            .Returns(moqPassive.Object);

            builder.RegisterMessageType <TestTextMessage>();
            builder.RegisterMessageHandler(typeof(TestTextMessage), handler.Object);

            var ph = builder.BuildPushingHub();

            using (var t = StringStream.Create(testXml))
            {
                using (var fakeTargetStream = new MemoryStream())
                {
                    await ph.InterpretingAsync(t.Stream, fakeTargetStream, null);

                    fakeTargetStream.Position = 0;
                    using (var reader = new StreamReader(fakeTargetStream))
                    {
                        var result = reader.ReadToEnd();
                        Assert.Equal(result, "");
                    }
                    moqPassive.Verify(x => x.SerializeTo(It.IsAny <Stream>(), It.IsAny <HubContext>()), Times.Never);
                }
            }
        }
        public void TestTypeParseCorrectly()
        {
            var testXml = @"
<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>";

            using (var t = StringStream.Create(testXml))
            {
                var xml      = XDocument.Load(t.Stream);
                var detector = new TypeDetector();
                var type     = detector.Detecting(xml);

                Assert.Equal(type.MsgType, "text");
                Assert.Equal(type.EventType, null);
            }
        }