// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; using Microsoft.Framework.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation { public class StringLengthAttributeAdapterTest { [Fact] [ReplaceCulture] public void GetClientValidationRules_WithMaxLength_ReturnsValidationParameters() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(8); var adapter = new StringLengthAttributeAdapter(attribute); var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, provider, requestServices); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("length", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(8, rule.ValidationParameters["max"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } [Fact] [ReplaceCulture] public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParameters() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(10) { MinimumLength = 3 }; var adapter = new StringLengthAttributeAdapter(attribute); var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, provider, requestServices); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("length", rule.ValidationType); Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal(3, rule.ValidationParameters["min"]); Assert.Equal(10, rule.ValidationParameters["max"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } } }