Skip to content

Commit 7ca9845

Browse files
committed
Fixed namespace issue for sitemap generation
1 parent 1e51d3e commit 7ca9845

File tree

6 files changed

+70
-12
lines changed

6 files changed

+70
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
6+
using LinkDotNet.Blog.Domain;
7+
using LinkDotNet.Blog.Infrastructure.Persistence;
8+
using LinkDotNet.Blog.Web.Shared.Services;
9+
using LinkDotNet.Blog.Web.Shared.Services.Sitemap;
10+
using Moq;
11+
using Xunit;
12+
13+
namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services
14+
{
15+
public sealed class SitemapServiceTests : IDisposable
16+
{
17+
private const string OutputDirectory = "wwwroot";
18+
private const string OutputFilename = $"{OutputDirectory}/sitemap.xml";
19+
private readonly SitemapService sut;
20+
21+
public SitemapServiceTests()
22+
{
23+
var repositoryMock = new Mock<IRepository<BlogPost>>();
24+
sut = new SitemapService(repositoryMock.Object, null, new XmlFileWriter());
25+
Directory.CreateDirectory("wwwroot");
26+
}
27+
28+
[Fact]
29+
public async Task ShouldSaveSitemapUrlInCorrectFormat()
30+
{
31+
var urlSet = new SitemapUrlSet()
32+
{
33+
Urls = new List<SitemapUrl>
34+
{
35+
new SitemapUrl
36+
{
37+
Location = "here",
38+
},
39+
},
40+
};
41+
await sut.SaveSitemapToFileAsync(urlSet);
42+
43+
var lines = await File.ReadAllTextAsync(OutputFilename);
44+
lines.Should().Be(
45+
@"<?xml version=""1.0"" encoding=""utf-8""?>
46+
<urlset xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">
47+
<url>
48+
<loc>here</loc>
49+
</url>
50+
</urlset>");
51+
}
52+
53+
public void Dispose()
54+
{
55+
if (File.Exists(OutputFilename))
56+
{
57+
File.Delete(OutputFilename);
58+
}
59+
60+
if (Directory.Exists(OutputDirectory))
61+
{
62+
Directory.Delete(OutputDirectory, true);
63+
}
64+
}
65+
}
66+
}

LinkDotNet.Blog.UnitTests/Web/Shared/Services/SitemapServiceTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public async Task ShouldCreateSitemap()
5858

5959
var sitemap = await sut.CreateSitemapAsync();
6060

61-
sitemap.Namespace.Should().Be("http://www.sitemaps.org/schemas/sitemap/0.9");
6261
sitemap.Urls.Should().HaveCount(5);
6362
sitemap.Urls[0].Location.Should().Be($"{fakeNavigationManager.BaseUri}");
6463
sitemap.Urls[1].Location.Should().Be($"{fakeNavigationManager.BaseUri}blogPost/id1");

LinkDotNet.Blog.Web/Shared/AccessControl.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<li><h6 class="dropdown-header">Others</h6></li>
1717
<li><a class="dropdown-item" href="Sitemap">Sitemap</a></li>
1818
<li><hr class="dropdown-divider"></li>
19-
<li><a class="dropdown-item" href="https://github.com/linkdotnet/Blog/releases">Version 2.7</a></li>
19+
<li><a class="dropdown-item" href="https://github.com/linkdotnet/Blog/releases">Version 2.71</a></li>
2020
</ul>
2121
</li>
2222
<li class="nav-item"><a class="nav-link" href="logout">Log out</a></li>

LinkDotNet.Blog.Web/Shared/Services/Sitemap/SitemapService.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public SitemapService(
2626

2727
public async Task<SitemapUrlSet> CreateSitemapAsync()
2828
{
29-
const string sitemapNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9";
30-
var urlSet = new SitemapUrlSet
31-
{
32-
Namespace = sitemapNamespace,
33-
};
29+
var urlSet = new SitemapUrlSet();
3430

3531
var blogPosts = (await repository.GetAllAsync(f => f.IsPublished, b => b.UpdatedDate)).ToList();
3632

LinkDotNet.Blog.Web/Shared/Services/Sitemap/SitemapUrlSet.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
namespace LinkDotNet.Blog.Web.Shared.Services.Sitemap;
55

6-
[XmlRoot(ElementName = "urlset")]
6+
[XmlRoot(ElementName = "urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
77
public class SitemapUrlSet
88
{
99
[XmlElement(ElementName = "url")]
1010
public List<SitemapUrl> Urls { get; set; } = new();
11-
12-
[XmlAttribute(AttributeName = "xmlns")]
13-
public string Namespace { get; set; }
1411
}

LinkDotNet.Blog.Web/Shared/Services/XmlFileWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class XmlFileWriter : IXmlFileWriter
1010
public async Task WriteObjectToXmlFileAsync<T>(T objectToSave, string fileName)
1111
{
1212
await using var file = File.Create(fileName);
13-
await using var xmlWriter = XmlWriter.Create(file, new XmlWriterSettings { Indent = true });
13+
await using var xmlWriter = XmlWriter.Create(file, new XmlWriterSettings { Indent = true, Async = true });
1414
var serializer = new XmlSerializer(typeof(T));
1515
serializer.Serialize(xmlWriter, objectToSave);
1616
xmlWriter.Close();

0 commit comments

Comments
 (0)