Skip to content

Commit

Permalink
change project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 12, 2024
1 parent 6634e56 commit 70400bd
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.DemoRules
{
public abstract class DomainParserTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.DemoRules
{
[TestClass]
public class DomainParserTestWithIdnMappingNormalization : DomainParserTest
{
protected override IDomainParser GetDomainParser(List<TldRule> rules)
{
return new DomainParser(rules, new IdnMappingDomainNormalizer());
var domainNormalizer = new IdnMappingDomainNormalizer();
return new DomainParser(rules, domainNormalizer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.DemoRules
{
[TestClass]
public class DomainParserTestWithInitializedStructure : DomainParserTest
Expand All @@ -12,6 +12,7 @@ protected override IDomainParser GetDomainParser(List<TldRule> rules)
{
var structure = new DomainDataStructure("*", new TldRule("*"));
structure.AddRules(rules);

return new DomainParser(structure);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.DemoRules
{
[TestClass]
public class DomainParserTestWithUriNormalization : DomainParserTest
{
protected override IDomainParser GetDomainParser(List<TldRule> rules)
{
return new DomainParser(rules, new UriDomainNormalizer());
var domainNormalizer = new UriDomainNormalizer();

return new DomainParser(rules, domainNormalizer);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.Exceptions;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.RealRules
{
public abstract class PublicSuffixTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.RuleProviders;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.RealRules
{
[TestClass]
public class PublicSuffixTestsWithIdnMappingNormalization : PublicSuffixTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Nager.PublicSuffix.DomainNormalizers;
using Nager.PublicSuffix.RuleProviders;

namespace Nager.PublicSuffix.UnitTest
namespace Nager.PublicSuffix.UnitTest.RealRules
{
[TestClass]
public class PublicSuffixTestsWithUriNormalization : PublicSuffixTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
namespace Nager.PublicSuffix.UnitTest
{
[TestClass]
public class TldRuleProviderTest
public class RuleProviderTest
{
[TestMethod]
public async Task WebTldRuleProviderTest()
{
var tldRuleProvider = new WebTldRuleProvider();
var rules = await tldRuleProvider.BuildAsync();
var webRuleProvider = new WebRuleProvider();

Check failure on line 14 in src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'httpClient' of 'WebRuleProvider.WebRuleProvider(HttpClient, string, ICacheProvider)'

Check failure on line 14 in src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'httpClient' of 'WebRuleProvider.WebRuleProvider(HttpClient, string, ICacheProvider)'
var rules = await webRuleProvider.BuildAsync();
Assert.IsNotNull(rules);
}

[TestMethod]
public async Task FileTldRuleProviderTest()
{
var tldRuleProvider = new FileTldRuleProvider("public_suffix_list.dat");
var rules = await tldRuleProvider.BuildAsync();
var localFileRuleProvider = new LocalFileRuleProvider("public_suffix_list.dat");
var rules = await localFileRuleProvider.BuildAsync();
Assert.AreEqual(9609, rules.Count());
Assert.IsNotNull(rules);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nager.PublicSuffix/DomainParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public DomainParser(IEnumerable<TldRule> rules, IDomainNormalizer domainNormaliz
/// <summary>
/// Creates and initializes a DomainParser
/// </summary>
/// <param name="ruleProvider">A rule provider from interface <see cref="ITopLevelDomainRuleProvider"/>.</param>
/// <param name="ruleProvider">A rule provider from interface <see cref="IRuleProvider"/>.</param>
/// <param name="domainNormalizer">An <see cref="IDomainNormalizer"/>.</param>
public DomainParser(ITopLevelDomainRuleProvider ruleProvider, IDomainNormalizer domainNormalizer = null)
public DomainParser(IRuleProvider ruleProvider, IDomainNormalizer domainNormalizer = null)
: this(domainNormalizer)
{
var rules = ruleProvider.BuildAsync().GetAwaiter().GetResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using Nager.PublicSuffix.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.RuleProviders
{
/// <summary>
/// ITopLevelDomainRuleProvider
/// Interface RuleProvider
/// </summary>
public interface ITopLevelDomainRuleProvider
public interface IRuleProvider
{
/// <summary>
/// Loads the plain text data from a source and parse the public suffix rules
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>Returns the TldRules</returns>
Task<IEnumerable<TldRule>> BuildAsync();
Task<IEnumerable<TldRule>> BuildAsync(CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
using Nager.PublicSuffix.RuleParsers;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.RuleProviders
{
/// <summary>
/// FileTldRuleProvider
/// LocalFileRuleProvider
/// </summary>
public class FileTldRuleProvider : ITopLevelDomainRuleProvider
public class LocalFileRuleProvider : IRuleProvider
{
private readonly string _fileName;
private readonly string _filePath;

/// <summary>
/// FileTldRuleProvider
/// LocalFileRuleProvider
/// </summary>
/// <param name="fileName"></param>
public FileTldRuleProvider(string fileName)
/// <param name="filePath"></param>
public LocalFileRuleProvider(string filePath)
{
this._fileName = fileName;
this._filePath = filePath;
}

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

Expand All @@ -34,12 +35,12 @@ public async Task<IEnumerable<TldRule>> BuildAsync()

private async Task<string> LoadFromFile()
{
if (!File.Exists(this._fileName))
if (!File.Exists(this._filePath))
{
throw new FileNotFoundException("Rule file does not exist");
throw new FileNotFoundException($"Rule file does not exist {this._filePath}");
}

using var reader = File.OpenText(this._fileName);
using var reader = File.OpenText(this._filePath);
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using Nager.PublicSuffix.RuleParsers;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.RuleProviders
{
/// <summary>
/// Web2TldRuleProvider
/// WebRuleProvider
/// </summary>
public class Web2TldRuleProvider : ITopLevelDomainRuleProvider
public class WebRuleProvider : IRuleProvider
{
private readonly string _fileUrl;
private readonly ICacheProvider _cacheProvider;
Expand All @@ -23,13 +24,13 @@ public class Web2TldRuleProvider : ITopLevelDomainRuleProvider
public ICacheProvider CacheProvider { get { return this._cacheProvider; } }

/// <summary>
/// Web2TldRuleProvider<br/>
/// WebRuleProvider<br/>
/// Loads the public suffix definition file from a given url
/// </summary>
/// <param name="httpClient"></param>
/// <param name="url"></param>
/// <param name="cacheProvider">default is <see cref="FileCacheProvider"/></param>
public Web2TldRuleProvider(
public WebRuleProvider(
HttpClient httpClient,
string url = "https://publicsuffix.org/list/public_suffix_list.dat",
ICacheProvider cacheProvider = null)
Expand All @@ -47,7 +48,7 @@ public Web2TldRuleProvider(
}

///<inheritdoc/>
public async Task<IEnumerable<TldRule>> BuildAsync()
public async Task<IEnumerable<TldRule>> BuildAsync(CancellationToken cancellationToken = default)
{
var ruleParser = new TldRuleParser();

Expand All @@ -58,7 +59,7 @@ public async Task<IEnumerable<TldRule>> BuildAsync()
}
else
{
ruleData = await this.LoadFromUrlAsync(this._fileUrl).ConfigureAwait(false);
ruleData = await this.LoadFromUrlAsync(this._fileUrl, cancellationToken).ConfigureAwait(false);
await this._cacheProvider.SetAsync(ruleData).ConfigureAwait(false);
}

Expand All @@ -70,10 +71,13 @@ public async Task<IEnumerable<TldRule>> BuildAsync()
/// Load the public suffix data from the given url
/// </summary>
/// <param name="url"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> LoadFromUrlAsync(string url)
public async Task<string> LoadFromUrlAsync(
string url,
CancellationToken cancellationToken)
{
using var response = await this._httpClient.GetAsync(url).ConfigureAwait(false);
using var response = await this._httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
{
Expand Down
82 changes: 0 additions & 82 deletions src/Nager.PublicSuffix/RuleProviders/WebTldRuleProvider.cs

This file was deleted.

0 comments on commit 70400bd

Please sign in to comment.