public void TestHubGroupMessage(string input, string hub)
        {
            var hubs = new List <string> {
                "hub1", "hub.hub1", "hub.hub1.h.hub2"
            };
            var parser                = new SignalRMessageParser(hubs, _resolver);
            var groupName             = GenerateRandomName();
            var fullName              = PrefixHelper.GetHubGroupName(hub + "." + groupName);
            var message               = SignalRMessageUtility.CreateMessage(fullName, input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as GroupBroadcastDataMessage;

            Assert.NotNull(msg);

            // For group message, it is the full name as the group, e.g. hg-hub.hub1.h.hub2.abcde
            Assert.Equal(fullName, msg.GroupName);
            Assert.Equal <string>(excludedConnectionIds, msg.ExcludedList);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
        public void TestRemoveFromGroupCommandMessage()
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = CommandType.RemoveFromGroup,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as LeaveGroupMessage;

            Assert.NotNull(msg);
            Assert.Equal(connectionId, msg.ConnectionId);
            Assert.Equal(groupName, msg.GroupName);
        }
        public void TestHubUserMessage(string userName, string input, string hub, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "hub1", "hub.hub1", "hub2.hub1.h.hub2", ".", ".."
            };
            var parser = new SignalRMessageParser(hubs, _resolver);

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubUserId(hub + "." + userName), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as UserDataMessage;

            Assert.NotNull(msg);
            Assert.Equal(userName, msg.UserId);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
        public void TestHubConnectionMessage(string connectionId, string input, string expectedId, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "hub", "hub1", "hub.hub1", "h", "hub.hub1.h.hub2", "hub.hub1.h"
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubConnectionId(connectionId), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as ConnectionDataMessage;

            Assert.NotNull(msg);
            Assert.Equal(expectedId, msg.ConnectionId);
            Assert.Equal(input, msg.Payload.First.GetSingleFramePayload());
        }
        public void TestHubMessage(string connectionId, string input, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "h-", "a", "a.connection1"
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubName(connectionId), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as BroadcastDataMessage;

            Assert.NotNull(msg);
            Assert.Equal <string>(excludedConnectionIds, msg.ExcludedList);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
        public void TestGetMessageWithUnknownKeyThrows(string key)
        {
            var hubs    = new List <string> {
            };
            var parser  = new SignalRMessageParser(hubs, _resolver);
            var raw     = "<script type=\"\"></script>";
            var message = new Message("foo", key, new ArraySegment <byte>(Encoding.Default.GetBytes(raw)));

            var result = Assert.Throws <NotSupportedException>(() => parser.GetMessages(message).ToList());
        }
        public void TestAddToGroupCommandMessageWithInvalidKeyThrows(CommandType type, string invalidKey)
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = type,
                Value       = groupName,
                WaitForAck  = true
            };

            var message = SignalRMessageUtility.CreateMessage(invalidKey, command);

            Assert.Throws <InvalidDataException>(() => parser.GetMessages(message).ToList());
        }
Ejemplo n.º 8
0
        private static ServiceHubDispatcher PrepareAndGetDispatcher(HubConfiguration configuration, ServiceOptions options, IServiceEndpointManager endpoint, IEndpointRouter router, string applicationName, IReadOnlyList <string> hubs, ILoggerFactory loggerFactory)
        {
            // TODO: Using IOptions looks wierd, thinking of a way removing it
            // share the same object all through
            var serviceOptions = Options.Create(options);

            // For safety, ALWAYS register abstract classes or interfaces
            // Some third-party DI frameworks such as Ninject, implicit self-binding concrete types:
            // https://github.com/ninject/ninject/wiki/dependency-injection-with-ninject#skipping-the-type-binding-bit--implicit-self-binding-of-concrete-types
            configuration.Resolver.Register(typeof(IOptions <ServiceOptions>), () => serviceOptions);

            var serviceProtocol = new ServiceProtocol();

            configuration.Resolver.Register(typeof(IServiceProtocol), () => serviceProtocol);

            var scm = new ServiceConnectionManager(applicationName, hubs);

            configuration.Resolver.Register(typeof(Microsoft.Azure.SignalR.AspNet.IServiceConnectionManager), () => scm);

            var ccm = new ClientConnectionManager(configuration);

            configuration.Resolver.Register(typeof(IClientConnectionManager), () => ccm);

            var atm = new AzureTransportManager(configuration.Resolver);

            configuration.Resolver.Register(typeof(ITransportManager), () => atm);

            var parser = new SignalRMessageParser(hubs, configuration.Resolver);

            configuration.Resolver.Register(typeof(IMessageParser), () => parser);

            var smb = new ServiceMessageBus(configuration.Resolver);

            configuration.Resolver.Register(typeof(IMessageBus), () => smb);

            if (hubs?.Count > 0)
            {
                return(new ServiceHubDispatcher(hubs, serviceProtocol, scm, ccm, endpoint, router, serviceOptions, loggerFactory));
            }
            else
            {
                loggerFactory.CreateLogger <IAppBuilder>().Log(LogLevel.Warning, "No hubs found.");
                return(null);
            }
        }
