Move error handling to Connection

The adapter shouldn't be responsible for translating errors
into the proper exception.
This commit is contained in:
Haacked
2013-09-30 10:45:03 -07:00
parent a8a5249726
commit 372fd25914
4 changed files with 53 additions and 36 deletions
+23
View File
@@ -1,10 +1,13 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Http;
using Octokit.Tests.Helpers;
using Xunit;
using Xunit.Extensions;
namespace Octokit.Tests.Http
{
@@ -116,6 +119,26 @@ namespace Octokit.Tests.Http
Assert.NotNull(resp.ApiInfo);
Assert.Equal("user", resp.ApiInfo.AcceptedOauthScopes.First());
}
[Theory]
[InlineData(HttpStatusCode.Forbidden)]
[InlineData(HttpStatusCode.Unauthorized)]
public async Task ThrowsAuthenticationExceptionExceptionForAppropriateStatusCodes(HttpStatusCode statusCode)
{
var httpClient = Substitute.For<IHttpClient>();
IResponse<string> response = new ApiResponse<string> { StatusCode = statusCode};
httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response));
var connection = new Connection(ExampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());
var exception = await AssertEx.Throws<AuthenticationException>(
async () => await connection.GetAsync<string>(new Uri("/endpoint", UriKind.Relative)));
Assert.Equal("You must be authenticated to call this method. Either supply a login/password or an " +
"oauth token.", exception.Message);
}
}
public class TheGetHtmlMethod