Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
(GH-14) Adding tests
Browse files Browse the repository at this point in the history
- To cover the functionality of the get_domain_from_host method
  • Loading branch information
gep13 committed Dec 2, 2015
1 parent 095ad47 commit 2fce966
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="..\SolutionVersion.cs">
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="infrastructure.app\IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecs.cs" />
<Compile Include="infrastructure\events\context\FakeMessage.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
namespace chocolatey.package.validator.tests.infrastructure.app
{
using chocolatey.package.validator.infrastructure.app.rules;
using Should;

public abstract class IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecsBase : TinySpec
{
protected IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline iconGuideline;

public override void Context()
{
iconGuideline = new IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline();
}
}

public class when_extracting_domain_from_host : IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecsBase
{
private string result;

public override void Because()
{
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_only_host_and_single_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("test.com");

domain.ShouldEqual("test.com");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_only_host_and_double_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("test.co.uk");

domain.ShouldEqual("test.co.uk");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_www_subdomain_and_single_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("www.test.com");

domain.ShouldEqual("test.com");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_www_subdomain_and_double_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("www.test.co.uk");

domain.ShouldEqual("test.co.uk");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_arbitrary_subdomain_and_single_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("somewildsubdomain.test.com");

domain.ShouldEqual("test.com");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_arbitrary_subdomain_and_double_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("somewildsubdomain.test.co.uk");

domain.ShouldEqual("test.co.uk");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_three_letter_host_and_single_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("somewildsubdomain.tla.com");

domain.ShouldEqual("tla.com");
}

[Fact]
public void GetDomainFromHost_should_return_domain_from_three_letter_host_and_double_level_top_level_domain()
{
Context();

var domain = iconGuideline.get_domain_from_host("somewildsubdomain.tla.co.uk");

domain.ShouldEqual("tla.co.uk");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,37 @@

namespace chocolatey.package.validator.infrastructure.app.rules
{
using System.Text.RegularExpressions;
using System.Linq;
using infrastructure.rules;
using NuGet;

public class IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline : BasePackageRule
{
public override string ValidationFailureMessage { get { return "The package IconUrl should ideally come from the same domain name as the Project Url, or hosted on the RawGit CDN. **NOTE:** For further information on how to setup your icon with a RawGit CDN URL, please visit this [article](https://github.com/chocolatey/choco/wiki/CreatePackages#package-icon-guidelines)."; } }

public string get_domain_from_host(string host)
{
// Taken from example shown here http://www.primaryobjects.com/2012/11/19/parsing-hostname-and-domain-from-a-url-with-javascript/
var domain = host;

if (host != null)
{
var parts = host.Split('.').Reverse().ToList();

if (parts.Count > 1)
{
domain = parts[1] + '.' + parts[0];

if (host.ToLower().IndexOf(".co.uk") != -1 && parts.Count > 2)
{
domain = parts[2] + '.' + domain;
}
}
}

return domain;
}

protected override PackageValidationOutput is_valid(IPackage package)
{
if (package.IconUrl == null) return true;
Expand All @@ -37,13 +60,5 @@ protected override PackageValidationOutput is_valid(IPackage package)

return iconUrlDomain == projectUrlDomain || iconUrlDomain == "rawgit.com";
}

private string get_domain_from_host(string host)
{
// Use Regular Expression to extract the Domain Name, from the Uri Host
// Taken from example shown here http://stackoverflow.com/a/17091145/671491
var match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
return match.Groups[1].Success ? match.Groups[1].Value : string.Empty;
}
}
}

0 comments on commit 2fce966

Please sign in to comment.