.Net Validate a URL


         internal bool IsValidURL(string value)
        {
            Uri validUri;
            return  Uri.TryCreate(value, UriKind.Absolute, out validUri);
        }


         [TestMethod]
        public void IsValidUrl()
        {
            // Act
            var actual = Subject.IsValidURL(@"http://www.google.com");
 
            // Assert
            Assert.IsTrue(actual);
        }
 
        [TestMethod]
        public void IsNotValidUrlWithoutHttp()
        {
            // Act
            var actual = Subject.IsValidURL(@"www.google.com");
 
            // Assert
            Assert.IsFalse(actual);
        }
 
        [TestMethod]
        public void UrlStartingWithHttpsIsValid()
        {
            // Act
            var actual = Subject.IsValidURL(@"https://www.google.com");
 
            // Assert
            Assert.IsTrue(actual);
        }

Comments

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?