コード例 #1
0
        public async Task CatchAnExceptionViaMiddlware()
        {
            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            bool caughtException       = false;

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                try
                {
                    await next();
                    Assert.Fail("Should not get here");
                }
                catch (Exception ex)
                {
                    Assert.IsTrue(ex.Message == "test");
                    caughtException = true;
                }
            }));

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                throw new Exception("test");
            }));

            await m.ReceiveActivity(null);

            Assert.IsTrue(caughtException);
        }
コード例 #2
0
        public async Task RunCodeBeforeAndAfter()
        {
            bool didRun1       = false;
            bool codeafter2run = false;
            bool didRun2       = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                Assert.IsFalse(didRun1, "Looks like the 1st middleware has already run");
                didRun1 = true;
                await next();
                Assert.IsTrue(didRun1, "The 2nd middleware should have run now.");
                codeafter2run = true;
            }));

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run");
                Assert.IsFalse(codeafter2run, "The code that runs after middleware 2 is complete has already run.");
                didRun2 = true;
                await next();
            }));

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
            Assert.IsTrue(codeafter2run);
        }
コード例 #3
0
        public async Task Status_TwoItemsOneDoesNotCallNext()
        {
            bool called1 = false;
            bool called2 = false;

            CallMeMiddlware one = new CallMeMiddlware(() =>
            {
                Assert.IsFalse(called2, "Second Middleware was called");
                called1 = true;
            });

            DoNotCallNextMiddleware two = new DoNotCallNextMiddleware(() =>
            {
                Assert.IsTrue(called1, "First Middleware was not called");
                called2 = true;
            });

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);
            m.Use(two);

            bool didAllRun = false;
            await m.ReceiveActivityWithStatus(null, async (ctx) => didAllRun = true);

            Assert.IsTrue(called1);
            Assert.IsTrue(called2);

            // The 2nd middleware did not call next, so the "final" action should not have run.
            Assert.IsFalse(didAllRun);
        }
コード例 #4
0
        public async Task MixedMiddlewareInOrderAnonymousLast()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();

            m.Use(
                new CallMeMiddlware(() =>
            {
                Assert.IsFalse(didRun2, "Second Middleware was called");
                didRun1 = true;
            }));

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run");
                didRun2 = true;
                await next();
            }));

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
コード例 #5
0
        public async Task TwoAnonymousMiddlewareInOrder()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            MiddlewareSet m = new Middleware.MiddlewareSet();

            m.Use(new AnonymousContextCreatedMiddleware(async(context, next) =>
            {
                Assert.IsFalse(didRun2, "Looks like the 2nd one has already run");
                didRun1 = true;
                await next();
            }));
            m.Use(new AnonymousContextCreatedMiddleware(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1nd one has not yet run");
                didRun2 = true;
                await next();
            }));

            await m.ContextCreated(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
コード例 #6
0
        public async Task TwoMiddlewareItemsInOrder()
        {
            bool called1 = false;
            bool called2 = false;

            CallMeMiddlware one = new CallMeMiddlware(() =>
            {
                Assert.IsFalse(called2, "Second Middleware was called");
                called1 = true;
            });

            CallMeMiddlware two = new CallMeMiddlware(() =>
            {
                Assert.IsTrue(called1, "First Middleware was not called");
                called2 = true;
            });

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);
            m.Use(two);

            await m.ReceiveActivity(null);

            Assert.IsTrue(called1);
            Assert.IsTrue(called2);
        }
コード例 #7
0
        public async Task MixedMiddlewareInOrderAnonymousFirst()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();

            m.Use(new AnonymousContextCreatedMiddleware(async(context, next) =>
            {
                Assert.IsFalse(didRun2, "Looks like the 2nd one has already run");
                didRun1 = true;
                await next();
            }));

            m.Use(
                new CallMeMiddlware(() =>
            {
                Assert.IsFalse(didRun2, "Second Middleware was called");
                didRun2 = true;
            }));

            await m.ContextCreated(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
コード例 #8
0
        public async Task TwoMiddlewareItems()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);
            m.Use(two);

            await m.ContextCreated(null);

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
        }
コード例 #9
0
        public async Task TwoMiddlewareItems()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);
            m.Use(two);

            await m.ReceiveActivity(null);

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
        }
コード例 #10
0
        public async Task OneMiddlewareItem()
        {
            WasCalledMiddlware simple = new WasCalledMiddlware();

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(simple);

            Assert.IsFalse(simple.Called);
            await m.ReceiveActivity(null);

            Assert.IsTrue(simple.Called);
        }
コード例 #11
0
        public async Task OneMiddlewareItem()
        {
            WasCalledMiddlware simple = new WasCalledMiddlware();

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(simple);

            Assert.IsFalse(simple.Called);
            await m.ContextCreated(null);

            Assert.IsTrue(simple.Called);
        }
コード例 #12
0
        public async Task BubbleUncaughtException()
        {
            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(new AnonymousContextCreatedMiddleware(async(context, next) =>
            {
                throw new InvalidOperationException("test");
            }));

            await m.ContextCreated(null);

            Assert.Fail("Should never have gotten here");
        }
コード例 #13
0
        public async Task TwoAnonymousMiddleware()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                didRun1 = true;
                await next();
            }));
            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                didRun2 = true;
                await next();
            }));

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
コード例 #14
0
        public async Task Status_OneMiddlewareRan()
        {
            bool called1 = false;

            CallMeMiddlware one = new CallMeMiddlware(() => { called1 = true; });

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);

            // The middlware in this pipeline calls next(), so the resulting
            // status should be TRUE.
            bool didAllRun = await m.ReceiveActivityWithStatus(null);

            Assert.IsTrue(called1);
            Assert.IsTrue(didAllRun);
        }
コード例 #15
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.Use(new AnonymousSendActivityMiddleware(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.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                context.Responses.Add(MessageFactory.Text(replyMessage));
                innerOnReceiveCalled = true;
                await next();
            }));

            inner.Use(new AnonymousContextCreatedMiddleware(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.");
        }
コード例 #16
0
        public async Task Status_OneEntryThatDoesNotCallNext()
        {
            bool called1 = false;

            DoNotCallNextMiddleware one = new DoNotCallNextMiddleware(() => { called1 = true; });

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.Use(one);

            // The middlware in this pipeline DOES NOT call next(), so this must not be called
            bool didAllRun = false;
            await m.ReceiveActivityWithStatus(null, async (ctx) => didAllRun = true);

            Assert.IsTrue(called1);

            // Our "Final" action MUST NOT have been called, as the Middlware Pipeline
            // didn't complete.
            Assert.IsFalse(didAllRun);
        }
コード例 #17
0
 public async Task ThrowOnNullMiddlware()
 {
     Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
     m.Use((Middleware.IMiddleware)null);
 }