Skip to content

Commit

Permalink
Use Authority property to include port number in comparison of host name
Browse files Browse the repository at this point in the history
  • Loading branch information
patkleef committed Nov 20, 2020
1 parent 2e89526 commit df7c327
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Geta.SEO.Sitemaps/XML/SitemapXmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class SitemapXmlGenerator : ISitemapXmlGenerator

protected readonly ISitemapRepository SitemapRepository;
protected readonly IContentRepository ContentRepository;
protected readonly UrlResolver UrlResolver;
protected readonly IUrlResolver UrlResolver;
protected readonly ISiteDefinitionRepository SiteDefinitionRepository;
protected readonly ILanguageBranchRepository LanguageBranchRepository;
protected readonly IContentFilter ContentFilter;
Expand All @@ -60,7 +60,7 @@ protected XNamespace SitemapXhtmlNamespace

public bool IsDebugMode { get; set; }

protected SitemapXmlGenerator(ISitemapRepository sitemapRepository, IContentRepository contentRepository, UrlResolver urlResolver, ISiteDefinitionRepository siteDefinitionRepository, ILanguageBranchRepository languageBranchRepository,
protected SitemapXmlGenerator(ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, ISiteDefinitionRepository siteDefinitionRepository, ILanguageBranchRepository languageBranchRepository,
IContentFilter contentFilter)
{
this.SitemapRepository = sitemapRepository;
Expand Down Expand Up @@ -489,11 +489,11 @@ protected CultureInfo GetMasterLanguage(IContent content)
return CultureInfo.InvariantCulture;
}

protected SiteDefinition GetSiteDefinitionFromSiteUri(Uri sitemapSiteUri)
public SiteDefinition GetSiteDefinitionFromSiteUri(Uri sitemapSiteUri)
{
return this.SiteDefinitionRepository
.List()
.FirstOrDefault(siteDef => siteDef.SiteUrl == sitemapSiteUri || siteDef.Hosts.Any(hostDef => hostDef.Name.Equals(sitemapSiteUri.Host, StringComparison.InvariantCultureIgnoreCase)));
.FirstOrDefault(siteDef => siteDef.SiteUrl == sitemapSiteUri || siteDef.Hosts.Any(hostDef => hostDef.Name.Equals(sitemapSiteUri.Authority, StringComparison.InvariantCultureIgnoreCase)));
}

protected string GetHostLanguageBranch()
Expand Down
2 changes: 1 addition & 1 deletion src/Geta.SEO.Sitemaps/XML/StandardSitemapXmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Geta.SEO.Sitemaps.XML
[ServiceConfiguration(typeof(IStandardSitemapXmlGenerator))]
public class StandardSitemapXmlGenerator : SitemapXmlGenerator, IStandardSitemapXmlGenerator
{
public StandardSitemapXmlGenerator(ISitemapRepository sitemapRepository, IContentRepository contentRepository, UrlResolver urlResolver, ISiteDefinitionRepository siteDefinitionRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter) : base(sitemapRepository, contentRepository, urlResolver, siteDefinitionRepository, languageBranchRepository, contentFilter)
public StandardSitemapXmlGenerator(ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, ISiteDefinitionRepository siteDefinitionRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter) : base(sitemapRepository, contentRepository, urlResolver, siteDefinitionRepository, languageBranchRepository, contentFilter)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CompressionHandlerTest.cs" />
<Compile Include="SitemapRepositoryTests.cs" />
<Compile Include="SitemapXmlGeneratorTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Geta.SEO.Sitemaps\Geta.SEO.Sitemaps.csproj">
Expand Down
68 changes: 68 additions & 0 deletions test/Geta.SEO.Sitemaps.Tests/SitemapXmlGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EPiServer;
using EPiServer.DataAbstraction;
using EPiServer.Web;
using EPiServer.Web.Routing;
using Geta.SEO.Sitemaps.Repositories;
using Geta.SEO.Sitemaps.Utils;
using Geta.SEO.Sitemaps.XML;
using Moq;
using Xunit;

namespace Tests
{
public class SitemapXmlGeneratorTests
{
private readonly Mock<ILanguageBranchRepository> _languageBranchRepository;

public SitemapXmlGeneratorTests()
{
_languageBranchRepository = new Mock<ILanguageBranchRepository>();
_languageBranchRepository
.Setup(x => x.Load(It.IsAny<CultureInfo>()))
.Returns(new LanguageBranch(new CultureInfo("en")));
}

[Theory]
[InlineData(new[] { "http://localhost", "http://localhost:5001" }, "http://localhost:5001")]
[InlineData(new[] { "http://localhost", "http://localhost:5001" }, "http://localhost")]
[InlineData(new[] { "https://xyz.com", "https://abc.xyz.com", "http://xyz.nl" }, "https://abc.xyz.com")]
public void Can_Get_SiteDefinition_By_Site_Url(string[] hostUrls, string url)
{
var list = new List<SiteDefinition>()
{
new SiteDefinition
{
Hosts = hostUrls.Select(x => new HostDefinition
{
Name = new Uri(x, UriKind.Absolute).Authority
}).ToList()
}
};

var sitemapRepository = new Mock<ISitemapRepository>();
var contentRepository = new Mock<IContentRepository>();
var urlResolver = new Mock<IUrlResolver>();
var siteDefinitionRepository = new Mock<ISiteDefinitionRepository>();
siteDefinitionRepository.Setup(x => x.List())
.Returns(list);

var standardSitemapXmlGenerator = new StandardSitemapXmlGenerator(
sitemapRepository.Object,
contentRepository.Object,
urlResolver.Object,
siteDefinitionRepository.Object,
_languageBranchRepository.Object,
new ContentFilter());

var siteDefinition = standardSitemapXmlGenerator.GetSiteDefinitionFromSiteUri(new Uri(url));

Assert.NotNull(siteDefinition);
}
}
}

0 comments on commit df7c327

Please sign in to comment.