public async Task DataContractFormatterThrowsOnReadWhenOverridenCreateReturnsNull() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); formatter.ReturnNullOnCreate = true; formatter.UseDataContractJsonSerializer = 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, "The 'DataContractJsonSerializer' serializer cannot serialize the type 'SampleType'." ); Assert.NotNull(formatter.InnerDataContractSerializer); Assert.Null(formatter.InnerJsonSerializer); }
public void ReadFromStreamAsync_RoundTripsJToken() { string beforeMessage = "Hello World"; TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); JToken before = beforeMessage; MemoryStream memStream = new MemoryStream(); JsonTextWriter jsonWriter = new JsonTextWriter(new StreamWriter(memStream)); before.WriteTo(jsonWriter); jsonWriter.Flush(); memStream.Position = 0; JToken after = Assert.Task.SucceedsWithResult <object>(formatter.ReadFromStreamAsync(typeof(JToken), memStream, null, null)) as JToken; Assert.NotNull(after); string afterMessage = after.ToObject <string>(); Assert.Equal(beforeMessage, afterMessage); }
public async Task FormatterThrowsOnReadWhenOverridenCreateFails() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); 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, "The 'CreateJsonSerializer' method threw an exception when attempting to create a JSON serializer."); Assert.Null(formatter.InnerDataContractSerializer); Assert.NotNull(formatter.InnerJsonSerializer); }
public void FormatterThrowsOnReadWhenOverridenCreateReturnsNull() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); formatter.ReturnNullOnCreate = 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, "The 'CreateJsonSerializer' method returned null. It must return a JSON serializer instance."); Assert.Null(formatter.InnerDataContractSerializer); Assert.NotNull(formatter.InnerJsonSerializer); }
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); } }
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."); } }); }