public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Holder( Type variationType, object testData ) { // Guard Assert.IsType <TestDataHolder <object> >(testData); // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper( formatter, variationType, testData ); // DBNull.Value can be read back as null object. Reach into objects. Assert.Equal(testData.GetType(), readObj.GetType()); TestDataHolder <object> readDataHolder = (TestDataHolder <object>)readObj; Assert.Null(readDataHolder.V1); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Enumerable( Type variationType, object testData ) { // Guard Assert.True((testData as IEnumerable <object>) != null); // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); IEnumerable <object> expectedEnumerable = (IEnumerable <object>)testData; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper( formatter, variationType, testData ); // DBNull.Value can be read back as null object. Reach into collections. Assert.Equal(testData.GetType(), readObj.GetType()); IEnumerable <object> readEnumerable = (IEnumerable <object>)readObj; Assert.Equal(expectedEnumerable.Count(), readEnumerable.Count()); foreach (object readContent in readEnumerable) { Assert.Null(readContent); } }
void CopyConstructor() { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter() { Indent = true, #if !NETFX_CORE // MaxDepth and DCJS not supported in client portable library MaxDepth = 42, UseDataContractJsonSerializer = true #endif }; TestJsonMediaTypeFormatter derivedFormatter = new TestJsonMediaTypeFormatter(formatter); #if !NETFX_CORE // MaxDepth and DCJS not supported in client portable library Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth); Assert.Equal( formatter.UseDataContractJsonSerializer, derivedFormatter.UseDataContractJsonSerializer ); #endif Assert.Equal(formatter.Indent, derivedFormatter.Indent); Assert.Same(formatter.SerializerSettings, derivedFormatter.SerializerSettings); Assert.Same( formatter.SerializerSettings.ContractResolver, derivedFormatter.SerializerSettings.ContractResolver ); }
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 CanReadTypeReturnsExpectedValues() { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { bool isSerializable = IsTypeSerializableWithJsonSerializer(type, obj); bool canSupport = formatter.CanReadTypeProxy(type); // If we don't agree, we assert only if the DCJ serializer says it cannot support something we think it should if (isSerializable != canSupport && isSerializable) { Assert.Fail(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.CanReadTypeProxy(type); if (isSerializable != canSupport && isSerializable) { Assert.Fail(string.Format("2nd CanReadType returned wrong value for '{0}'.", type)); } }); }
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 async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_KnownTypes( Type variationType, object testData ) { // Guard bool canSerialize = IsTypeSerializableWithJsonSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType); if (canSerialize) { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter { AddDBNullKnownType = true, }; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper( formatter, variationType, testData ); Assert.Equal(testData, readObj); } }
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 void ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Dictionary(Type variationType, object testData) { // Guard Assert.IsType <Dictionary <string, object> >(testData); // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); IDictionary <string, object> expectedDictionary = (IDictionary <string, object>)testData; // Arrange & Act & Assert object readObj = ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // DBNull.Value can be read back as null object. Reach into collections. Assert.Equal(testData.GetType(), readObj.GetType()); IDictionary <string, object> readDictionary = (IDictionary <string, object>)readObj; Assert.Equal(expectedDictionary.Count, readDictionary.Count); foreach (string key in expectedDictionary.Keys) { Assert.True(readDictionary.ContainsKey(key)); Assert.Null(readDictionary[key]); } }
public void CanWriteType_ReturnsTrueOnJtoken() { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); foreach (Type type in JTokenTypes) { Assert.True(formatter.CanWriteTypeProxy(type), "formatter should have returned false."); } }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull(Type variationType, object testData) { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // DBNull.Value can be read back as null object. Assert.Null(readObj); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNullString() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); Type variationType = typeof(string); object testData = DBNull.Value; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // DBNull.value can be read as null of any nullable type Assert.Null(readObj); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNull() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); Type variationType = typeof(DBNull); object testData = DBNull.Value; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // Only JSON case where DBNull.Value round-trips Assert.Equal(testData, readObj); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNull() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); Type variationType = typeof(DBNull); 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); }
public void CanReadType_ReturnsExpectedValues(Type variationType, object testData) { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); bool isSerializable = IsTypeSerializableWithJsonSerializer(variationType, testData); bool canSupport = formatter.CanReadTypeProxy(variationType); // If we don't agree, we assert only if the DCJ serializer says it cannot support something we think it should Assert.False(isSerializable != canSupport && isSerializable, String.Format("CanReadType returned wrong value for '{0}'.", variationType)); // Ask a 2nd time to probe whether the cached result is treated the same canSupport = formatter.CanReadTypeProxy(variationType); Assert.False(isSerializable != canSupport && isSerializable, String.Format("2nd CanReadType returned wrong value for '{0}'.", variationType)); }
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 ReadFromStreamAsync_RoundTripsWriteToStreamAsync(Type variationType, object testData) { // Guard bool canSerialize = IsTypeSerializableWithJsonSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType); if (canSerialize) { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); // Arrange & Act & Assert object readObj = ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); Assert.Equal(testData, readObj); } }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsEmptyString() { // Arrange TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter { AddDBNullKnownType = true, }; Type variationType = typeof(string); 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 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); }
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 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."); } }); }
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 TestJsonMediaTypeFormatter(TestJsonMediaTypeFormatter formatter) : base(formatter) { }