예제 #1
0
        public async Task InterceptAndMutateOnSend()
        {
            bool foundIt = false;

            void ValidateResponses(Activity[] activities)
            {
                Assert.IsNotNull(activities);
                Assert.IsTrue(activities.Length == 1);
                Assert.IsTrue(activities[0].Id == "changed");
                foundIt = true;
            }

            SimpleAdapter a = new SimpleAdapter(ValidateResponses);
            TurnContext   c = new TurnContext(a, new Activity());

            c.OnSendActivities(async(context, activities, next) =>
            {
                Assert.IsNotNull(activities, "Null Array passed in");
                Assert.IsTrue(activities.Count() == 1);
                Assert.IsTrue(activities[0].Id == "1234", "Unknown Id Passed In");
                activities[0].Id = "changed";
                return(await next());
            });

            await c.SendActivity(TestMessage.Message());

            // Intercepted the message, changed it, and sent it on to the Adapter
            Assert.IsTrue(foundIt);
        }
예제 #2
0
        public async Task AllowInterceptionOfDeliveryOnSend()
        {
            bool responsesSent = false;

            void ValidateResponses(Activity[] activities)
            {
                responsesSent = true;
                Assert.Fail("Should not be called. Interceptor did not work");
            }

            SimpleAdapter a = new SimpleAdapter(ValidateResponses);
            TurnContext   c = new TurnContext(a, new Activity());

            int count = 0;

            c.OnSendActivities(async(context, activities, next) =>
            {
                Assert.IsNotNull(activities, "Null Array passed in");
                count = activities.Count();
                // Do not call next.
                return(null);
            });

            await c.SendActivity(TestMessage.Message());

            Assert.IsTrue(count == 1);
            Assert.IsFalse(responsesSent, "Responses made it to the adapter.");
        }
예제 #3
0
        public async Task AllowInterceptionOfDeliveryOnSend()
        {
            bool responsesSent = false;

            void ValidateResponses(Activity[] activities)
            {
                responsesSent = true;
                Assert.True(false); // Should not be called. Interceptor did not work
            }

            var a = new SimpleAdapter(ValidateResponses);
            var c = new TurnContext(a, new Activity());

            int count = 0;

            c.OnSendActivities((context, activities, next) =>
            {
                Assert.NotNull(activities); // Null Array passed in
                count = activities.Count();

                // Do not call next.
                return(Task.FromResult <ResourceResponse[]>(null));
            });

            await c.SendActivityAsync(TestMessage.Message());

            Assert.True(count == 1);
            Assert.False(responsesSent, "Responses made it to the adapter.");
        }
예제 #4
0
        public void TestTurnContextClone()
        {
            var c1 = new TurnContext(new SimpleAdapter(), new Activity()
            {
                Text = "one"
            });

            c1.TurnState.Add("x", "test");
            c1.OnSendActivities((context, activities, next) => next());
            c1.OnDeleteActivity((context, activity, next) => next());
            c1.OnUpdateActivity((context, activity, next) => next());
            var c2 = new TurnContext(c1, new Activity()
            {
                Text = "two"
            });

            Assert.Equal("one", c1.Activity.Text);
            Assert.Equal("two", c2.Activity.Text);
            Assert.Equal(c1.Adapter, c2.Adapter);
            Assert.Equal(c1.TurnState, c2.TurnState);

            var binding       = BindingFlags.Instance | BindingFlags.NonPublic;
            var onSendField   = typeof(TurnContext).GetField("_onSendActivities", binding);
            var onDeleteField = typeof(TurnContext).GetField("_onDeleteActivity", binding);
            var onUpdateField = typeof(TurnContext).GetField("_onUpdateActivity", binding);

            Assert.Equal(onSendField.GetValue(c1), onSendField.GetValue(c2));
            Assert.Equal(onDeleteField.GetValue(c1), onDeleteField.GetValue(c2));
            Assert.Equal(onUpdateField.GetValue(c1), onUpdateField.GetValue(c2));
        }
예제 #5
0
        public async Task CallOnSendBeforeDelivery()
        {
            SimpleAdapter a = new SimpleAdapter();
            TurnContext   c = new TurnContext(a, new Activity());

            int count = 0;

            c.OnSendActivities(async(context, activities, next) =>
            {
                Assert.IsNotNull(activities, "Null Array passed in");
                count = activities.Count();
                return(await next());
            });

            await c.SendActivity(TestMessage.Message());

            Assert.IsTrue(count == 1);
        }
예제 #6
0
        public async Task ThrowExceptionInOnSend()
        {
            SimpleAdapter a = new SimpleAdapter();
            TurnContext   c = new TurnContext(a, new Activity());

            c.OnSendActivities(async(context, activities, next) =>
            {
                throw new Exception("test");
            });

            try
            {
                await c.SendActivity(TestMessage.Message());

                Assert.Fail("Should not get here");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message == "test");
            }
        }
예제 #7
0
        public async Task ThrowExceptionInOnSend()
        {
            var a = new SimpleAdapter();
            var c = new TurnContext(a, new Activity());

            c.OnSendActivities((context, activities, next) =>
            {
                throw new Exception("test");
            });

            try
            {
                await c.SendActivityAsync(TestMessage.Message());

                Assert.True(false); // Should not get here
            }
            catch (Exception ex)
            {
                Assert.True(ex.Message == "test");
            }
        }