コード例 #1
0
        public async Task NamingStrategy_DoesNotAffectSerialization_WithJsonProperty(NamingStrategy strategy)
        {
            // Arrange
            var user = new UserWithJsonProperty
            {
                Name        = "Joe",
                AnotherName = "Joe",
                ThirdName   = "Joe",
            };
            var expected = "{\"ThisIsTheFullName\":\"Joe\",\"another_name\":\"Joe\",\"ThisIsTheThirdName\":\"Joe\"}";
            var context  = GetOutputFormatterContext(user, typeof(UserWithJsonProperty));

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = strategy,
                },
            };
            var formatter = new TestableJsonOutputFormatter(settings);

            // Act
            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);
        }
コード例 #2
0
        public async Task NamingStrategy_DoesNotAffectSerialization_WithJsonProperty(NamingStrategy strategy)
        {
            // Arrange
            var user = new UserWithJsonProperty
            {
                Name = "Joe",
                AnotherName = "Joe",
                ThirdName = "Joe",
            };
            var expected = "{\"ThisIsTheFullName\":\"Joe\",\"another_name\":\"Joe\",\"ThisIsTheThirdName\":\"Joe\"}";
            var context = GetOutputFormatterContext(user, typeof(UserWithJsonProperty));

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = strategy,
                },
            };
            var formatter = new TestableJsonOutputFormatter(settings);

            // Act
            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);
        }