Beispiel #1
0
        public void ParserDoesNotConsumePartialData(byte[] payload)
        {
            var binder = new TestBinder(new[] { typeof(string) }, typeof(string));
            var data   = new ReadOnlySequence <byte>(payload);
            var result = _hubProtocol.TryParseMessage(ref data, binder, out var message);

            Assert.Null(message);
        }
        public void ParserDoesNotConsumePartialData(byte[] payload, int expectedMessagesCount)
        {
            var binder = new TestBinder(new[] { typeof(string) }, typeof(string));
            var result = _hubProtocol.TryParseMessages(payload, binder, out var messages);

            Assert.True(result || messages.Count == 0);
            Assert.Equal(expectedMessagesCount, messages.Count);
        }
        public void CustomInvalidMessages(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder = new TestBinder(Array.Empty<Type>(), typeof(object));
            var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(input));
            var ex = Assert.Throws<InvalidDataException>(() => JsonHubProtocol.TryParseMessage(ref data, binder, out var _));
            Assert.Equal(expectedMessage, ex.Message);
        }
Beispiel #4
0
        protected void TestInvalidMessageDate(InvalidMessageData testData)
        {
            var buffer    = Frame(testData.Encoded);
            var binder    = new TestBinder(new[] { typeof(string) }, typeof(string));
            var data      = new ReadOnlySequence <byte>(buffer);
            var exception = Assert.Throws <InvalidDataException>(() => HubProtocol.TryParseMessage(ref data, binder, out _));

            Assert.Equal(testData.ErrorMessage, exception.Message);
        }
Beispiel #5
0
        public void InvalidMessagesWithBinder(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool));
            var protocol = new JsonHubProtocol();
            var ex       = Assert.Throws <FormatException>(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages));

            Assert.Equal(expectedMessage, ex.Message);
        }
Beispiel #6
0
        public void ReadToEndOfArgumentArrayOnError()
        {
            var binder = new TestBinder(new[] { typeof(string) });
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(Frame("{\"type\":1,\"invocationId\":\"42\",\"target\":\"foo\",\"arguments\":[[],{\"target\":\"foo2\"}]}")));

            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);
            var bindingFailure = Assert.IsType <InvocationBindingFailureMessage>(message);

            Assert.Equal("foo", bindingFailure.Target);
        }
Beispiel #7
0
        public void InvalidNestingWhileBindingTypesFails(string input)
        {
            input = Frame(input);

            var binder = new TestBinder(paramTypes: new[] { typeof(int[]) }, returnType: null);
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));
            var ex     = Assert.Throws <InvalidDataException>(() => JsonHubProtocol.TryParseMessage(ref data, binder, out var message));

            Assert.Equal("Error reading JSON.", ex.Message);
        }
Beispiel #8
0
        public void ExtraItemsInMessageAreIgnored(string input)
        {
            input = Frame(input);

            var binder = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool));
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            Assert.True(JsonHubProtocol.TryParseMessage(ref data, binder, out var message));
            Assert.NotNull(message);
        }
        public void InvalidMessages(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder();
            var protocol = new JsonHubProtocol();
            var ex       = Assert.Throws <InvalidDataException>(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages));

            Assert.Equal(expectedMessage, ex.Message);
        }
        public void ReadToEndOfArgumentArrayOnError()
        {
            var binder   = new TestBinder(new[] { typeof(string) });
            var protocol = new NewtonsoftJsonHubProtocol();
            var data     = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(Frame("{'type':1,'invocationId':'42','target':'foo','arguments':[[],{'target':'foo2'}]}")));

            protocol.TryParseMessage(ref data, binder, out var message);
            var bindingFailure = Assert.IsType <InvocationBindingFailureMessage>(message);

            Assert.Equal("foo", bindingFailure.Target);
        }
Beispiel #11
0
        public void InvalidMessages(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder(Array.Empty <Type>(), typeof(object));
            var protocol = new JsonHubProtocol();
            var messages = new List <HubMessage>();
            var ex       = Assert.Throws <InvalidDataException>(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, messages));

            Assert.Equal(expectedMessage, ex.Message);
        }
Beispiel #12
0
        public void ParserThrowsForInvalidMessages(string invalidPayloadName)
        {
            var testData = InvalidPayloads[invalidPayloadName];

            var buffer    = Frame(testData.Encoded);
            var binder    = new TestBinder(new[] { typeof(string) }, typeof(string));
            var data      = new ReadOnlySequence <byte>(buffer);
            var exception = Assert.Throws <InvalidDataException>(() => _hubProtocol.TryParseMessage(ref data, binder, out _));

            Assert.Equal(testData.ErrorMessage, exception.Message);
        }
        public void MagicCast()
        {
            var input = Frame("{\"type\":1,\"target\":\"Method\",\"arguments\":[1.1]}");
            var expectedMessage = new InvocationMessage("Method", new object[] { 1 });

            var binder = new TestBinder(new[] { typeof(int) });
            var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(input));
            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(expectedMessage, message);
        }
