This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- This functionality can be disabled by setting the `Switch.Microsoft.AspNetCore.Mvc.AllowJsonHtml` switch in an app.config. - Updated tests to react to this new behavior. - Exposed a new `PublicSerializerSettings` property on `JsonOutputFormatter` so we can accurately copy settings from the used output formatter in `JsonHelper`.
- Loading branch information
1 parent
82fd1c5
commit 7cea779
Showing
4 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/JsonHelperTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Buffers; | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures | ||
{ | ||
public class JsonHelperTest | ||
{ | ||
[Fact] | ||
public void Serialize_EscapesHtmlByDefault() | ||
{ | ||
// Arrange | ||
var settings = new JsonSerializerSettings() | ||
{ | ||
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii, | ||
}; | ||
var helper = new JsonHelper( | ||
new JsonOutputFormatter(settings, ArrayPool<char>.Shared), | ||
ArrayPool<char>.Shared); | ||
var obj = new | ||
{ | ||
HTML = "<b>John Doe</b>" | ||
}; | ||
var expectedOutput = "{\"HTML\":\"\\u003cb\\u003eJohn Doe\\u003c/b\\u003e\"}"; | ||
|
||
// Act | ||
var result = helper.Serialize(obj); | ||
|
||
// Assert | ||
var htmlString = Assert.IsType<HtmlString>(result); | ||
Assert.Equal(expectedOutput, htmlString.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_MaintainsSettingsAndEscapesHtml() | ||
{ | ||
// Arrange | ||
var settings = new JsonSerializerSettings() | ||
{ | ||
ContractResolver = new DefaultContractResolver | ||
{ | ||
NamingStrategy = new CamelCaseNamingStrategy(), | ||
}, | ||
}; | ||
var helper = new JsonHelper( | ||
new JsonOutputFormatter(settings, ArrayPool<char>.Shared), | ||
ArrayPool<char>.Shared); | ||
var obj = new | ||
{ | ||
FullHtml = "<b>John Doe</b>" | ||
}; | ||
var expectedOutput = "{\"fullHtml\":\"\\u003cb\\u003eJohn Doe\\u003c/b\\u003e\"}"; | ||
|
||
// Act | ||
var result = helper.Serialize(obj); | ||
|
||
// Assert | ||
var htmlString = Assert.IsType<HtmlString>(result); | ||
Assert.Equal(expectedOutput, htmlString.ToString()); | ||
} | ||
} | ||
} |