Files
octokit.net/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs
Haacked a1887837be Implement 403 exception handling
This implements exception handling for the various 403 cases based off
of the Octokit.rb implementation.
https://github.com/octokit/octokit.rb/blob/master/lib/octokit/error.rb

Fixes #85
2013-10-17 22:22:56 -07:00

57 lines
2.1 KiB
C#

using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using Octokit.Internal;
using Xunit;
namespace Octokit.Tests.Exceptions
{
public class ApiValidationExceptionTests
{
public class TheConstructor
{
[Fact]
public void CreatesGitHubErrorFromJsonResponse()
{
var response = new ApiResponse<object>
{
Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " +
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}",
StatusCode = (HttpStatusCode)422
};
var exception = new ApiValidationException(response);
Assert.Equal("Validation Failed", exception.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
#if !NETFX_CORE
[Fact]
public void CanPopulateObjectFromSerializedData()
{
var response = new ApiResponse<object>
{
Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " +
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}",
StatusCode = (HttpStatusCode)422
};
var exception = new ApiValidationException(response);
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
var deserialized = (ApiValidationException)formatter.Deserialize(stream);
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
}
#endif
}
}
}