Skip to content

Commit 8288768

Browse files
author
Chris Santero
committed
serialize http errors
1 parent 10c7537 commit 8288768

14 files changed

Lines changed: 539 additions & 235 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"errors": [
3+
{
4+
"id": "TEST-ERROR-ID",
5+
"status": "500",
6+
"title": "System.Exception",
7+
"detail": "This is the exception message!",
8+
"inner": null,
9+
"stackTrace": "Stack trace would go here"
10+
}
11+
]
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"test":"foo"}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<Prefer32Bit>false</Prefer32Bit>
3939
</PropertyGroup>
4040
<ItemGroup>
41+
<Reference Include="FluentAssertions">
42+
<HintPath>..\packages\FluentAssertions.3.2.2\lib\net45\FluentAssertions.dll</HintPath>
43+
</Reference>
44+
<Reference Include="FluentAssertions.Core">
45+
<HintPath>..\packages\FluentAssertions.3.2.2\lib\net45\FluentAssertions.Core.dll</HintPath>
46+
</Reference>
4147
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
4248
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4349
<SpecificVersion>False</SpecificVersion>
@@ -60,6 +66,8 @@
6066
<SpecificVersion>False</SpecificVersion>
6167
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\net45\System.Web.Http.WebHost.dll</HintPath>
6268
</Reference>
69+
<Reference Include="System.XML" />
70+
<Reference Include="System.Xml.Linq" />
6371
</ItemGroup>
6472
<ItemGroup>
6573
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
@@ -68,7 +76,9 @@
6876
</ItemGroup>
6977
<ItemGroup>
7078
<Compile Include="Core\MetadataManagerTests.cs" />
71-
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
79+
<Compile Include="Json\ErrorSerializerTests.cs" />
80+
<Compile Include="Json\JsonApiMediaFormatterTests.cs" />
81+
<Compile Include="Json\JsonHelpers.cs" />
7282
<Compile Include="Models\Author.cs" />
7383
<Compile Include="Models\Comment.cs" />
7484
<Compile Include="Models\Post.cs" />
@@ -82,6 +92,12 @@
8292
</ItemGroup>
8393
<ItemGroup>
8494
<None Include="app.config" />
95+
<None Include="Data\FormatterErrorSerializationTest.json">
96+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97+
</None>
98+
<None Include="Data\ErrorSerializerTest.json">
99+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100+
</None>
85101
<None Include="Data\SerializerIntegrationTest.json">
86102
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
87103
</None>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using System.Web.Http;
4+
using FluentAssertions;
5+
using JSONAPI.Json;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Newtonsoft.Json;
8+
9+
namespace JSONAPI.Tests.Json
10+
{
11+
[TestClass]
12+
public class ErrorSerializerTests
13+
{
14+
private class TestErrorIdProvider : IErrorIdProvider
15+
{
16+
public string GenerateId(HttpError error)
17+
{
18+
return "TEST-ERROR-ID";
19+
}
20+
}
21+
22+
[TestMethod]
23+
public void CanSerialize_returns_true_for_HttpError()
24+
{
25+
var serializer = new ErrorSerializer();
26+
var result = serializer.CanSerialize(typeof (HttpError));
27+
result.Should().BeTrue();
28+
}
29+
30+
[TestMethod]
31+
public void CanSerialize_returns_false_for_Exception()
32+
{
33+
var serializer = new ErrorSerializer();
34+
var result = serializer.CanSerialize(typeof(Exception));
35+
result.Should().BeFalse();
36+
}
37+
38+
[TestMethod]
39+
[DeploymentItem(@"Data\ErrorSerializerTest.json")]
40+
public void SerializeError_serializes_httperror()
41+
{
42+
using (var stream = new MemoryStream())
43+
{
44+
var textWriter = new StreamWriter(stream);
45+
var writer = new JsonTextWriter(textWriter);
46+
var error = new HttpError(new Exception("This is the exception message!"), true)
47+
{
48+
StackTrace = "Stack trace would go here"
49+
};
50+
var jsonSerializer = new JsonSerializer();
51+
52+
var serializer = new ErrorSerializer(new TestErrorIdProvider());
53+
serializer.SerializeError(error, stream, writer, jsonSerializer);
54+
55+
writer.Flush();
56+
57+
var expectedJson = File.ReadAllText("ErrorSerializerTest.json");
58+
var minifiedExpectedJson = JsonHelpers.MinifyJson(expectedJson);
59+
var output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
60+
output.Should().Be(minifiedExpectedJson);
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)