public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Enumerable( Type variationType, object testData ) { // Guard Assert.True((testData as IEnumerable <object>) != null); // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); 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); } }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Holder( Type variationType, object testData ) { // Guard Assert.IsType <TestDataHolder <object> >(testData); // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); // 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 FormatterThrowsOnWriteWhenOverridenCreateReturnsNull() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter { ReturnNullOncreate = 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 'CreateJsonWriter' method returned null. It must return a JSON writer instance." ); Assert.False(formatter.WasCreateJsonReaderCalled); Assert.True(formatter.WasCreateJsonWriterCalled); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Dictionary( Type variationType, object testData ) { // Guard IDictionary <string, object> expectedDictionary = Assert.IsType < Dictionary <string, object> >(testData); // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); // 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()); 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]); } }
void CopyConstructor() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter() { #if !NETFX_CORE // MaxDepth is not supported in portable library MaxDepth = 42, #endif }; // Replace serializable settings and switch one property's value JsonSerializerSettings oldSettings = formatter.SerializerSettings; formatter.SerializerSettings = formatter.CreateDefaultSerializerSettings(); formatter.SerializerSettings.CheckAdditionalContent = !formatter.SerializerSettings.CheckAdditionalContent; // Act TestBsonMediaTypeFormatter derivedFormatter = new TestBsonMediaTypeFormatter(formatter); // Assert #if !NETFX_CORE // MaxDepth is not supported in portable library Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth); #endif Assert.NotSame(oldSettings, formatter.SerializerSettings); Assert.NotEqual( oldSettings.CheckAdditionalContent, formatter.SerializerSettings.CheckAdditionalContent ); Assert.Same(formatter.SerializerSettings, derivedFormatter.SerializerSettings); Assert.Same( formatter.SerializerSettings.ContractResolver, derivedFormatter.SerializerSettings.ContractResolver ); }
public async Task FormatterThrowsOnWriteWhenOverridenCreateFails() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter { ThrowExceptionOnCreate = 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 <Exception>( action, "Throwing exception directly, since it needs to get caught by a catch all" ); Assert.False(formatter.WasCreateJsonReaderCalled); Assert.True(formatter.WasCreateJsonWriterCalled); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull(Type variationType, object testData) { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); // 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_DBNull() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); Type variationType = typeof(DBNull); object testData = DBNull.Value; // Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // Only BSON case where DBNull.Value round-trips Assert.Equal(testData, readObj); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNullString() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter(); Type variationType = typeof(string); object testData = DBNull.Value; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); // Null on wire can be read as null of any nullable type Assert.Null(readObj); }
public async Task FormatterThrowsOnReadWhenOverridenCreateReturnsNull() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter { ReturnNullOncreate = 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 'CreateJsonReader' method returned null. It must return a JSON reader instance."); Assert.True(formatter.WasCreateJsonReaderCalled); Assert.False(formatter.WasCreateJsonWriterCalled); }
public async Task FormatterThrowsOnReadWhenOverridenCreateFails() { // Arrange TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter { ThrowExceptionOnCreate = 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 <Exception>(action, "Throwing exception directly, since it needs to get caught by a catch all"); Assert.True(formatter.WasCreateJsonReaderCalled); Assert.False(formatter.WasCreateJsonWriterCalled); }
public TestBsonMediaTypeFormatter(TestBsonMediaTypeFormatter formatter) : base(formatter) { }