1212using System . Collections . Generic ;
1313using System . IO ;
1414using System . Diagnostics ;
15+ using Moq ;
1516
1617namespace JSONAPI . Tests . Json
1718{
@@ -21,6 +22,7 @@ public class JsonApiMediaFormaterTests
2122 Author a ;
2223 Post p , p2 , p3 , p4 ;
2324 Sample s1 , s2 ;
25+ Tag t1 , t2 , t3 ;
2426
2527 private class MockErrorSerializer : IErrorSerializer
2628 {
@@ -54,6 +56,22 @@ public void SetupModels()
5456 Name = "Jason Hater" ,
5557 } ;
5658
59+ t1 = new Tag
60+ {
61+ Id = 1 ,
62+ Text = "Ember"
63+ } ;
64+ t2 = new Tag
65+ {
66+ Id = 2 ,
67+ Text = "React"
68+ } ;
69+ t3 = new Tag
70+ {
71+ Id = 3 ,
72+ Text = "Angular"
73+ } ;
74+
5775 p = new Post ( )
5876 {
5977 Id = 1 ,
@@ -234,7 +252,7 @@ public void SerializeArrayIntegrationTest()
234252
235253 [ TestMethod ]
236254 [ DeploymentItem ( @"Data\AttributeSerializationTest.json" ) ]
237- public void Serializes_attributes_properly ( )
255+ public void Serializes_attributes_properly ( )
238256 {
239257 // Arrang
240258 JsonApiFormatter formatter = new JsonApiFormatter ( new PluralizationService ( ) ) ;
@@ -250,6 +268,24 @@ public void Serializes_attributes_properly()
250268 Assert . AreEqual ( expected , output . Trim ( ) ) ;
251269 }
252270
271+ [ TestMethod ]
272+ [ DeploymentItem ( @"Data\ByteIdSerializationTest.json" ) ]
273+ public void Serializes_byte_ids_properly ( )
274+ {
275+ // Arrang
276+ JsonApiFormatter formatter = new JsonApiFormatter ( new PluralizationService ( ) ) ;
277+ MemoryStream stream = new MemoryStream ( ) ;
278+
279+ // Act
280+ formatter . WriteToStreamAsync ( typeof ( Tag ) , new [ ] { t1 , t2 , t3 } , stream , null , null ) ;
281+
282+ // Assert
283+ string output = System . Text . Encoding . ASCII . GetString ( stream . ToArray ( ) ) ;
284+ Trace . WriteLine ( output ) ;
285+ var expected = JsonHelpers . MinifyJson ( File . ReadAllText ( "ByteIdSerializationTest.json" ) ) ;
286+ Assert . AreEqual ( expected , output . Trim ( ) ) ;
287+ }
288+
253289 [ TestMethod ]
254290 [ DeploymentItem ( @"Data\ReformatsRawJsonStringWithUnquotedKeys.json" ) ]
255291 public void Reformats_raw_json_string_with_unquoted_keys ( )
@@ -315,20 +351,29 @@ public void SerializeErrorIntegrationTest()
315351 JsonApiFormatter formatter = new JSONAPI . Json . JsonApiFormatter ( new JSONAPI . Core . PluralizationService ( ) ) ;
316352 MemoryStream stream = new MemoryStream ( ) ;
317353
318- // Act
319- var payload = new HttpError ( new Exception ( "This is the exception message!" ) , true )
354+ var mockInnerException = new Mock < Exception > ( MockBehavior . Strict ) ;
355+ mockInnerException . Setup ( m => m . Message ) . Returns ( "Inner exception message" ) ;
356+ mockInnerException . Setup ( m => m . StackTrace ) . Returns ( "Inner stack trace" ) ;
357+
358+ var outerException = new Exception ( "Outer exception message" , mockInnerException . Object ) ;
359+
360+ var payload = new HttpError ( outerException , true )
320361 {
321- StackTrace = "Stack trace would go here "
362+ StackTrace = "Outer stack trace "
322363 } ;
364+
365+ // Act
323366 formatter . WriteToStreamAsync ( typeof ( HttpError ) , payload , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . TransportContext ) null ) ;
324367
325368 // Assert
326369 var expectedJson = File . ReadAllText ( "ErrorSerializerTest.json" ) ;
327370 var minifiedExpectedJson = JsonHelpers . MinifyJson ( expectedJson ) ;
328371 var output = System . Text . Encoding . ASCII . GetString ( stream . ToArray ( ) ) ;
329- output = Regex . Replace ( output ,
330- @"[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}" ,
331- "TEST-ERROR-ID" ) ; // We don't know what the GUID will be, so replace it
372+
373+ // We don't know what the GUIDs will be, so replace them
374+ var regex = new Regex ( @"[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}" ) ;
375+ output = regex . Replace ( output , "OUTER-ID" , 1 ) ;
376+ output = regex . Replace ( output , "INNER-ID" , 1 ) ;
332377 output . Should ( ) . Be ( minifiedExpectedJson ) ;
333378 }
334379
0 commit comments