Skip to content

Commit

Permalink
Merge branch 'sitemap-encoding' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCaney committed Feb 21, 2020
2 parents a83b844 + 32895f9 commit 32e8289
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
21 changes: 11 additions & 10 deletions OnTopic.AspNetCore.Mvc.Tests/TopicControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,24 @@ public void RedirectController_TopicRedirect_ReturnsRedirectResult() {
/// <summary>
/// Triggers the index action of the <see cref="SitemapController.Index()" /> action.
/// </summary>
/// <remarks>
/// Because the <see cref="SitemapController.Index()"/> method references the <see cref="Controller.Response"/> property,
/// which is not set during unit testing, this test is <i>expected</i> to throw an exception. This is not ideal. In the
/// future, this may be modified to instead use a mock <see cref="ControllerContext"/> for a more sophisticated test.
/// </remarks>
[TestMethod]
[ExpectedException(typeof(NullReferenceException), AllowDerivedTypes=false)]
public void SitemapController_Index_ReturnsSitemapXml() {

var controller = new SitemapController(_topicRepository);
var result = controller.Index() as ViewResult;
var model = result.Model as string;
var actionContext = new ActionContext {
HttpContext = new DefaultHttpContext(),
RouteData = new RouteData(),
ActionDescriptor = new ControllerActionDescriptor()
};
var controller = new SitemapController(_topicRepository) {
ControllerContext = new ControllerContext(actionContext)
};
var result = controller.Index() as ContentResult;
var model = result.Content as string;

controller.Dispose();

Assert.IsNotNull(model);
Assert.IsTrue(model.StartsWith("<?xml version=\"1.0\" encoding=\"utf-16\"?>"));
Assert.IsTrue(model.StartsWith("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>"));
Assert.IsTrue(model.Contains("/Web/Web_1/Web_1_1/Web_1_1_1/</loc>"));

}
Expand Down
10 changes: 3 additions & 7 deletions OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ public virtual ActionResult Index() {
/*------------------------------------------------------------------------------------------------------------------------
| Establish sitemap
\-----------------------------------------------------------------------------------------------------------------------*/
var sitemap = GenerateSitemap(rootTopic);
var sitemapFile = new StringBuilder();
using (var writer = new StringWriter(sitemapFile)) {
sitemap.Save(writer);
}
var declaration = new XDeclaration("1.0", "utf-8", "no");
var sitemap = GenerateSitemap(rootTopic);

/*------------------------------------------------------------------------------------------------------------------------
| Return the homepage view
\-----------------------------------------------------------------------------------------------------------------------*/
return Content(sitemapFile.ToString(), "text/xml");
return Content(declaration.ToString() + sitemap.ToString(), "text/xml");

}

Expand All @@ -129,7 +126,6 @@ public virtual ActionResult Index() {
/// <returns>The site's homepage view.</returns>
public virtual XDocument GenerateSitemap(Topic rootTopic) =>
new XDocument(
new XDeclaration("1.0", "utf-8", String.Empty),
new XElement(_sitemapNamespace + "urlset",
from topic in rootTopic?.Children
select AddTopic(topic)
Expand Down

0 comments on commit 32e8289

Please # to comment.