Esempio n. 1
0
        public async Task BubbleUncaughtException()
        {
            MiddlewareSet m = new MiddlewareSet();

            m.OnSendActivity(async(context, activities, next) =>
            {
                throw new InvalidOperationException("test");
            });

            await m.SendActivity(null, new List <IActivity>());

            Assert.Fail("Should never have gotten here");
        }
Esempio n. 2
0
        public async Task NestedSet_AllIsRun()
        {
            bool   innerOnSendCalled    = false;
            bool   innerOnReceiveCalled = false;
            bool   innerOnCreatedCalled = false;
            string replyMessage         = Guid.NewGuid().ToString();

            MiddlewareSet inner = new MiddlewareSet();

            inner.OnSendActivity(async(context, activities, next) =>
            {
                Assert.IsTrue(activities.Count == 1, "incorrect activity count");
                Assert.IsTrue(activities[0].AsMessageActivity().Text == replyMessage, "unexpected message");

                innerOnSendCalled = true;
                await next();
            });

            inner.OnReceive(async(context, next) =>
            {
                context.Responses.Add(MessageFactory.Text(replyMessage));
                innerOnReceiveCalled = true;
                await next();
            });

            inner.OnContextCreated(async(context, next) =>
            {
                innerOnCreatedCalled = true;
                await next();
            });

            Middleware.MiddlewareSet outer = new Middleware.MiddlewareSet();
            outer.Use(inner);

            IBotContext c = TestUtilities.CreateEmptyContext();
            await outer.ContextCreated(c);

            await outer.ReceiveActivity(c);

            await outer.SendActivity(c, c.Responses);

            Assert.IsTrue(innerOnReceiveCalled, "Inner Middleware Receive Activity was not called.");
            Assert.IsTrue(innerOnCreatedCalled, "Inner Middleware Create Context was not called.");
            Assert.IsTrue(innerOnSendCalled, "Inner Middleware SendActivity was not called.");
        }
Esempio n. 3
0
        public async Task NestedSet_OnPostActivity()
        {
            bool innerOnSendCalled = false;

            MiddlewareSet inner = new MiddlewareSet();

            inner.OnSendActivity(async(context, activities, next) =>
            {
                innerOnSendCalled = true;
                await next();
            });

            MiddlewareSet outer = new MiddlewareSet();

            outer.Use(inner);

            await outer.SendActivity(null, new List <IActivity>());

            Assert.IsTrue(innerOnSendCalled, "Inner Middleware SendActivity was not called.");
        }
Esempio n. 4
0
        public async Task ActivityListPassedThrough()
        {
            bool   didRun  = false;
            string message = Guid.NewGuid().ToString();

            MiddlewareSet m = new MiddlewareSet();

            m.OnSendActivity(async(context, activities, next) =>
            {
                Assert.IsTrue(activities.Count == 1);
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message);

                didRun = true;
                await next();
            });

            Assert.IsFalse(didRun);
            await m.SendActivity(null, new List <IActivity> {
                MessageFactory.Text(message)
            });

            Assert.IsTrue(didRun);
        }
Esempio n. 5
0
        public async Task ManiuplateActivityList()
        {
            bool didRun1 = false;
            bool didRun2 = false;
            bool didRun3 = false;

            string message1 = Guid.NewGuid().ToString();
            string message2 = Guid.NewGuid().ToString();
            string message3 = Guid.NewGuid().ToString();

            MiddlewareSet m = new MiddlewareSet();

            m.OnSendActivity(async(context, activities, next) =>
            {
                Assert.IsTrue(activities.Count == 1);
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message1);
                activities.Add(MessageFactory.Text(message2));
                didRun1 = true;

                await next();

                // After the next 2 middleware elements fire, the list has been changed.
                // As the "parent" this method can see those changes.
                Assert.IsTrue(activities.Count == 2);
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message3);
                Assert.IsTrue(activities[1].AsMessageActivity().Text == message2);
            });
            m.OnSendActivity(async(context, activities, next) =>
            {
                Assert.IsTrue(activities.Count == 2);
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message1);
                Assert.IsTrue(activities[1].AsMessageActivity().Text == message2);

                // change the text of the 1st message and verify it's passed through
                activities[0].AsMessageActivity().Text = message3;
                didRun2 = true;

                await next();

                // After the "3" middleware runs, it's manipulated the messages.
                // Verify this middleware can see them.
                Assert.IsTrue(activities.Count == 2);
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message3);
                Assert.IsTrue(activities[1].AsMessageActivity().Text == message2);
            });

            m.OnSendActivity(async(context, activities, next) =>
            {
                Assert.IsTrue(activities.Count == 2);

                // "Message 0" was previously maniupulated to have it's text changed.
                Assert.IsTrue(activities[0].AsMessageActivity().Text == message3);
                Assert.IsTrue(activities[1].AsMessageActivity().Text == message2);
                didRun3 = true;
                await next();
            });

            await m.SendActivity(null, new List <IActivity> {
                MessageFactory.Text(message1)
            });

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
            Assert.IsTrue(didRun3);
        }