Example #1
0
        public void CanConvertFromTypeThrowsWithNullTypeForTtoObjectContentOfT()
        {
            ObjectContent objectContent           = new ObjectContent <int>(5);
            HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(objectContent.GetType());

            ExceptionAssert.ThrowsArgumentNull("type", () => converter.CanConvertFromType(null));
        }
Example #2
0
 public void GetValueConverterHttpContent()
 {
     foreach (HttpContent httpContent in HttpTestData.StandardHttpContents)
     {
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(httpContent.GetType());
         Assert.IsNotNull(converter, "GetValueConverter returned null.");
         Assert.AreEqual(httpContent.GetType(), converter.Type, "Converter Type was not correct.");
     }
 }
Example #3
0
        public void CanConvertFromStringReturnsFalseForObjectContent()
        {
            ObjectContent objectContent           = new ObjectContent <int>(5);
            HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(objectContent.GetType());

            if (converter.CanConvertFromString)
            {
                Assert.Fail(string.Format("CanConvertFromString was wrong for ObjectContent."));
            }
        }
Example #4
0
        public void CanConvertFromStringReturnsFalseForHttpRequestMessage()
        {
            HttpRequestMessage          request   = new HttpRequestMessage <int>();
            HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(request.GetType());

            if (converter.CanConvertFromString)
            {
                Assert.Fail(string.Format("CanConvertFromString was wrong for HttpRequestMessage."));
            }
        }
Example #5
0
 public void CanConvertFromStringReturnsTrueForT()
 {
     foreach (TestData testData in HttpTestData.ConvertableValueTypes)
     {
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(testData.Type);
         if (!converter.CanConvertFromString)
         {
             Assert.Fail(string.Format("CanConvertFromString was wrong for {0}.", testData.Type.Name));
         }
     }
 }
Example #6
0
 public void GetValueConverterHttpResponseMessageOfT()
 {
     ObjectContentAssert.ExecuteForEachHttpResponseMessage(
         HttpTestData.RepresentativeValueAndRefTypeTestDataCollection,
         TestDataVariations.All,
         (response, type, obj) =>
     {
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(response.GetType());
         Assert.AreEqual(response.GetType(), converter.Type, "Converter Type is wrong.");
     });
 }
Example #7
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));
     });
 }
Example #8
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>.");
     });
 }
Example #9
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));
     });
 }
Example #10
0
 public void ValueConverterReturnsConverterForAllHttpResponseMessageOfTTypes()
 {
     ObjectContentAssert.ExecuteForEachHttpResponseMessage(
         HttpTestData.ConvertableValueTypes,
         TestDataVariations.All,
         (response, type, obj) =>
     {
         Type convertType  = response.GetType();
         HttpParameter hpd = new HttpParameter("aName", convertType);
         HttpParameterValueConverter converter = hpd.ValueConverter;
         Assert.IsNotNull("ValueConverter returned null.");
         Assert.AreEqual(convertType, converter.Type, "ValueConverter was made for wrong type.");
     });
 }
Example #11
0
 public void CanConvertFromTypeReturnsTrueForObjectContentOfTtoT()
 {
     ObjectContentAssert.ExecuteForEachObjectContent(
         HttpTestData.RepresentativeValueAndRefTypeTestDataCollection,
         TestDataVariations.All,
         (objectContent, type, obj) =>
     {
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(objectContent.Type);
         if (!converter.CanConvertFromType(objectContent.GetType()))
         {
             Assert.Fail(string.Format("CanConvertFromType failed for {0}.", objectContent.Type));
         }
     });
 }
Example #12
0
 public void GetValueConverterReturnsConverterForAllValueTypes()
 {
     TestDataAssert.Execute(
         HttpTestData.ConvertableValueTypes,
         TestDataVariations.AllSingleInstances | TestDataVariations.AsNullable,
         "GetValueConverter failed",
         (type, obj) =>
     {
         Type convertType = obj.GetType();
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType);
         Assert.IsNotNull(converter, "GetValueConverter returned null.");
         Assert.AreEqual(convertType, converter.Type, "Converter Type was not correct.");
     });
 }
Example #13
0
 public void CanConvertFromTypeReturnsTrueForHttpResponseMessageOfTtoT()
 {
     ObjectContentAssert.ExecuteForEachHttpResponseMessage(
         HttpTestData.RepresentativeValueAndRefTypeTestDataCollection,
         TestDataVariations.All,
         (response, type, obj) =>
     {
         Type convertType = obj == null ? type : obj.GetType();
         HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType);
         if (!converter.CanConvertFromType(response.GetType()))
         {
             Assert.Fail(string.Format("CanConvertFromType failed for {0}.", convertType));
         }
     });
 }
Example #14
0
 public void ValueConverterReturnsConverterForAllValueTypes()
 {
     TestDataAssert.Execute(
         HttpTestData.ConvertableValueTypes,
         TestDataVariations.All,
         "ValueConverter failed",
         (type, obj) =>
     {
         Type convertType  = obj.GetType();
         HttpParameter hpd = new HttpParameter("aName", convertType);
         HttpParameterValueConverter converter = hpd.ValueConverter;
         Assert.IsNotNull("ValueConverter returned null.");
         Assert.AreEqual(convertType, converter.Type, "ValueConverter was made for wrong type.");
     });
 }
Example #15
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"));
     });
 }
Example #16
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.");
     });
 }
Example #17
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.");
     });
 }
Example #18
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));
         }
     });
 }
Example #19
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));
                }
            });
        }
Example #20
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>.");
            });
        }
Example #21
0
 public void GetValueConverterThrowsWithNullType()
 {
     ExceptionAssert.ThrowsArgumentNull("type", () => HttpParameterValueConverter.GetValueConverter(null));
 }