예제 #1
0
        public void IsValid()
        {
            var attribute = new MustBeValidUriAttribute();

            Assert.AreEqual(true, attribute.IsValid(null));
            Assert.AreEqual(true, attribute.IsValid(DBNull.Value));
            Assert.AreEqual(false, attribute.IsValid(string.Empty));
            Assert.AreEqual(false, attribute.IsValid("sdfdsf"));
            Assert.AreEqual(false, attribute.IsValid("www.google.com"));
            Assert.AreEqual(false, attribute.IsValid("google.com"));
            Assert.AreEqual(true, attribute.IsValid("http://google"));
            Assert.AreEqual(true, attribute.IsValid("http://www.google.com/"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com/"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com/index.html"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com/search?text=aaa"));

            attribute.HostOnly = true;

            Assert.AreEqual(true, attribute.IsValid("http://www.google.com/"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com/"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com"));
            Assert.AreEqual(false, attribute.IsValid("https://www.google.com/index.html"));
            Assert.AreEqual(false, attribute.IsValid("https://www.google.com/search?text=aaa"));

            attribute.ValidSchemas = new string[] { "https" };

            Assert.AreEqual(false, attribute.IsValid("http://www.google.com/"));
            Assert.AreEqual(false, attribute.IsValid("ftp://www.google.com/"));
            Assert.AreEqual(true, attribute.IsValid("https://www.google.com/"));
        }
예제 #2
0
        public void IsValidFail(object value)
        {
            var attribute = new MustBeValidUriAttribute();

            try
            {
                attribute.IsValid(value);

                Assert.Fail();
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Value must be of type String.", ex.Message);
            }
        }