Skip to content

Commit

Permalink
re #5 - ensures priority is string formatted using invariant culture (#6
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Philo authored Jan 12, 2018
1 parent 3fe1990 commit 423ff44
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 124 deletions.
45 changes: 23 additions & 22 deletions src/Sitemapify/Extensions/SitemapUrlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Linq;
using System.Xml.Linq;
using Sitemapify.Models;

namespace Sitemapify.Extensions
{
internal static class SitemapUrlExtensions
{
public static XElement ToXElement(this SitemapUrl sitemapUrl, string ns = SitemapUrl.SitemapNs)
{
var parts = new object[]
{
new XElement(XName.Get(nameof(SitemapUrl.Loc).ToLowerInvariant(),ns), Uri.EscapeUriString(sitemapUrl.Loc)),
sitemapUrl.Lastmod.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.Lastmod).ToLowerInvariant(), ns), sitemapUrl.Lastmod.Value.ToString("O")) : null,
sitemapUrl.ChangeFreq.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.ChangeFreq).ToLowerInvariant(), ns), sitemapUrl.ChangeFreq.Value.ToString().ToLowerInvariant()) : null,
sitemapUrl.Priority.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.Priority).ToLowerInvariant(), ns), sitemapUrl.Priority.Value.ToString("F1")) : null
}.ToList();

return new XElement(XName.Get("url", ns), parts);
}
}
using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using Sitemapify.Models;

namespace Sitemapify.Extensions
{
internal static class SitemapUrlExtensions
{
public static XElement ToXElement(this SitemapUrl sitemapUrl, string ns = SitemapUrl.SitemapNs)
{
var parts = new object[]
{
new XElement(XName.Get(nameof(SitemapUrl.Loc).ToLowerInvariant(),ns), Uri.EscapeUriString(sitemapUrl.Loc)),
sitemapUrl.Lastmod.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.Lastmod).ToLowerInvariant(), ns), sitemapUrl.Lastmod.Value.ToString("O")) : null,
sitemapUrl.ChangeFreq.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.ChangeFreq).ToLowerInvariant(), ns), sitemapUrl.ChangeFreq.Value.ToString().ToLowerInvariant()) : null,
sitemapUrl.Priority.HasValue ? new XElement(XName.Get(nameof(SitemapUrl.Priority).ToLowerInvariant(), ns), sitemapUrl.Priority.Value.ToString("F1", CultureInfo.InvariantCulture)) : null
}.ToList();

return new XElement(XName.Get("url", ns), parts);
}
}
}
44 changes: 41 additions & 3 deletions test/Sitemapify.Tests/SitemapUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void When()
Configure.With(c => c.UsingContentProvider(contentProvider.Object));

var httpContext = new Mock<HttpContextBase>(MockBehavior.Loose);
httpContext.Setup(s => s.Request).Returns(() => new HttpRequestWrapper(new HttpRequest("sitemap.xml", "http://localtest.me/sitemap.xml", "")));
httpContext.Setup(s => s.Request).Returns(() =>
new HttpRequestWrapper(new HttpRequest("sitemap.xml", "http://localtest.me/sitemap.xml", "")));
httpContext.Setup(s => s.Response).Returns(() => new HttpResponseWrapper(new HttpResponse(responseWriter)));

var handler = new SitemapifyHttpHandler();
Expand All @@ -49,7 +50,43 @@ public void When()
(from urls in
document.Descendants(XName.Get("urlset", SitemapUrl.SitemapNs))
.Elements(XName.Get("url", SitemapUrl.SitemapNs))
select urls).Count().Should().Be(3);
select urls).Count().Should().Be(3);
}

[Fact]
[UseCultureAttribute("fr-FR")]
public void SitemapUrlPriorityShouldBeInvariantCulture()
{
var sb = new StringBuilder();
var responseWriter = new StringWriter(sb);
var contentProvider = new Mock<ISitemapContentProvider>();
contentProvider.Setup(s => s.GetSitemapUrls(It.IsAny<Uri>()))
.Returns(() => new List<SitemapUrl>
{
SitemapUrl.Create("http://localtest.me/index.html", DateTimeOffset.UtcNow, priority: 1.1)
});

Configure.With(c => c.UsingContentProvider(contentProvider.Object));

var httpContext = new Mock<HttpContextBase>(MockBehavior.Loose);
httpContext.Setup(s => s.Request).Returns(() =>
new HttpRequestWrapper(new HttpRequest("sitemap.xml", "http://localtest.me/sitemap.xml", "")));
httpContext.Setup(s => s.Response).Returns(() => new HttpResponseWrapper(new HttpResponse(responseWriter)));

var handler = new SitemapifyHttpHandler();
handler.ProcessRequest(httpContext.Object);

var document = XDocument.Parse(sb.ToString());

var sitemapUrlElement = (from urls in
document.Descendants(XName.Get("urlset", SitemapUrl.SitemapNs))
.Elements(XName.Get("url", SitemapUrl.SitemapNs))
select urls).First();

var priority = sitemapUrlElement.Element(XName.Get("priority", SitemapUrl.SitemapNs));

priority.Should().NotBeNull();
priority.Value.Should().Be("1.1");
}
}

Expand Down Expand Up @@ -88,7 +125,8 @@ public override string ToString()
}
}

public IEnumerable<ContentModelStub> Matching(ContentModelStub node, Func<ContentModelStub, bool> predicate = null, Func<ContentModelStub, bool> includeChildren = null)
public IEnumerable<ContentModelStub> Matching(ContentModelStub node,
Func<ContentModelStub, bool> predicate = null, Func<ContentModelStub, bool> includeChildren = null)
{
if (predicate?.Invoke(node) ?? true)
{
Expand Down
199 changes: 100 additions & 99 deletions test/Sitemapify.Tests/Sitemapify.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,100 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D5C04526-1AE5-46E5-B7EA-D7344A4C6D77}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sitemapify.Tests</RootNamespace>
<AssemblyName>Sitemapify.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions, Version=4.13.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.13.0\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions.Core, Version=4.13.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.13.0\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.5.19.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\..\packages\Moq.4.5.19\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\VersionAssemblyInfo.cs">
<Link>Properties\VersionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="SitemapUrlTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Sitemapify\Sitemapify.csproj">
<Project>{16ed145c-cd29-4063-95c0-bd7c46d86d18}</Project>
<Name>Sitemapify</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D5C04526-1AE5-46E5-B7EA-D7344A4C6D77}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sitemapify.Tests</RootNamespace>
<AssemblyName>Sitemapify.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions, Version=4.13.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.13.0\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions.Core, Version=4.13.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.13.0\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.5.19.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\..\packages\Moq.4.5.19\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\VersionAssemblyInfo.cs">
<Link>Properties\VersionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="SitemapUrlTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UseCultureAttribute.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Sitemapify\Sitemapify.csproj">
<Project>{16ed145c-cd29-4063-95c0-bd7c46d86d18}</Project>
<Name>Sitemapify</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit 423ff44

Please sign in to comment.