7
7
using System . Text ;
8
8
using System . Threading . Tasks ;
9
9
using Microsoft . AspNetCore . Mvc . Formatters . Xml ;
10
- using Microsoft . AspNetCore . Testing ;
11
- using Microsoft . AspNetCore . Testing . xunit ;
12
10
using Xunit ;
13
11
14
12
namespace Microsoft . AspNetCore . Mvc . FunctionalTests
@@ -22,22 +20,15 @@ public SerializableErrorTests(MvcTestFixture<XmlFormattersWebSite.Startup> fixtu
22
20
23
21
public HttpClient Client { get ; }
24
22
25
- public static TheoryData AcceptHeadersData
23
+ public static TheoryData < string > AcceptHeadersData
26
24
{
27
25
get
28
26
{
29
- var data = new TheoryData < string >
27
+ return new TheoryData < string >
30
28
{
29
+ "application/xml-dcs" ,
31
30
"application/xml-xmlser"
32
31
} ;
33
-
34
- // Mono issue - https://github.com/aspnet/External/issues/18
35
- if ( ! TestPlatformHelper . IsMono )
36
- {
37
- data . Add ( "application/xml-dcs" ) ;
38
- }
39
-
40
- return data ;
41
32
}
42
33
}
43
34
@@ -62,19 +53,16 @@ public async Task ModelStateErrors_AreSerialized(string acceptHeader)
62
53
XmlAssert . Equal ( expectedXml , responseData ) ;
63
54
}
64
55
65
- [ ConditionalTheory ]
66
- // Mono issue - https://github.com/aspnet/External/issues/18
67
- // XmlSerializer test is disabled Mono.Xml2.XmlTextReader.ReadText is unable to read the XML.
68
- // This is fixed in mono 4.3.0.
69
- [ FrameworkSkipCondition ( RuntimeFrameworks . Mono ) ]
70
- [ InlineData ( "application/xml-xmlser" ) ]
71
- [ InlineData ( "application/xml-dcs" ) ]
56
+ [ Theory ]
57
+ [ MemberData ( nameof ( AcceptHeadersData ) ) ]
72
58
public async Task PostedSerializableError_IsBound ( string acceptHeader )
73
59
{
74
60
// Arrange
75
61
var expectedXml = "<Error><key1>key1-error</key1><key2>The input was not valid.</key2></Error>" ;
76
- var request = new HttpRequestMessage ( HttpMethod . Post , "http://localhost/SerializableError/LogErrors" ) ;
77
- request . Content = new StringContent ( expectedXml , Encoding . UTF8 , acceptHeader ) ;
62
+ var request = new HttpRequestMessage ( HttpMethod . Post , "http://localhost/SerializableError/LogErrors" )
63
+ {
64
+ Content = new StringContent ( expectedXml , Encoding . UTF8 , acceptHeader )
65
+ } ;
78
66
request . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( acceptHeader ) ) ;
79
67
80
68
// Act
@@ -89,25 +77,80 @@ public async Task PostedSerializableError_IsBound(string acceptHeader)
89
77
XmlAssert . Equal ( expectedXml , responseData ) ;
90
78
}
91
79
92
- [ ConditionalTheory ]
93
- // Mono issue - https://github.com/aspnet/External/issues/18
94
- // XmlSerializer test is disabled Mono.Xml2.XmlTextReader.ReadText is unable to read the XML.
95
- // This is fixed in mono 4.3.0.
96
- [ FrameworkSkipCondition ( RuntimeFrameworks . Mono ) ]
97
- [ InlineData ( "application/xml-xmlser" ) ]
98
- [ InlineData ( "application/xml-dcs" ) ]
99
- public async Task IsReturnedInExpectedFormat ( string acceptHeader )
80
+ public static TheoryData < string , string > InvalidInputAndHeadersData
81
+ {
82
+ get
83
+ {
84
+ return new TheoryData < string , string >
85
+ {
86
+ {
87
+ "application/xml-dcs" ,
88
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" +
89
+ "<Employee xmlns =\" http://schemas.datacontract.org/2004/07/XmlFormattersWebSite.Models\" >" +
90
+ "<Id>2</Id><Name>foo</Name></Employee>"
91
+ } ,
92
+ {
93
+ "application/xml-xmlser" ,
94
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" +
95
+ "<Employee>" +
96
+ "<Id>2</Id><Name>foo</Name></Employee>"
97
+ } ,
98
+ } ;
99
+ }
100
+ }
101
+
102
+ [ Theory ]
103
+ [ MemberData ( nameof ( InvalidInputAndHeadersData ) ) ]
104
+ public async Task IsReturnedInExpectedFormat ( string acceptHeader , string inputXml )
100
105
{
101
106
// Arrange
102
- var input = "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" +
103
- "<Employee xmlns=\" http://schemas.datacontract.org/2004/07/XmlFormattersWebSite.Models\" >" +
104
- "<Id>2</Id><Name>foo</Name></Employee>" ;
105
107
var expected = "<Error><Id>The field Id must be between 10 and 100.</Id>" +
106
108
"<Name>The field Name must be a string or array type with a minimum " +
107
109
"length of '15'.</Name></Error>" ;
108
110
var request = new HttpRequestMessage ( HttpMethod . Post , "http://localhost/SerializableError/CreateEmployee" ) ;
109
111
request . Headers . Accept . Add ( MediaTypeWithQualityHeaderValue . Parse ( acceptHeader ) ) ;
110
- request . Content = new StringContent ( input , Encoding . UTF8 , "application/xml-dcs" ) ;
112
+ request . Content = new StringContent ( inputXml , Encoding . UTF8 , acceptHeader ) ;
113
+
114
+ // Act
115
+ var response = await Client . SendAsync ( request ) ;
116
+
117
+ // Assert
118
+ Assert . Equal ( HttpStatusCode . BadRequest , response . StatusCode ) ;
119
+ var responseData = await response . Content . ReadAsStringAsync ( ) ;
120
+ XmlAssert . Equal ( expected , responseData ) ;
121
+ }
122
+
123
+ public static TheoryData < string , string > IncorrectTopLevelInputAndHeadersData
124
+ {
125
+ get
126
+ {
127
+ return new TheoryData < string , string >
128
+ {
129
+ {
130
+ "application/xml-dcs" ,
131
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" +
132
+ "<Employees xmlns =\" http://schemas.datacontract.org/2004/07/XmlFormattersWebSite.Models\" >" +
133
+ "<Id>2</Id><Name>foo</Name></Employee>"
134
+ } ,
135
+ {
136
+ "application/xml-xmlser" ,
137
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" +
138
+ "<Employees>" +
139
+ "<Id>2</Id><Name>foo</Name></Employee>"
140
+ } ,
141
+ } ;
142
+ }
143
+ }
144
+
145
+ [ Theory ]
146
+ [ MemberData ( nameof ( IncorrectTopLevelInputAndHeadersData ) ) ]
147
+ public async Task IncorrectTopLevelElement_ReturnsExpectedError ( string acceptHeader , string inputXml )
148
+ {
149
+ // Arrange
150
+ var expected = "<Error><MVC-Empty>An error occurred while deserializing input data.</MVC-Empty></Error>" ;
151
+ var request = new HttpRequestMessage ( HttpMethod . Post , "http://localhost/SerializableError/CreateEmployee" ) ;
152
+ request . Headers . Accept . Add ( MediaTypeWithQualityHeaderValue . Parse ( acceptHeader ) ) ;
153
+ request . Content = new StringContent ( inputXml , Encoding . UTF8 , acceptHeader ) ;
111
154
112
155
// Act
113
156
var response = await Client . SendAsync ( request ) ;
0 commit comments