public void InvocationMessageParseTest(string protocolName, string invocationId, string target, object[] arguments)
        {
            var                     message  = new AspNetCore.SignalR.Protocol.InvocationMessage(invocationId, target, arguments);
            IHubProtocol            protocol = protocolName == "json" ? (IHubProtocol) new JsonHubProtocol() : new MessagePackHubProtocol();
            var                     bytes    = new ReadOnlySequence <byte>(protocol.GetMessageBytes(message));
            ReadOnlySequence <byte> payload;

            if (protocolName == "json")
            {
                TextMessageParser.TryParseMessage(ref bytes, out payload);
            }
            else
            {
                BinaryMessageParser.TryParseMessage(ref bytes, out payload);
            }
            var serverlessProtocol = protocolName == "json" ? (IServerlessProtocol) new JsonServerlessProtocol() : new MessagePackServerlessProtocol();

            Assert.True(serverlessProtocol.TryParseMessage(ref payload, out var parsedMessage));
            var invocationMessage = (InvocationMessage)parsedMessage;

            Assert.Equal(1, invocationMessage.Type);
            Assert.Equal(invocationId, invocationMessage.InvocationId);
            Assert.Equal(target, invocationMessage.Target);
            var expected = JsonConvert.SerializeObject(arguments);
            var actual   = JsonConvert.SerializeObject(invocationMessage.Arguments);

            Assert.Equal(expected, actual);
        }
예제 #2
0
        public void GlobalSetup()
        {
            switch (HubProtocol)
            {
            case Protocol.MsgPack:
                _hubProtocol = new MessagePackHubProtocol();
                break;

            case Protocol.Json:
                _hubProtocol = new NewtonsoftJsonHubProtocol();
                break;
            }

            switch (Input)
            {
            case Message.NoArguments:
                _hubMessage = new InvocationMessage("Target", Array.Empty <object>());
                break;

            case Message.FewArguments:
                _hubMessage = new InvocationMessage("Target", new object[] { 1, "Foo", 2.0f });
                break;

            case Message.ManyArguments:
                _hubMessage = new InvocationMessage("Target", new object[] { 1, "string", 2.0f, true, (byte)9, new byte[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L });
                break;

            case Message.LargeArguments:
                _hubMessage = new InvocationMessage("Target", new object[] { new string('F', 10240), new byte[10240] });
                break;
            }

            _binaryInput = _hubProtocol.GetMessageBytes(_hubMessage);
            _binder      = new TestBinder(_hubMessage);
        }
예제 #3
0
        public async Task <string> SendHubMessageAsync(HubMessage message)
        {
            var payload = _protocol.GetMessageBytes(message);

            await Connection.Application.Output.WriteAsync(payload);

            return(message is HubInvocationMessage hubMessage ? hubMessage.InvocationId : null);
        }
예제 #4
0
        public void WriteSingleMessage()
        {
            var bytes = _hubProtocol.GetMessageBytes(_hubMessage);

            if (bytes.Length != _binaryInput.Length)
            {
                throw new InvalidOperationException("Failed to write message");
            }
        }
예제 #5
0
        private static bool TryGetPayload(IHubProtocol protocol, InvocationMessage invocationMessage, out ReadOnlySequence <byte> payload)
        {
            var buffer = new ReadOnlySequence <byte>(protocol.GetMessageBytes(invocationMessage));

            if (protocol is JsonHubProtocol)
            {
                return(TextMessageParser.TryParseMessage(ref buffer, out payload));
            }
            else if (protocol is MessagePackHubProtocol)
            {
                return(BinaryMessageParser.TryParseMessage(ref buffer, out payload));
            }

            throw new ArgumentException($"{protocol.GetType()} is not supported");
        }
예제 #6
0
        public ReadOnlyMemory <byte> GetSerializedMessage(IHubProtocol protocol)
        {
            if (!TryGetCached(protocol.Name, out var serialized))
            {
                if (Message == null)
                {
                    throw new InvalidOperationException(
                              "This message was received from another server that did not have the requested protocol available.");
                }

                serialized = protocol.GetMessageBytes(Message);
                SetCache(protocol.Name, serialized);
            }

            return(serialized);
        }
        public async Task SignalRInvocationMethodExecutorTest(string protocolName)
        {
            var resolver       = new SignalRRequestResolver(false);
            var methodExecutor = new SignalRInvocationMethodExecutor(resolver, new ExecutionContext {
                Executor = _triggeredFunctionExecutor
            });
            var hub          = Guid.NewGuid().ToString();
            var category     = Guid.NewGuid().ToString();
            var @event       = Guid.NewGuid().ToString();
            var connectionId = Guid.NewGuid().ToString();
            var arguments    = new object[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };

            var                     message     = new Microsoft.AspNetCore.SignalR.Protocol.InvocationMessage(Guid.NewGuid().ToString(), @event, arguments);
            IHubProtocol            protocol    = protocolName == "json" ? (IHubProtocol) new JsonHubProtocol() : new MessagePackHubProtocol();
            var                     contentType = protocolName == "json" ? Constants.JsonContentType : Constants.MessagePackContentType;
            var                     bytes       = new ReadOnlySequence <byte>(protocol.GetMessageBytes(message));
            ReadOnlySequence <byte> payload;

            if (protocolName == "json")
            {
                TextMessageParser.TryParseMessage(ref bytes, out payload);
            }
            else
            {
                BinaryMessageParser.TryParseMessage(ref bytes, out payload);
            }

            var request = TestHelpers.CreateHttpRequestMessage(hub, category, @event, connectionId, contentType: contentType, content: payload.ToArray());
            await methodExecutor.ExecuteAsync(request);

            var result      = await _triggeredFunctionDataTcs.Task;
            var triggerData = (SignalRTriggerEvent)result.TriggerValue;

            Assert.NotNull(triggerData.TaskCompletionSource);
            Assert.Equal(hub, triggerData.Context.Hub);
            Assert.Equal(category, triggerData.Context.Category);
            Assert.Equal(@event, triggerData.Context.Event);
            Assert.Equal(connectionId, triggerData.Context.ConnectionId);
            Assert.Equal(hub, triggerData.Context.Hub);
            Assert.Equal(arguments, triggerData.Context.Arguments);
        }
예제 #8
0
 /// <summary>
 /// 向服务器发送 HubMessage 消息
 /// </summary>
 /// <param name="message"></param>
 internal void SendHubMessage(HubMessage message)
 {
     _connection.Transport.SendMessage(_protocol.GetMessageBytes(message));
     UpdateNextActivationTime();
 }