public void BinderThrowsIfBindingContextIsNull() {
            // Arrange
            DeserializeAttribute attr = new DeserializeAttribute();
            IModelBinder binder = attr.GetBinder();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    binder.BindModel(null, null);
                }, "bindingContext");
        }
        public void BinderReturnsNullIfValueProviderDoesNotContainKey() {
            // Arrange
            DeserializeAttribute attr = new DeserializeAttribute();
            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new SimpleValueProvider()
            };

            // Act
            object retVal = binder.BindModel(null, mbContext);

            // Assert
            Assert.IsNull(retVal, "Binder should return null if no data was present.");
        }
Ejemplo n.º 3
0
        public void BinderReturnsDeserializedValue() {
            // Arrange
            DeserializeAttribute attr = new DeserializeAttribute();
            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new SimpleValueProvider() {
                    { "someKey", "/wECKg==" }
                }
            };

            // Act
            object retVal = binder.BindModel(null, mbContext);

            // Assert
            Assert.AreEqual(42, retVal, "Object was not properly deserialized.");
        }
        public void BinderReturnsDeserializedValue() {
            // Arrange
            DeserializeAttribute attr = new DeserializeAttribute();
            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new Dictionary<string, ValueProviderResult>() {
                    { "someKey", new ValueProviderResult("/wECKg==", "/wECKg==", CultureInfo.InvariantCulture) }
                }
            };

            // Act
            object retVal = binder.BindModel(null, mbContext);

            // Assert
            Assert.AreEqual(42, retVal, "Object was not properly deserialized.");
        }
        public void BinderThrowsIfDataCorrupt() {
            // Arrange
            Mock<MvcSerializer> mockSerializer = new Mock<MvcSerializer>();
            mockSerializer.Setup(o => o.Deserialize(It.IsAny<string>(), It.IsAny<SerializationMode>())).Throws(new SerializationException());
            DeserializeAttribute attr = new DeserializeAttribute() { Serializer = mockSerializer.Object };

            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new SimpleValueProvider() {
                    { "someKey", "This data is corrupted." }
                }
            };

            // Act & assert
            Exception exception = ExceptionHelper.ExpectException<SerializationException>(
                delegate {
                    binder.BindModel(null, mbContext);
                });
        }
        public void BinderReturnsDeserializedValue() {
            // Arrange
            Mock<MvcSerializer> mockSerializer = new Mock<MvcSerializer>();
            mockSerializer.Setup(o => o.Deserialize("some-value", SerializationMode.EncryptedAndSigned)).Returns(42);
            DeserializeAttribute attr = new DeserializeAttribute(SerializationMode.EncryptedAndSigned) { Serializer = mockSerializer.Object };

            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new SimpleValueProvider() {
                    { "someKey", "some-value" }
                }
            };

            // Act
            object retVal = binder.BindModel(null, mbContext);

            // Assert
            Assert.AreEqual(42, retVal, "Object was not properly deserialized.");
        }
Ejemplo n.º 7
0
        public void BinderThrowsIfDataCorrupt() {
            // Arrange
            DeserializeAttribute attr = new DeserializeAttribute();
            IModelBinder binder = attr.GetBinder();
            ModelBindingContext mbContext = new ModelBindingContext() {
                ModelName = "someKey",
                ValueProvider = new SimpleValueProvider() {
                    { "someKey", "This data is corrupted." }
                }
            };

            // Act & assert
            Exception exception = ExceptionHelper.ExpectException<SerializationException>(
                delegate {
                    binder.BindModel(null, mbContext);
                },
                @"Deserialization failed. Verify that the data is being deserialized using the same SerializationMode with which it was serialized. Otherwise see the inner exception.");

            Assert.IsNotNull(exception.InnerException, "Inner exception was not propagated correctly.");
        }