Exemple #1
0
        public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsNotCompatibleType_SerializesAndDeserializesValue()
        {
            _formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
            _formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
            var value   = new TestClass();
            var content = new ObjectContent <TestClass>(value, _formatterMock.Object, _mediaType);

            SetupUpRoundTripSerialization(type => new TestClass());

            await Assert.ThrowsAsync <InvalidCastException>(
                () => content.ReadAsAsync <string>(_formatters)
                );

            Assert.IsNotType <string>(await content.ReadAsAsync(typeof(string), _formatters));

            _formatterMock.Verify(
                f =>
                f.ReadFromStreamAsync(
                    It.IsAny <Type>(),
                    It.IsAny <Stream>(),
                    content,
                    It.IsAny <IFormatterLogger>()
                    ),
                Times.Exactly(2)
                );
        }
        public void ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNotNullable_SerializesAndDeserializesValue()
        {
            _formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
            _formatterMock.Setup(f => f.CanReadType(typeof(Int32))).Returns(true);
            var content = new ObjectContent <object>(null, _formatterMock.Object, _mediaType.MediaType);

            SetupUpRoundTripSerialization();

            Assert.IsType <Int32>(content.ReadAsAsync <Int32>(_formatters).Result);
            Assert.IsType <Int32>(content.ReadAsAsync(typeof(Int32), _formatters).Result);

            _formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny <Type>(), It.IsAny <Stream>(), content.Headers, It.IsAny <IFormatterLogger>()), Times.Exactly(2));
        }
        public void ReadAsAsync_WhenContentIsObjectContentAndValueIsCompatibleType_ReadsValueFromObjectContent()
        {
            _formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
            var value   = new TestClass();
            var content = new ObjectContent <TestClass>(value, _formatterMock.Object);

            Assert.Same(value, content.ReadAsAsync <object>(_formatters).Result);
            Assert.Same(value, content.ReadAsAsync <TestClass>(_formatters).Result);
            Assert.Same(value, content.ReadAsAsync(typeof(object), _formatters).Result);
            Assert.Same(value, content.ReadAsAsync(typeof(TestClass), _formatters).Result);

            _formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny <Type>(), It.IsAny <Stream>(), content.Headers, It.IsAny <IFormatterLogger>()), Times.Never());
        }
        public void ReadAsAsyncOfT_WhenContentIsObjectContent_GoesThroughSerializationCycleToConvertTypes()
        {
            var content = new ObjectContent <int[]>(new int[] { 10, 20, 30, 40 }, new JsonMediaTypeFormatter());

            byte[] result = content.ReadAsAsync <byte[]>().Result;

            Assert.Equal(new byte[] { 10, 20, 30, 40 }, result);
        }
        public async void FormatterShouldBeAbleToDeserializeArticle()
        {
            var content = new ObjectContent<Article>(_article, _formatter);

            var deserializedItem = await content.ReadAsAsync<Article>(new[] { _formatter });

            Assert.That(_article, Is.SameAs(deserializedItem));
        }
Exemple #6
0
        public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNullable_SerializesAndDeserializesValue()
        {
            _formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
            _formatterMock.Setup(f => f.CanReadType(It.IsAny <Type>())).Returns(true);
            var content = new ObjectContent <object>(null, _formatterMock.Object);

            SetupUpRoundTripSerialization(type => null);

            Assert.Null(await content.ReadAsAsync <object>(_formatters));
            Assert.Null(await content.ReadAsAsync <TestClass>(_formatters));
            Assert.Null(await content.ReadAsAsync <Nullable <int> >(_formatters));
            Assert.Null(await content.ReadAsAsync(typeof(object), _formatters));
            Assert.Null(await content.ReadAsAsync(typeof(TestClass), _formatters));
            Assert.Null(await content.ReadAsAsync(typeof(Nullable <int>), _formatters));

            _formatterMock.Verify(
                f =>
                f.ReadFromStreamAsync(
                    It.IsAny <Type>(),
                    It.IsAny <Stream>(),
                    content,
                    It.IsAny <IFormatterLogger>()
                    ),
                Times.Exactly(6)
                );
        }