public async Task ReadFromStream_AsyncRoundTripsWriteToStreamUsingDataContractSerializer_KnownTypes( Type variationType, object testData ) { // Guard bool canSerialize = IsSerializableWithDataContractSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType); if (canSerialize) { // Arrange TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); formatter.SetSerializer( variationType, new DataContractSerializer(variationType, new Type[] { typeof(DBNull), }) ); // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper( formatter, variationType, testData ); Assert.Equal(testData, readObj); } }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerializer( Type variationType, object testData ) { // Guard bool canSerialize = IsSerializableWithXmlSerializer(variationType, testData) && Assert.Http.CanRoundTrip(variationType); if (canSerialize) { // Arrange TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); formatter.SetSerializer(variationType, new XmlSerializer(variationType)); // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper( formatter, variationType, testData ); Assert.Equal(testData, readObj); } }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerializer_DBNull() { // Arrange TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); Type variationType = typeof(DBNull); formatter.SetSerializer(variationType, new XmlSerializer(variationType)); object testData = DBNull.Value; // Arrange & Act & Assert object readObj = await ReadFromStreamAsync_RoundTripsWriteToStreamAsync_Helper(formatter, variationType, testData); Assert.Equal(testData, readObj); }
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContractSerializer_DBNullAsEmptyString() { // Arrange TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); Type variationType = typeof(string); formatter.SetSerializer(variationType, new DataContractSerializer(variationType, new Type[] { typeof(DBNull), })); 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 async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContractSerializer_DBNull() { // Arrange TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); Type variationType = typeof(DBNull); formatter.SetSerializer(variationType, new DataContractSerializer(variationType)); 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 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); } }
public void ReadFromStreamRoundTripsWriteToStreamUsingDataContractSerializer() { TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); HttpContentHeaders contentHeaders = new StringContent(string.Empty).Headers; TestDataAssert.Execute( TestData.ValueAndRefTypeTestDataCollection, (type, obj) => { bool canSerialize = IsSerializableWithDataContractSerializer(type, obj) && HttpTestData.CanRoundTrip(type); if (canSerialize) { formatter.SetSerializer(type, new DataContractSerializer(type)); object readObj = null; StreamAssert.WriteAndRead( (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null), (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders)); TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object"); } }); }
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); } }
public void ReadFromStreamRoundTripsWriteToStreamUsingXmlSerializer() { TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter(); HttpContentHeaders contentHeaders = new StringContent(string.Empty).Headers; // Excludes ReferenceDataContractType tests because XmlSerializer cannot handle circular references TestDataAssert.Execute( TestData.ValueAndRefTypeTestDataCollection.Where((td) => !(typeof(RefTypeTestData <ReferenceDataContractType>).IsAssignableFrom(td.GetType()))), (type, obj) => { bool canSerialize = IsSerializableWithXmlSerializer(type, obj) && HttpTestData.CanRoundTrip(type); if (canSerialize) { formatter.SetSerializer(type, new XmlSerializer(type)); object readObj = null; StreamAssert.WriteAndRead( (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null), (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders)); TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object"); } }); }