diff --git a/Octokit.Tests/Clients/MiscellaneousClientTests.cs b/Octokit.Tests/Clients/MiscellaneousClientTests.cs index c9d8045e..50d1bec6 100644 --- a/Octokit.Tests/Clients/MiscellaneousClientTests.cs +++ b/Octokit.Tests/Clients/MiscellaneousClientTests.cs @@ -14,10 +14,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task RequestsTheEmojiEndpoint() { - IApiResponse response = new ApiResponse(new Response - { - Body = "Test" - }, "Test"); + IApiResponse response = new ApiResponse(new Response(), "Test"); var connection = Substitute.For(); connection.Post(Args.Uri, "**Test**", "text/html", "text/plain") .Returns(Task.FromResult(response)); diff --git a/Octokit.Tests/Exceptions/ApiExceptionTests.cs b/Octokit.Tests/Exceptions/ApiExceptionTests.cs index 8d789dec..124f5540 100644 --- a/Octokit.Tests/Exceptions/ApiExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiExceptionTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; @@ -44,12 +45,13 @@ namespace Octokit.Tests.Exceptions [Fact] public void CreatesGitHubErrorFromJsonResponse() { - var response = new Response - { - Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + + var response = new Response( + HttpStatusCode.GatewayTimeout, + @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - StatusCode = HttpStatusCode.GatewayTimeout - }; + new Dictionary(), + "application/json" + ); var exception = new ApiException(response); @@ -65,11 +67,11 @@ namespace Octokit.Tests.Exceptions [InlineData("

502 Bad Gateway

The server returned an invalid or incomplete response.")] public void CreatesGitHubErrorIfResponseMessageIsNotValidJson(string responseContent) { - var response = new Response - { - Body = responseContent, - StatusCode = HttpStatusCode.GatewayTimeout - }; + var response = new Response( + HttpStatusCode.GatewayTimeout, + responseContent, + new Dictionary(), + "application/json"); var exception = new ApiException(response); @@ -84,8 +86,8 @@ namespace Octokit.Tests.Exceptions response.Body.Returns("test"); var exception = new ApiException(); - var anotherException = new ApiException(new Response { Body = "message1" }); - var thirdException = new ApiException(new Response { Body = "message2" }); + var anotherException = new ApiException(new Response(HttpStatusCode.ServiceUnavailable, "message1", new Dictionary(), "application/json")); + var thirdException = new ApiException(new Response(HttpStatusCode.ServiceUnavailable, "message2", new Dictionary(), "application/json")); // 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); @@ -97,12 +99,12 @@ namespace Octokit.Tests.Exceptions [Fact] public void CanPopulateObjectFromSerializedData() { - var response = new Response - { - Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - StatusCode = (HttpStatusCode)422 - }; + IResponse response = new Response( + (HttpStatusCode)422, + @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", + new Dictionary(), + "application/json"); var exception = new ApiException(response); diff --git a/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs b/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs index a2ea8625..fd7d8cc9 100644 --- a/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Formatters.Binary; @@ -14,12 +15,13 @@ namespace Octokit.Tests.Exceptions [Fact] public void CreatesGitHubErrorFromJsonResponse() { - var response = new Response - { - Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - StatusCode = (HttpStatusCode)422 - }; + var response = new Response( + (HttpStatusCode)422, + @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", + new Dictionary(), + "application/json" + ); var exception = new ApiValidationException(response); @@ -44,13 +46,13 @@ namespace Octokit.Tests.Exceptions [Fact] public void CanPopulateObjectFromSerializedData() { - var response = new Response - { - Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - StatusCode = (HttpStatusCode)422 - }; - + IResponse response = new Response( + (HttpStatusCode)422, + @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", + new Dictionary(), + "application/json"); + var exception = new ApiValidationException(response); using (var stream = new MemoryStream()) diff --git a/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs b/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs index fa0b8c71..0d620a37 100644 --- a/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.Collections.Generic; +using System.Net; using Octokit.Internal; using Xunit; @@ -13,7 +14,11 @@ namespace Octokit.Tests.Exceptions { const string responseBody = "{\"message\":\"YOU SHALL NOT PASS!\"," + "\"documentation_url\":\"http://developer.github.com/v3\"}"; - var response = new Response { Body = responseBody, StatusCode = HttpStatusCode.Forbidden }; + var response = new Response( + HttpStatusCode.Forbidden, + responseBody, + new Dictionary(), + "application/json"); var forbiddenException = new ForbiddenException(response); Assert.Equal("YOU SHALL NOT PASS!", forbiddenException.ApiError.Message); diff --git a/Octokit.Tests/Http/ApiConnectionTests.cs b/Octokit.Tests/Http/ApiConnectionTests.cs index 9845fd4b..66cac0ea 100644 --- a/Octokit.Tests/Http/ApiConnectionTests.cs +++ b/Octokit.Tests/Http/ApiConnectionTests.cs @@ -19,7 +19,7 @@ namespace Octokit.Tests.Http public async Task MakesGetRequestForItem() { var getUri = new Uri("anything", UriKind.Relative); - IApiResponse response = new ApiResponse(new Response { Body = new object() }); + IApiResponse response = new ApiResponse(new Response()); var connection = Substitute.For(); connection.Get(Args.Uri, null, null).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -35,7 +35,7 @@ namespace Octokit.Tests.Http { var getUri = new Uri("anything", UriKind.Relative); const string accepts = "custom/accepts"; - IApiResponse response = new ApiResponse(new Response { Body = new object() }); + IApiResponse response = new ApiResponse(new Response()); var connection = Substitute.For(); connection.Get(Args.Uri, null, Args.String).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -62,7 +62,7 @@ namespace Octokit.Tests.Http public async Task MakesHtmlRequest() { var getUri = new Uri("anything", UriKind.Relative); - IApiResponse response = new ApiResponse(new Response { Body = "" }, ""); + IApiResponse response = new ApiResponse(new Response(), ""); var connection = Substitute.For(); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -142,7 +142,7 @@ namespace Octokit.Tests.Http var patchUri = new Uri("anything", UriKind.Relative); var sentData = new object(); var accepts = "custom/accepts"; - IApiResponse response = new ApiResponse(new Response { Body = new object() }); + IApiResponse response = new ApiResponse(new Response()); var connection = Substitute.For(); connection.Patch(Args.Uri, Args.Object, Args.String).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -225,7 +225,7 @@ namespace Octokit.Tests.Http { var putUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response { Body = new object() }); + IApiResponse response = new ApiResponse(new Response()); var connection = Substitute.For(); connection.Put(Args.Uri, Args.Object).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -241,7 +241,7 @@ namespace Octokit.Tests.Http { var putUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response { Body = new object() }); + IApiResponse response = new ApiResponse(new Response()); var connection = Substitute.For(); connection.Put(Args.Uri, Args.Object, "two-factor").Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 4e9de6f6..9e0a1575 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -158,12 +158,13 @@ namespace Octokit.Tests.Http public async Task ThrowsApiValidationExceptionFor422Response() { var httpClient = Substitute.For(); - IResponse response = new Response - { - StatusCode = (HttpStatusCode)422, - Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}" - }; + IResponse response = new Response( + (HttpStatusCode)422, + @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", + new Dictionary(), + "application/json" + ); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -182,12 +183,12 @@ namespace Octokit.Tests.Http public async Task ThrowsRateLimitExceededExceptionForForbidderResponse() { var httpClient = Substitute.For(); - IResponse response = new Response - { - StatusCode = HttpStatusCode.Forbidden, - Body = "{\"message\":\"API rate limit exceeded. " + - "See http://developer.github.com/v3/#rate-limiting for details.\"}" - }; + IResponse response = new Response( + HttpStatusCode.Forbidden, + "{\"message\":\"API rate limit exceeded. " + + "See http://developer.github.com/v3/#rate-limiting for details.\"}", + new Dictionary(), + "application/json"); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -206,12 +207,12 @@ namespace Octokit.Tests.Http public async Task ThrowsLoginAttemptsExceededExceptionForForbiddenResponse() { var httpClient = Substitute.For(); - IResponse response = new Response - { - StatusCode = HttpStatusCode.Forbidden, - Body = "{\"message\":\"Maximum number of login attempts exceeded\"," + - "\"documentation_url\":\"http://developer.github.com/v3\"}" - }; + IResponse response = new Response( + HttpStatusCode.Forbidden, + "{\"message\":\"Maximum number of login attempts exceeded\"," + + "\"documentation_url\":\"http://developer.github.com/v3\"}", + new Dictionary(), + "application/json"); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -230,11 +231,12 @@ namespace Octokit.Tests.Http public async Task ThrowsNotFoundExceptionForFileNotFoundResponse() { var httpClient = Substitute.For(); - IResponse response = new Response - { - StatusCode = HttpStatusCode.NotFound, - Body = "GONE BYE BYE!" - }; + IResponse response = new Response( + HttpStatusCode.NotFound, + "GONE BYE BYE!", + new Dictionary(), + "application/json"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -252,11 +254,11 @@ namespace Octokit.Tests.Http public async Task ThrowsForbiddenExceptionForUnknownForbiddenResponse() { var httpClient = Substitute.For(); - IResponse response = new Response - { - StatusCode = HttpStatusCode.Forbidden, - Body = "YOU SHALL NOT PASS!" - }; + IResponse response = new Response( + HttpStatusCode.Forbidden, + "YOU SHALL NOT PASS!", + new Dictionary(), + "application/json"); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, diff --git a/Octokit/Clients/ApiPagination.cs b/Octokit/Clients/ApiPagination.cs index c277496b..6e326bc5 100644 --- a/Octokit/Clients/ApiPagination.cs +++ b/Octokit/Clients/ApiPagination.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; +using System.Globalization; #if NET_45 using System.Collections.ObjectModel; #endif using System.Threading.Tasks; -using Octokit.Internal; using System.Net; namespace Octokit @@ -34,12 +34,7 @@ namespace Octokit catch (NotFoundException) { throw new NotFoundException( - new Response - { - StatusCode = HttpStatusCode.NotFound, - Body = string.Format("{0} was not found.", uri.OriginalString) - }); - + string.Format(CultureInfo.InvariantCulture, "{0} was not found.", uri.OriginalString), HttpStatusCode.NotFound); } } } diff --git a/Octokit/Exceptions/NotFoundException.cs b/Octokit/Exceptions/NotFoundException.cs index a8410303..8169c7d7 100644 --- a/Octokit/Exceptions/NotFoundException.cs +++ b/Octokit/Exceptions/NotFoundException.cs @@ -24,6 +24,15 @@ namespace Octokit { } + /// + /// Constructs an instance of NotFoundException + /// + /// The exception message + /// The http status code returned by the response + public NotFoundException(string message, HttpStatusCode statusCode) : base(message, statusCode) + { + } + /// /// Constructs an instance of NotFoundException /// diff --git a/Octokit/Http/Response.cs b/Octokit/Http/Response.cs index 919f13ca..757a920f 100644 --- a/Octokit/Http/Response.cs +++ b/Octokit/Http/Response.cs @@ -30,7 +30,7 @@ namespace Octokit.Internal ContentType = contentType; } - public object Body { get; set; } + public object Body { get; private set; } public IReadOnlyDictionary Headers { get; private set; } public ApiInfo ApiInfo { get; private set; } public HttpStatusCode StatusCode { get; set; }