forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonNetValidationTest.cs
More file actions
129 lines (109 loc) · 4.15 KB
/
Copy pathJsonNetValidationTest.cs
File metadata and controls
129 lines (109 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Net.Http.Formatting
{
public class JsonNetValidationTest
{
public static TheoryDataSet<string, Type, int> Theories
{
get
{
return new TheoryDataSet<string, Type, int>()
{
// Type coercion
{"null", typeof(int), 1},
{"45", typeof(string), 0},
{"random text", typeof(DateTimeOffset), 1},
{"[1,2,3]", typeof(string[]), 0},
{"\"foo\"", typeof(int), 1},
{"\"foo\"", typeof(DateTime), 1},
{"[\"a\",\"b\",\"45\",34]", typeof(int[]), 2},
{
"[\"a\",\"b\",\"45\",34]",
typeof(DateTime[]),
#if NEWTONSOFTJSON10 // Json.NET 10 detects an additional error over earlier versions.
5
#else
4
#endif
},
// Required members
{"{}", typeof(DataContractWithRequiredMembers), 2},
{"[{},{},{}]", typeof(DataContractWithRequiredMembers[]), 6},
// Throwing setters
{"{\"Throws\":\"foo\"}", typeof(TypeWithThrowingSetter), 1},
{"[{\"Throws\":\"foo\"},{\"Throws\":\"foo\"}]", typeof(TypeWithThrowingSetter[]), 2},
};
}
}
#if !NETFX_CORE // IRequiredMemeberSelector is not in portable libraries because there is no model state on the client.
[Theory]
[PropertyData("Theories")]
public async Task ModelErrorsPopulatedWithValidationErrors(string json, Type type, int expectedErrors)
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
formatter.RequiredMemberSelector = new SimpleRequiredMemberSelector();
Mock<IFormatterLogger> mockLogger = new Mock<IFormatterLogger>() { };
await JsonNetSerializationTest.DeserializeAsync(json, type, formatter, mockLogger.Object);
mockLogger.Verify(mock => mock.LogError(It.IsAny<string>(), It.IsAny<Exception>()), Times.Exactly(expectedErrors));
}
#endif
[Fact]
public async Task HittingMaxDepthRaisesOnlyOneValidationError()
{
// Arrange
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
Mock<IFormatterLogger> mockLogger = new Mock<IFormatterLogger>();
StringBuilder sb = new StringBuilder("{'A':null}");
for (int i = 0; i < 5000; i++)
{
sb.Insert(0, "{'A':");
sb.Append('}');
}
string json = sb.ToString();
// Act
await JsonNetSerializationTest.DeserializeAsync(json, typeof(Nest), formatter, mockLogger.Object);
// Assert
mockLogger.Verify(mock => mock.LogError(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once());
}
}
#if !NETFX_CORE // IRequiredMemeberSelector is not in portable libraries because there is no model state on the client.
// this IRMS treats all member names that start with "Required" as required
public class SimpleRequiredMemberSelector : IRequiredMemberSelector
{
public bool IsRequiredMember(MemberInfo member)
{
return member.Name.StartsWith("Required");
}
}
#endif
public class DataContractWithRequiredMembers
{
public string Required1;
public string Required2;
public string Optional;
}
public class TypeWithThrowingSetter
{
public string Throws
{
get
{
return "foo";
}
set
{
throw new NotImplementedException();
}
}
}
public class Nest
{
public Nest A { get; set; }
}
}