Ejemplo n.º 9
0
        private static void RegisterServiceObjects(HubConfiguration configuration, ServiceOptions options, string applicationName, IReadOnlyList <string> hubs)
        {
            // TODO: Using IOptions looks wierd, thinking of a way removing it
            // share the same object all through
            var serviceOptions = Options.Create(options);

            // For safety, ALWAYS register abstract classes or interfaces
            // Some third-party DI frameworks such as Ninject, implicit self-binding concrete types:
            // https://github.com/ninject/ninject/wiki/dependency-injection-with-ninject#skipping-the-type-binding-bit--implicit-self-binding-of-concrete-types
            configuration.Resolver.Register(typeof(IOptions <ServiceOptions>), () => serviceOptions);

            var serviceProtocol = new ServiceProtocol();

            configuration.Resolver.Register(typeof(IServiceProtocol), () => serviceProtocol);

            var provider = new EmptyProtectedData();

            configuration.Resolver.Register(typeof(IProtectedData), () => provider);

            var endpoint = new ServiceEndpointProvider(serviceOptions.Value);

            configuration.Resolver.Register(typeof(IServiceEndpointProvider), () => endpoint);

            var scm = new ServiceConnectionManager(applicationName, hubs);

            configuration.Resolver.Register(typeof(IServiceConnectionManager), () => scm);

            var ccm = new ClientConnectionManager(configuration);

            configuration.Resolver.Register(typeof(IClientConnectionManager), () => ccm);

            var atm = new AzureTransportManager(configuration.Resolver);

            configuration.Resolver.Register(typeof(ITransportManager), () => atm);

            var parser = new SignalRMessageParser(hubs, configuration.Resolver);

            configuration.Resolver.Register(typeof(IMessageParser), () => parser);

            var smb = new ServiceMessageBus(configuration.Resolver);

            configuration.Resolver.Register(typeof(IMessageBus), () => smb);
        }
        public void TestOtherGroupCommandMessagesAreIgnored(CommandType type)
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = type,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Empty(msgs);
        }
        public void TestHubUserMessageWithMultiplePossiblities()
        {
            var hubs = new List <string> {
                "hub", "hub.hub1", "hub.hub1.h.hub2", ".", "......"
            };
            var parser   = new SignalRMessageParser(hubs, _resolver);
            var fullName = "hub.hub1.h.hub2.user1";
            var message  = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubUserId(fullName), null);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Equal(3, msgs.Count);
            var msg = msgs[0].Message as UserDataMessage;

            Assert.NotNull(msg);
            Assert.Equal("h.hub2.user1", msg.UserId);
            msg = msgs[1].Message as UserDataMessage;
            Assert.NotNull(msg);
            Assert.Equal("user1", msg.UserId);
            msg = msgs[2].Message as UserDataMessage;
            Assert.NotNull(msg);
            Assert.Equal("hub1.h.hub2.user1", msg.UserId);
        }
Ejemplo n.º 12
0
        public void TestAddToGroupCommandMessage()
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver, NullLogger <SignalRMessageParser> .Instance);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = CommandType.AddToGroup,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = Assert.IsType <JoinGroupWithAckMessage>(msgs[0].Message);

            Assert.Equal(connectionId, msg.ConnectionId);
            Assert.Equal(groupName, msg.GroupName);
        }