Files
octokit.net/Octokit.Tests/Exceptions/ApiExceptionTests.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

94 lines
3.6 KiB
C#

using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using NSubstitute;
using Octokit.Internal;
using Xunit;
using Xunit.Extensions;
namespace Octokit.Tests.Exceptions
{
public class ApiExceptionTests
{
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.GatewayTimeout
};
var exception = new ApiException(response);
Assert.Equal("Validation Failed", exception.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
Assert.Equal(HttpStatusCode.GatewayTimeout, exception.StatusCode);
}
[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("{{{{{")]
public void CreatesGitHubErrorIfResponseMessageIsNotValidJson(string responseContent)
{
var response = new ApiResponse<object>
{
Body = responseContent,
StatusCode = HttpStatusCode.GatewayTimeout
};
var exception = new ApiException(response);
Assert.Equal(responseContent, exception.ApiError.Message);
Assert.Equal(HttpStatusCode.GatewayTimeout, exception.StatusCode);
}
[Fact]
public void CreatesEmptyGitHubErrorWhenResponseBodyIsNull()
{
var response = Substitute.For<IResponse>();
response.Body.Returns("test");
var exception = new ApiException();
var anotherException = new ApiException(new ApiResponse<object> { Body = "message1" });
var thirdException = new ApiException(new ApiResponse<object> { Body = "message2" });
// It's fine if the message is null when there's no response body as long as this doesn't throw.
Assert.Null(exception.ApiError.Message);
Assert.Equal("message1", anotherException.ApiError.Message);
Assert.Equal("message2", thirdException.ApiError.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 ApiException(response);
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
var deserialized = (ApiException)formatter.Deserialize(stream);
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
}
#endif
}
}
}