Esempio n. 1
0
        public void Should_deserialize_nullable_enum(TestEnum?propertyValue)
        {
            var context = new BindingContext()
            {
                DestinationType          = typeof(TestModel),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestModel>().ToList(),
            };

            var model = new TestModel {
                NullableEnumProperty = propertyValue
            };

            var s          = new JavaScriptSerializer();
            var serialized = s.Serialize(model);
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(serialized));

            // When
            var result = (TestModel)this.deserialize.Deserialize(
                "application/json",
                bodyStream,
                context);

            // Then
            result.NullableEnumProperty.ShouldEqual(propertyValue);
        }
Esempio n. 2
0
        private static IEnumerable <BindingMemberInfo> GetBindingMembers(Type modelType, Type genericType, IEnumerable <string> blackList)
        {
            var blackListHash = new HashSet <string>(blackList, StringComparer.InvariantCulture);

            return(BindingMemberInfo.Collect(genericType ?? modelType)
                   .Where(member => !blackListHash.Contains(member.Name)));
        }
Esempio n. 3
0
        public void Should_return_PropertyType_for_properties_or_fields()
        {
            // Given
            var properties = BindingMemberInfo.Collect <TestModel>();

            // When

            // Then
            properties.ShouldHaveCount(4);

            foreach (var propInfo in properties)
            {
                if (propInfo.Name.StartsWith("Int"))
                {
                    propInfo.PropertyType.ShouldEqual(typeof(int));
                }
                else if (propInfo.Name.StartsWith("String"))
                {
                    propInfo.PropertyType.ShouldEqual(typeof(string));
                }
                else
                {
                    throw new AssertException("Internal error in unit test: Test model property/field name does not follow the expected convention: " + propInfo.Name);
                }
            }
        }
Esempio n. 4
0
        public void Should_deserialize_list_of_complex_objects()
        {
            // Given
            var context = new BindingContext()
            {
                DestinationType          = typeof(TestModel),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestModel>().ToList(),
            };

            var model =
                new TestModel
            {
                ListOfComplexObjectsProperty = new List <ModelWithStringValues>
                {
                    new ModelWithStringValues()
                    {
                        Value1 = "one", Value2 = "two"
                    },
                    new ModelWithStringValues()
                    {
                        Value1 = "three", Value2 = "four"
                    }
                },
                ListOfComplexObjectsField = new List <ModelWithStringValues>
                {
                    new ModelWithStringValues()
                    {
                        Value1 = "five", Value2 = "six"
                    },
                    new ModelWithStringValues()
                    {
                        Value1 = "seven", Value2 = "eight"
                    }
                }
            };

            var s          = new JavaScriptSerializer();
            var serialized = s.Serialize(model);
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(serialized));

            // When
            var result = (TestModel)this.deserialize.Deserialize(
                "application/json",
                bodyStream,
                context);

            // Then
            result.ListOfComplexObjectsProperty.ShouldHaveCount(2);
            result.ListOfComplexObjectsProperty[0].Value1.ShouldEqual("one");
            result.ListOfComplexObjectsProperty[0].Value2.ShouldEqual("two");
            result.ListOfComplexObjectsProperty[1].Value1.ShouldEqual("three");
            result.ListOfComplexObjectsProperty[1].Value2.ShouldEqual("four");
            result.ListOfComplexObjectsField.ShouldHaveCount(2);
            result.ListOfComplexObjectsField[0].Value1.ShouldEqual("five");
            result.ListOfComplexObjectsField[0].Value2.ShouldEqual("six");
            result.ListOfComplexObjectsField[1].Value1.ShouldEqual("seven");
            result.ListOfComplexObjectsField[1].Value2.ShouldEqual("eight");
        }
Esempio n. 5
0
        public void Should_be_able_to_use_except_on_two_lists_of_bindingMemberInfos()
        {
            // Given
            var properties = BindingMemberInfo.Collect <TestModel>();
            var except     = BindingMemberInfo.Collect <TestModel>().Where(i => i.Name.Contains("Property"));

            // When
            var res = properties.Except(except).ToList();

            // Then
            res.Count.ShouldEqual(2);
            res[0].Name.ShouldEqual("IntField");
            res[1].Name.ShouldEqual("StringField");
        }
Esempio n. 6
0
        public void Should_collect_all_bindable_members_and_skip_all_others()
        {
            // Given

            // When
            var properties = BindingMemberInfo.Collect <BiggerTestModel>();

            // Then
            properties.ShouldHaveCount(16);

            foreach (var property in properties)
            {
                property.Name.ShouldStartWith("Bindable");
            }
        }
Esempio n. 7
0
        public void Should_set_fields()
        {
            // Given
            var propInfo = BindingMemberInfo.Collect <TestModel>().Where(prop => prop.Name.EndsWith("Field"));
            var model    = new TestModel();

            // When
            propInfo.Single(prop => prop.PropertyType == typeof(int))
            .SetValue(model, 42);

            propInfo.Single(prop => prop.PropertyType == typeof(string))
            .SetValue(model, "nineteen");

            // Then
            model.IntField.ShouldEqual(42);
            model.StringField.ShouldEqual("nineteen");
        }
