Exemplo n.º 1
0
 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));
     });
 }
Exemplo n.º 2
0
 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));
     });
 }
Exemplo n.º 3
0
 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>.");
     });
 }
Exemplo n.º 4
0
 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"));
     });
 }
Exemplo n.º 5
0
 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.");
     });
 }
Exemplo n.º 6
0
 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.");
     });
 }
Exemplo n.º 7
0
 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));
         }
     });
 }
Exemplo n.º 8
0
        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));
                }
            });
        }
Exemplo n.º 9
0
        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>.");
            });
        }