public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var expected = TestPlatformHelper.IsMono ? "Invalid length." : "The input is not a valid Base-64 string as it contains a non-base 64 character," + " more than two padding characters, or an illegal character among the padding characters. "; var valueProvider = new SimpleHttpValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.True(binderResult); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["foo"].Errors); Assert.Equal(expected, error.ErrorMessage); }
public async Task BindModelSetsModelToNullOnNullOrEmptyString(string value) { // Arrange var valueProvider = new SimpleValueProvider() { { "foo", value } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); var modelState = Assert.Single(bindingContext.ModelState); Assert.Equal("foo", modelState.Key); Assert.Equal(string.Empty, modelState.Value.RawValue); }
public async Task BindModelSetsModelToNullOnNullOrEmptyString(string value) { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "foo", value } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); var modelState = Assert.Single(bindingContext.ModelState); Assert.Equal("foo", modelState.Key); Assert.NotNull(modelState.Value.Value); Assert.Equal(value, modelState.Value.Value.RawValue); }
public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var expected = TestPlatformHelper.IsMono ? "Invalid length." : "The supplied value is invalid for foo."; var valueProvider = new SimpleHttpValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["foo"].Errors); Assert.Equal(expected, error.ErrorMessage); }
public void BindModelThrowsIfBindingContextIsNull() { // Arrange ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act & assert Assert.ThrowsArgumentNull( delegate { binder.BindModel(null, null); }, "bindingContext"); }
public void BindModelThrowsIfBindingContextIsNull() { // Arrange ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act & assert ExceptionHelper.ExpectArgumentNullException( delegate { binder.BindModel(null, null); }, "bindingContext"); }
public async Task BindModel_ReturnsNull_ForOtherTypes() { // Arrange var bindingContext = GetBindingContext(null, typeof(int[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.Equal(ModelBindingResult.NoResult, binderResult); }
public async Task BindModel_ReturnsNull_ForOtherTypes() { // Arrange var bindingContext = GetBindingContext(new SimpleValueProvider(), typeof(int[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.Equal(default(ModelBindingResult), binderResult); }
public async Task ByteArrayModelBinderReturnsFalseForOtherTypes() { // Arrange var bindingContext = GetBindingContext(null, typeof(int[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.False(binderResult); }
public async Task BindModelSetsModelToNullOnNullOrEmptyString(string value) { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "foo", value } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(binderResult); }
public async Task BindModelReturnsFalseWhenValueNotFound() { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "someName", "" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.False(binderResult); }
public void BinderWithEmptyStringValueReturnsNull() { // Arrange ValueProviderDictionary valueProvider = new ValueProviderDictionary(null) { { "foo", new ValueProviderResult(String.Empty, null, null) } }; ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = valueProvider }; ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act object binderResult = binder.BindModel(null, bindingContext); // Assert Assert.IsNull(binderResult); }
public async Task BindModel() { // Arrange var valueProvider = new SimpleValueProvider() { { "foo", "Fys1" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); var bytes = Assert.IsType<byte[]>(binderResult.Model); Assert.Equal(new byte[] { 23, 43, 53 }, bytes); }
public void BindModelWithBase64QuotedValueReturnsByteArray() { // Arrange string base64Value = Base64TestString; ValueProviderDictionary valueProvider = new ValueProviderDictionary(null) { { "foo", new ValueProviderResult("\"" + base64Value + "\"", "\"" + base64Value + "\"", null) } }; ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = valueProvider }; ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act byte[] boundValue = binder.BindModel(null, bindingContext) as byte[]; // Assert CollectionAssert.AreEqual(Base64TestBytes, boundValue); }
public async Task BindModel() { // Arrange var valueProvider = new SimpleValueProvider() { { "foo", "Fys1" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(NullLoggerFactory.Instance); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.True(bindingContext.Result.IsModelSet); var bytes = Assert.IsType <byte[]>(bindingContext.Result.Model); Assert.Equal(new byte[] { 23, 43, 53 }, bytes); }
public async Task BindModel() { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "foo", "Fys1" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.True(binderResult); var bytes = Assert.IsType <byte[]>(bindingContext.Model); Assert.Equal(new byte[] { 23, 43, 53 }, bytes); }
public async Task BindModel_ReturnsWithIsModelSetFalse_WhenValueNotFound() { // Arrange var valueProvider = new SimpleValueProvider() { { "someName", "" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(NullLoggerFactory.Instance); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.False(bindingContext.Result.IsModelSet); Assert.Null(bindingContext.Result.Model); Assert.Empty(bindingContext.ModelState); // No submitted data for "foo". }
public void CreateBinder_CreatesNoOpBinder_WhenPropertyBindingIsNotAllowed() { // Arrange var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForProperty <Widget>(nameof(Widget.Id)) .BindingDetails(m => m.IsBindingAllowed = false); var modelBinder = new ByteArrayModelBinder(NullLoggerFactory.Instance); var options = Options.Create(new MvcOptions()); options.Value.ModelBinderProviders.Add(new TestModelBinderProvider(c => { if (c.Metadata.ModelType == typeof(WidgetId)) { return(modelBinder); } return(null); })); var factory = new ModelBinderFactory( metadataProvider, options, GetServices()); var context = new ModelBinderFactoryContext() { Metadata = metadataProvider.GetMetadataForProperty(typeof(Widget), nameof(Widget.Id)), }; // Act var result = factory.CreateBinder(context); // Assert Assert.NotNull(result); Assert.IsType <NoOpBinder>(result); }
public void BinderWithEmptyStringValueReturnsNull() { // Arrange SimpleValueProvider valueProvider = new SimpleValueProvider() { { "foo", "" } }; ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = valueProvider }; ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act object binderResult = binder.BindModel(null, bindingContext); // Assert Assert.Null(binderResult); }
public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.True(binderResult); Assert.False(bindingContext.ModelState.IsValid); Assert.Equal(1, bindingContext.ModelState.Values.Count); Assert.Equal("The input is not a valid Base-64 string as it contains a non-base 64 character," + " more than two padding characters, or an illegal character among the padding characters. ", bindingContext.ModelState.Values.First().Errors[0].ErrorMessage); }
public async Task BindModelSetsModelToNullOnNullOrEmptyString(string value) { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "foo", value } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); Assert.Empty(bindingContext.ModelState); // No submitted value for "foo". }
public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var expected = "The value '\"Fys1\"' is not valid for Byte[]."; var valueProvider = new SimpleValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["foo"].Errors); Assert.Equal(expected, error.ErrorMessage); }
public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var expected = "The value '\"Fys1\"' is not valid for Byte[]."; var valueProvider = new SimpleValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["foo"].Errors); Assert.Equal(expected, error.ErrorMessage); }
public void BindModelWithBase64UnquotedValueReturnsByteArray() { // Arrange string base64Value = Base64TestString; SimpleValueProvider valueProvider = new SimpleValueProvider() { { "foo", base64Value } }; ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = valueProvider }; ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act byte[] boundValue = binder.BindModel(null, bindingContext) as byte[]; // Assert Assert.Equal(Base64TestBytes, boundValue); }
public void BindModelWithBase64UnquotedValueReturnsByteArray() { // Arrange string base64Value = Base64TestString; ValueProviderDictionary valueProvider = new ValueProviderDictionary(null) { { "foo", new ValueProviderResult(base64Value, base64Value, null) } }; ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = valueProvider }; ByteArrayModelBinder binder = new ByteArrayModelBinder(); // Act byte[] boundValue = binder.BindModel(null, bindingContext) as byte[]; // Assert CollectionAssert.AreEqual(Base64TestBytes, boundValue); }
public async Task BindModel_ReturnsWithIsModelSetFalse_WhenValueNotFound() { // Arrange var valueProvider = new SimpleValueProvider() { { "someName", "" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); Assert.Empty(bindingContext.ModelState); // No submitted data for "foo". }
/// <summary> /// Bind a <see cref="DynamicViewModel" />. /// </summary> /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param> /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param> /// <returns>A <see cref="DynamicViewModel" /> binded with its corresponding values from the binding context value provider.</returns> public object BindDynamicViewModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { #if DEBUG var step = MiniProfiler.Current.Step("InfrastructureModelBinder.BindDynamicViewModel"); try { #endif byte[] serializedModel = new ByteArrayModelBinder().BindModel(controllerContext, bindingContext) as byte[]; if (serializedModel == null) { return(null); } // Deserialize to retrieve dynamic view model without user supplied values var model = serializedModel.Deserialize() as DynamicViewModel; int count = 0; string indexName = CreateSubIndexName(bindingContext.ModelName, count); if (model != null) { var updateIndex = new Dictionary <int, object>(); int valueIndex = 0; foreach (var value in model.Values) { if (value is LabelViewModel) { valueIndex++; continue; } var properties = TypeDescriptor.GetProperties(value).Cast <PropertyDescriptor>(); // Loop through each property in the current row foreach (var property in properties) { if (property.Name != "Value") { continue; } var propertyName = string.Format("{0}.Value", indexName); // Prepare new binding context for current property var propertyBindingContext = new ModelBindingContext { ModelMetadata = System.Web.Mvc.ModelMetadataProviders.Current.GetMetadataForProperty(() => value, value.GetType(), property.Name), ModelName = propertyName, ModelState = bindingContext.ModelState, PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; // Get binded value var bindedPropertyValue = BindModel(controllerContext, propertyBindingContext); var bindable = propertyBindingContext.ModelMetadata.GetAttribute <BindableAttribute>(); // Only bind if bindable if (bindable != null && bindable.IsBindable()) { property.SetValue(value, bindedPropertyValue); updateIndex.Add(valueIndex, value); } // Get next index name indexName = CreateSubIndexName(bindingContext.ModelName, ++count); } valueIndex++; } foreach (var update in updateIndex) { model.Values[update.Key] = update.Value; } } return(model); #if DEBUG } finally { if (step != null) { step.Dispose(); } } #endif }
public async Task BindModel_ReturnsNull_ForOtherTypes() { // Arrange var bindingContext = GetBindingContext(new SimpleValueProvider(), typeof(int[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.Equal(ModelBindingResult.NoResult, binderResult); }
public async Task BindModel_ReturnsNull_ForOtherTypes() { // Arrange var bindingContext = GetBindingContext(null, typeof(int[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(binderResult); }
/// <summary>Binds the model by using the specified controller context and binding context.</summary> /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param> /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param> /// <returns>The bound object.</returns> /// <exception cref="T:System.ArgumentNullException">The <paramref name="bindingContext" />parameter is null.</exception> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { #if DEBUG var step = MiniProfiler.Current.Step("InfrastructureModelBinder.BindModel"); try { #endif if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } var serialized = bindingContext.ModelMetadata.GetAttribute <SerializedAttribute>(); if (serialized != null) { byte[] model = new ByteArrayModelBinder().BindModel(controllerContext, bindingContext) as byte[]; if (model == null) { return(null); } return(model.Deserialize()); } // Allow binding for currency values if (bindingContext.ModelMetadata.DataTypeName == DataType.Currency.ToString()) { string attemptedValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue; if (bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(double?)) { double d; double.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out d); return(d); } if (bindingContext.ModelType == typeof(decimal) || bindingContext.ModelType == typeof(decimal?)) { decimal d; decimal.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out d); return(d); } if (bindingContext.ModelType == typeof(float) || bindingContext.ModelType == typeof(float?)) { float f; float.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out f); return(f); } } if (bindingContext.ModelType == typeof(SelectList)) { return(BindSelectList(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(MultiSelectList)) { return(BindMultiSelectList(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(IEnumerable <SelectListItem>)) { return(BindEnumerableSelectListItem(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(IEnumerable <string>)) { return(BindEnumerableString(controllerContext, bindingContext)); } if (bindingContext.ModelType.IsGenericType && new[] { typeof(IPageable <>), typeof(Pageable <>) }.Contains(bindingContext.ModelType.GetGenericTypeDefinition())) { return(BindPageable(controllerContext, bindingContext)); } if (bindingContext.ModelMetadata.DataTypeName == CustomDataType.Grid || bindingContext.ModelMetadata.DataTypeName == CustomDataType.GridEditable) { return(BindGrid(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(DynamicViewModel)) { return(BindDynamicViewModel(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(AddressViewModel)) { return(BindAddressViewModel(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(JobseekerSearchViewModel)) { return(BindJobSeekerSearchViewModel(controllerContext, bindingContext)); } if (bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(DateTime?)) { return(BindDateTime(controllerContext, bindingContext)); } return(base.BindModel(controllerContext, bindingContext)); #if DEBUG } finally { if (step != null) { step.Dispose(); } } #endif }
public async Task BindModelReturnsFalseWhenValueNotFound() { // Arrange var valueProvider = new SimpleHttpValueProvider() { { "someName", "" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(binderResult); }