11using System ;
22using System . Linq ;
3+ using System . Text . RegularExpressions ;
4+ using System . Web . Http ;
5+ using FluentAssertions ;
36using Microsoft . VisualStudio . TestTools . UnitTesting ;
47using JSONAPI . Tests . Models ;
58using Newtonsoft . Json ;
@@ -17,6 +20,22 @@ public class JsonApiMediaFormaterTests
1720 Author a ;
1821 Post p , p2 , p3 , p4 ;
1922
23+ private class MockErrorSerializer : IErrorSerializer
24+ {
25+ public bool CanSerialize ( Type type )
26+ {
27+ return true ;
28+ }
29+
30+ public void SerializeError ( object error , Stream writeStream , JsonWriter writer , JsonSerializer serializer )
31+ {
32+ writer . WriteStartObject ( ) ;
33+ writer . WritePropertyName ( "test" ) ;
34+ serializer . Serialize ( writer , "foo" ) ;
35+ writer . WriteEndObject ( ) ;
36+ }
37+ }
38+
2039 [ TestInitialize ]
2140 public void SetupModels ( )
2241 {
@@ -155,6 +174,52 @@ public void SerializeArrayIntegrationTest()
155174 //Assert.AreEqual("[2,3,4]", sw.ToString());
156175 }
157176
177+ [ TestMethod ]
178+ [ DeploymentItem ( @"Data\FormatterErrorSerializationTest.json" ) ]
179+ public void Should_serialize_error ( )
180+ {
181+ // Arrange
182+ var formatter = new JSONAPI . Json . JsonApiFormatter ( new MockErrorSerializer ( ) ) ;
183+ formatter . PluralizationService = new JSONAPI . Core . PluralizationService ( ) ;
184+ var stream = new MemoryStream ( ) ;
185+
186+ // Act
187+ var payload = new HttpError ( new Exception ( ) , true ) ;
188+ formatter . WriteToStreamAsync ( typeof ( HttpError ) , payload , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . TransportContext ) null ) ;
189+
190+ // Assert
191+ var expectedJson = File . ReadAllText ( "FormatterErrorSerializationTest.json" ) ;
192+ var minifiedExpectedJson = JsonHelpers . MinifyJson ( expectedJson ) ;
193+ var output = System . Text . Encoding . ASCII . GetString ( stream . ToArray ( ) ) ;
194+ output . Should ( ) . Be ( minifiedExpectedJson ) ;
195+ }
196+
197+ [ TestMethod ]
198+ [ DeploymentItem ( @"Data\ErrorSerializerTest.json" ) ]
199+ public void SerializeErrorIntegrationTest ( )
200+ {
201+ // Arrange
202+ JsonApiFormatter formatter = new JSONAPI . Json . JsonApiFormatter ( ) ;
203+ formatter . PluralizationService = new JSONAPI . Core . PluralizationService ( ) ;
204+ MemoryStream stream = new MemoryStream ( ) ;
205+
206+ // Act
207+ var payload = new HttpError ( new Exception ( "This is the exception message!" ) , true )
208+ {
209+ StackTrace = "Stack trace would go here"
210+ } ;
211+ formatter . WriteToStreamAsync ( typeof ( HttpError ) , payload , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . TransportContext ) null ) ;
212+
213+ // Assert
214+ var expectedJson = File . ReadAllText ( "ErrorSerializerTest.json" ) ;
215+ var minifiedExpectedJson = JsonHelpers . MinifyJson ( expectedJson ) ;
216+ var output = System . Text . Encoding . ASCII . GetString ( stream . ToArray ( ) ) ;
217+ output = Regex . Replace ( output ,
218+ @"[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}" ,
219+ "TEST-ERROR-ID" ) ; // We don't know what the GUID will be, so replace it
220+ output . Should ( ) . Be ( minifiedExpectedJson ) ;
221+ }
222+
158223 [ TestMethod ]
159224 public void DeserializeCollectionIntegrationTest ( )
160225 {
0 commit comments