-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added support for sitemap images #7
- Loading branch information
Marthijn van den Heuvel
committed
Mar 15, 2024
1 parent
89c0d8e
commit a0750c6
Showing
10 changed files
with
277 additions
and
11 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/Sidio.Sitemap.Core.Tests/Extensions/SitemapImageLocationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Sidio.Sitemap.Core.Extensions; | ||
|
||
namespace Sidio.Sitemap.Core.Tests.Extensions; | ||
|
||
public sealed class SitemapImageLocationTests | ||
{ | ||
[Fact] | ||
public void Construct_WithValidArguments_SitemapImageLocationConstructed() | ||
{ | ||
// arrange | ||
const string Url = "http://www.example.com"; | ||
|
||
// act | ||
var sitemapNode = new SitemapImageLocation(Url); | ||
|
||
// assert | ||
sitemapNode.Url.Should().Be(Url); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void Construct_WithEmptyUrl_ThrowException(string? url) | ||
{ | ||
// act | ||
var sitemapNodeAction = () => new SitemapImageLocation(url!); | ||
|
||
// assert | ||
sitemapNodeAction.Should().ThrowExactly<ArgumentNullException>(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Sidio.Sitemap.Core.Tests/Extensions/SitemapImageNodeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Sidio.Sitemap.Core.Extensions; | ||
|
||
namespace Sidio.Sitemap.Core.Tests.Extensions; | ||
|
||
public sealed class SitemapImageNodeTests | ||
{ | ||
private readonly Fixture _fixture = new (); | ||
|
||
[Fact] | ||
public void Construct_WithValidArguments_SitemapImageLocationConstructed() | ||
{ | ||
// arrange | ||
const string Url = "http://www.example.com"; | ||
var imageLocation = new SitemapImageLocation(Url); | ||
|
||
// act | ||
var sitemapNode = new SitemapImageNode(Url, imageLocation); | ||
|
||
// assert | ||
sitemapNode.Url.Should().Be(Url); | ||
sitemapNode.Images.Should().HaveCount(1); | ||
sitemapNode.Images.Should().Contain(imageLocation); | ||
} | ||
|
||
[Fact] | ||
public void Construct_WithValidArguments_MultipleImages_SitemapImageLocationConstructed() | ||
{ | ||
// arrange | ||
const string Url = "http://www.example.com"; | ||
var imageLocations = _fixture.CreateMany<SitemapImageLocation>().ToList(); | ||
|
||
// act | ||
var sitemapNode = new SitemapImageNode(Url, imageLocations); | ||
|
||
// assert | ||
sitemapNode.Url.Should().Be(Url); | ||
sitemapNode.Images.Should().HaveCount(imageLocations.Count); | ||
sitemapNode.Images.Should().Contain(imageLocations); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void Construct_WithEmptyUrl_ThrowException(string? url) | ||
{ | ||
// act | ||
var sitemapNodeAction = () => new SitemapImageNode(url!, new SitemapImageLocation("http://www.example.com")); | ||
|
||
// assert | ||
sitemapNodeAction.Should().ThrowExactly<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void Construct_WithoutImages_ThrowException() | ||
{ | ||
// act | ||
var sitemapNodeAction = () => new SitemapImageNode("http://www.example.com", []); | ||
|
||
// assert | ||
sitemapNodeAction.Should().ThrowExactly<ArgumentException>(); | ||
} | ||
|
||
[Fact] | ||
public void Construct_WithoutTooManyImages_ThrowException() | ||
{ | ||
// act | ||
var sitemapNodeAction = () => new SitemapImageNode("http://www.example.com", new List<SitemapImageLocation>(1001).ToArray()); | ||
|
||
// assert | ||
sitemapNodeAction.Should().ThrowExactly<ArgumentException>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Sidio.Sitemap.Core.Extensions; | ||
|
||
/// <summary> | ||
/// Represents the location of an image in a sitemap. | ||
/// </summary> | ||
public sealed record SitemapImageLocation | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SitemapImageLocation"/> class. | ||
/// </summary> | ||
/// <param name="url">The URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.</param> | ||
public SitemapImageLocation(string url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
{ | ||
throw new ArgumentNullException(nameof(url)); | ||
} | ||
|
||
Url = url; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the image URL. | ||
/// </summary> | ||
public string Url { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace Sidio.Sitemap.Core.Extensions; | ||
|
||
/// <summary> | ||
/// Represents a node in a sitemap with images. | ||
/// </summary> | ||
public sealed record SitemapImageNode : ISitemapNode | ||
{ | ||
private const int MaxImages = 1000; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SitemapImageNode"/> class. | ||
/// </summary> | ||
/// <param name="url">The URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.</param> | ||
/// <param name="imageLocations">One or more image locations.</param> | ||
/// <exception cref="ArgumentNullException">Thrown when a required argument is null or empty.</exception> | ||
/// <exception cref="ArgumentException">Thrown when an argument has an invalid value.</exception> | ||
public SitemapImageNode(string url, IEnumerable<SitemapImageLocation> imageLocations) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
{ | ||
throw new ArgumentNullException(nameof(url)); | ||
} | ||
|
||
Url = url; | ||
Images = imageLocations.ToList(); | ||
|
||
switch (Images.Count) | ||
{ | ||
case 0: | ||
throw new ArgumentException($"A {nameof(SitemapImageNode)} must contain at least one image location.", nameof(imageLocations)); | ||
case > MaxImages: | ||
throw new ArgumentException($"A {nameof(SitemapImageNode)} must contain at most {MaxImages} image locations.", nameof(imageLocations)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SitemapImageNode"/> class. | ||
/// </summary> | ||
/// <param name="url">The URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.</param> | ||
/// <param name="imageLocation">An image locations.</param> | ||
/// <exception cref="ArgumentNullException">Thrown when a required argument is null or empty.</exception> | ||
/// <exception cref="ArgumentException">Thrown when an argument has an invalid value.</exception> | ||
public SitemapImageNode(string url, SitemapImageLocation imageLocation) | ||
: this(url, new[] { imageLocation }) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string Url { get; } | ||
|
||
/// <summary> | ||
/// Gets the image locations. | ||
/// </summary> | ||
public IReadOnlyCollection<SitemapImageLocation> Images { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Sidio.Sitemap.Core; | ||
|
||
/// <summary> | ||
/// Represents a node in a sitemap. | ||
/// </summary> | ||
public interface ISitemapNode | ||
{ | ||
/// <summary> | ||
/// Gets the URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters. | ||
/// </summary> | ||
public string Url { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Sidio.Sitemap.Core.Extensions; | ||
|
||
namespace Sidio.Sitemap.Core.Serialization; | ||
|
||
internal static class SitemapExtensions | ||
{ | ||
public static bool HasImageNodes(this Sitemap sitemap) => sitemap.Nodes.Any(node => node is SitemapImageNode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters