public async Task ReadFromStream_AsyncRoundTripsWriteToStreamUsingDataContractSerializer_KnownTypes(
            Type variationType,
            object testData
            )
        {
            // Guard
            bool canSerialize =
                IsSerializableWithDataContractSerializer(variationType, testData) &&
                Assert.Http.CanRoundTrip(variationType);

            if (canSerialize)
            {
                // Arrange
                TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
                formatter.SetSerializer(
                    variationType,
                    new DataContractSerializer(variationType, new Type[] { typeof(DBNull), })
                    );

                // Arrange & Act & Assert
                object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(
                    formatter,
                    variationType,
                    testData
                    );

                Assert.Equal(testData, readObj);
            }
        }
        public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerializer(
            Type variationType,
            object testData
            )
        {
            // Guard
            bool canSerialize =
                IsSerializableWithXmlSerializer(variationType, testData) &&
                Assert.Http.CanRoundTrip(variationType);

            if (canSerialize)
            {
                // Arrange
                TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
                formatter.SetSerializer(variationType, new XmlSerializer(variationType));

                // Arrange & Act & Assert
                object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(
                    formatter,
                    variationType,
                    testData
                    );

                Assert.Equal(testData, readObj);
            }
        }
        public async Task DataContractFormatterThrowsOnWriteWhenOverridenCreateReturnsNull()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();

            formatter.ReturnNullOnCreate = true;
            formatter.UseXmlSerializer   = true;

            MemoryStream memoryStream = new MemoryStream();
            HttpContent  content      = new StringContent(String.Empty);

            // Act & Assert
            Func <Task> action = () =>
                                 formatter.WriteToStreamAsync(
                typeof(SampleType),
                new SampleType(),
                memoryStream,
                content,
                transportContext: null
                );
            await Assert.ThrowsAsync <InvalidOperationException>(action);

            Assert.Null(formatter.InnerDataContractSerializer);
            Assert.NotNull(formatter.InnerXmlSerializer);
        }
        public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerializer_DBNull()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
            Type variationType = typeof(DBNull);

            formatter.SetSerializer(variationType, new XmlSerializer(variationType));
            object testData = DBNull.Value;

            // Arrange & Act & Assert
            object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData);

            Assert.Equal(testData, readObj);
        }
        public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContractSerializer_DBNullAsEmptyString()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
            Type variationType = typeof(string);

            formatter.SetSerializer(variationType, new DataContractSerializer(variationType, new Type[] { typeof(DBNull), }));
            object testData = DBNull.Value;

            // Arrange & Act & Assert
            object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData);

            // Lower levels convert DBNull.Value to empty string on read
            Assert.Equal(String.Empty, readObj);
        }
        public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContractSerializer_DBNull()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
            Type variationType = typeof(DBNull);

            formatter.SetSerializer(variationType, new DataContractSerializer(variationType));
            object testData = DBNull.Value;

            // Arrange & Act & Assert
            object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData);

            // DBNull.Value round-trips as either Object or DBNull because serialization includes its type
            Assert.Equal(testData, readObj);
        }
Example #7
0
        public void FormatterThrowsOnWriteWhenOverridenCreateFails()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();

            formatter.ThrowAnExceptionOnCreate = true;

            MemoryStream memoryStream = new MemoryStream();
            HttpContent  content      = new StringContent(String.Empty);

            // Act & Assert
            Action action = () => formatter.WriteToStreamAsync(typeof(SampleType), new SampleType(), memoryStream, content, transportContext: null).Wait();

            Assert.Throws <InvalidOperationException>(action);

            Assert.NotNull(formatter.InnerDataContractSerializer);
            Assert.Null(formatter.InnerXmlSerializer);
        }
Example #8
0
        void CopyConstructor()
        {
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter()
            {
                Indent = true,
#if !NETFX_CORE // We don't support MaxDepth in the portable library
                MaxDepth = 42,
#endif
                UseXmlSerializer = true
            };

            TestXmlMediaTypeFormatter derivedFormatter = new TestXmlMediaTypeFormatter(formatter);

            Assert.Equal(formatter.Indent, derivedFormatter.Indent);
#if !NETFX_CORE // We don't support MaxDepth in the portable library
            Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth);
#endif
            Assert.Equal(formatter.UseXmlSerializer, derivedFormatter.UseXmlSerializer);
        }
        public async Task FormatterThrowsOnReadWhenOverridenCreateFails()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();

            formatter.ThrowAnExceptionOnCreate = true;

            byte[]       array        = Encoding.UTF8.GetBytes("foo");
            MemoryStream memoryStream = new MemoryStream(array);

            HttpContent content = new StringContent("foo");

            // Act & Assert
            Func <Task> action = () => formatter.ReadFromStreamAsync(typeof(SampleType), memoryStream, content, null);
            await Assert.ThrowsAsync <InvalidOperationException>(action);

            Assert.NotNull(formatter.InnerDataContractSerializer);
            Assert.Null(formatter.InnerXmlSerializer);
        }