Beispiel #14
0
        public void ArgumentBindingErrors(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool));
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);
            var bindingFailure = Assert.IsType <InvocationBindingFailureMessage>(message);

            Assert.Equal(expectedMessage, bindingFailure.BindingFailure.SourceException.Message);
        }
        public void ArgumentBindingErrors(string input, string expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool));
            var protocol = new JsonHubProtocol();

            protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages);
            var ex = Assert.Throws <InvalidDataException>(() => ((HubMethodInvocationMessage)messages[0]).Arguments);

            Assert.Equal(expectedMessage, ex.Message);
        }
Beispiel #16
0
        public void ParseOutOfOrderJson(string input, HubMessage expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder(expectedMessage);
            var protocol = new JsonHubProtocol();
            var data     = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            protocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(expectedMessage, message, TestHubMessageEqualityComparer.Instance);
        }
Beispiel #17
0
        public void ParseOutOfOrderJson(string input, HubMessage expectedMessage)
        {
            input = Frame(input);

            var binder   = new TestBinder(expectedMessage);
            var protocol = new JsonHubProtocol();
            var messages = new List <HubMessage>();

            protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, messages);

            Assert.Equal(expectedMessage, messages[0], TestHubMessageEqualityComparer.Instance);
        }
Beispiel #18
0
        public void DateTimeReturnValuePreservesUtcKind(string input)
        {
            var binder = new TestBinder(typeof(DateTime));
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(Frame(input)));

            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);
            var invocationMessage = Assert.IsType <CompletionMessage>(message);

            var dt = Assert.IsType <DateTime>(invocationMessage.Result);

            Assert.Equal(DateTimeKind.Utc, dt.Kind);
        }
Beispiel #19
0
        public void CustomParseMessage(string protocolTestDataName)
        {
            var testData = CustomProtocolTestData[protocolTestDataName];

            var input = Frame(testData.Json);

            var binder = new TestBinder(testData.Message);
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(testData.Message, message, TestHubMessageEqualityComparer.Instance);
        }
        public void CustomParseMessage(string protocolTestDataName)
        {
            var testData = CustomProtocolTestData[protocolTestDataName];

            var input = Frame(testData.Json);

            var binder = new TestBinder(testData.Message);
            var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(input));
            var protocol = GetProtocolWithOptions(testData.UseCamelCase, testData.IgnoreNullValues);
            protocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(testData.Message, message, TestHubMessageEqualityComparer.Instance);
        }
Beispiel #21
0
        public void GettingArgumentsThrowsIfBindingFailed(string argumentBindingErrorName)
        {
            var testData = ArgumentBindingErrors[argumentBindingErrorName];

            var buffer = Frame(testData.Encoded);
            var binder = new TestBinder(new[] { typeof(string) }, typeof(string));
            var data   = new ReadOnlySequence <byte>(buffer);

            _hubProtocol.TryParseMessage(ref data, binder, out var message);
            var bindingFailure = Assert.IsType <InvocationBindingFailureMessage>(message);

            Assert.Equal(testData.ErrorMessage, bindingFailure.BindingFailure.SourceException.Message);
        }
Beispiel #22
0
        public void DateTimeArgumentPreservesUtcKind(string input)
        {
            var binder = new TestBinder(new[] { typeof(DateTime) });
            var data   = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(Frame(input)));

            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);
            var invocationMessage = Assert.IsType <InvocationMessage>(message);

            Assert.Single(invocationMessage.Arguments);
            var dt = Assert.IsType <DateTime>(invocationMessage.Arguments[0]);

            Assert.Equal(DateTimeKind.Utc, dt.Kind);
        }
        public void GettingArgumentsThrowsIfBindingFailed(string argumentBindingErrorName)
        {
            var testData = ArgumentBindingErrors[argumentBindingErrorName];

            var buffer = Frame(testData.Encoded);
            var binder = new TestBinder(new[] { typeof(string) }, typeof(string));
            var data   = new ReadOnlySequence <byte>(buffer);

            _hubProtocol.TryParseMessage(ref data, binder, out var message);
            var exception = Assert.Throws <InvalidDataException>(() => ((HubMethodInvocationMessage)message).Arguments);

            Assert.Equal(testData.ErrorMessage, exception.Message);
        }
        public void ParseOutOfOrderJson(string outOfOrderJsonTestDataName)
        {
            var testData = OutOfOrderJsonTestData[outOfOrderJsonTestDataName];

            var input = Frame(testData.Json);

            var binder   = new TestBinder(testData.Message);
            var protocol = new NewtonsoftJsonHubProtocol();
            var data     = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            protocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(testData.Message, message, TestHubMessageEqualityComparer.Instance);
        }
        public void ParserThrowsForInvalidMessages(byte[] payload, string expectedExceptionMessage)
        {
            var payloadSize = payload.Length;

            Debug.Assert(payloadSize <= 0xff, "This test does not support payloads larger than 255");

            // prefix payload with the size
            var buffer = new byte[8 + payloadSize];

            buffer[7] = (byte)(payloadSize & 0xff);
            Array.Copy(payload, 0, buffer, 8, payloadSize);

            var binder    = new TestBinder(new[] { typeof(string) }, typeof(string));
            var exception = Assert.Throws <FormatException>(() => _hubProtocol.TryParseMessages(buffer, binder, out var messages));

            Assert.Equal(expectedExceptionMessage, exception.Message);
        }
