Пример #1
0
        public void DeserializeUsing_Serializer_SerializerSet()
        {
            var builder    = new TestConsumerEndpointBuilder();
            var serializer = new BinaryFileMessageSerializer();

            var endpoint = builder.DeserializeUsing(serializer).Build();

            endpoint.Serializer.Should().BeSameAs(serializer);
        }
        public async Task SerializeAsync_StreamWithHardcodedType_ReturnedUnmodified()
        {
            var messageStream = new MemoryStream(Encoding.UTF8.GetBytes("test"));

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();

            var serialized = await serializer.SerializeAsync(
                messageStream,
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            serialized.Should().BeSameAs(messageStream);
        }
        public async Task DeserializeAsync_EmptyMessageWithHardcodedType_CustomBinaryFileReturned()
        {
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();

            var(deserializedObject, type) = await serializer
                                            .DeserializeAsync(new MemoryStream(), headers, MessageSerializationContext.Empty);

            deserializedObject.Should().BeOfType <InheritedBinaryFileMessage>();
            deserializedObject.As <BinaryFileMessage>().Content.ReadAll() !.Length.Should().Be(0);
            type.Should().Be(typeof(InheritedBinaryFileMessage));
        }
        public async Task SerializeAsync_ByteArrayWithHardcodedType_ReturnedUnmodified()
        {
            var messageBytes = Encoding.UTF8.GetBytes("test");

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();

            var serialized = await serializer.SerializeAsync(
                messageBytes,
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            serialized.ReadAll().Should().BeEquivalentTo(messageBytes);
        }
        public async Task DeserializeAsync_NullMessageWithHardcodedType_CustomBinaryFileReturned()
        {
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();

            var(deserializedObject, type) = await serializer
                                            .DeserializeAsync(null, headers, MessageSerializationContext.Empty);

            deserializedObject.Should().BeOfType <InheritedBinaryFileMessage>();
            deserializedObject.Should().BeEquivalentTo(
                new InheritedBinaryFileMessage
            {
                Content = null
            });
            type.Should().Be(typeof(InheritedBinaryFileMessage));
        }
        public async Task SerializeAsync_Message_TypeHeaderAdded()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer();

            await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var typeHeaderValue = headers["x-message-type"];

            typeHeaderValue.Should().NotBeNullOrEmpty();
            typeHeaderValue.Should()
            .StartWith("Silverback.Messaging.Messages.BinaryFileMessage, Silverback.Integration,");
        }
        public async Task DeserializeAsync_WithHardcodedType_CustomBinaryFileReturned()
        {
            var rawContent = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 });
            var headers    = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();

            var(deserializedObject, type) = await serializer
                                            .DeserializeAsync(rawContent, headers, MessageSerializationContext.Empty);

            deserializedObject.Should().BeOfType <InheritedBinaryFileMessage>();
            deserializedObject.Should().BeEquivalentTo(
                new InheritedBinaryFileMessage
            {
                Content = rawContent
            });
            type.Should().Be(typeof(InheritedBinaryFileMessage));
        }
        public async Task SerializeDeserializeAsync_HardcodedType_CorrectlyDeserialized()
        {
            var message = new InheritedBinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer <InheritedBinaryFileMessage>();
            var serialized = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var(deserialized, _) = await serializer
                                   .DeserializeAsync(serialized, headers, MessageSerializationContext.Empty);

            var message2 = deserialized as BinaryFileMessage;

            message2.Should().NotBeNull();
            message2.Should().BeEquivalentTo(message);
        }