Skip to content

Commit

Permalink
optimize demo project
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed May 31, 2024
1 parent e1851cb commit e5609c4
Showing 1 changed file with 70 additions and 49 deletions.
119 changes: 70 additions & 49 deletions src/Nager.PublicSuffix.TestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,87 @@
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Nager.PublicSuffix;
using Nager.PublicSuffix.RuleProviders;

#region CachedHttpRuleProvider
Console.WriteLine("Run - DemoCachedHttpRuleProviderAsync");
await DemoCachedHttpRuleProviderAsync();
Console.WriteLine("Run - DemoSimpleHttpRuleProviderAsync");
await DemoSimpleHttpRuleProviderAsync();
Console.WriteLine("Run - DemoLocalFileRuleProviderAsync");
await DemoLocalFileRuleProviderAsync();

//using var loggerFactory = LoggerFactory.Create(builder =>
//{
// builder.AddConsole();
//});

//var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>();

//IConfiguration configuration = new ConfigurationBuilder()
// .AddInMemoryCollection(new List<KeyValuePair<string, string?>>
// {
// new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list1.dat")
// })
// .Build();

//var httpClient = new HttpClient();
//var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();

//var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient);

#endregion

#region SimpleHttpRuleProvider

var ruleProvider = new SimpleHttpRuleProvider();

#endregion
async Task DemoCachedHttpRuleProviderAsync()
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});

#region LocalFileRuleProvider
var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>();

//var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat");
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new List<KeyValuePair<string, string?>>
{
new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list.dat")
})
.Build();

#endregion
using var httpClient = new HttpClient();
var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();

await ruleProvider.BuildAsync(ignoreCache: true);
var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient);

var domainParser = new DomainParser(ruleProvider);
var domainInfo = domainParser.Parse("www.google.com");
await CheckAsync(ruleProvider);
}

if (domainInfo != null)
async Task DemoSimpleHttpRuleProviderAsync()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine($"{"TLD:", 20} {domainInfo.TopLevelDomain}");
Console.WriteLine($"{"FQDN:", 20} { domainInfo.FullyQualifiedDomainName}");
Console.WriteLine($"{"RegistrableDomain:", 20} {domainInfo.RegistrableDomain}");
Console.WriteLine($"{"Subdomain:",20} {domainInfo.Subdomain}");
Console.WriteLine("------------------------------------------------");
}
using var ruleProvider = new SimpleHttpRuleProvider();

var validDomains = new[] { "www.google.com", "amazon.com", "microsoft.de", "mail.google.com", "microsoft.de." };
var invalidDomains = new[] { "www", "uk", "co.uk", ".", "[email protected]" };
await CheckAsync(ruleProvider);
}

foreach (var validDomain in validDomains)
async Task DemoLocalFileRuleProviderAsync()
{
Console.WriteLine($"{validDomain} -> {domainParser.IsValidDomain(validDomain)}");
var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat");

await CheckAsync(ruleProvider);
}

foreach (var invalidDomain in invalidDomains)
async Task CheckAsync(IRuleProvider ruleProvider)
{
Console.WriteLine($"{invalidDomain} -> {domainParser.IsValidDomain(invalidDomain)}");
}
if (!await ruleProvider.BuildAsync(ignoreCache: true))
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Build error");
Console.ResetColor();
return;
}

var domainParser = new DomainParser(ruleProvider);
var domainInfo = domainParser.Parse("www.google.com");

if (domainInfo != null)
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine($"{"TLD:",20} {domainInfo.TopLevelDomain}");
Console.WriteLine($"{"FQDN:",20} {domainInfo.FullyQualifiedDomainName}");
Console.WriteLine($"{"RegistrableDomain:",20} {domainInfo.RegistrableDomain}");
Console.WriteLine($"{"Subdomain:",20} {domainInfo.Subdomain}");
Console.WriteLine("------------------------------------------------");
}

var validDomains = new[] { "www.google.com", "amazon.com", "microsoft.de", "mail.google.com", "microsoft.de." };
var invalidDomains = new[] { "www", "uk", "co.uk", ".", "[email protected]" };

foreach (var validDomain in validDomains)
{
Console.WriteLine($"{validDomain} -> {domainParser.IsValidDomain(validDomain)}");
}

foreach (var invalidDomain in invalidDomains)
{
Console.WriteLine($"{invalidDomain} -> {domainParser.IsValidDomain(invalidDomain)}");
}
}

0 comments on commit e5609c4

Please sign in to comment.