public async Task DataContractFormatterThrowsOnWriteWhenOverridenCreateReturnsNull()
        {
            // Arrange
            TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter();

            formatter.ReturnNullOnCreate            = true;
            formatter.UseDataContractJsonSerializer = 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,
                "The 'DataContractJsonSerializer' serializer cannot serialize the type 'SampleType'."
                );

            Assert.NotNull(formatter.InnerDataContractSerializer);
            Assert.Null(formatter.InnerJsonSerializer);
        }
        public async Task FormatterThrowsOnWriteWhenOverridenCreateFails()
        {
            // Arrange
            TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter();

            formatter.ThrowAnExceptionOnCreate = 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,
                "The 'CreateJsonSerializer' method threw an exception when attempting to create a JSON serializer."
                );

            Assert.Null(formatter.InnerDataContractSerializer);
            Assert.NotNull(formatter.InnerJsonSerializer);
        }
        public void WriteToStreamAsync_RoundTripsJToken()
        {
            string beforeMessage = "Hello World";
            TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter();
            JToken       before    = new JValue(beforeMessage);
            MemoryStream memStream = new MemoryStream();

            Assert.Task.Succeeds(formatter.WriteToStreamAsync(typeof(JToken), before, memStream, null, null));
            memStream.Position = 0;
            JToken after        = JToken.Load(new JsonTextReader(new StreamReader(memStream)));
            string afterMessage = after.ToObject <string>();

            Assert.Equal(beforeMessage, afterMessage);
        }
        public void FormatterThrowsOnWriteWhenOverridenCreateReturnsNull()
        {
            // Arrange
            TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter();

            formatter.ReturnNullOnCreate = 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, "The 'CreateJsonSerializer' method returned null. It must return a JSON serializer instance.");

            Assert.Null(formatter.InnerDataContractSerializer);
            Assert.NotNull(formatter.InnerJsonSerializer);
        }
Exemplo n.º 5
0
        public void ReadFromStreamAsync_RoundTripsWriteToStreamAsync(Type variationType, object testData)
        {
            TestJsonMediaTypeFormatter formatter      = new TestJsonMediaTypeFormatter();
            HttpContentHeaders         contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();

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

            if (canSerialize)
            {
                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);
            }
        }
Exemplo n.º 6
0
        public void ReadFromStreamAsyncRoundTripsWriteToStreamAsync()
        {
            TestJsonMediaTypeFormatter formatter      = new TestJsonMediaTypeFormatter();
            HttpContentHeaders         contentHeaders = new StringContent(string.Empty).Headers;

            TestDataAssert.Execute(
                TestData.RepresentativeValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool canSerialize = IsTypeSerializableWithJsonSerializer(type, obj) && HttpTestData.CanRoundTrip(type);
                if (canSerialize)
                {
                    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.");
                }
            });
        }