Beispiel #26
0
        public void ParseMessage(HubMessage expectedMessage, bool camelCase, NullValueHandling nullValueHandling, string input)
        {
            input = Frame(input);

            var jsonSerializer = new JsonSerializer
            {
                NullValueHandling = nullValueHandling,
                ContractResolver  = camelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver()
            };

            var binder   = new TestBinder(expectedMessage);
            var protocol = new JsonHubProtocol(jsonSerializer);

            protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages);

            Assert.Equal(expectedMessage, messages[0], TestHubMessageEqualityComparer.Instance);
        }
        public void ReadCaseInsensitivePropertiesByDefault()
        {
            var input = Frame("{\"type\":2,\"invocationId\":\"123\",\"item\":{\"StrIngProp\":\"test\",\"DoublePrOp\":3.14159,\"IntProp\":43,\"DateTimeProp\":\"2019-06-03T22:00:00\",\"NuLLProp\":null,\"ByteARRProp\":\"AgQG\"}}");

            var binder = new TestBinder(null, typeof(TemporaryCustomObject));
            var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(input));
            JsonHubProtocol.TryParseMessage(ref data, binder, out var message);

            var streamItemMessage = Assert.IsType<StreamItemMessage>(message);
            Assert.Equal(new TemporaryCustomObject()
            {
                ByteArrProp = new byte[] { 2, 4, 6 },
                IntProp = 43,
                DoubleProp = 3.14159,
                StringProp = "test",
                DateTimeProp = DateTime.Parse("6/3/2019 10:00:00 PM")
            }, streamItemMessage.Item);
        }
Beispiel #28
0
        public void GettingArgumentsThrowsIfBindingFailed(byte[] payload, string expectedExceptionMessage)
        {
            var payloadSize = payload.Length;

            Debug.Assert(payloadSize <= 0x7f, "This test does not support payloads larger than 127 bytes");

            // prefix payload with the size
            var buffer = new byte[1 + payloadSize];

            buffer[0] = (byte)(payloadSize & 0x7f);
            Array.Copy(payload, 0, buffer, 1, payloadSize);

            var binder = new TestBinder(new[] { typeof(string) }, typeof(string));

            _hubProtocol.TryParseMessages(buffer, binder, out var messages);
            var exception = Assert.Throws <FormatException>(() => ((HubMethodInvocationMessage)messages[0]).Arguments);

            Assert.Equal(expectedExceptionMessage, exception.Message);
        }
Beispiel #29
0
        public void ParseMessage(HubMessage expectedMessage, bool camelCase, NullValueHandling nullValueHandling, string input)
        {
            input = Frame(input);

            var protocolOptions = new JsonHubProtocolOptions
            {
                PayloadSerializerSettings = new JsonSerializerSettings
                {
                    NullValueHandling = nullValueHandling,
                    ContractResolver  = camelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver()
                }
            };

            var binder   = new TestBinder(expectedMessage);
            var protocol = new JsonHubProtocol(Options.Create(protocolOptions));
            var messages = new List <HubMessage>();

            protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, messages);

            Assert.Equal(expectedMessage, messages[0], TestHubMessageEqualityComparer.Instance);
        }
        public void ParseMessage(string protocolTestDataName)
        {
            var testData = ProtocolTestData[protocolTestDataName];

            var input = Frame(testData.Json);

            var protocolOptions = new JsonHubProtocolOptions
            {
                PayloadSerializerSettings = new JsonSerializerSettings
                {
                    NullValueHandling = testData.NullValueHandling,
                    ContractResolver  = testData.CamelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver()
                }
            };

            var binder   = new TestBinder(testData.Message);
            var protocol = new JsonHubProtocol(Options.Create(protocolOptions));
            var data     = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input));

            protocol.TryParseMessage(ref data, binder, out var message);

            Assert.Equal(testData.Message, message, TestHubMessageEqualityComparer.Instance);
        }