Skip to content

Commit 32e8289

Browse files
committed
Merge branch 'sitemap-encoding' into develop
2 parents a83b844 + 32895f9 commit 32e8289

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

OnTopic.AspNetCore.Mvc.Tests/TopicControllerTest.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,24 @@ public void RedirectController_TopicRedirect_ReturnsRedirectResult() {
126126
/// <summary>
127127
/// Triggers the index action of the <see cref="SitemapController.Index()" /> action.
128128
/// </summary>
129-
/// <remarks>
130-
/// Because the <see cref="SitemapController.Index()"/> method references the <see cref="Controller.Response"/> property,
131-
/// which is not set during unit testing, this test is <i>expected</i> to throw an exception. This is not ideal. In the
132-
/// future, this may be modified to instead use a mock <see cref="ControllerContext"/> for a more sophisticated test.
133-
/// </remarks>
134129
[TestMethod]
135-
[ExpectedException(typeof(NullReferenceException), AllowDerivedTypes=false)]
136130
public void SitemapController_Index_ReturnsSitemapXml() {
137131

138-
var controller = new SitemapController(_topicRepository);
139-
var result = controller.Index() as ViewResult;
140-
var model = result.Model as string;
132+
var actionContext = new ActionContext {
133+
HttpContext = new DefaultHttpContext(),
134+
RouteData = new RouteData(),
135+
ActionDescriptor = new ControllerActionDescriptor()
136+
};
137+
var controller = new SitemapController(_topicRepository) {
138+
ControllerContext = new ControllerContext(actionContext)
139+
};
140+
var result = controller.Index() as ContentResult;
141+
var model = result.Content as string;
141142

142143
controller.Dispose();
143144

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

148149
}

OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,13 @@ public virtual ActionResult Index() {
107107
/*------------------------------------------------------------------------------------------------------------------------
108108
| Establish sitemap
109109
\-----------------------------------------------------------------------------------------------------------------------*/
110-
var sitemap = GenerateSitemap(rootTopic);
111-
var sitemapFile = new StringBuilder();
112-
using (var writer = new StringWriter(sitemapFile)) {
113-
sitemap.Save(writer);
114-
}
110+
var declaration = new XDeclaration("1.0", "utf-8", "no");
111+
var sitemap = GenerateSitemap(rootTopic);
115112

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

121118
}
122119

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

0 commit comments

Comments
 (0)