public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(ComplexModel), false /* allowNullModel */); ComplexModel complexModel = (ComplexModel)bindingContext.Model; foreach (ModelMetadata propertyMetadata in complexModel.PropertyMetadata) { ModelBindingContext propertyBindingContext = new ModelBindingContext(bindingContext) { ModelMetadata = propertyMetadata, ModelName = ModelBinderUtil.CreatePropertyModelName(bindingContext.ModelName, propertyMetadata.PropertyName) }; // bind and propagate the values IModelBinder propertyBinder = bindingContext.ModelBinderProviders.GetBinder(modelBindingExecutionContext, propertyBindingContext); if (propertyBinder != null) { if (propertyBinder.BindModel(modelBindingExecutionContext, propertyBindingContext)) { complexModel.Results[propertyMetadata] = new ComplexModelResult(propertyBindingContext.Model, propertyBindingContext.ValidationNode); } else { complexModel.Results[propertyMetadata] = null; } } } return(true); }
public void GetPossibleBinderInstance_NoMatch_ReturnsNull() { // Act IExtensibleModelBinder binder = ModelBinderUtil.GetPossibleBinderInstance(typeof(ArraySegment <int>), typeof(List <>), typeof(SampleGenericBinder <>)); // Assert Assert.Null(binder); }
public void CreatePropertyModelName_EmptyParentName() { // Act string fullChildName = ModelBinderUtil.CreatePropertyModelName("", "childName"); // Assert Assert.Equal("childName", fullChildName); }
public void ValidateBindingContextThrowsIfBindingContextIsNull() { // Act & assert ExceptionHelper.ExpectArgumentNullException( delegate { ModelBinderUtil.ValidateBindingContext(null, typeof(string), true); }, "bindingContext"); }
public void CreateIndexModelName_IntIndex() { // Act string fullChildName = ModelBinderUtil.CreateIndexModelName("parentName", 42); // Assert Assert.Equal("parentName[42]", fullChildName); }
public void RawValueToObjectArray_RawValueIsObject_WrapsObjectInSingleElementArray() { // Act object[] retVal = ModelBinderUtil.RawValueToObjectArray(42); // Assert Assert.Equal(new object[] { 42 }, retVal); }
public void CastOrDefault_CorrectType_ReturnsInput() { // Act int retVal = ModelBinderUtil.CastOrDefault <int>(42); // Assert Assert.Equal(42, retVal); }
public void CreateIndexModelName_StringIndex() { // Act string fullChildName = ModelBinderUtil.CreateIndexModelName("parentName", "index"); // Assert Assert.Equal("parentName[index]", fullChildName); }
public void RawValueToObjectArray_RawValueIsString_WrapsStringInSingleElementArray() { // Act object[] retVal = ModelBinderUtil.RawValueToObjectArray("hello"); // Assert Assert.Equal(new object[] { "hello" }, retVal); }
public void CastOrDefault_IncorrectType_ReturnsDefaultTModel() { // Act DateTime retVal = ModelBinderUtil.CastOrDefault <DateTime>(42); // Assert Assert.Equal(default(DateTime), retVal); }
public void CreateIndexModelName_EmptyParentName() { // Act string fullChildName = ModelBinderUtil.CreateIndexModelName("", 42); // Assert Assert.Equal("[42]", fullChildName); }
public void GetPossibleBinderInstance_Match_ReturnsBinder() { // Act IExtensibleModelBinder binder = ModelBinderUtil.GetPossibleBinderInstance(typeof(List <int>), typeof(List <>), typeof(SampleGenericBinder <>)); // Assert Assert.IsType <SampleGenericBinder <int> >(binder); }
public void ValidateBindingContextThrowsIfBindingContextIsNull() { // Act & assert Assert.ThrowsArgumentNull( delegate { ModelBinderUtil.ValidateBindingContext(null, typeof(string), true); }, "bindingContext" ); }
public void ValidateBindingContextThrowsIfModelMetadataIsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert Assert.Throws <ArgumentException>( delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); }, "The binding context cannot have a null ModelMetadata." + Environment.NewLine + "Parameter name: bindingContext"); }
public void RawValueToObjectArray_RawValueIsObjectArray_ReturnsInputInstance() { // Assert object[] original = new object[2]; // Act object[] retVal = ModelBinderUtil.RawValueToObjectArray(original); // Assert Assert.Same(original, retVal); }
public void ValidateBindingContextThrowsIfModelMetadataIsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert ExceptionHelper.ExpectArgumentException( delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); }, @"The binding context cannot have a null ModelMetadata. Parameter name: bindingContext"); }
public void ValidateBindingContext_SuccessWithNullModel() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = GetMetadata(typeof(string)) }; // Act ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); // Assert // Nothing to do - if we got this far without throwing, the test succeeded }
public void RawValueToObjectArray_RawValueIsEnumerable_ReturnsInputAsArray() { // Assert List <int> original = new List <int> { 1, 2, 3, 4 }; // Act object[] retVal = ModelBinderUtil.RawValueToObjectArray(original); // Assert Assert.Equal(new object[] { 1, 2, 3, 4 }, retVal); }
public void ValidateBindingContextThrowsIfModelIsNullButCannotBe() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = GetMetadata(typeof(string)) }; // Act & assert Assert.Throws <ArgumentException>( delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), false); }, "The binding context has a null Model, but this binder requires a non-null model of type 'System.String'." + Environment.NewLine + "Parameter name: bindingContext"); }
public void ValidateBindingContextThrowsIfModelTypeIsWrong() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = GetMetadata(typeof(object)) }; // Act & assert Assert.Throws <ArgumentException>( delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); }, "The binding context has a ModelType of 'System.Object', but this binder can only operate on models of type 'System.String'." + Environment.NewLine + "Parameter name: bindingContext"); }
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullDisabled_ModelIsNotEmptyString_LeavesModelAlone() { // Arrange ModelMetadata modelMetadata = GetMetadata(typeof(string)); modelMetadata.ConvertEmptyStringToNull = true; // Act object model = 42; ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model); // Assert Assert.Equal(42, model); }
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullEnabled_ModelIsWhitespaceString_ReplacesModelWithNull() { // Arrange ModelMetadata modelMetadata = GetMetadata(typeof(string)); modelMetadata.ConvertEmptyStringToNull = true; // Act object model = " "; // whitespace ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model); // Assert Assert.Null(model); }
public void Login_DeveSer_CPF() { var ctr = GetContaController(); var form = new FormCollection { { nameof(LoginVM.Login), "*****@*****.**" }, { nameof(LoginVM.Senha), "0123456789012345678901234567890123456789" }, }; var model = ModelBinderUtil.BindModel <LoginVM>(ctr, form); var view = ctr.Login(model) as ViewResult; Assert.AreEqual(Mensagem.M011, ctr.ModelState["Login"].Errors.First().ErrorMessage); Assert.IsNotNull(view); Assert.AreEqual("Login", view.ViewName); }
public void ValidateBindingContextThrowsIfModelInstanceIsWrongType() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = GetMetadata(typeof(string)) }; bindingContext.ModelMetadata.Model = 42; // Act & assert Assert.Throws <ArgumentException>( delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); }, @"The binding context has a Model of type 'System.Int32', but this binder can only operate on models of type 'System.String'. Parameter name: bindingContext"); }
public void Login_Invalido_Lanca_Excecao() { var mockServicoUsuario = new Mock <IUsuarioServico>(); mockServicoUsuario.Setup(x => x.Autenticar(It.IsAny <UsuarioDTO>())) .Throws(new NegocioException(Mensagem.M001)); var ctr = GetContaController(mockServicoUsuario); var form = new FormCollection { { nameof(LoginVM.Login), "01614865175" }, { nameof(LoginVM.Senha), "1234567890" }, }; var model = ModelBinderUtil.BindModel <LoginVM>(ctr, form); ctr.Login(model); }
public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) { ValueProviderResult vpResult = GetCompatibleValueProviderResult(bindingContext); if (vpResult == null) { return(false); // conversion would have failed } bindingContext.ModelState.SetModelValue(bindingContext.ModelName, vpResult); object model = vpResult.RawValue; ModelBinderUtil.ReplaceEmptyStringWithNull(bindingContext.ModelMetadata, ref model); bindingContext.Model = model; return(true); }
public void Login_Valido_Redireciona_DeVoltaPadrao_Se_NaoInformar_URÇ() { var mockServicoUsuario = new Mock <IUsuarioServico>(); var ctr = GetContaController(mockServicoUsuario); var form = new FormCollection { { nameof(LoginVM.Login), "01614865175" }, { nameof(LoginVM.Senha), "1234567890" }, }; var model = ModelBinderUtil.BindModel <LoginVM>(ctr, form); var view = ctr.Login(model) as RedirectResult; mockServicoUsuario.Verify(m => m.Autenticar(It.IsAny <UsuarioDTO>()), Times.Once()); Assert.IsNotNull(view); Assert.IsTrue(view.Url == ReturnedUrl); }
internal static ValueProviderResult GetCompatibleValueProviderResult(ModelBindingContext bindingContext) { ModelBinderUtil.ValidateBindingContext(bindingContext); ValueProviderResult vpResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, skipValidation: !bindingContext.ValidateRequest); if (vpResult == null) { return(null); // the value doesn't exist } if (!TypeHelpers.IsCompatibleObject(bindingContext.ModelType, vpResult.RawValue)) { return(null); // value is of incompatible type } return(vpResult); }
public override IModelBinder GetBinder(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) { ModelBinderUtil.ValidateBindingContext(bindingContext); if (!bindingContext.UnvalidatedValueProvider.ContainsPrefix(bindingContext.ModelName)) { // no values to bind return(null); } if (bindingContext.ModelType == typeof(ComplexModel)) { // forbidden type - will cause a stack overflow if we try binding this type return(null); } return(new MutableObjectModelBinder()); }
public void Senha_DeveSer_Obrigatorio() { var ctrl = GetContaController(); var form = new FormCollection { { nameof(LoginVM.Login), "01614865175" }, }; var model = ModelBinderUtil.BindModel <LoginVM>(ctrl, form); var view = ctrl.Login(model) as ViewResult; Assert.IsFalse(ctrl.ModelState.IsValid); Assert.IsTrue(ctrl.ModelState["Senha"].Errors.Any(m => m.ErrorMessage == Mensagem.M003)); Assert.IsNotNull(view); Assert.AreEqual("Login", view.ViewName); }