Example #10
0
        public void DataContractFormatterThrowsOnReadWhenOverridenCreateReturnsNull()
        {
            // Arrange
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();

            formatter.ReturnNullOnCreate = true;
            formatter.UseXmlSerializer   = true;

            byte[]       array        = Encoding.UTF8.GetBytes("foo");
            MemoryStream memoryStream = new MemoryStream(array);

            HttpContent content = new StringContent("foo");

            // Act & Assert
            Action action = () => formatter.ReadFromStreamAsync(typeof(SampleType), memoryStream, content, null).Wait();

            Assert.Throws <InvalidOperationException>(action);

            Assert.Null(formatter.InnerDataContractSerializer);
            Assert.NotNull(formatter.InnerXmlSerializer);
        }
        public void ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerializer(Type variationType, object testData)
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();

            bool canSerialize = IsSerializableWithXmlSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType);

            if (canSerialize)
            {
                formatter.SetSerializer(variationType, new XmlSerializer(variationType));

                object readObj = null;
                Assert.Stream.WriteAndRead(
                    stream =>
                {
                    Assert.Task.Succeeds(formatter.WriteToStreamAsync(variationType, testData, stream, contentHeaders, transportContext: null));
                    contentHeaders.ContentLength = stream.Length;
                },
                    stream => readObj = Assert.Task.SucceedsWithResult(formatter.ReadFromStreamAsync(variationType, stream, contentHeaders, null)));
                Assert.Equal(testData, readObj);
            }
        }
Example #12
0
        public void ReadFromStreamRoundTripsWriteToStreamUsingDataContractSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

            TestDataAssert.Execute(
                TestData.ValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool canSerialize = IsSerializableWithDataContractSerializer(type, obj) && HttpTestData.CanRoundTrip(type);
                if (canSerialize)
                {
                    formatter.SetSerializer(type, new DataContractSerializer(type));

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null),
                        (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders));
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object");
                }
            });
        }
Example #13
0
        public void CanReadTypeReturnsSameResultAsXmlSerializerConstructor()
        {
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();

            TestDataAssert.Execute(
                TestData.RepresentativeValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool isSerializable = IsSerializableWithXmlSerializer(type, obj);
                bool canSupport     = formatter.CanReadTypeCaller(type);
                if (isSerializable != canSupport)
                {
                    Assert.AreEqual(isSerializable, canSupport, string.Format("CanReadType returned wrong value for '{0}'.", type));
                }

                // Ask a 2nd time to probe whether the cached result is treated the same
                canSupport = formatter.CanReadTypeCaller(type);
                if (isSerializable != canSupport)
                {
                    Assert.Fail(string.Format("2nd CanReadType returned wrong value for '{0}'.", type));
                }
            });
        }
        public void ReadFromStream_AsyncRoundTripsWriteToStreamUsingDataContractSerializer(Type variationType, object testData)
        {
            TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter();
            HttpContent        content          = new StringContent(String.Empty);
            HttpContentHeaders contentHeaders   = content.Headers;

            bool canSerialize = IsSerializableWithDataContractSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType);

            if (canSerialize)
            {
                formatter.SetSerializer(variationType, new DataContractSerializer(variationType));

                object readObj = null;
                Assert.Stream.WriteAndRead(
                    stream =>
                {
                    Assert.Task.Succeeds(formatter.WriteToStreamAsync(variationType, testData, stream, content, transportContext: null));
                    contentHeaders.ContentLength = stream.Length;
                },
                    stream => readObj = Assert.Task.SucceedsWithResult(formatter.ReadFromStreamAsync(variationType, stream, content, null))
                    );
                Assert.Equal(testData, readObj);
            }
        }
Example #15
0
        public void ReadFromStreamRoundTripsWriteToStreamUsingXmlSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

            // Excludes ReferenceDataContractType tests because XmlSerializer cannot handle circular references
            TestDataAssert.Execute(
                TestData.ValueAndRefTypeTestDataCollection.Where((td) => !(typeof(RefTypeTestData <ReferenceDataContractType>).IsAssignableFrom(td.GetType()))),
                (type, obj) =>
            {
                bool canSerialize = IsSerializableWithXmlSerializer(type, obj) && HttpTestData.CanRoundTrip(type);

                if (canSerialize)
                {
                    formatter.SetSerializer(type, new XmlSerializer(type));

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null),
                        (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders));
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object");
                }
            });
        }
Example #16
0
 public TestXmlMediaTypeFormatter(TestXmlMediaTypeFormatter formatter)
     : base(formatter)
 {
 }