mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-05 20:34:07 +02:00
Add BrowserUtils.IsValidUrl for http(s)/ftp/mailto url checking with unit tests
This commit is contained in:
parent
7b23686dc6
commit
9e225530a6
@ -27,6 +27,17 @@ public static string HeaderUserAgent{
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsValidUrl(string url){
|
||||
Uri uri;
|
||||
|
||||
if (Uri.TryCreate(url, UriKind.Absolute, out uri)){
|
||||
string scheme = uri.Scheme;
|
||||
return scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeHttps || scheme == Uri.UriSchemeFtp || scheme == Uri.UriSchemeMailto;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void OpenExternalBrowser(string url){
|
||||
using(Process.Start(url)){}
|
||||
}
|
||||
|
@ -4,6 +4,37 @@
|
||||
namespace UnitTests.Core.Utils{
|
||||
[TestClass]
|
||||
public class TestBrowserUtils{
|
||||
[TestMethod]
|
||||
public void TestIsValidUrl(){
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com")); // base
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://www.google.com")); // www.
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.co.uk")); // co.uk
|
||||
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("https://google.com")); // https
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("ftp://google.com")); // ftp
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("mailto:someone@google.com")); // mailto
|
||||
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com/")); // trailing slash
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com/?")); // trailing question mark
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com/?a=5&b=x")); // parameters
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com/#hash")); // parameters + hash
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://google.com/?a=5&b=x#hash")); // parameters + hash
|
||||
|
||||
foreach(string tld in new string[]{ "accountants", "blackfriday", "cancerresearch", "coffee", "cool", "foo", "travelersinsurance" }){
|
||||
Assert.IsTrue(BrowserUtils.IsValidUrl("http://test."+tld)); // long and unusual TLDs
|
||||
}
|
||||
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("explorer")); // file
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("explorer.exe")); // file
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("://explorer.exe")); // file-sorta
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("file://explorer.exe")); // file-proper
|
||||
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("")); // empty
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("lol")); // random
|
||||
|
||||
Assert.IsFalse(BrowserUtils.IsValidUrl("gopher://nobody.cares")); // lmao rekt
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetFileNameFromUrl(){
|
||||
Assert.AreEqual("index.html", BrowserUtils.GetFileNameFromUrl("http://test.com/index.html"));
|
||||
|
Loading…
Reference in New Issue
Block a user