Esempio n. 8
0
        public void Should_set_properties()
        {
            // Given
            var propInfo = BindingMemberInfo.Collect <TestModel>().Where(prop => prop.Name.EndsWith("Property"));
            var model    = new TestModel();

            // When
            propInfo.Single(prop => prop.PropertyType == typeof(int))
            .SetValue(model, 2600);

            propInfo.Single(prop => prop.PropertyType == typeof(string))
            .SetValue(model, "R2D2");

            // Then
            model.IntProperty.ShouldEqual(2600);
            model.StringProperty.ShouldEqual("R2D2");
        }
Esempio n. 9
0
        public void Should_get_fields()
        {
            // Given
            var propInfo = BindingMemberInfo.Collect <TestModel>().Where(prop => prop.Name.EndsWith("Field"));
            var model    = new TestModel();

            // When
            model.IntField    = 669;
            model.StringField = "testing";

            // Then
            propInfo.Single(prop => prop.PropertyType == typeof(int))
            .GetValue(model)
            .ShouldEqual(669);

            propInfo.Single(prop => prop.PropertyType == typeof(string))
            .GetValue(model)
            .ShouldEqual("testing");
        }
Esempio n. 10
0
        public void Should_get_properties()
        {
            // Given
            var propInfo = BindingMemberInfo.Collect <TestModel>().Where(prop => prop.Name.EndsWith("Property"));
            var model    = new TestModel();

            // When
            model.IntProperty    = 1701;
            model.StringProperty = "NancyFX Unit Testing";

            // Then
            propInfo.Single(prop => prop.PropertyType == typeof(int))
            .GetValue(model)
            .ShouldEqual(1701);

            propInfo.Single(prop => prop.PropertyType == typeof(string))
            .GetValue(model)
            .ShouldEqual("NancyFX Unit Testing");
        }
Esempio n. 11
0
        public void Should_deserialize_enum()
        {
            // Given
            var json       = this.serializer.Serialize(TestEnum.One);
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
            var context    = new BindingContext()
            {
                DestinationType          = typeof(TestEnum),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestEnum>().ToList(),
            };

            // When
            var result = (TestEnum)this.deserialize.Deserialize(
                "application/json",
                bodyStream,
                context);

            // Then
            result.ShouldEqual(TestEnum.One);
        }
Esempio n. 12
0
        public void Should_deserialize_timespan()
        {
            // Given
            var json       = this.serializer.Serialize(TimeSpan.FromDays(14));
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
            var context    = new BindingContext()
            {
                DestinationType          = typeof(TimeSpan),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TimeSpan>().ToList(),
            };

            // When
            var result = (TimeSpan)this.deserialize.Deserialize(
                "application/json",
                bodyStream,
                context);

            // Then
            result.Days.ShouldEqual(14);
        }
Esempio n. 13
0
        public void Should_deserialize_list_of_primitives()
        {
            // Given
            var context = new BindingContext()
            {
                DestinationType          = typeof(TestModel),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestModel>().ToList(),
            };

            var model =
                new TestModel
            {
                ListOfPrimitivesProperty = new List <int> {
                    1, 3, 5
                },
                ListOfPrimitivesField = new List <int> {
                    2, 4, 6
                },
            };

            var s          = new JavaScriptSerializer();
            var serialized = s.Serialize(model);
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(serialized));

            // When
            var result = (TestModel)this.deserialize.Deserialize(
                "application/json",
                bodyStream,
                context);

            // Then
            result.ListOfPrimitivesProperty.ShouldHaveCount(3);
            result.ListOfPrimitivesProperty[0].ShouldEqual(1);
            result.ListOfPrimitivesProperty[1].ShouldEqual(3);
            result.ListOfPrimitivesProperty[2].ShouldEqual(5);

            result.ListOfPrimitivesField.ShouldHaveCount(3);
            result.ListOfPrimitivesField[0].ShouldEqual(2);
            result.ListOfPrimitivesField[1].ShouldEqual(4);
            result.ListOfPrimitivesField[2].ShouldEqual(6);
        }
        public void Should_deserialize_xml_model()
        {
            // Given
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(this.testModelXml));
            var context    = new BindingContext()
            {
                DestinationType          = typeof(TestModel),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestModel>().ToArray(),
            };

            // When
            var result = (TestModel)this.deserialize.Deserialize(
                "application/xml",
                bodyStream,
                context);

            // Then
            result.ShouldNotBeNull();
            result.ShouldBeOfType(typeof(TestModel));
            result.ShouldEqual(this.testModel);
        }
        public void Should_deserialize_if_body_stream_accessed_before_bind()
        {
            // Given
            var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(this.testModelXml));
            var context    = new BindingContext()
            {
                DestinationType          = typeof(TestModel),
                ValidModelBindingMembers = BindingMemberInfo.Collect <TestModel>(),
            };

            var reader = new StreamReader(bodyStream);
            var dataThatDoesntResetStreamPosition = reader.ReadToEnd();

            // When
            var result = (TestModel)this.deserialize.Deserialize(
                "application/xml",
                bodyStream,
                context);

            // Then
            result.ShouldNotBeNull();
            result.ShouldBeOfType(typeof(TestModel));
            result.ShouldEqual(this.testModel);
        }