Example #1
0
        public void Filter_PushUpdateIntercepted()
        {
            var vm = new FilterTestVM();

            var hubEmulator = new HubEmulatorBuilder()
                              .Register(nameof(FilterTestVM), vm)
                              .UseMiddleware <JwtBearerAuthenticationMiddleware>(_tokenValidationParameters)
                              .UseFilter <AuthorizeFilter>()
                              .UseFilter <CustomFilter1>()
                              .UseFilter <CustomFilter2>()
                              .Build();

            var client = hubEmulator.CreateClient();

            Action filter1Assertions = null;

            CustomFilter1.Invoked += (sender, tuple) =>
            {
                if (tuple.Item2.HubContext.CallType != "Response_VM")
                {
                    return;
                }

                var attr          = tuple.Item1;
                var vmContext     = tuple.Item2;
                var context       = vmContext.HubContext;
                var connectionId  = context.CallerContext.ConnectionId;
                var authToken     = (context.Headers as JObject)["Authorization"].ToString();
                var principalName = context.Principal.Identity.Name;
                var pipelineData  = new Dictionary <string, object>(context.PipelineData);

                filter1Assertions = () =>
                {
                    Assert.AreEqual(client.ConnectionId, connectionId);
                    Assert.IsTrue(authToken.StartsWith("Bearer "));
                    Assert.AreEqual("admin", principalName);
                    Assert.AreEqual(0, pipelineData.Count);
                    Assert.AreEqual(vm, vmContext.Instance);
                    Assert.AreEqual("Welcome", attr.Property);
                };
            };

            client.Connect(nameof(FilterTestVM), _vmConnectOptions);
            vm.TriggerProperty = true;
            vm.PushUpdates();

            filter1Assertions();
        }
Example #2
0
        public void Filter_PushUpdateIntercepted()
        {
            VMController.Register <FilterTestVM>();
            var hub = new MockDotNetifyHub()
                      .UseMiddleware <ExtractHeadersMiddleware>()
                      .UseMiddleware <JwtBearerAuthenticationMiddleware>()
                      .UseFilter <AuthorizeFilter>()
                      .UseFilter <CustomFilter1>()
                      .UseFilter <CustomFilter2>()
                      .Create();

            Action filter1Assertions = null;

            _filter1.Invoked += (sender, tuple) =>
            {
                if (tuple.Item2.HubContext.CallType != "Response_VM")
                {
                    return;
                }

                var attr          = tuple.Item1;
                var vmContext     = tuple.Item2;
                var context       = vmContext.HubContext;
                var connectionId  = context.CallerContext.ConnectionId;
                var authToken     = (context.Headers as JObject)["Authorization"].ToString();
                var principalName = context.Principal.Identity.Name;
                var pipelineData  = new Dictionary <string, object>(context.PipelineData);

                filter1Assertions = () =>
                {
                    Assert.AreEqual(hub.ConnectionId, connectionId);
                    Assert.IsTrue(authToken.StartsWith("Bearer "));
                    Assert.AreEqual("admin", principalName);
                    Assert.AreEqual(0, pipelineData.Count);
                    Assert.AreEqual(_vm, vmContext.Instance);
                    Assert.AreEqual("Welcome", attr.Property);
                };
            };

            hub.RequestVM(nameof(FilterTestVM), _vmArg);
            _vm.TriggerProperty = true;
            _vm.PushUpdates();

            filter1Assertions();
        }