public async Task MixedMiddlewareInOrderAnonymousFirst()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            MiddlewareSet m = new MiddlewareSet();

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                Assert.IsFalse(didRun1, "First middleware already ran");
                Assert.IsFalse(didRun2, "Looks like the second middleware was already run");
                didRun1 = true;
                await next();
                Assert.IsTrue(didRun2, "Second middleware should have completed running");
            }));

            m.Use(
                new CallMeMiddlware(() =>
            {
                Assert.IsTrue(didRun1, "First middleware should have already been called");
                Assert.IsFalse(didRun2, "Second middleware should not have been invoked yet");
                didRun2 = true;
            }));

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
        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;
            });

            MiddlewareSet m = new 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);
        }
        public async Task TwoAnonymousMiddlewareInOrder()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            MiddlewareSet m = new MiddlewareSet();

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

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
        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;
            });

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            await m.ReceiveActivity(null);

            Assert.IsTrue(called1);
            Assert.IsTrue(called2);
        }
Beispiel #5
0
        public async Task TwoMiddlewareItems()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            await m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken));

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
        }
        public async Task TwoMiddlewareItems()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            await m.ReceiveActivity(null);

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
        }
Beispiel #7
0
        public async Task Status_OneEntryThatDoesNotCallNext()
        {
            bool called1 = false;

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

            var m = new MiddlewareSet();

            m.Use(one);

            // The middlware in this pipeline DOES NOT call next(), so this must not be called
            bool didAllRun = false;
            await m.ReceiveActivityWithStatusAsync(
                null,
                (ctx, cancellationToken) =>
            {
                didAllRun = true;
                return(Task.CompletedTask);
            },
                default(CancellationToken));

            Assert.IsTrue(called1);

            // Our "Final" action MUST NOT have been called, as the Middlware Pipeline
            // didn't complete.
            Assert.IsFalse(didAllRun);
        }
Beispiel #8
0
        public static MiddlewareSet Filter(Func <IBotContext, bool> filterPredicate, params IMiddleware[] middlewareList)
        {
            var middlewareSet = new MiddlewareSet();

            foreach (var middleware in middlewareList)
            {
                Func <IBotContext, MiddlewareSet.NextDelegate, Task> contextCreated = (context, next) =>
                {
                    var testPredicate = (filterPredicate(context) && ((IContextCreated)middleware).ContextCreated(context, next) != null);
                    return(testPredicate ? ((IContextCreated)middleware).ContextCreated(context, next) : next());
                };

                Func <IBotContext, MiddlewareSet.NextDelegate, Task> receiveActivity = (context, next) =>
                {
                    var testPredicate = (filterPredicate(context) && ((IReceiveActivity)middleware).ReceiveActivity(context, next) != null);
                    return(testPredicate ? ((IReceiveActivity)middleware).ReceiveActivity(context, next) : next());
                };

                Func <IBotContext, IList <IActivity>, MiddlewareSet.NextDelegate, Task> sendActivity = (context, activities, next) =>
                {
                    var testPredicate = (filterPredicate(context) && ((ISendActivity)middleware).SendActivity(context, activities, next) != null);
                    return(testPredicate ? ((ISendActivity)middleware).SendActivity(context, activities, next) : next());
                };

                middlewareSet.Use(new Middleware {
                    _contextCreated  = contextCreated,
                    _receiveActivity = receiveActivity,
                    _sendActivity    = sendActivity
                });
            }

            return(middlewareSet);
        }
Beispiel #9
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.");
        }
Beispiel #10
0
        public async Task CatchAnExceptionViaMiddlware()
        {
            var  m = new MiddlewareSet();
            bool caughtException = false;

            m.Use(new AnonymousReceiveMiddleware(async(context, next, cancellationToken) =>
            {
                var message = await Assert.ThrowsAsync <Exception>(() => next(cancellationToken));
                Assert.Equal("test", message.Message);
                caughtException = true;
            }));

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

            await m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken));

            Assert.True(caughtException);
        }
Beispiel #11
0
        public async Task BubbleUncaughtException()
        {
            MiddlewareSet m = new MiddlewareSet();

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

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                                 m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken)));
        }
Beispiel #12
0
        public async Task OneMiddlewareItem()
        {
            WasCalledMiddlware simple = new WasCalledMiddlware();
            MiddlewareSet      m      = new MiddlewareSet();

            m.Use(simple);

            Assert.IsFalse(simple.Called);
            await m.SendActivity(null, new List <IActivity>());

            Assert.IsTrue(simple.Called);
        }
        public async Task OneMiddlewareItemWithDelegate()
        {
            WasCalledMiddlware simple = new WasCalledMiddlware();

            MiddlewareSet m = new MiddlewareSet();

            m.Use(simple);

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

            Assert.IsTrue(simple.Called);
        }
Beispiel #14
0
        public async Task OneMiddlewareItemWithDelegate()
        {
            WasCalledMiddlware simple = new WasCalledMiddlware();

            MiddlewareSet m = new MiddlewareSet();

            m.Use(simple);

            Assert.IsFalse(simple.Called);
            await m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken));

            Assert.IsTrue(simple.Called);
        }
Beispiel #15
0
        public async Task BubbleUncaughtException()
        {
            MiddlewareSet m = new MiddlewareSet();

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

            await m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken));

            Assert.Fail("Should never have gotten here");
        }
        public async Task BubbleUncaughtException()
        {
            MiddlewareSet m = new MiddlewareSet();

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

            await m.ReceiveActivity(null);

            Assert.Fail("Should never have gotten here");
        }
