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);
        }
Ejemplo n.º 2
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);
        }
        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);
            }
        }
Ejemplo n.º 4
0
        public void ReadFromStreamAsyncRoundTripsWriteToStreamUsingDataContractSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

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

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => TaskAssert.Succeeds(formatter.WriteToStreamAsync(type, obj, stream, contentHeaders, /*transportContext*/ null)),
                        (stream) => readObj = TaskAssert.SucceedsWithResult(formatter.ReadFromStreamAsync(type, stream, contentHeaders))
                        );
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object.");
                }
            });
        }
Ejemplo n.º 5
0
        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);
            }
        }