Skip to content

Commit

Permalink
cleanup project logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 12, 2024
1 parent ac92867 commit 473b625
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleProviders;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest.DemoRules
Expand All @@ -10,8 +11,10 @@ public class DomainParserTestWithIdnMappingNormalization : DomainParserTest
{
protected override IDomainParser GetDomainParser(List<TldRule> rules)
{
var ruleProvider = new StaticRuleProvider(rules);
var domainNormalizer = new IdnMappingDomainNormalizer();
return new DomainParser(rules, domainNormalizer);

return new DomainParser(ruleProvider, domainNormalizer);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleProviders;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest.DemoRules
Expand All @@ -10,9 +11,10 @@ public class DomainParserTestWithUriNormalization : DomainParserTest
{
protected override IDomainParser GetDomainParser(List<TldRule> rules)
{
var ruleProvider = new StaticRuleProvider(rules);
var domainNormalizer = new UriDomainNormalizer();

return new DomainParser(rules, domainNormalizer);
return new DomainParser(ruleProvider, domainNormalizer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.Exceptions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleProviders;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest
Expand All @@ -27,8 +28,10 @@ public void Initialize()
new TldRule("com")
};

this._parserUsingUriNormalization = new DomainParser(rules, new UriDomainNormalizer());
this._parserUsingIdnNormalization = new DomainParser(rules, new IdnMappingDomainNormalizer());
var ruleProvider = new StaticRuleProvider(rules);

this._parserUsingUriNormalization = new DomainParser(ruleProvider, new UriDomainNormalizer());
this._parserUsingIdnNormalization = new DomainParser(ruleProvider, new IdnMappingDomainNormalizer());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PublicSuffixTestsWithIdnMappingNormalization : PublicSuffixTest
[TestInitialize()]
public void Initialize()
{
var domainParser = new DomainParser(new FileTldRuleProvider("public_suffix_list.dat"), new IdnMappingDomainNormalizer());
var domainParser = new DomainParser(new LocalFileRuleProvider("public_suffix_list.dat"), new IdnMappingDomainNormalizer());
this._domainParser = domainParser;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PublicSuffixTestsWithUriNormalization : PublicSuffixTest
[TestInitialize()]
public void Initialize()
{
var domainParser = new DomainParser(new FileTldRuleProvider("public_suffix_list.dat"), new UriDomainNormalizer());
var domainParser = new DomainParser(new LocalFileRuleProvider("public_suffix_list.dat"), new UriDomainNormalizer());
this._domainParser = domainParser;
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.RuleProviders;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.UnitTest
Expand All @@ -11,18 +13,24 @@ public class RuleProviderTest
[TestMethod]
public async Task WebTldRuleProviderTest()
{
var webRuleProvider = new WebRuleProvider();
var rules = await webRuleProvider.BuildAsync();
Assert.IsNotNull(rules);
var builder = new ConfigurationBuilder();
using var httpClient = new HttpClient();

var configuration = builder.Build();

var webRuleProvider = new WebRuleProvider(configuration, httpClient);
var domainDataStructure = await webRuleProvider.BuildAsync();
Assert.IsNotNull(domainDataStructure);
}

[TestMethod]
public async Task FileTldRuleProviderTest()
{
var localFileRuleProvider = new LocalFileRuleProvider("public_suffix_list.dat");
var rules = await localFileRuleProvider.BuildAsync();
Assert.AreEqual(9609, rules.Count());
Assert.IsNotNull(rules);
var domainDataStructure = await localFileRuleProvider.BuildAsync();

Assert.IsNotNull(domainDataStructure);
Assert.AreEqual(1460, domainDataStructure.Nested.Count);
}
}
}
3 changes: 3 additions & 0 deletions src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Nager.PublicSuffix.DomainNormalizers
{
/// <summary>
/// Interface DomainNormalizer
/// </summary>
public interface IDomainNormalizer
{
List<string> PartlyNormalizeDomainAndExtractFullyNormalizedParts(string domain, out string partlyNormalizedDomain);

Check warning on line 10 in src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'

Check warning on line 10 in src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'

Check warning on line 10 in src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Nager.PublicSuffix.DomainNormalizers
{
/// <summary>
/// IdnMappingNormalizer
/// IdnMapping DomainNormalizer
/// </summary>
public class IdnMappingDomainNormalizer : IDomainNormalizer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Nager.PublicSuffix.DomainNormalizers
{
/// <summary>
/// Uri DomainNormalizer
/// </summary>
public class UriDomainNormalizer : IDomainNormalizer
{
public List<string> PartlyNormalizeDomainAndExtractFullyNormalizedParts(string domain, out string partlyNormalizedDomain)

Check warning on line 13 in src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UriDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'

Check warning on line 13 in src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UriDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'

Check warning on line 13 in src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UriDomainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(string, out string)'
Expand Down
51 changes: 9 additions & 42 deletions src/Nager.PublicSuffix/DomainParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.Exceptions;
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleProviders;
using System;
Expand All @@ -14,55 +13,30 @@ namespace Nager.PublicSuffix
/// </summary>
public class DomainParser : IDomainParser
{
private DomainDataStructure _domainDataStructure;
private readonly DomainDataStructure _domainDataStructure;
private readonly IDomainNormalizer _domainNormalizer;
private readonly TldRule _rootTldRule = new TldRule("*");

/// <summary>
/// Creates and initializes a DomainParser
/// </summary>
/// <param name="rules">The list of rules.</param>
/// <param name="domainNormalizer">An <see cref="IDomainNormalizer"/>.</param>
public DomainParser(IEnumerable<TldRule> rules, IDomainNormalizer domainNormalizer = null)
: this(domainNormalizer)
{
if (rules == null)
{
throw new ArgumentNullException(nameof(rules));
}

this.AddRules(rules);
}

/// <summary>
/// Creates and initializes a DomainParser
/// </summary>
/// <param name="ruleProvider">A rule provider from interface <see cref="IRuleProvider"/>.</param>
/// <param name="domainNormalizer">An <see cref="IDomainNormalizer"/>.</param>
public DomainParser(IRuleProvider ruleProvider, IDomainNormalizer domainNormalizer = null)
public DomainParser(
IRuleProvider ruleProvider,
IDomainNormalizer domainNormalizer = default)
: this(domainNormalizer)
{
var rules = ruleProvider.BuildAsync().GetAwaiter().GetResult();
this.AddRules(rules);
}

/// <summary>
/// Creates a DomainParser based on an already initialzed tree.
/// </summary>
/// <param name="initializedDataStructure">An already initialized tree.</param>
/// <param name="domainNormalizer">An <see cref="IDomainNormalizer"/>.</param>
public DomainParser(DomainDataStructure initializedDataStructure, IDomainNormalizer domainNormalizer = null)
: this(domainNormalizer)
{
this._domainDataStructure = initializedDataStructure;
var domainDataStructure = ruleProvider.BuildAsync().GetAwaiter().GetResult();
this._domainDataStructure = domainDataStructure;
}

private DomainParser(IDomainNormalizer domainNormalizer)
{
this._domainNormalizer = domainNormalizer ?? new UriDomainNormalizer();
}

///<inheritdoc/>
/// <inheritdoc/>
public DomainInfo Parse(Uri domain)
{
var partlyNormalizedDomain = domain.Host;
Expand All @@ -76,14 +50,14 @@ public DomainInfo Parse(Uri domain)
return this.GetDomainFromParts(partlyNormalizedDomain, parts);
}

///<inheritdoc/>
/// <inheritdoc/>
public DomainInfo Parse(string domain)
{
var parts = this._domainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(domain, out string partlyNormalizedDomain);
return this.GetDomainFromParts(partlyNormalizedDomain, parts);
}

///<inheritdoc/>
/// <inheritdoc/>
public bool IsValidDomain(string domain)
{
if (string.IsNullOrEmpty(domain))
Expand Down Expand Up @@ -129,13 +103,6 @@ public bool IsValidDomain(string domain)
}
}

private void AddRules(IEnumerable<TldRule> tldRules)
{
this._domainDataStructure = this._domainDataStructure ?? new DomainDataStructure("*", this._rootTldRule);

this._domainDataStructure.AddRules(tldRules);
}

private DomainInfo GetDomainFromParts(string domain, List<string> parts)
{
if (parts == null || parts.Count == 0 || parts.Any(x => x.Equals(string.Empty)))
Expand Down
3 changes: 2 additions & 1 deletion src/Nager.PublicSuffix/Models/DomainDataStructure.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using System.Linq;

namespace Nager.PublicSuffix.Models
{
/// <summary>
/// Represents a tree of TLD domains
/// Represents a tree of Top Level Domains (TLD)
/// </summary>
public class DomainDataStructure
{
Expand Down
4 changes: 4 additions & 0 deletions src/Nager.PublicSuffix/Nager.PublicSuffix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions src/Nager.PublicSuffix/RuleProviders/IRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -15,6 +14,6 @@ public interface IRuleProvider
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>Returns the TldRules</returns>
Task<IEnumerable<TldRule>> BuildAsync(CancellationToken cancellationToken = default);
Task<DomainDataStructure> BuildAsync(CancellationToken cancellationToken = default);
}
}
14 changes: 9 additions & 5 deletions src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleParsers;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -23,15 +23,19 @@ public LocalFileRuleProvider(string filePath)
this._filePath = filePath;
}

///<inheritdoc/>
public async Task<IEnumerable<TldRule>> BuildAsync(
/// <inheritdoc/>
public async Task<DomainDataStructure> BuildAsync(
CancellationToken cancellationToken = default)
{
var ruleData = await this.LoadFromFile().ConfigureAwait(false);

var ruleParser = new TldRuleParser();
var rules = ruleParser.ParseRules(ruleData);
return rules;

var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(rules);

return domainDataStructure;
}

private async Task<string> LoadFromFile()
Expand Down
44 changes: 44 additions & 0 deletions src/Nager.PublicSuffix/RuleProviders/StaticRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.RuleProviders
{
/// <summary>
/// StaticRuleProvider
/// </summary>
public class StaticRuleProvider : IRuleProvider
{
private readonly DomainDataStructure _domainDataStructure;

/// <summary>
/// StaticRuleProvider
/// </summary>
/// <param name="domainDataStructure"></param>
public StaticRuleProvider(DomainDataStructure domainDataStructure)
{
this._domainDataStructure = domainDataStructure;
}

/// <summary>
/// StaticRuleProvider
/// </summary>
/// <param name="tldRules"></param>
public StaticRuleProvider(IEnumerable<TldRule> tldRules)
{
var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(tldRules);

this._domainDataStructure = domainDataStructure;
}

/// <inheritdoc/>
public Task<DomainDataStructure> BuildAsync(
CancellationToken cancellationToken = default)
{
return Task.FromResult(this._domainDataStructure);
}
}
}
Loading

0 comments on commit 473b625

Please sign in to comment.