diff options
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, |