diff options
author | Pranav Krishnamoorthy <prkrishn@microsoft.com> | 2020-04-14 20:49:57 +0300 |
---|---|---|
committer | Pranav Krishnamoorthy <prkrishn@microsoft.com> | 2020-04-14 20:49:57 +0300 |
commit | 8d09403118ca5d3d972b599b50f942be359a9a49 (patch) | |
tree | 00969df497693e00e4a7b9d85ef7615f6fb3562d /src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs | |
parent | 1aa1069bbf3f77a38306ab5f483597268a49b073 (diff) |
Merged PR 7268: Avoid caching JsonSerializerv2.1.18
Avoid caching JsonSerializer
Diffstat (limited to 'src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs')
-rw-r--r-- | src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs b/src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs index 15e735a328..595ebc426d 100644 --- a/src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs +++ b/src/Mvc/Mvc.Formatters.Json/test/JsonOutputFormatterTests.cs @@ -403,7 +403,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { // Arrange var formatter = new JsonOutputFormatter(new JsonSerializerSettings(), ArrayPool<char>.Shared); - + var body = new MemoryStream(); var actionContext = GetActionContext(MediaTypeHeaderValue.Parse(mediaType), body); var outputFormatterContext = new OutputFormatterWriteContext( @@ -425,6 +425,40 @@ namespace Microsoft.AspNetCore.Mvc.Formatters Assert.Equal(new StringSegment(expectedContentType), outputFormatterContext.ContentType); } + [Fact] + public async Task SerializingWithPreserveReferenceHandling() + { + // Arrange + var expected = "{\"$id\":\"1\",\"fullName\":\"John\",\"age\":35}"; + var user = new User { FullName = "John", age = 35 }; + + var settings = new JsonSerializerSettings + { + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy(), + }, + PreserveReferencesHandling = PreserveReferencesHandling.All, + }; + var formatter = new TestableJsonOutputFormatter(settings); + + for (var i = 0; i < 3; i++) + { + // Act + var context = GetOutputFormatterContext(user, typeof(User)); + await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); + + // Assert + var body = context.HttpContext.Response.Body; + + Assert.NotNull(body); + body.Position = 0; + + var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); + Assert.Equal(expected, content); + } + } + private static Encoding CreateOrGetSupportedEncoding( JsonOutputFormatter formatter, string encodingAsString, |