Skip to content

Commit

Permalink
working on fixing scheme normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Jan 3, 2025
1 parent 4c00ffb commit c51aafb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
68 changes: 67 additions & 1 deletion src/LinkValidator.Tests/ParseHelperSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
namespace LinkValidator.Tests;
using FluentAssertions;
using LinkValidator.Actors;
using LinkValidator.Util;

namespace LinkValidator.Tests;

public class ParseHelperSpecs
{
// create a string that contains HTML linking to a few different URLs using relative links
private const string RelativeHtml = """
<html>
<head>
<title>Test Page</title>
</head>
<body>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<a href="/faq">FAQ</a>
</body>
</html>
""";

[Fact]
public void ParseHelper_should_return_absolute_uris()
{
// Arrange
var uri = new AbsoluteUri(new Uri("http://example.com"));

// Act
var uris = ParseHelpers.ParseLinks(RelativeHtml, uri);

// Assert
uris.Should().HaveCount(3);
uris.Should().Contain(new AbsoluteUri(new Uri("http://example.com/about")));
uris.Should().Contain(new AbsoluteUri(new Uri("http://example.com/contact")));
uris.Should().Contain(new AbsoluteUri(new Uri("http://example.com/faq")));
}

// create a string that contains HTML linking to a few different URLs using absolute links
private const string AbsoluteHtml = """
<html>
<head>
<title>Test Page</title>
</head>
<body>
<a href="http://example.com/about">About</a>
<a href="http://example.com/contact">Contact</a>
<a href="http://example.com/faq">FAQ</a>
</body>
</html>
""";

[Fact]
public void ParseHelper_should_return_absolute_uris_when_given_absolute_links()
{
// Arrange
var uri = new AbsoluteUri(new Uri("https://example.com"));

// Act
var uris = ParseHelpers.ParseLinks(AbsoluteHtml, uri);

// Assert
uris.Should().HaveCount(3);

// notice that we convert the scheme to https
uris.Should().Contain(new AbsoluteUri(new Uri("https://example.com/about")));
uris.Should().Contain(new AbsoluteUri(new Uri("https://example.com/contact")));
uris.Should().Contain(new AbsoluteUri(new Uri("https://example.com/faq")));
}
}
7 changes: 6 additions & 1 deletion src/LinkValidator.Tests/UriHelperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public void AbsoluteUriIsInDomain_should_NOT_return_true_when_host_is_diffrent(s
{
{ 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")) }
{ new AbsoluteUri(new Uri("http://example.com")), "some/path", new AbsoluteUri(new Uri("http://example.com/some/path")) },
{ new AbsoluteUri(new Uri("https://example.com")), "some/path", new AbsoluteUri(new Uri("https://example.com/some/path")) },

// HTTP vs. HTTPS - should normalize to use whatever the baseUri uses
{ new AbsoluteUri(new Uri("https://example.com")), "http://example.com/some/path", new AbsoluteUri(new Uri("https://example.com/some/path")) },
{ new AbsoluteUri(new Uri("http://example.com")), "https://example.com/some/path", new AbsoluteUri(new Uri("http://example.com/some/path")) }
};

[Theory]
Expand Down

0 comments on commit c51aafb

Please sign in to comment.