Skip to content

Commit

Permalink
validated all UriHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Jan 3, 2025
1 parent 29e9264 commit d228287
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
37 changes: 36 additions & 1 deletion src/LinkValidator.Tests/UriHelperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ namespace LinkValidator.Tests;

public class UriHelperSpecs
{
public static readonly TheoryData<AbsoluteUri, string, bool> CanMakeAbsoluteUriData = new()
{
{ new AbsoluteUri(new Uri("http://example.com")), "http://example.com/some/path", true },
{ new AbsoluteUri(new Uri("http://example.com")), "https://example.com/some/path", true },
{ new AbsoluteUri(new Uri("http://example.com")), "/some/path", true },
{ new AbsoluteUri(new Uri("http://example.com")), "mailto:[email protected]", false }
};

[Theory]
[MemberData(nameof(CanMakeAbsoluteUriData))]
public void CanMakeAbsoluteUri_should_return_expected_results(AbsoluteUri baseUri, string rawUri, bool expected)
{
// Act
var result = UriHelpers.CanMakeAbsoluteHttpUri(baseUri, rawUri);

// Assert
result.Should().Be(expected);
}

[Theory]
[InlineData("http://example.com", "http://example.com/some/path")]
[InlineData("https://example.com", "http://example.com/some/path")]
Expand Down Expand Up @@ -39,6 +58,22 @@ public void AbsoluteUriIsInDomain_should_NOT_return_true_when_host_is_diffrent(s
// Assert
result.Should().BeFalse();
}


public static readonly TheoryData<AbsoluteUri, string, AbsoluteUri> ToAbsoluteUriData = new()
{
{ new AbsoluteUri(new Uri("http://example.com")), "http://example.com/some/path", new AbsoluteUri(new Uri("http://example.com/some/path")) },
{ new AbsoluteUri(new Uri("http://example.com")), "/some/path", new AbsoluteUri(new Uri("http://example.com/some/path")) },
{ new AbsoluteUri(new Uri("http://example.com")), "some/path", new AbsoluteUri(new Uri("http://example.com/some/path")) }
};

[Theory]
[MemberData(nameof(ToAbsoluteUriData))]
public void ToAbsoluteUri_should_return_expected_results(AbsoluteUri baseUri, string rawUri, AbsoluteUri expected)
{
// Act
var result = UriHelpers.ToAbsoluteUri(baseUri, rawUri);

// Assert
result.Should().Be(expected);
}
}
4 changes: 2 additions & 2 deletions src/LinkValidator/Util/UriHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public static string NormalizeUrl(string baseUrl, string href)
return href;
}

public static bool CanMakeAbsoluteUri(AbsoluteUri baseUri, string rawUri)
public static bool CanMakeAbsoluteHttpUri(AbsoluteUri baseUri, string rawUri)
{
// this will not return true for things like "mailto:" or "tel:" links
if (Uri.IsWellFormedUriString(rawUri, UriKind.Absolute))
if (Uri.IsWellFormedUriString(rawUri, UriKind.Absolute) && (rawUri.StartsWith(Uri.UriSchemeHttp) || rawUri.StartsWith(Uri.UriSchemeHttps)))
return true;
try
{
Expand Down

0 comments on commit d228287

Please sign in to comment.