public async Task TryBindStrongModel_InnerBinderReturnsAResult_ReturnsInnerBinderResult( object model, bool isSuccess) { // Arrange ModelBindingResult?innerResult; if (isSuccess) { innerResult = ModelBindingResult.Success("somename.key", model); } else { innerResult = ModelBindingResult.Failed("somename.key"); } var innerBinder = new StubModelBinder(context => { Assert.Equal("someName.key", context.ModelName); return(innerResult); }); var bindingContext = GetBindingContext(new SimpleValueProvider(), innerBinder); var binder = new KeyValuePairModelBinder <int, string>(); // Act var result = await binder.TryBindStrongModel <int>(bindingContext, "key"); // Assert Assert.Equal(innerResult.Value, result); Assert.Empty(bindingContext.ModelState); }
public async Task ModelBinder_ReturnsNonEmptyResult_SetsNullValue_SetsModelStateKey() { // Arrange var bindingContext = new DefaultModelBindingContext { FallbackToEmptyPrefix = true, ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(List <int>)), ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext(), ValueProvider = new SimpleValueProvider { { "someOtherName", "dummyValue" } }, FieldName = "someName", }; var mockIntBinder = new StubModelBinder(context => { context.Result = ModelBindingResult.Success("someName", model: null); }); var composite = CreateCompositeBinder(mockIntBinder); // Act var result = await composite.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); Assert.Equal("someName", result.Key); Assert.Null(result.Model); }
public async Task ModelBinder_DoesNotFallBackToEmpty_IfErrorsAreAdded() { // Arrange var bindingContext = new DefaultModelBindingContext { FallbackToEmptyPrefix = false, ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(List <int>)), ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext(), ValueProvider = new SimpleValueProvider { { "someOtherName", "dummyValue" } }, FieldName = "someName", }; var mockIntBinder = new StubModelBinder(context => { Assert.Equal("someName", context.ModelName); context.ModelState.AddModelError(context.ModelName, "this is an error message"); context.Result = ModelBindingResult.Failed("someName"); }); var composite = CreateCompositeBinder(mockIntBinder); // Act & Assert var result = await composite.BindModelResultAsync(bindingContext); Assert.Equal(1, mockIntBinder.BindModelCount); }
public async Task ModelBinder_ReturnsNothing_IfBinderMatchesButDoesNotSetModel() { // Arrange var bindingContext = new DefaultModelBindingContext { FallbackToEmptyPrefix = true, ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(List <int>)), ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext(), ValueProvider = new SimpleValueProvider { { "someOtherName", "dummyValue" } }, FieldName = "someName", }; var mockIntBinder = new StubModelBinder(context => { context.Result = ModelBindingResult.Failed("someName"); }); var composite = CreateCompositeBinder(mockIntBinder); // Act var result = await composite.BindModelResultAsync(bindingContext); // Assert Assert.Equal(default(ModelBindingResult), result); }
private static IModelBinder CreateIntBinder() { var mockIntBinder = new StubModelBinder(mbc => { if (mbc.ModelType == typeof(int)) { var model = 42; return(ModelBindingResult.Success(mbc.ModelName, model)); } return(null); }); return(mockIntBinder); }
public async Task BindModel_DoesNotAddAValidationNode_IfModelBindingResultIsNothing() { // Arrange var mockBinder = new StubModelBinder(); var binder = CreateCompositeBinder(mockBinder); var valueProvider = new SimpleValueProvider(); var bindingContext = CreateBindingContext(binder, valueProvider, typeof(SimplePropertiesModel)); // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.Equal(default(ModelBindingResult), result); }
public async Task BindModel_SuccessfulBind_ComplexTypeFallback_ReturnsModel() { // Arrange var expectedModel = new List <int> { 1, 2, 3, 4, 5 }; var bindingContext = new DefaultModelBindingContext { FallbackToEmptyPrefix = true, IsTopLevelObject = true, ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(List <int>)), ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext(), ValueProvider = new SimpleValueProvider { { "someOtherName", "dummyValue" } }, ValidationState = new ValidationStateDictionary(), FieldName = "someName", }; var mockIntBinder = new StubModelBinder(mbc => { if (!string.IsNullOrEmpty(mbc.ModelName)) { return; } Assert.Same(bindingContext.ModelMetadata, mbc.ModelMetadata); Assert.Equal("", mbc.ModelName); Assert.Same(bindingContext.ValueProvider, mbc.ValueProvider); mbc.Result = ModelBindingResult.Success(string.Empty, expectedModel); }); var shimBinder = CreateCompositeBinder(mockIntBinder); // Act var result = await shimBinder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); Assert.Equal(string.Empty, result.Key); Assert.Equal(expectedModel, result.Model); }
private static DefaultModelBindingContext GetModelBindingContext( bool isReadOnly, IDictionary <string, KeyValuePair <int, string> > values) { var metadataProvider = new TestModelMetadataProvider(); metadataProvider .ForProperty <ModelWithIDictionaryProperty>(nameof(ModelWithIDictionaryProperty.DictionaryProperty)) .BindingDetails(bd => bd.IsReadOnly = isReadOnly); var metadata = metadataProvider.GetMetadataForProperty( typeof(ModelWithIDictionaryProperty), nameof(ModelWithIDictionaryProperty.DictionaryProperty)); var binder = new StubModelBinder(mbc => { KeyValuePair <int, string> value; if (values.TryGetValue(mbc.ModelName, out value)) { mbc.Result = ModelBindingResult.Success(mbc.ModelName, value); } }); var valueProvider = new SimpleValueProvider(); foreach (var kvp in values) { valueProvider.Add(kvp.Key, string.Empty); } var bindingContext = new DefaultModelBindingContext { ModelMetadata = metadata, ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext { ModelBinder = binder.Object, MetadataProvider = metadataProvider, ValueProvider = valueProvider, }, ValueProvider = valueProvider, ValidationState = new ValidationStateDictionary(), }; return(bindingContext); }
public async Task BindModel_UsesTheValidationNodeOnModelBindingResult_IfPresent() { // Arrange var valueProvider = new SimpleValueProvider(); var mockBinder = new StubModelBinder(ModelBindingResult.Success("someName", 42)); var binder = CreateCompositeBinder(mockBinder); var bindingContext = CreateBindingContext(binder, valueProvider, typeof(SimplePropertiesModel)); // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); }
public async Task BindModel_SuccessfulBind_SetsValidationStateAtTopLevel() { // Arrange var bindingContext = new DefaultModelBindingContext { FallbackToEmptyPrefix = true, IsTopLevelObject = true, ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(int)), ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext(), ValueProvider = new SimpleValueProvider { { "someName", "dummyValue" } }, ValidationState = new ValidationStateDictionary(), FieldName = "someName", }; var mockIntBinder = new StubModelBinder(context => { Assert.Same(bindingContext.ModelMetadata, context.ModelMetadata); Assert.Equal("someName", context.ModelName); Assert.Same(bindingContext.ValueProvider, context.ValueProvider); context.Result = ModelBindingResult.Success("someName", 42); }); var shimBinder = CreateCompositeBinder(mockIntBinder); // Act var result = await shimBinder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); Assert.Equal(42, result.Model); Assert.Contains(result.Model, bindingContext.ValidationState.Keys); var entry = bindingContext.ValidationState[result.Model]; Assert.Equal("someName", entry.Key); Assert.Same(bindingContext.ModelMetadata, entry.Metadata); }
public async Task BindModel_MissingKeyAndMissingValue_DoNotAddModelStateError() { // Arrange var valueProvider = new SimpleValueProvider(); // Create int binder to create the value but not the key. var bindingContext = GetBindingContext(valueProvider); var mockBinder = new StubModelBinder(); bindingContext.OperationBindingContext.ModelBinder = mockBinder; var binder = new KeyValuePairModelBinder <int, string>(); // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.Equal(default(ModelBindingResult), result); Assert.True(bindingContext.ModelState.IsValid); Assert.Equal(0, bindingContext.ModelState.ErrorCount); }