Beispiel #17
0
        public async Task Status_TwoItemsOneDoesNotCallNext()
        {
            bool called1 = false;
            bool called2 = false;

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

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

            var m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            bool didAllRun = false;
            await m.ReceiveActivityWithStatusAsync(
                null,
                (ctx, cancellationToken) =>
            {
                didAllRun = true;
                return(Task.CompletedTask);
            },
                default(CancellationToken));

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

            // The 2nd middleware did not call next, so the "final" action should not have run.
            Assert.IsFalse(didAllRun);
        }
Beispiel #18
0
        public AdapterWithErrorHandler(IConfiguration configuration, LuisRecognizerMiddleware luisRecognizerMiddleware, ILogger <BotFrameworkHttpAdapter> logger)
            : base(configuration, logger)
        {
            MiddlewareSet.Use(luisRecognizerMiddleware);

            OnTurnError = async(turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                logger.LogError($"Exception caught : {exception.Message}");

                // Send a catch-all apology to the user.
                await turnContext.SendActivityAsync("Se ha producido un error.");
            };
        }
Beispiel #19
0
        public async Task TwoAnonymousMiddleware()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            MiddlewareSet m = new MiddlewareSet();

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

            await m.ReceiveActivityWithStatusAsync(null, null, default(CancellationToken));

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
Beispiel #20
0
        public async Task BubbleUncaughtException()
        {
            MiddlewareSet m = new MiddlewareSet();

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

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

            Assert.Fail("Should never have gotten here");
        }
Beispiel #21
0
        public async Task TwoMiddlewareItemsWithDelegate()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            int called = 0;

            async Task CallMe(ITurnContext context)
            {
                called++;
            }

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            await m.ReceiveActivityWithStatus(null, CallMe);

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
            Assert.IsTrue(called == 1, "Incorrect number of calls to Delegate");
        }
        public async Task TwoAnonymousMiddleware()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            MiddlewareSet m = new 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);
        }
Beispiel #23
0
        public async Task TwoMiddlewareItemsWithDelegate()
        {
            WasCalledMiddlware one = new WasCalledMiddlware();
            WasCalledMiddlware two = new WasCalledMiddlware();

            int called = 0;

            Task CallMe(ITurnContext turnContext, CancellationToken cancellationToken)
            {
                called++;
                return(Task.CompletedTask);
            }

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);
            m.Use(two);

            await m.ReceiveActivityWithStatusAsync(null, CallMe, default(CancellationToken));

            Assert.IsTrue(one.Called);
            Assert.IsTrue(two.Called);
            Assert.IsTrue(called == 1, "Incorrect number of calls to Delegate");
        }
        public async Task AnonymousMiddleware()
        {
            bool didRun = false;

            var m = new MiddlewareSet();

            m.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                didRun = true;
                await next();
            }));

            Assert.IsFalse(didRun);
            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun);
        }
Beispiel #25
0
        public async Task Status_OneMiddlewareRan()
        {
            bool called1 = false;

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

            MiddlewareSet m = new MiddlewareSet();

            m.Use(one);

            // The middlware in this pipeline calls next(), so the delegate should be called
            bool didAllRun = false;
            await m.ReceiveActivityWithStatus(null, async (ctx) => didAllRun = true);

            Assert.IsTrue(called1);
            Assert.IsTrue(didAllRun);
        }
        public async Task AnonymousMiddleware()
        {
            bool didRun = false;

            MiddlewareSet m = new MiddlewareSet();

            m.Use(new AnonymousContextCreatedMiddleware(async(context, next) =>
            {
                didRun = true;
                await next();
            }));

            Assert.IsFalse(didRun);
            await m.ContextCreated(null);

            Assert.IsTrue(didRun);
        }
        public async Task AnonymousMiddleware()
        {
            bool didRun = false;

            var m = new MiddlewareSet();

            m.Use(new AnonymousReceiveMiddleware(async(context, next, cancellationToken) =>
            {
                didRun = true;
                await next(cancellationToken);
            }));

            Assert.IsFalse(didRun);
            await m.ReceiveActivityAsync(null, default(CancellationToken));

            Assert.IsTrue(didRun);
        }
Beispiel #28
0
        public async Task NestedSet_OnContextCreated()
        {
            bool innerOnCreatedCalled = false;

            MiddlewareSet inner = new MiddlewareSet();

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

            MiddlewareSet outer = new MiddlewareSet();

            outer.Use(inner);

            await outer.ContextCreated(null);

            Assert.IsTrue(innerOnCreatedCalled, "Inner Middleware ContextCreated was not called.");
        }
        public async Task NestedSet_OnReceive()
        {
            bool innerOnReceiveCalled = false;

            MiddlewareSet inner = new MiddlewareSet();

            inner.Use(new AnonymousReceiveMiddleware(async(context, next) =>
            {
                innerOnReceiveCalled = true;
                await next();
            }));

            MiddlewareSet outer = new MiddlewareSet();

            outer.Use(inner);

            await outer.ReceiveActivity(null);

            Assert.IsTrue(innerOnReceiveCalled, "Inner Middleware Receive was not called.");
        }
Beispiel #30
0
        public async Task NestedSet_OnPostActivity()
        {
            bool innerOnSendCalled = false;

            MiddlewareSet inner = new MiddlewareSet();

            inner.Use(new AnonymousSendActivityMiddleware(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.");
        }