public async Task BindParameter_WithEmptyData_AndPerTypeMessage_AddsGivenMessage(Type parameterType) { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForType(parameterType) .BindingDetails((Action <ModelBinding.Metadata.BindingMetadata>)(binding => { // A real details provider could customize message based on BindingMetadataProviderContext. binding.ModelBindingMessageProvider.ValueMustNotBeNullAccessor = value => $"Hurts when '{ value }' is provided."; })); var argumentBinder = new DefaultControllerActionArgumentBinder( metadataProvider, ModelBindingTestHelper.GetObjectValidator()); var parameter = new ParameterDescriptor { Name = "Parameter1", BindingInfo = new BindingInfo(), ParameterType = parameterType }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("Parameter1", string.Empty); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.False(modelBindingResult.IsModelSet); // Model Assert.Null(modelBindingResult.Model); // ModelState Assert.False(modelState.IsValid); var key = Assert.Single(modelState.Keys); Assert.Equal("Parameter1", key); Assert.Equal(string.Empty, modelState[key].AttemptedValue); Assert.Equal(string.Empty, modelState[key].RawValue); var error = Assert.Single(modelState[key].Errors); Assert.Equal("Hurts when '' is provided.", error.ErrorMessage, StringComparer.Ordinal); Assert.Null(error.Exception); }
public async Task KeyValuePairModelBinder_SimpleTypes_WithNoValue_AndCustomizedMessage_AddsGivenMessage() { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForType(typeof(KeyValuePair <string, int>)) .BindingDetails((System.Action <ModelBinding.Metadata.BindingMetadata>)(binding => { // A real details provider could customize message based on BindingMetadataProviderContext. binding.ModelBindingMessageProvider.MissingKeyOrValueAccessor = () => $"Hurts when nothing is provided."; })); var argumentBinder = new DefaultControllerActionArgumentBinder( metadataProvider, ModelBindingTestHelper.GetObjectValidator()); var parameter = new ParameterDescriptor { Name = "parameter", ParameterType = typeof(KeyValuePair <string, int>) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString("?parameter.Key=10"); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); Assert.Equal(2, modelState.Count); Assert.False(modelState.IsValid); Assert.Equal(1, modelState.ErrorCount); var entry = Assert.Single(modelState, kvp => kvp.Key == "parameter.Key").Value; Assert.Empty(entry.Errors); Assert.Equal("10", entry.AttemptedValue); Assert.Equal("10", entry.RawValue); entry = Assert.Single(modelState, kvp => kvp.Key == "parameter.Value").Value; var error = Assert.Single(entry.Errors); Assert.Null(error.Exception); Assert.Equal("Hurts when nothing is provided.", error.ErrorMessage); }
public async Task MutableObjectModelBinder_WithBindRequired_NoData_AndCustomizedMessage_AddsGivenMessage() { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForType(typeof(Order10)) .BindingDetails((Action<ModelBinding.Metadata.BindingMetadata>)(binding => { // A real details provider could customize message based on BindingMetadataProviderContext. binding.ModelBindingMessageProvider.MissingBindRequiredValueAccessor = name => $"Hurts when '{ name }' is not provided."; })); var argumentBinder = new DefaultControllerActionArgumentBinder( metadataProvider, ModelBindingTestHelper.GetObjectValidator()); var parameter = new ParameterDescriptor() { Name = "parameter", ParameterType = typeof(Order10) }; // No Data var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType<Order10>(modelBindingResult.Model); Assert.Null(model.Customer); Assert.Equal(1, modelState.Count); Assert.Equal(1, modelState.ErrorCount); Assert.False(modelState.IsValid); var entry = Assert.Single(modelState, e => e.Key == "Customer").Value; Assert.Null(entry.RawValue); Assert.Null(entry.AttemptedValue); var error = Assert.Single(modelState["Customer"].Errors); Assert.Equal("Hurts when 'Customer' is not provided.", error.ErrorMessage); }
public async Task KeyValuePairModelBinder_SimpleTypes_WithNoKey_AndCustomizedMessage_AddsGivenMessage() { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForType(typeof(KeyValuePair<string, int>)) .BindingDetails((System.Action<ModelBinding.Metadata.BindingMetadata>)(binding => { // A real details provider could customize message based on BindingMetadataProviderContext. binding.ModelBindingMessageProvider.MissingKeyOrValueAccessor = () => $"Hurts when nothing is provided."; })); var argumentBinder = new DefaultControllerActionArgumentBinder( metadataProvider, ModelBindingTestHelper.GetObjectValidator()); var parameter = new ParameterDescriptor { Name = "parameter", ParameterType = typeof(KeyValuePair<string, int>) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString("?parameter.Value=10"); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); Assert.Equal(2, modelState.Count); Assert.False(modelState.IsValid); Assert.Equal(1, modelState.ErrorCount); var entry = Assert.Single(modelState, kvp => kvp.Key == "parameter.Key").Value; var error = Assert.Single(entry.Errors); Assert.Null(error.Exception); Assert.Equal("Hurts when nothing is provided.", error.ErrorMessage); entry = Assert.Single(modelState, kvp => kvp.Key == "parameter.Value").Value; Assert.Empty(entry.Errors); Assert.Equal("10", entry.AttemptedValue); Assert.Equal("10", entry.RawValue); }
public async Task BindParameter_WithEmptyData_AndPerTypeMessage_AddsGivenMessage(Type parameterType) { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForType(parameterType) .BindingDetails((Action<ModelBinding.Metadata.BindingMetadata>)(binding => { // A real details provider could customize message based on BindingMetadataProviderContext. binding.ModelBindingMessageProvider.ValueMustNotBeNullAccessor = value => $"Hurts when '{ value }' is provided."; })); var argumentBinder = new DefaultControllerActionArgumentBinder( metadataProvider, ModelBindingTestHelper.GetObjectValidator()); var parameter = new ParameterDescriptor { Name = "Parameter1", BindingInfo = new BindingInfo(), ParameterType = parameterType }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("Parameter1", string.Empty); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.False(modelBindingResult.IsModelSet); // Model Assert.Null(modelBindingResult.Model); // ModelState Assert.False(modelState.IsValid); var key = Assert.Single(modelState.Keys); Assert.Equal("Parameter1", key); Assert.Equal(string.Empty, modelState[key].AttemptedValue); Assert.Equal(string.Empty, modelState[key].RawValue); var error = Assert.Single(modelState[key].Errors); Assert.Equal("Hurts when '' is provided.", error.ErrorMessage, StringComparer.Ordinal); Assert.Null(error.Exception); }