public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceType_set_but_ErrorMessageResourceName_not_set()
 {
     var attribute = new UrlAttribute();
     attribute.ErrorMessageResourceName = null;
     attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
 }
        public static void Validate_throws_for_invalid_values()
        {
            var attribute = new UrlAttribute();

            Assert.Throws <ValidationException>(() => attribute.Validate("file:///foo.bar", s_testValidationContext)); // file scheme
            Assert.Throws <ValidationException>(() => attribute.Validate("foo.png", s_testValidationContext));         // no scheme
        }
        public static void Validate_throws_InvalidOperationException_if_ErrorMessage_is_null()
        {
            var attribute = new UrlAttribute();

            attribute.ErrorMessage = null; // note: this overrides the default value
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
        }
 public static void Validate_throws_InvalidOperationException_if_ErrorMessage_and_ErrorMessageResourceName_are_set()
 {
     var attribute = new UrlAttribute();
     attribute.ErrorMessage = "SomeErrorMessage";
     attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
 }
        public static void UrlAttribute_creation_DataType_and_CustomDataType()
        {
            var attribute = new UrlAttribute();

            Assert.Equal(DataType.Url, attribute.DataType);
            Assert.Null(attribute.CustomDataType);
        }
        public static void Validate_throws_for_invalid_values()
        {
            var attribute = new UrlAttribute();

            Assert.Throws<ValidationException>(() => attribute.Validate("file:///foo.bar", s_testValidationContext)); // file scheme
            Assert.Throws<ValidationException>(() => attribute.Validate("foo.png", s_testValidationContext)); // no scheme
        }
        public static void Validate_throws_InvalidOperationException_if_ErrorMessage_and_ErrorMessageResourceName_are_set()
        {
            var attribute = new UrlAttribute();

            attribute.ErrorMessage             = "SomeErrorMessage";
            attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
        }
        public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceType_set_but_ErrorMessageResourceName_not_set()
        {
            var attribute = new UrlAttribute();

            attribute.ErrorMessageResourceName = null;
            attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
        }
        public static void Validate_successful_for_valid_values()
        {
            var attribute = new UrlAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate("http://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("https://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("ftp://foo.bar", s_testValidationContext));
        }
        public void IsValidWithoutRequiringProtocol()
        {
            var attribute = new UrlAttribute(requireProtocol: false);

            Assert.IsTrue(attribute.IsValid("foo.bar"));
            Assert.IsTrue(attribute.IsValid("www.foo.bar"));
            Assert.IsTrue(attribute.IsValid("http://foo.bar"));
            Assert.IsFalse(attribute.IsValid("htp://foo.bar"));
        }
        public void IsValidWithUppercaseLetters()
        {
            var attribute = new UrlAttribute();

            Assert.IsTrue(attribute.IsValid("http://FOO.bar"));
            Assert.IsTrue(attribute.IsValid("https://foo.BAR"));
            Assert.IsTrue(attribute.IsValid("ftp://FOO.BAR"));
            Assert.IsFalse(attribute.IsValid("file:///Foo.Bar"));
        }
示例#12
0
        public static void Validate_throws_for_invalid_values()
        {
            var attribute = new UrlAttribute();

            Assert.Throws <ValidationException>(() => attribute.Validate("file:///foo.bar", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("http://user%[email protected]/", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("foo.png", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("http://foo\0.bar", s_testValidationContext)); // Illegal character
        }
示例#13
0
        public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
        {
            var attribute         = new UrlAttribute();
            var toBeTested        = new UrlClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "UrlPropertyToBeTested";
            AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
        }
示例#14
0
        public static void Validate_throws_for_invalid_values()
        {
            var attribute = new UrlAttribute();

            Assert.Throws<ValidationException>(() => attribute.Validate("file:///foo.bar", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("http://user%[email protected]/", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("foo.png", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("http://foo\0.bar", s_testValidationContext)); // Illegal character
        }
示例#15
0
        public static void Validate_successful_for_valid_values()
        {
            var attribute = new UrlAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate("http://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("https://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("ftp://foo.bar", s_testValidationContext));
        }
        public void IsValidExcludeProtocol()
        {
            var attribute = new UrlAttribute(excludeProtocol: true);

            Assert.IsTrue(attribute.IsValid("foo.bar"));
            Assert.IsTrue(attribute.IsValid("www.foo.bar"));
            Assert.IsFalse(attribute.IsValid("http://foo.bar"));
            Assert.IsFalse(attribute.IsValid("htp://foo.bar"));
        }
示例#17
0
 public static void GetValidationResult_returns_ErrorMessage_if_ErrorMessage_overrides_default()
 {
     var attribute = new UrlAttribute();
     attribute.ErrorMessage = "SomeErrorMessage";
     var toBeTested = new UrlClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "UrlPropertyToBeTested";
     var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
     Assert.Equal("SomeErrorMessage", validationResult.ErrorMessage);
 }
        public void IsValidWithoutRequiringProtocol()
        {
            var attribute = new UrlAttribute(UrlOptions.OptionalProtocol);

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("foo.bar"));
            Assert.IsTrue(attribute.IsValid("www.foo.bar"));
            Assert.IsTrue(attribute.IsValid("http://foo.bar"));
            Assert.IsFalse(attribute.IsValid("htp://foo.bar"));
        }
        public void ErrorMessageTest()
        {
            var attribute = new UrlAttribute();
            attribute.ErrorMessage = "SampleErrorMessage";

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("SampleErrorMessage", result.ErrorMessage);
        }
        public void IsValidDisallowProtocol()
        {
            var attribute = new UrlAttribute(UrlOptions.DisallowProtocol);

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("foo.bar"));
            Assert.IsTrue(attribute.IsValid("www.foo.bar"));
            Assert.IsTrue(attribute.IsValid("localhost"));
            Assert.IsFalse(attribute.IsValid("https://localhost"));
            Assert.IsFalse(attribute.IsValid("http://foo.bar"));
            Assert.IsFalse(attribute.IsValid("htp://foo.bar"));
        }
        public void IsValidTests()
        {
            var attribute = new UrlAttribute();

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("http://foo.bar"));
            Assert.IsTrue(attribute.IsValid("https://foo.bar"));
            Assert.IsTrue(attribute.IsValid("ftp://foo.bar"));
            Assert.IsFalse(attribute.IsValid("file:///foo.bar"));
            Assert.IsFalse(attribute.IsValid("http://user%[email protected]/"));
            Assert.IsFalse(attribute.IsValid("foo.png"));
        }
        public void ErrorResourcesTest()
        {
            var attribute = new UrlAttribute();
            attribute.ErrorMessageResourceName = "ErrorMessage";
            attribute.ErrorMessageResourceType = typeof(ErrorResources);

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("error message", result.ErrorMessage);
        }
示例#23
0
        public static void GetValidationResult_returns_ErrorMessage_if_ErrorMessage_overrides_default()
        {
            var attribute = new UrlAttribute();

            attribute.ErrorMessage = "SomeErrorMessage";
            var toBeTested        = new UrlClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "UrlPropertyToBeTested";
            var validationResult = attribute.GetValidationResult(toBeTested, validationContext);

            Assert.Equal("SomeErrorMessage", validationResult.ErrorMessage);
        }
        public void GlobalizedErrorResourcesTest()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

            var attribute = new UrlAttribute();
            attribute.ErrorMessageResourceName = "ErrorMessage";
            attribute.ErrorMessageResourceType = typeof(ErrorResources);

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("mensaje de error", result.ErrorMessage);
        }
示例#25
0
        public static void GetValidationResultWithErrorMessageResourceNameAndType()
        {
            var attribute = new UrlAttribute();

            attribute.ErrorMessageResourceName = "InternalErrorMessageTestProperty";
            attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
            var toBeTested        = new UrlClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "UrlPropertyToBeTested";
            var validationResult = attribute.GetValidationResult(toBeTested, validationContext);

            Assert.Equal(
                "Error Message from ErrorMessageResources.InternalErrorMessageTestProperty",
                validationResult.ErrorMessage);
        }
        public void IsValidWithUri()
        {
            var attribute = new UrlAttribute();

            Assert.IsTrue(attribute.IsValid(new Uri("http://foo.bar")));
            Assert.IsTrue(attribute.IsValid(new Uri("http://FOO.bar")));
            Assert.IsTrue(attribute.IsValid(new Uri("https://foo.BAR")));
            Assert.IsTrue(attribute.IsValid(new Uri("ftp://FOO.BAR")));

            Assert.IsFalse(attribute.IsValid(new Uri("file:///foo.bar")));
        }
示例#27
0
 public static void UrlAttribute_creation_DataType_and_CustomDataType()
 {
     var attribute = new UrlAttribute();
     Assert.Equal(DataType.Url, attribute.DataType);
     Assert.Null(attribute.CustomDataType);
 }
示例#28
0
 public static void Validate_throws_InvalidOperationException_if_ErrorMessage_is_null()
 {
     var attribute = new UrlAttribute();
     attribute.ErrorMessage = null; // note: this overrides the default value
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("foo.png", s_testValidationContext));
 }
示例#29
0
 public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
 {
     var attribute = new UrlAttribute();
     var toBeTested = new UrlClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "UrlPropertyToBeTested";
     AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
 }
示例#30
0
 public static void GetValidationResultWithErrorMessageResourceNameAndType()
 {
     var attribute = new UrlAttribute();
     attribute.ErrorMessageResourceName = "InternalErrorMessageTestProperty";
     attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
     var toBeTested = new UrlClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "UrlPropertyToBeTested";
     var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
     Assert.Equal(
         "Error Message from ErrorMessageResources.InternalErrorMessageTestProperty",
         validationResult.ErrorMessage);
 }