public async Task BindParameterFromService_WithData_GetsBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BinderModelName = "CustomParameter", BindingSource = BindingSource.Services }, // Using a service type already in defaults. ParameterType = typeof(JsonOutputFormatter) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = operationContext.ActionContext.ModelState; // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult Assert.True(modelBindingResult.IsModelSet); // Model var outputFormatter = Assert.IsType <JsonOutputFormatter>(modelBindingResult.Model); Assert.NotNull(outputFormatter); // ModelState Assert.True(modelState.IsValid); Assert.Empty(modelState.Keys); }
public async Task TryUpdateModel_SettableArrayModel_WithPrefix_CreatesArray() { // Arrange var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); var modelState = new ModelStateDictionary(); var model = new Person4(); // Act var result = await TryUpdateModel(model, "prefix", operationContext, modelState); // Assert Assert.True(result); // Model Assert.NotNull(model.Address); Assert.Equal(1, model.Address.Length); Assert.Equal("SomeStreet", model.Address[0].Street); Assert.Null(model.Address[0].City); // ModelState Assert.True(modelState.IsValid); var entry = Assert.Single(modelState); Assert.Equal("prefix.Address[0].Street", entry.Key); var state = entry.Value; Assert.NotNull(state.Value); Assert.Equal("SomeStreet", state.Value.AttemptedValue); Assert.Equal("SomeStreet", state.Value.RawValue); Assert.Empty(state.Errors); Assert.Equal(ModelValidationState.Valid, state.ValidationState); }
public async Task BindParameter_WithData_WithPrefix_GetsBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BinderType = typeof(SuccessModelBinder), BinderModelName = "CustomParameter" }, ParameterType = typeof(Person2) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.NotNull(modelBindingResult); Assert.Equal("Success", modelBindingResult.Model); Assert.Equal("CustomParameter", modelBindingResult.Key); // ModelState Assert.True(modelState.IsValid); var key = Assert.Single(modelState.Keys); Assert.Equal("CustomParameter", key); Assert.Equal(ModelValidationState.Valid, modelState[key].ValidationState); Assert.NotNull(modelState[key].Value); // Value is set by test model binder, no need to validate it. }
public async Task BindParameter_NoData_BindsWithEmptyCollection() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor { Name = "Parameter1", BindingInfo = new BindingInfo { BinderModelName = "CustomParameter", }, ParameterType = typeof(FormCollection) }; // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.NotNull(modelBindingResult); var collection = Assert.IsType <FormCollection>(modelBindingResult.Model); // ModelState Assert.True(modelState.IsValid); Assert.Empty(modelState); // FormCollection Assert.Empty(collection); Assert.Empty(collection.Files); Assert.Empty(collection.Keys); }
public async Task Validation_StringLengthAttribute_OnPropertyOfPOCO_Valid() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "parameter", ParameterType = typeof(Order6) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString("?parameter.Name=bill"); }); var modelState = operationContext.ActionContext.ModelState; // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType <Order6>(modelBindingResult.Model); Assert.Equal("bill", model.Name); Assert.Equal(1, modelState.Count); Assert.Equal(0, modelState.ErrorCount); Assert.True(modelState.IsValid); var entry = Assert.Single(modelState, e => e.Key == "parameter.Name").Value; Assert.Equal("bill", entry.AttemptedValue); Assert.Equal("bill", entry.RawValue); Assert.Empty(entry.Errors); }
public async Task BindParameter_WithEmptyData_BindsMutableAndNullableObjects(Type parameterType) { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); 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.True(modelBindingResult.IsModelSet); // Model Assert.Null(modelBindingResult.Model); // ModelState Assert.True(modelState.IsValid); var key = Assert.Single(modelState.Keys); Assert.Equal("Parameter1", key); Assert.Equal(string.Empty, modelState[key].AttemptedValue); Assert.Equal(new string[] { string.Empty }, modelState[key].RawValue); Assert.Empty(modelState[key].Errors); }
public async Task Validation_RequiredAttribute_OnPOCOPropertyOfBoundElement_WithData() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "parameter", ParameterType = typeof(List <Order5>) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString("?parameter[0].ProductId=17"); }); var modelState = operationContext.ActionContext.ModelState; // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType <List <Order5> >(modelBindingResult.Model); Assert.Equal(17, Assert.Single(model).ProductId); Assert.Equal(1, modelState.Count); Assert.Equal(0, modelState.ErrorCount); Assert.True(modelState.IsValid); var entry = Assert.Single(modelState, e => e.Key == "parameter[0].ProductId").Value; Assert.Equal("17", entry.AttemptedValue); Assert.Equal("17", entry.RawValue); Assert.Empty(entry.Errors); }
public async Task Validation_RequiredAttribute_OnSimpleTypeProperty_WithData() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "parameter", ParameterType = typeof(Order1) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString("?parameter.CustomerName=bill"); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType <Order1>(modelBindingResult.Model); Assert.Equal("bill", model.CustomerName); Assert.Equal(1, modelState.Count); Assert.Equal(0, modelState.ErrorCount); Assert.True(modelState.IsValid); var entry = Assert.Single(modelState, e => e.Key == "parameter.CustomerName").Value; Assert.Equal("bill", entry.AttemptedValue); Assert.Equal("bill", entry.RawValue); Assert.Empty(entry.Errors); }
public async Task KeyValuePairModelBinder_SimpleTypes_WithNoKey_AddsError() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); 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("A value is required.", 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 BindPropertyFromService_WithData_WithPrefix_GetsBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BinderModelName = "CustomParameter", }, ParameterType = typeof(Person) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.True(modelBindingResult.IsModelSet); // Model var boundPerson = Assert.IsType <Person>(modelBindingResult.Model); Assert.NotNull(boundPerson); Assert.NotNull(boundPerson.Address.OutputFormatter); // ModelState Assert.True(modelState.IsValid); Assert.Empty(modelState.Keys); }
public async Task CollectionModelBinder_BindsListOfComplexType_ExplicitPrefix_Success(string queryString) { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "parameter", BindingInfo = new BindingInfo() { BinderModelName = "prefix", }, ParameterType = typeof(List <Person>) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString(queryString); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType <List <Person> >(modelBindingResult.Model); Assert.Equal(10, model[0].Id); Assert.Equal(11, model[1].Id); Assert.Equal(2, modelState.Count); Assert.Equal(0, modelState.ErrorCount); Assert.True(modelState.IsValid); }
public async Task BindParameter_WithData_GetsBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BinderModelName = "CustomParameter", }, ParameterType = typeof(CancellationToken) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.NotNull(modelBindingResult); Assert.True(modelBindingResult.IsModelSet); // Model var token = Assert.IsType <CancellationToken>(modelBindingResult.Model); Assert.NotNull(token); // ModelState Assert.True(modelState.IsValid); Assert.Equal(0, modelState.Count); }
public async Task TryUpdateModel_SettableCollectionModel_EmptyPrefix_CreatesCollection() { // Arrange var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); var modelState = operationContext.ActionContext.ModelState; var model = new Person2(); // Act var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); // Model Assert.NotNull(model.Address); Assert.Equal(1, model.Address.Count); Assert.Equal("SomeStreet", model.Address[0].Street); Assert.Null(model.Address[0].City); // ModelState Assert.True(modelState.IsValid); var entry = Assert.Single(modelState); Assert.Equal("Address[0].Street", entry.Key); var state = entry.Value; Assert.Equal("SomeStreet", state.AttemptedValue); Assert.Equal("SomeStreet", state.RawValue); Assert.Empty(state.Errors); Assert.Equal(ModelValidationState.Valid, state.ValidationState); }
public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_EmptyPrefix_Success(string queryString) { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "parameter", ParameterType = typeof(Dictionary <string, Person>) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString(queryString); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.NotNull(modelBindingResult); Assert.True(modelBindingResult.IsModelSet); var model = Assert.IsType <Dictionary <string, Person> >(modelBindingResult.Model); Assert.Equal(new Dictionary <string, Person> { { "key0", new Person { Id = 10 } }, }, model); Assert.NotEmpty(modelState); Assert.Equal(0, modelState.ErrorCount); Assert.True(modelState.IsValid); }
public async Task BindParameter_NoData_DoesNotGetBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BinderModelName = "CustomParameter", }, ParameterType = typeof(IFormFile) }; // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.ContentType = "multipart/form-data"; }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert // ModelBindingResult Assert.NotNull(modelBindingResult); // Fails due to bug #2456 Assert.Null(modelBindingResult.Model); // ModelState Assert.True(modelState.IsValid); Assert.Empty(modelState.Keys); }
public async Task ActionParameter_ReadOnlyCollectionModel_EmptyPrefix_DoesNotGetBound() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "prefix", ParameterType = typeof(Person6) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); var modelState = new ModelStateDictionary(); // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); // Assert Assert.NotNull(modelBindingResult); Assert.True(modelBindingResult.IsModelSet); // Model var boundModel = Assert.IsType <Person6>(modelBindingResult.Model); Assert.NotNull(boundModel); Assert.NotNull(boundModel.Address); // Arrays should not be updated. Assert.Equal(0, boundModel.Address.Count()); // ModelState Assert.True(modelState.IsValid); Assert.Empty(modelState.Keys); }
public async Task TryUpdateModel_ExistingModel_WithPrefix_GetsOverWritten() { // Arrange var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = QueryString.Create("prefix.Street", "SomeStreet"); }); var modelState = new ModelStateDictionary(); var model = new Address { Street = "DefaultStreet" }; var oldModel = model; // Act var result = await TryUpdateModel(model, "prefix", operationContext, modelState); // Assert Assert.True(result); // Model Assert.Same(oldModel, model); Assert.Equal("SomeStreet", model.Street); // ModelState Assert.True(modelState.IsValid); Assert.Equal(1, modelState.Keys.Count); var key = Assert.Single(modelState.Keys, k => k == "prefix.Street"); Assert.NotNull(modelState[key].Value); Assert.Equal("SomeStreet", modelState[key].Value.AttemptedValue); Assert.Equal("SomeStreet", modelState[key].Value.RawValue); Assert.Empty(modelState[key].Errors); Assert.Equal(ModelValidationState.Valid, modelState[key].ValidationState); }
public async Task ModelMetaDataTypeAttribute_ValidBaseClass_NoModelStateErrors() { // Arrange var input = "{ \"Name\": \"MVC\", \"Contact\":\"4258959019\", \"Category\":\"Technology\"," + "\"CompanyName\":\"Microsoft\", \"Country\":\"USA\",\"Price\": 21, " + "\"ProductDetails\": {\"Detail1\": \"d1\", \"Detail2\": \"d2\", \"Detail3\": \"d3\"}}"; var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", ParameterType = typeof(ProductViewModel), BindingInfo = new BindingInfo() { BindingSource = BindingSource.Body } }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext( request => { request.Body = new MemoryStream(Encoding.UTF8.GetBytes(input)); request.ContentType = "application/json;charset=utf-8"; }); var modelState = operationContext.ActionContext.ModelState; // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var boundPerson = Assert.IsType <ProductViewModel>(modelBindingResult.Model); Assert.True(modelState.IsValid); Assert.NotNull(boundPerson); }
public async Task ModelMetaDataTypeAttribute_ValidDerivedClass_NoModelStateErrors() { // Arrange var input = "{ \"Name\": \"MVC\", \"Contact\":\"4258959019\", \"Category\":\"Technology\"," + "\"CompanyName\":\"Microsoft\", \"Country\":\"USA\", \"Version\":\"2\"," + "\"DatePurchased\": \"/Date(1297246301973)/\", \"Price\" : \"110\" }"; var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor() { Name = "Parameter1", BindingInfo = new BindingInfo() { BindingSource = BindingSource.Body }, ParameterType = typeof(SoftwareViewModel) }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext( request => { request.Body = new MemoryStream(Encoding.UTF8.GetBytes(input)); request.ContentType = "application/json"; }); var modelState = operationContext.ActionContext.ModelState; // Act var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); var boundPerson = Assert.IsType <SoftwareViewModel>(modelBindingResult.Model); Assert.NotNull(boundPerson); Assert.True(modelState.IsValid); }
public async Task BindParameterFromService_NoService_Throws() { // Arrange var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); var parameter = new ParameterDescriptor { Name = "ControllerProperty", BindingInfo = new BindingInfo { BindingSource = BindingSource.Services, }, // Use a service type not available in DI. ParameterType = typeof(IActionResult), }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); // Act & Assert var exception = await Assert.ThrowsAsync <InvalidOperationException>( () => argumentBinder.BindModelAsync(parameter, operationContext)); Assert.Contains(typeof(IActionResult).FullName, exception.Message); }