public void WriteToStreamAsyncCallsOnWriteToStreamInTask() { Type calledType = null; object calledObj = null; Stream calledStream = null; HttpContentHeaders calledHeaders = null; Task createdTask = null; SMediaTypeFormatter formatter = new SMediaTypeFormatter() { CallBase = true }; formatter.OnWriteToStreamTypeObjectStreamHttpContentHeadersTransportContext = (type, obj, stream, headers, context) => { calledType = type; calledObj = obj; calledStream = stream; calledHeaders = headers; }; SHttpContent content = new SHttpContent(); HttpContentHeaders contentHeaders = content.Headers; StreamAssert.WriteAndRead( (stream) => createdTask = formatter.WriteToStreamAsync(typeof(int), 5, stream, contentHeaders, /*transportContext*/ null), (stream) => { }); TaskAssert.Succeeds(createdTask); Assert.AreEqual(typeof(int), calledType, "OnWriteToStream was not called or did not pass Type."); Assert.AreEqual(5, calledObj, "OnWriteToStream was not called or did not pass the object value."); Assert.IsNotNull(calledStream, "OnWriteToStream was not called or did not pass Type."); Assert.AreSame(contentHeaders, calledHeaders, "OnWriteToStream was not called or did not pass ContentHeaders."); }
public void ReadFromStreamAsyncCallsOnReadFromStreamAsync() { Type calledType = null; Stream calledStream = null; HttpContentHeaders calledHeaders = null; SMediaTypeFormatter formatter = new SMediaTypeFormatter() { CallBase = true }; formatter.OnReadFromStreamAsyncTypeStreamHttpContentHeaders = (type, stream, headers) => { calledType = type; calledStream = stream; calledHeaders = headers; return(null); }; SHttpContent content = new SHttpContent(); HttpContentHeaders contentHeaders = content.Headers; StreamAssert.WriteAndRead( (stream) => { }, (stream) => formatter.ReadFromStreamAsync(typeof(int), stream, contentHeaders)); Assert.AreEqual(typeof(int), calledType, "OnReadFromStreamAsync was not called or did not pass Type."); Assert.IsNotNull(calledStream, "OnReadFromStreamAsync was not called or did not pass Type."); Assert.AreSame(contentHeaders, calledHeaders, "OnReadFromStreamAsync was not called or did not pass ContentHeaders."); }
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 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 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"); } }); }