public void ConvertTtoT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualObj = converter.Convert(obj); TestDataAssert.AreEqual(obj, actualObj, string.Format("Conversion failed for {0}.", convertType.Name)); }); }
public void ConvertHttpResponseMessageOfTtoT() { ObjectContentAssert.ExecuteForEachHttpResponseMessage( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (response, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualValue = converter.Convert(response); TestDataAssert.AreEqual(obj, actualValue, string.Format("Convert from HttpResponseMessage<T> to T failed for {0}.", convertType)); }); }
public void ConvertObjectContentOfTtoT() { ObjectContentAssert.ExecuteForEachObjectContent( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (objectContent, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualValue = converter.Convert(objectContent); TestDataAssert.AreEqual(obj, actualValue, "Convert failed to return T from ObjectContent<T>."); }); }
public void ConvertStringToHttpResponseMessageOfTThrows() { ObjectContentAssert.ExecuteForEachHttpResponseMessage( HttpTestData.ConvertableValueTypes, TestDataVariations.AllSingleInstances, (response, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(response.GetType()); string errorMessage = SR.ValueConversionFailed(typeof(string).FullName, converter.Type.FullName); ExceptionAssert.Throws <InvalidOperationException>( errorMessage, () => converter.Convert("random string")); }); }
public void ConvertHttpResponseMessageOfTtoObjectContentOfT() { ObjectContentAssert.ExecuteForEachHttpResponseMessage( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (response, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); ObjectContent objectContent = (ObjectContent)GenericTypeAssert.InvokeConstructor(typeof(ObjectContent <>), convertType, new Type[] { convertType }, new object[] { obj }); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(objectContent.GetType()); ObjectContent convertedContent = converter.Convert(response) as ObjectContent; Assert.IsNotNull(convertedContent, "Failed to convert to ObjectContent."); Assert.AreEqual(((ObjectContent)response.Content).ReadAs(), convertedContent.ReadAs(), "Incorrect value."); }); }
public void ConvertNullableOfTtoT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AsNullable, "Nullable<T> to T failed.", (type, obj) => { Type nonNullableType = obj.GetType(); Assert.IsNull(Nullable.GetUnderlyingType(nonNullableType), "Test error: did not expect nullable object instance."); Assert.AreEqual(nonNullableType, Nullable.GetUnderlyingType(type), "Test error: expected only nullable types."); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(type); object actualValue = converter.Convert(obj); TestDataAssert.AreEqual(obj, actualValue, "Convert failed on Nullable<T> to T."); }); }
public void ConvertThrowsWithTtoHttpResponseMessageOfT() { ObjectContentAssert.ExecuteForEachHttpResponseMessage( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (response, type, obj) => { if (obj != null) { Type convertType = obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(response.GetType()); string errorMessage = SR.ValueConversionFailed(convertType.FullName, converter.Type.FullName); ExceptionAssert.Throws <InvalidOperationException>( errorMessage, () => converter.Convert(obj)); } }); }
public void ConvertStringToT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AllSingleInstances, "Convert(string) failed", (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); if (HttpParameterAssert.CanConvertToStringAndBack(obj)) { HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); string objAsString = obj.ToString(); object actualObj = converter.Convert(objAsString); Assert.IsNotNull(actualObj, "Convert from string returned null."); Assert.AreEqual(obj.GetType(), actualObj.GetType(), "Convert from string returned wrong type."); string actualObjAsString = actualObj.ToString(); Assert.AreEqual(objAsString, actualObjAsString, string.Format("Conversion failed for {0}.", convertType.Name)); } }); }
public void ConvertHttpResponseMessageOfNullableTtoT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AsNullable, "HttpResponseMessage<Nullable<T>> failied.", (type, obj) => { Type nonNullableType = obj.GetType(); Assert.IsNull(Nullable.GetUnderlyingType(nonNullableType), "Test error: did not expect nullable object instance."); Assert.AreEqual(nonNullableType, Nullable.GetUnderlyingType(type), "Test error: expected only nullable types."); HttpResponseMessage request = (HttpResponseMessage)GenericTypeAssert.InvokeConstructor( typeof(HttpResponseMessage <>), type, new Type[] { type }, new object[] { obj }); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(nonNullableType); object actualValue = converter.Convert(request); TestDataAssert.AreEqual(obj, actualValue, "Convert failed to return T from HttpReesponseMessage<T>."); }); }