From 287861e4a7b6ccfa03f8b92c1e191dbf9b01ec55 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Tue, 14 Apr 2020 11:30:15 -0300 Subject: [PATCH] rewrite the setup of responses to use a standard helper function (#2177) --- Octokit.Tests/Clients/AssigneesClientTests.cs | 10 +- .../Clients/AuthorizationsClientTests.cs | 7 +- Octokit.Tests/Clients/BlobClientTests.cs | 10 +- .../Enterprise/EnterpriseProbeTests.cs | 15 ++- Octokit.Tests/Clients/EventsClientTests.cs | 10 +- Octokit.Tests/Clients/FollowersClientTests.cs | 15 +-- Octokit.Tests/Clients/GistsClientTests.cs | 12 +- .../Clients/IssueCommentsClientTests.cs | 14 +-- Octokit.Tests/Clients/IssuesClientTests.cs | 8 +- .../Clients/OrganizationMembersClientTests.cs | 15 +-- .../Clients/RepoCollaboratorsClientTests.cs | 14 ++- .../Clients/RepositoryCommentsClientTests.cs | 8 +- Octokit.Tests/Clients/StarredClientTests.cs | 6 +- Octokit.Tests/Clients/TreesClientTests.cs | 10 +- Octokit.Tests/Clients/WatchedClientTests.cs | 10 +- .../Exceptions/AbuseExceptionTests.cs | 27 ++--- Octokit.Tests/Exceptions/ApiExceptionTests.cs | 62 ++++------ .../Exceptions/ApiValidationExceptionTests.cs | 23 ++-- .../Exceptions/ForbiddenExceptionTests.cs | 19 ++-- .../LegalRestrictionExceptionTests.cs | 10 +- .../LoginAttemptsExceededExceptionTests.cs | 12 +- .../RateLimitExceededExceptionTests.cs | 13 ++- .../TwoFactorRequiredExceptionTests.cs | 8 +- Octokit.Tests/Helpers/TaskHelper.cs | 16 --- Octokit.Tests/Helpers/TestSetup.cs | 44 ++++++++ Octokit.Tests/Http/ApiConnectionTests.cs | 42 ++++--- Octokit.Tests/Http/ConnectionTests.cs | 106 +++++++++--------- Octokit.Tests/Http/JsonHttpPipelineTests.cs | 24 ++-- Octokit.Tests/Http/RateLimitTests.cs | 7 +- Octokit.Tests/Http/ResponseTests.cs | 6 +- .../Models/ReadOnlyPagedCollectionTests.cs | 9 +- .../ObservableIssueTimelineClientTests.cs | 27 ++--- .../Reactive/ObservableIssuesClientTests.cs | 21 ++-- .../ObservableIssuesEventsClientTests.cs | 50 ++------- .../ObservableMilestonesClientTests.cs | 7 +- ...blePullRequestReviewCommentsClientTests.cs | 15 ++- ...ObservablePullRequestReviewsClientTests.cs | 11 +- .../ObservablePullRequestsClientTests.cs | 20 ++-- .../ObservableRepositoriesClientTests.cs | 13 ++- ...ObservableRepositoryContentsClientTests.cs | 75 ++++--------- Octokit/Http/Response.cs | 7 +- 41 files changed, 390 insertions(+), 448 deletions(-) delete mode 100644 Octokit.Tests/Helpers/TaskHelper.cs create mode 100644 Octokit.Tests/Helpers/TestSetup.cs diff --git a/Octokit.Tests/Clients/AssigneesClientTests.cs b/Octokit.Tests/Clients/AssigneesClientTests.cs index 738e3eb0..652838e8 100644 --- a/Octokit.Tests/Clients/AssigneesClientTests.cs +++ b/Octokit.Tests/Clients/AssigneesClientTests.cs @@ -6,6 +6,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class AssigneesClientTests @@ -114,7 +116,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repos/foo/bar/assignees/cody"), null, null).Returns(responseTask); @@ -132,7 +134,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCodeWithRepositoryId(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repositories/1/assignees/cody"), null, null) @@ -150,7 +152,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var responseTask = TestSetup.GetApiResponse(HttpStatusCode.Conflict); + var responseTask = CreateApiResponse(HttpStatusCode.Conflict); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repos/foo/bar/assignees/cody"), null, null) @@ -166,7 +168,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCodeWithRepositoryId() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repositories/1/assignees/cody"), diff --git a/Octokit.Tests/Clients/AuthorizationsClientTests.cs b/Octokit.Tests/Clients/AuthorizationsClientTests.cs index b442c627..0464fbcc 100644 --- a/Octokit.Tests/Clients/AuthorizationsClientTests.cs +++ b/Octokit.Tests/Clients/AuthorizationsClientTests.cs @@ -5,9 +5,10 @@ using System.Net; using System.Reflection; using System.Threading.Tasks; using NSubstitute; -using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { /// @@ -152,9 +153,7 @@ namespace Octokit.Tests.Clients var data = new NewAuthorization(); var client = Substitute.For(); client.Put(Args.Uri, Args.Object, Args.String) - .ThrowsAsync( - new AuthorizationException( - new Response(HttpStatusCode.Unauthorized, null, new Dictionary(), "application/json"))); + .ThrowsAsync(new AuthorizationException(CreateResponse(HttpStatusCode.Unauthorized))); var authEndpoint = new AuthorizationsClient(client); await Assert.ThrowsAsync(() => diff --git a/Octokit.Tests/Clients/BlobClientTests.cs b/Octokit.Tests/Clients/BlobClientTests.cs index 36bd0bb2..d4f7dff2 100644 --- a/Octokit.Tests/Clients/BlobClientTests.cs +++ b/Octokit.Tests/Clients/BlobClientTests.cs @@ -1,11 +1,12 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class BlobClientTests @@ -110,11 +111,10 @@ namespace Octokit.Tests.Clients "\"sha\": \"3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15\"," + "\"size\": 100" + "}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - blobResponseJson, - new Dictionary(), - "application/json"); + blobResponseJson); + var jsonPipeline = new JsonHttpPipeline(); var response = jsonPipeline.DeserializeResponse(httpResponse); diff --git a/Octokit.Tests/Clients/Enterprise/EnterpriseProbeTests.cs b/Octokit.Tests/Clients/Enterprise/EnterpriseProbeTests.cs index 0d225f58..186c7509 100644 --- a/Octokit.Tests/Clients/Enterprise/EnterpriseProbeTests.cs +++ b/Octokit.Tests/Clients/Enterprise/EnterpriseProbeTests.cs @@ -11,6 +11,8 @@ using Octokit.Internal; using Octokit.Tests; using Xunit; +using static Octokit.Internal.TestSetup; + public class EnterpriseProbeTests { public class TheProbeMethod @@ -25,9 +27,7 @@ public class EnterpriseProbeTests { "Server", "REVERSE-PROXY" }, { "X-GitHub-Request-Id", Guid.NewGuid().ToString() } }; - var response = Substitute.For(); - response.StatusCode.Returns(httpStatusCode); - response.Headers.Returns(headers); + var response = CreateResponse(httpStatusCode, headers); var productHeader = new ProductHeaderValue("GHfW", "99"); var httpClient = Substitute.For(); @@ -49,18 +49,17 @@ public class EnterpriseProbeTests [Fact] public async Task ReturnsExistsForApiExceptionWithCorrectHeaders() { - var headers = new Dictionary() { { "Server", "GitHub.com" }, { "X-GitHub-Request-Id", Guid.NewGuid().ToString() } }; - var httpClient = Substitute.For(); - var response = Substitute.For(); - response.Headers.Returns(headers); - var apiException = new ApiException(response); + var apiException = new ApiException(CreateResponse(HttpStatusCode.OK, headers)); + + var httpClient = Substitute.For(); httpClient.Send(Args.Request, CancellationToken.None).ThrowsAsync(apiException); + var productHeader = new ProductHeaderValue("GHfW", "99"); var enterpriseProbe = new EnterpriseProbe(productHeader, httpClient); diff --git a/Octokit.Tests/Clients/EventsClientTests.cs b/Octokit.Tests/Clients/EventsClientTests.cs index 29d19660..d2bb2bfd 100644 --- a/Octokit.Tests/Clients/EventsClientTests.cs +++ b/Octokit.Tests/Clients/EventsClientTests.cs @@ -8,6 +8,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class EventsClientTests @@ -1004,12 +1006,12 @@ namespace Octokit.Tests.Clients Assert.Equal("started", payload.Action); } - private EventsClient GetTestingEventsClient(JsonObject response) + private EventsClient GetTestingEventsClient(JsonObject json) { - var responseString = response.ToString(); + var responseString = json.ToString(); var httpClientMock = Substitute.For(); - httpClientMock.Send(Arg.Is((IRequest r) => r.Endpoint.ToString().Contains("events")), Arg.Any()).Returns(Task.FromResult( - new Response(HttpStatusCode.Accepted, responseString, new Dictionary(), "application/json") as IResponse)); + var response = CreateResponse(HttpStatusCode.Accepted, responseString); + httpClientMock.Send(Arg.Is((IRequest r) => r.Endpoint.ToString().Contains("events")), Arg.Any()).Returns(Task.FromResult(response)); return new EventsClient(new ApiConnection(new Connection(new ProductHeaderValue("mock"), httpClientMock))); } diff --git a/Octokit.Tests/Clients/FollowersClientTests.cs b/Octokit.Tests/Clients/FollowersClientTests.cs index 38aaf25f..c1f18e13 100644 --- a/Octokit.Tests/Clients/FollowersClientTests.cs +++ b/Octokit.Tests/Clients/FollowersClientTests.cs @@ -1,11 +1,12 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { /// @@ -204,7 +205,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "user/following/alfhenrik"), @@ -221,7 +222,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "user/following/alfhenrik"), @@ -251,7 +252,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "users/alfhenrik/following/alfhenrik-test"), @@ -268,7 +269,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "users/alfhenrik/following/alfhenrik-test"), @@ -299,7 +300,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NoContent, true)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -318,7 +319,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); diff --git a/Octokit.Tests/Clients/GistsClientTests.cs b/Octokit.Tests/Clients/GistsClientTests.cs index 03920bf4..ac42d6cf 100644 --- a/Octokit.Tests/Clients/GistsClientTests.cs +++ b/Octokit.Tests/Clients/GistsClientTests.cs @@ -1,13 +1,15 @@ -using NSubstitute; -using Octokit.Internal; -using System; +using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; +using NSubstitute; +using Octokit.Internal; using Octokit; using Octokit.Tests; using Xunit; +using static Octokit.Internal.TestSetup; + public class GistsClientTests { public static Dictionary DictionaryWithSince @@ -499,7 +501,7 @@ public class GistsClientTests [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -518,7 +520,7 @@ public class GistsClientTests [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); diff --git a/Octokit.Tests/Clients/IssueCommentsClientTests.cs b/Octokit.Tests/Clients/IssueCommentsClientTests.cs index e83a82ad..4decf526 100644 --- a/Octokit.Tests/Clients/IssueCommentsClientTests.cs +++ b/Octokit.Tests/Clients/IssueCommentsClientTests.cs @@ -6,6 +6,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class IssueCommentsClientTests @@ -438,11 +440,9 @@ namespace Octokit.Tests.Clients "\"created_at\": \"2011-04-14T16:00:49Z\"," + "\"updated_at\": \"2011-04-14T16:00:49Z\"" + "}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - issueResponseJson, - new Dictionary(), - "application/json"); + issueResponseJson); var jsonPipeline = new JsonHttpPipeline(); @@ -481,11 +481,9 @@ namespace Octokit.Tests.Clients "\"url\": \"https://api.github.com/repos/octocat/Hello-World/issues/comments/1/reactions\"" + "}" + "}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - issueResponseJson, - new Dictionary(), - "application/json"); + issueResponseJson); var jsonPipeline = new JsonHttpPipeline(); diff --git a/Octokit.Tests/Clients/IssuesClientTests.cs b/Octokit.Tests/Clients/IssuesClientTests.cs index 893c3561..d30b518c 100644 --- a/Octokit.Tests/Clients/IssuesClientTests.cs +++ b/Octokit.Tests/Clients/IssuesClientTests.cs @@ -6,6 +6,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class IssuesClientTests @@ -592,11 +594,9 @@ namespace Octokit.Tests.Clients "ed_events\",\"type\":\"User\",\"site_admin\":false},\"labels\":[],\"state\":\"open\",\"assignee" + "\":null,\"milestone\":null,\"comments\":0,\"created_at\":\"2013-10-22T17:02:48Z\",\"updated_at\"" + ":\"2013-10-22T17:02:48Z\",\"closed_at\":null,\"body\":\"A new unassigned issue\",\"closed_by\":null}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - issueResponseJson, - new Dictionary(), - "application/json"); + issueResponseJson); var jsonPipeline = new JsonHttpPipeline(); diff --git a/Octokit.Tests/Clients/OrganizationMembersClientTests.cs b/Octokit.Tests/Clients/OrganizationMembersClientTests.cs index b808c166..d81e6d25 100644 --- a/Octokit.Tests/Clients/OrganizationMembersClientTests.cs +++ b/Octokit.Tests/Clients/OrganizationMembersClientTests.cs @@ -1,11 +1,12 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { /// @@ -309,7 +310,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.Found, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -328,7 +329,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -361,7 +362,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -380,7 +381,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -437,7 +438,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NoContent, true)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var response = new Response(status, null, new Dictionary(), "application/json"); + var response = CreateResponse(status); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); @@ -456,7 +457,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var response = new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Conflict); var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); diff --git a/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs b/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs index d0e972c0..c3177f51 100644 --- a/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs +++ b/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; using NSubstitute; -using Xunit; -using System.Net; using Octokit.Internal; +using Xunit; + +using static Octokit.Internal.TestSetup; namespace Octokit.Tests.Clients { @@ -206,7 +208,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repos/owner/test/collaborators/user1"), null, null) @@ -226,7 +228,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCodeWithRepositoryId(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repositories/1/collaborators/user1"), null, null) @@ -244,7 +246,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { - var responseTask = TestSetup.GetApiResponse(HttpStatusCode.Conflict); + var responseTask = CreateApiResponse(HttpStatusCode.Conflict); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repos/foo/bar/assignees/cody"), null, null) @@ -260,7 +262,7 @@ namespace Octokit.Tests.Clients [Fact] public async Task ThrowsExceptionForInvalidStatusCodeWithRepositoryId() { - var responseTask = TestSetup.GetApiResponse(HttpStatusCode.Conflict); + var responseTask = TestSetup.CreateApiResponse(HttpStatusCode.Conflict); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repositories/1/assignees/cody"), null, null) diff --git a/Octokit.Tests/Clients/RepositoryCommentsClientTests.cs b/Octokit.Tests/Clients/RepositoryCommentsClientTests.cs index afa8fcb8..40432f96 100644 --- a/Octokit.Tests/Clients/RepositoryCommentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryCommentsClientTests.cs @@ -8,6 +8,8 @@ using Octokit.Internal; using Octokit.Tests; using Xunit; +using static Octokit.Internal.TestSetup; + public class RepositoryCommentsClientTests { public class TheGetMethod @@ -387,11 +389,9 @@ public class RepositoryCommentsClientTests "\"created_at\": \"2011-04-14T16:00:49Z\"," + "\"updated_at\": \"2011-04-14T16:00:49Z\"" + "}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - commitCommentResponseJson, - new Dictionary(), - "application/json"); + commitCommentResponseJson); var jsonPipeline = new JsonHttpPipeline(); diff --git a/Octokit.Tests/Clients/StarredClientTests.cs b/Octokit.Tests/Clients/StarredClientTests.cs index afae6812..5b07c2d0 100644 --- a/Octokit.Tests/Clients/StarredClientTests.cs +++ b/Octokit.Tests/Clients/StarredClientTests.cs @@ -6,6 +6,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class StarredClientTests @@ -505,7 +507,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.NotFound, false)] public async Task ReturnsCorrectResultBasedOnStatus(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "user/starred/yes/no"), null, null) @@ -529,7 +531,7 @@ namespace Octokit.Tests.Clients [InlineData(HttpStatusCode.OK, false)] public async Task ReturnsCorrectResultBasedOnStatus(HttpStatusCode status, bool expected) { - var responseTask = TestSetup.GetApiResponse(status); + var responseTask = CreateApiResponse(status); var connection = Substitute.For(); connection.Put(Arg.Is(u => u.ToString() == "user/starred/yes/no"), Args.Object, Args.String) diff --git a/Octokit.Tests/Clients/TreesClientTests.cs b/Octokit.Tests/Clients/TreesClientTests.cs index b876eeab..345931bd 100644 --- a/Octokit.Tests/Clients/TreesClientTests.cs +++ b/Octokit.Tests/Clients/TreesClientTests.cs @@ -1,11 +1,12 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests { public class TreesClientTests @@ -207,11 +208,10 @@ namespace Octokit.Tests "}" + "]" + "}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - issueResponseJson, - new Dictionary(), - "application/json"); + issueResponseJson); + var jsonPipeline = new JsonHttpPipeline(); var response = jsonPipeline.DeserializeResponse(httpResponse); diff --git a/Octokit.Tests/Clients/WatchedClientTests.cs b/Octokit.Tests/Clients/WatchedClientTests.cs index 7bee44c2..facd9f97 100644 --- a/Octokit.Tests/Clients/WatchedClientTests.cs +++ b/Octokit.Tests/Clients/WatchedClientTests.cs @@ -1,11 +1,11 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; -using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { public class WatchedClientTests @@ -231,7 +231,8 @@ namespace Octokit.Tests.Clients var endpoint = new Uri("repos/fight/club/subscription", UriKind.Relative); var connection = Substitute.For(); - var response = new Response(HttpStatusCode.NotFound, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.NotFound); + connection.Get(endpoint).Returns>(x => { throw new NotFoundException(response); @@ -250,7 +251,8 @@ namespace Octokit.Tests.Clients var endpoint = new Uri("repositories/1/subscription", UriKind.Relative); var connection = Substitute.For(); - var response = new Response(HttpStatusCode.NotFound, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.NotFound); + connection.Get(endpoint).Returns>(x => { throw new NotFoundException(response); diff --git a/Octokit.Tests/Exceptions/AbuseExceptionTests.cs b/Octokit.Tests/Exceptions/AbuseExceptionTests.cs index 80285bc1..ab29184a 100644 --- a/Octokit.Tests/Exceptions/AbuseExceptionTests.cs +++ b/Octokit.Tests/Exceptions/AbuseExceptionTests.cs @@ -1,8 +1,9 @@ using System.Collections.Generic; using System.Net; -using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class AbuseExceptionTests @@ -17,11 +18,9 @@ namespace Octokit.Tests.Exceptions const string responseBody = "{\"message\":\"You have triggered an abuse detection mechanism. Please wait a few minutes before you try again.\"," + "\"documentation_url\":\"https://developer.github.com/v3/#abuse-rate-limits\"}"; - var response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, - responseBody, - new Dictionary(), - "application/json"); + responseBody); var abuseException = new AbuseException(response); @@ -31,7 +30,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void HasDefaultMessage() { - var response = new Response(HttpStatusCode.Forbidden, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden); var abuseException = new AbuseException(response); Assert.Equal("Request Forbidden - Abuse Detection", abuseException.Message); @@ -47,7 +46,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "Retry-After", "30" } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.Equal(30, abuseException.RetryAfterSeconds); @@ -58,7 +57,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "retry-after", "20" } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.Equal(20, abuseException.RetryAfterSeconds); @@ -69,7 +68,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "Retry-After", "0" } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.Equal(0, abuseException.RetryAfterSeconds); @@ -81,9 +80,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void NoRetryAfterHeader_RetryAfterSecondsIsSetToTheDefaultOfNull() { - var headerDictionary = new Dictionary(); - - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden); var abuseException = new AbuseException(response); Assert.False(abuseException.RetryAfterSeconds.HasValue); @@ -97,7 +94,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "Retry-After", emptyValueToTry } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.False(abuseException.RetryAfterSeconds.HasValue); @@ -108,7 +105,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "Retry-After", "ABC" } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.False(abuseException.RetryAfterSeconds.HasValue); @@ -119,7 +116,7 @@ namespace Octokit.Tests.Exceptions { var headerDictionary = new Dictionary { { "Retry-After", "-123" } }; - var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headerDictionary); var abuseException = new AbuseException(response); Assert.False(abuseException.RetryAfterSeconds.HasValue); diff --git a/Octokit.Tests/Exceptions/ApiExceptionTests.cs b/Octokit.Tests/Exceptions/ApiExceptionTests.cs index 639c3992..bad38f72 100644 --- a/Octokit.Tests/Exceptions/ApiExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiExceptionTests.cs @@ -1,16 +1,14 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; #if !NO_SERIALIZABLE using System.Runtime.Serialization.Formatters.Binary; +using System.IO; #endif -using System.Text; using NSubstitute; -using Octokit.Internal; using Xunit; -using Xunit.Extensions; + +using static Octokit.Internal.TestSetup; namespace Octokit.Tests.Exceptions { @@ -48,13 +46,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void CreatesGitHubErrorFromJsonResponse() { - var response = new Response( + var response = CreateResponse( HttpStatusCode.GatewayTimeout, @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - new Dictionary(), - "application/json" - ); + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}"); var exception = new ApiException(response); @@ -70,11 +65,9 @@ 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( + var response = CreateResponse( HttpStatusCode.GatewayTimeout, - responseContent, - new Dictionary(), - "application/json"); + responseContent); var exception = new ApiException(response); @@ -89,8 +82,8 @@ namespace Octokit.Tests.Exceptions response.Body.Returns("test"); var exception = new ApiException(); - 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")); + var anotherException = new ApiException(CreateResponse(HttpStatusCode.ServiceUnavailable, "message1")); + var thirdException = new ApiException(CreateResponse(HttpStatusCode.ServiceUnavailable, "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); @@ -102,12 +95,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void CanPopulateObjectFromSerializedData() { - IResponse response = new Response( + var response = CreateResponse( (HttpStatusCode)422, @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - new Dictionary(), - "application/json"); + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}"); var exception = new ApiException(response); @@ -131,12 +122,9 @@ namespace Octokit.Tests.Exceptions { const string responseBody = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}"; - var response = new Response( + var response = CreateResponse( HttpStatusCode.GatewayTimeout, - responseBody, - new Dictionary(), - "application/json" - ); + responseBody); var exception = new ApiException(response); var stringRepresentation = exception.ToString(); @@ -146,12 +134,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void DoesNotThrowIfBodyIsNotDefined() { - var response = new Response( - HttpStatusCode.GatewayTimeout, - null, - new Dictionary(), - "application/json" - ); + var response = CreateResponse(HttpStatusCode.GatewayTimeout); var exception = new ApiException(response); var stringRepresentation = exception.ToString(); @@ -161,12 +144,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void DoesNotPrintImageContent() { - var responceBody = new byte[0]; - var response = new Response( + var responseBody = new byte[0]; + var response = CreateResponse( HttpStatusCode.GatewayTimeout, - responceBody, - new Dictionary(), - "image/*" + responseBody ); var exception = new ApiException(response); @@ -177,13 +158,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void DoesNotPrintNonStringContent() { - var responceBody = new byte[0]; - var response = new Response( + var responseBody = new byte[0]; + var response = CreateResponse( HttpStatusCode.GatewayTimeout, - responceBody, - new Dictionary(), - "application/json" - ); + responseBody); var exception = new ApiException(response); var stringRepresentation = exception.ToString(); diff --git a/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs b/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs index 1348deeb..00575f12 100644 --- a/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs @@ -1,13 +1,13 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; +using System.Linq; using System.Net; #if !NO_SERIALIZABLE using System.Runtime.Serialization.Formatters.Binary; +using System.IO; #endif -using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class ApiValidationExceptionTests @@ -17,13 +17,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void CreatesGitHubErrorFromJsonResponse() { - var response = new Response( + var response = CreateResponse( (HttpStatusCode)422, @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - new Dictionary(), - "application/json" - ); + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}"); var exception = new ApiValidationException(response); @@ -34,7 +31,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void ProvidesDefaultMessage() { - var response = new Response((HttpStatusCode)422, null, new Dictionary(), "application/json"); + var response = CreateResponse((HttpStatusCode)422); var exception = new ApiValidationException(response); @@ -45,12 +42,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void CanPopulateObjectFromSerializedData() { - IResponse response = new Response( + var response = CreateResponse( (HttpStatusCode)422, @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - new Dictionary(), - "application/json"); + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}"); var exception = new ApiValidationException(response); diff --git a/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs b/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs index 909cbd9a..0555926b 100644 --- a/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ForbiddenExceptionTests.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using System.Net; -using Octokit.Internal; +using System.Net; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class ForbiddenExceptionTests @@ -12,13 +12,9 @@ namespace Octokit.Tests.Exceptions [Fact] public void IdentifiesMaxLoginAttemptsExceededReason() { - const string responseBody = "{\"message\":\"YOU SHALL NOT PASS!\"," + - "\"documentation_url\":\"http://developer.github.com/v3\"}"; - var response = new Response( - HttpStatusCode.Forbidden, - responseBody, - new Dictionary(), - "application/json"); + var responseBody = "{\"message\":\"YOU SHALL NOT PASS!\", \"documentation_url\":\"http://developer.github.com/v3\"}"; + var response = CreateResponse(HttpStatusCode.Forbidden, responseBody); + var forbiddenException = new ForbiddenException(response); Assert.Equal("YOU SHALL NOT PASS!", forbiddenException.ApiError.Message); @@ -27,7 +23,8 @@ namespace Octokit.Tests.Exceptions [Fact] public void HasDefaultMessage() { - var response = new Response(HttpStatusCode.Forbidden, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden); + var forbiddenException = new ForbiddenException(response); Assert.Equal("Request Forbidden", forbiddenException.Message); diff --git a/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs b/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs index c6190cea..07d0a570 100644 --- a/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs +++ b/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using System.Net; -using Octokit.Internal; +using System.Net; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class LegalRestrictionExceptionTests @@ -10,10 +10,10 @@ namespace Octokit.Tests.Exceptions [Fact] public void HasDefaultMessage() { - var response = new Response((HttpStatusCode)451, null, new Dictionary(), "application/json"); + var response = CreateResponse((HttpStatusCode)451); var legalRestrictionException = new LegalRestrictionException(response); Assert.Equal("Resource taken down due to a DMCA notice.", legalRestrictionException.Message); } } -} \ No newline at end of file +} diff --git a/Octokit.Tests/Exceptions/LoginAttemptsExceededExceptionTests.cs b/Octokit.Tests/Exceptions/LoginAttemptsExceededExceptionTests.cs index 7b59fcfd..9b0e43d8 100644 --- a/Octokit.Tests/Exceptions/LoginAttemptsExceededExceptionTests.cs +++ b/Octokit.Tests/Exceptions/LoginAttemptsExceededExceptionTests.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using Octokit.Internal; +using System.Net; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class LoginAttemptsExceededExceptionTests @@ -16,7 +12,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void SetsDefaultMessage() { - var response = new Response(HttpStatusCode.Forbidden, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden); var exception = new LoginAttemptsExceededException(response); diff --git a/Octokit.Tests/Exceptions/RateLimitExceededExceptionTests.cs b/Octokit.Tests/Exceptions/RateLimitExceededExceptionTests.cs index 82dc63a2..42da065a 100644 --- a/Octokit.Tests/Exceptions/RateLimitExceededExceptionTests.cs +++ b/Octokit.Tests/Exceptions/RateLimitExceededExceptionTests.cs @@ -1,14 +1,15 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Net; #if !NO_SERIALIZABLE +using System.IO; using System.Runtime.Serialization.Formatters.Binary; #endif -using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class RateLimitExceededExceptionTests @@ -24,7 +25,7 @@ namespace Octokit.Tests.Exceptions {"X-RateLimit-Remaining", "42"}, {"X-RateLimit-Reset", "1372700873"} }; - var response = new Response(HttpStatusCode.Forbidden, null, headers, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headers); var exception = new RateLimitExceededException(response); @@ -48,7 +49,7 @@ namespace Octokit.Tests.Exceptions {"X-RateLimit-Remaining", "XXXX"}, {"X-RateLimit-Reset", "XXXX"} }; - var response = new Response(HttpStatusCode.Forbidden, null, headers, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headers); var exception = new RateLimitExceededException(response); @@ -65,7 +66,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void HandlesMissingHeaderValues() { - var response = new Response(HttpStatusCode.Forbidden, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden); var exception = new RateLimitExceededException(response); Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode); @@ -87,7 +88,7 @@ namespace Octokit.Tests.Exceptions {"X-RateLimit-Remaining", "42"}, {"X-RateLimit-Reset", "1372700873"} }; - var response = new Response(HttpStatusCode.Forbidden, null, headers, "application/json"); + var response = CreateResponse(HttpStatusCode.Forbidden, headers); var exception = new RateLimitExceededException(response); diff --git a/Octokit.Tests/Exceptions/TwoFactorRequiredExceptionTests.cs b/Octokit.Tests/Exceptions/TwoFactorRequiredExceptionTests.cs index 8920c0c9..3d4dae90 100644 --- a/Octokit.Tests/Exceptions/TwoFactorRequiredExceptionTests.cs +++ b/Octokit.Tests/Exceptions/TwoFactorRequiredExceptionTests.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using System.Net; -using Octokit.Internal; +using System.Net; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Exceptions { public class TwoFactorRequiredExceptionTests @@ -12,7 +12,7 @@ namespace Octokit.Tests.Exceptions [Fact] public void SetsDefaultMessage() { - var response = new Response(HttpStatusCode.Unauthorized, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Unauthorized); var exception = new TwoFactorRequiredException(response, TwoFactorType.Sms); diff --git a/Octokit.Tests/Helpers/TaskHelper.cs b/Octokit.Tests/Helpers/TaskHelper.cs deleted file mode 100644 index 9c85c31a..00000000 --- a/Octokit.Tests/Helpers/TaskHelper.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Collections.Generic; -using System.Net; -using System.Threading.Tasks; - -namespace Octokit.Internal -{ - public static class TestSetup - { - public static Task> GetApiResponse(HttpStatusCode statusCode) - { - var response = new Response(statusCode, null, new Dictionary(), "application/json"); - return Task.FromResult>(new ApiResponse(response)); - - } - } -} diff --git a/Octokit.Tests/Helpers/TestSetup.cs b/Octokit.Tests/Helpers/TestSetup.cs new file mode 100644 index 00000000..ed4f2ed0 --- /dev/null +++ b/Octokit.Tests/Helpers/TestSetup.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; + +namespace Octokit.Internal +{ + public static class TestSetup + { + public static Task> CreateApiResponse(HttpStatusCode statusCode) + { + var response = CreateResponse(statusCode); + return Task.FromResult>(new ApiResponse(response)); + } + + public static IResponse CreateResponse(HttpStatusCode statusCode) + { + object body = null; + return CreateResponse(statusCode, body); + } + + public static IResponse CreateResponse(HttpStatusCode statusCode, IDictionary headers) + { + var response = new Response(statusCode, null, headers, "application/json"); + return response; + } + + public static IResponse CreateResponse(HttpStatusCode statusCode, object body) + { + var response = new Response(statusCode, body, new Dictionary(), "application/json"); + return response; + } + + public static IResponse CreateResponse(HttpStatusCode statusCode, object body, IDictionary headers) + { + return CreateResponse(statusCode, body, headers, "application/json"); + } + + public static IResponse CreateResponse(HttpStatusCode statusCode, object body, IDictionary headers, string contentType) + { + var response = new Response(statusCode, body, headers, contentType); + return response; + } + } +} diff --git a/Octokit.Tests/Http/ApiConnectionTests.cs b/Octokit.Tests/Http/ApiConnectionTests.cs index b74011c0..660ee1ed 100644 --- a/Octokit.Tests/Http/ApiConnectionTests.cs +++ b/Octokit.Tests/Http/ApiConnectionTests.cs @@ -1,13 +1,15 @@ using System; -using System.Net; using System.Collections.Generic; using System.IO; +using System.Net; using System.Threading; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Http { public class ApiConnectionTests @@ -18,7 +20,7 @@ namespace Octokit.Tests.Http public async Task MakesGetRequestForItem() { var getUri = new Uri("anything", UriKind.Relative); - IApiResponse response = new ApiResponse(new Response()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK)); var connection = Substitute.For(); connection.Get(Args.Uri, null, null).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -34,7 +36,7 @@ namespace Octokit.Tests.Http { var getUri = new Uri("anything", UriKind.Relative); const string accepts = "custom/accepts"; - IApiResponse response = new ApiResponse(new Response()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK)); var connection = Substitute.For(); connection.Get(Args.Uri, null, Args.String).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -61,7 +63,7 @@ namespace Octokit.Tests.Http public async Task MakesHtmlRequest() { var getUri = new Uri("anything", UriKind.Relative); - IApiResponse response = new ApiResponse(new Response(), ""); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), ""); var connection = Substitute.For(); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -87,7 +89,7 @@ namespace Octokit.Tests.Http { var getAllUri = new Uri("anything", UriKind.Relative); IApiResponse> response = new ApiResponse>( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new object(), new object() }); var connection = Substitute.For(); connection.Get>(Args.Uri, Args.EmptyDictionary, null).Returns(Task.FromResult(response)); @@ -124,7 +126,7 @@ namespace Octokit.Tests.Http { var patchUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response(), new object()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), new object()); var connection = Substitute.For(); connection.Patch(Args.Uri, Args.Object).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -141,7 +143,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()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK)); var connection = Substitute.For(); connection.Patch(Args.Uri, Args.Object, Args.String).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -184,7 +186,7 @@ namespace Octokit.Tests.Http { var postUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response(), new object()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), new object()); var connection = Substitute.For(); connection.Post(Args.Uri, Args.Object, null, null).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -199,7 +201,7 @@ namespace Octokit.Tests.Http public async Task MakesUploadRequest() { var uploadUrl = new Uri("anything", UriKind.Relative); - IApiResponse response = new ApiResponse(new Response(), "the response"); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), "the response"); var connection = Substitute.For(); connection.Post(Args.Uri, Arg.Any(), Args.String, Args.String) .Returns(Task.FromResult(response)); @@ -237,7 +239,7 @@ namespace Octokit.Tests.Http { var putUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK)); var connection = Substitute.For(); connection.Put(Args.Uri, Args.Object).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -253,7 +255,7 @@ namespace Octokit.Tests.Http { var putUri = new Uri("anything", UriKind.Relative); var sentData = new object(); - IApiResponse response = new ApiResponse(new Response()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK)); var connection = Substitute.For(); connection.Put(Args.Uri, Args.Object, "two-factor").Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -319,8 +321,7 @@ namespace Octokit.Tests.Http { var queuedOperationUrl = new Uri("anything", UriKind.Relative); - const HttpStatusCode statusCode = HttpStatusCode.PartialContent; - IApiResponse response = new ApiResponse(new Response(statusCode, null, new Dictionary(), "application/json"), new object()); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.PartialContent), new object()); var connection = Substitute.For(); connection.GetResponse(queuedOperationUrl, Args.CancellationToken).Returns(Task.FromResult(response)); var apiConnection = new ApiConnection(connection); @@ -334,8 +335,7 @@ namespace Octokit.Tests.Http var queuedOperationUrl = new Uri("anything", UriKind.Relative); var result = new[] { new object() }; - const HttpStatusCode statusCode = HttpStatusCode.OK; - var httpResponse = new Response(statusCode, null, new Dictionary(), "application/json"); + var httpResponse = CreateResponse(HttpStatusCode.OK); IApiResponse> response = new ApiResponse>(httpResponse, result); var connection = Substitute.For(); connection.GetResponse>(queuedOperationUrl, Args.CancellationToken) @@ -352,8 +352,7 @@ namespace Octokit.Tests.Http var queuedOperationUrl = new Uri("anything", UriKind.Relative); var result = new[] { new object() }; - const HttpStatusCode statusCode = HttpStatusCode.NoContent; - var httpResponse = new Response(statusCode, null, new Dictionary(), "application/json"); + var httpResponse = CreateResponse(HttpStatusCode.NoContent); IApiResponse> response = new ApiResponse>( httpResponse, result); var connection = Substitute.For(); @@ -371,10 +370,8 @@ namespace Octokit.Tests.Http var queuedOperationUrl = new Uri("anything", UriKind.Relative); var result = new[] { new object() }; - IApiResponse> firstResponse = new ApiResponse>( - new Response(HttpStatusCode.Accepted, null, new Dictionary(), "application/json"), result); - IApiResponse> completedResponse = new ApiResponse>( - new Response(HttpStatusCode.OK, null, new Dictionary(), "application/json"), result); + IApiResponse> firstResponse = new ApiResponse>(CreateResponse(HttpStatusCode.Accepted), result); + IApiResponse> completedResponse = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); var connection = Substitute.For(); connection.GetResponse>(queuedOperationUrl, Args.CancellationToken) .Returns(x => Task.FromResult(firstResponse), @@ -394,8 +391,7 @@ namespace Octokit.Tests.Http var queuedOperationUrl = new Uri("anything", UriKind.Relative); var result = new[] { new object() }; - IApiResponse> accepted = new ApiResponse>( - new Response(HttpStatusCode.Accepted, null, new Dictionary(), "application/json"), result); + IApiResponse> accepted = new ApiResponse>(CreateResponse(HttpStatusCode.Accepted), result); var connection = Substitute.For(); connection.GetResponse>(queuedOperationUrl, Args.CancellationToken) .Returns(x => Task.FromResult(accepted)); diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 3bca0373..c38aee07 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -10,6 +10,8 @@ using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Http { public class ConnectionTests @@ -23,7 +25,7 @@ namespace Octokit.Tests.Http public async Task SendsProperlyFormattedRequest() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -45,7 +47,7 @@ namespace Octokit.Tests.Http public async Task CanMakeMultipleRequestsWithSameConnection() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -71,7 +73,7 @@ namespace Octokit.Tests.Http { { "X-Accepted-OAuth-Scopes", "user" } }; - IResponse response = new Response(headers); + var response = CreateResponse(HttpStatusCode.OK, headers); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), @@ -89,7 +91,7 @@ namespace Octokit.Tests.Http public async Task ThrowsAuthorizationExceptionExceptionForUnauthorizedResponse() { var httpClient = Substitute.For(); - IResponse response = new Response(HttpStatusCode.Unauthorized, null, new Dictionary(), "application/json"); + var response = CreateResponse(HttpStatusCode.Unauthorized); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -113,7 +115,7 @@ namespace Octokit.Tests.Http { var headers = new Dictionary { { headerKey, otpHeaderValue } }; var httpClient = Substitute.For(); - IResponse response = new Response(HttpStatusCode.Unauthorized, null, headers, "application/json"); + var response = CreateResponse(HttpStatusCode.Unauthorized, headers); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -139,7 +141,7 @@ namespace Octokit.Tests.Http TwoFactorType expectedFactorType) { var headers = new Dictionary { { headerKey, otpHeaderValue } }; - IResponse response = new Response(HttpStatusCode.Unauthorized, null, headers, "application/json"); + var response = CreateResponse(HttpStatusCode.Unauthorized, headers); var httpClient = Substitute.For(); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), @@ -158,12 +160,10 @@ namespace Octokit.Tests.Http public async Task ThrowsApiValidationExceptionFor422Response() { var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( (HttpStatusCode)422, @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + - @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}", - new Dictionary(), - "application/json" + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}" ); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), @@ -183,12 +183,11 @@ namespace Octokit.Tests.Http public async Task ThrowsRateLimitExceededExceptionForForbidderResponse() { var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"API rate limit exceeded. " + - "See http://developer.github.com/v3/#rate-limiting for details.\"}", - new Dictionary(), - "application/json"); + "See http://developer.github.com/v3/#rate-limiting for details.\"}"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -207,12 +206,11 @@ namespace Octokit.Tests.Http public async Task ThrowsLoginAttemptsExceededExceptionForForbiddenResponse() { var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"Maximum number of login attempts exceeded\"," + - "\"documentation_url\":\"http://developer.github.com/v3\"}", - new Dictionary(), - "application/json"); + "\"documentation_url\":\"http://developer.github.com/v3\"}"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -231,11 +229,9 @@ namespace Octokit.Tests.Http public async Task ThrowsNotFoundExceptionForFileNotFoundResponse() { var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.NotFound, - "GONE BYE BYE!", - new Dictionary(), - "application/json"); + "GONE BYE BYE!"); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), @@ -254,11 +250,10 @@ namespace Octokit.Tests.Http public async Task ThrowsForbiddenExceptionForUnknownForbiddenResponse() { var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, - "YOU SHALL NOT PASS!", - new Dictionary(), - "application/json"); + "YOU SHALL NOT PASS!"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -278,12 +273,11 @@ namespace Octokit.Tests.Http var messageText = "blahblahblah this does not matter because we are testing the URL"; var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"" + messageText + "\"," + - "\"documentation_url\":\"https://developer.github.com/v3/#abuse-rate-limits\"}", - new Dictionary(), - "application/json"); + "\"documentation_url\":\"https://developer.github.com/v3/#abuse-rate-limits\"}"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -301,12 +295,11 @@ namespace Octokit.Tests.Http var messageText = "You have triggered an abuse detection mechanism. Please wait a few minutes before you try again."; var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"" + messageText + "\"," + - "\"documentation_url\":\"https://ThisURLDoesNotMatter.com\"}", - new Dictionary(), - "application/json"); + "\"documentation_url\":\"https://ThisURLDoesNotMatter.com\"}"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -330,12 +323,12 @@ namespace Octokit.Tests.Http { "Retry-After", "45" } }; - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"" + messageText + "\"," + "\"documentation_url\":\"https://ThisURLDoesNotMatter.com\"}", - headerDictionary, - "application/json"); + headerDictionary); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -355,12 +348,11 @@ namespace Octokit.Tests.Http string messageText = string.Empty; var httpClient = Substitute.For(); - IResponse response = new Response( + var response = CreateResponse( HttpStatusCode.Forbidden, "{\"message\":\"" + messageText + "\"," + - "\"documentation_url\":\"https://developer.github.com/v3/#abuse-rate-limits\"}", - new Dictionary(), - "application/json"); + "\"documentation_url\":\"https://developer.github.com/v3/#abuse-rate-limits\"}"); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -381,7 +373,7 @@ namespace Octokit.Tests.Http public async Task SendsProperlyFormattedRequestWithProperAcceptHeader() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -412,7 +404,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); serializer.Serialize(body).Returns(expectedData); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); var httpClient = Substitute.For(); httpClient.Send(Args.Request, Args.CancellationToken) .Returns(Task.FromResult(response)); @@ -439,7 +431,8 @@ namespace Octokit.Tests.Http public async Task RunsConfiguredAppWithAcceptsOverride() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -462,7 +455,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); serializer.Serialize(body).Returns(expectedBody); @@ -492,7 +485,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); serializer.Serialize(body).Returns(expectedBody); @@ -522,7 +515,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); serializer.Serialize(body).Returns(expectedBody); @@ -553,7 +546,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); serializer.Serialize(body).Returns(expectedBody); @@ -586,7 +579,7 @@ namespace Octokit.Tests.Http var serializer = Substitute.For(); var data = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); serializer.Serialize(body).Returns(data); @@ -613,7 +606,8 @@ namespace Octokit.Tests.Http public async Task SendsProperlyFormattedPostRequestWithCorrectHeaders() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -641,7 +635,8 @@ namespace Octokit.Tests.Http public async Task SetsAcceptsHeader() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -668,7 +663,7 @@ namespace Octokit.Tests.Http public async Task SendsProperlyFormattedDeleteRequest() { var httpClient = Substitute.For(); - IResponse response = new Response(); + var response = CreateResponse(HttpStatusCode.OK); httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, @@ -815,10 +810,9 @@ namespace Octokit.Tests.Http var httpClient = Substitute.For(); // We really only care about the ApiInfo property... - var expectedResponse = new Response(HttpStatusCode.OK, null, new Dictionary(), "application/json") - { - ApiInfo = apiInfo - }; + var expectedResponse = Substitute.For(); + expectedResponse.StatusCode.Returns(HttpStatusCode.OK); + expectedResponse.ApiInfo.Returns(apiInfo); httpClient.Send(Arg.Any(), Arg.Any()) .Returns(Task.FromResult(expectedResponse)); diff --git a/Octokit.Tests/Http/JsonHttpPipelineTests.cs b/Octokit.Tests/Http/JsonHttpPipelineTests.cs index d533f041..02f62a9c 100644 --- a/Octokit.Tests/Http/JsonHttpPipelineTests.cs +++ b/Octokit.Tests/Http/JsonHttpPipelineTests.cs @@ -6,6 +6,8 @@ using System.Net; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Http { public class JsonHttpPipelineTests @@ -107,11 +109,10 @@ namespace Octokit.Tests.Http public void DeserializesResponse() { const string data = "works"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - SimpleJson.SerializeObject(data), - new Dictionary(), - "application/json"); + SimpleJson.SerializeObject(data)); + var jsonPipeline = new JsonHttpPipeline(); var response = jsonPipeline.DeserializeResponse(httpResponse); @@ -124,7 +125,7 @@ namespace Octokit.Tests.Http public void IgnoresResponsesNotIdentifiedAsJsonWhenNotDeserializingToString() { const string data = "works"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, SimpleJson.SerializeObject(data), new Dictionary(), @@ -141,11 +142,9 @@ namespace Octokit.Tests.Http { const string data = "{\"name\":\"Haack\"}"; var jsonPipeline = new JsonHttpPipeline(); - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - data, - new Dictionary(), - "application/json"); + data); var response = jsonPipeline.DeserializeResponse>(httpResponse); @@ -175,11 +174,10 @@ namespace Octokit.Tests.Http ""sha"": ""object-sha"", ""url"": ""object-url"" }}"; - var httpResponse = new Response( + var httpResponse = CreateResponse( HttpStatusCode.OK, - data, - new Dictionary(), - "application/json"); + data); + var jsonPipeline = new JsonHttpPipeline(); var response = jsonPipeline.DeserializeResponse(httpResponse); diff --git a/Octokit.Tests/Http/RateLimitTests.cs b/Octokit.Tests/Http/RateLimitTests.cs index 2012ec29..b0c3eaf0 100644 --- a/Octokit.Tests/Http/RateLimitTests.cs +++ b/Octokit.Tests/Http/RateLimitTests.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; #if !NO_SERIALIZABLE +using System.IO; using System.Runtime.Serialization.Formatters.Binary; #endif using Xunit; @@ -104,7 +104,8 @@ namespace Octokit.Tests.Http [Fact] public void EnsuresHeadersNotNull() { - Assert.Throws(() => new RateLimit(null)); + IDictionary dictionary = null; + Assert.Throws(() => new RateLimit(dictionary)); } } @@ -127,4 +128,4 @@ namespace Octokit.Tests.Http } } } -} \ No newline at end of file +} diff --git a/Octokit.Tests/Http/ResponseTests.cs b/Octokit.Tests/Http/ResponseTests.cs index a8861306..ec057a14 100644 --- a/Octokit.Tests/Http/ResponseTests.cs +++ b/Octokit.Tests/Http/ResponseTests.cs @@ -1,6 +1,8 @@ -using Octokit.Internal; +using System.Net; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Http { public class ResponseTests @@ -10,7 +12,7 @@ namespace Octokit.Tests.Http [Fact] public void InitializesAllRequiredProperties() { - var r = new Response(); + var r = CreateResponse(HttpStatusCode.OK); Assert.NotNull(r.Headers); } diff --git a/Octokit.Tests/Models/ReadOnlyPagedCollectionTests.cs b/Octokit.Tests/Models/ReadOnlyPagedCollectionTests.cs index 87c3cc55..8928625b 100644 --- a/Octokit.Tests/Models/ReadOnlyPagedCollectionTests.cs +++ b/Octokit.Tests/Models/ReadOnlyPagedCollectionTests.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Models { public class ReadOnlyPagedCollectionTests @@ -15,7 +18,7 @@ namespace Octokit.Tests.Models public async Task ReturnsTheNextPage() { var nextPageUrl = new Uri("https://example.com/page/2"); - var listResponse = new ApiResponse>(new Response(), new List { new object(), new object() }); + var listResponse = new ApiResponse>(CreateResponse(HttpStatusCode.OK), new List { new object(), new object() }); var nextPageResponse = Task.FromResult>>(listResponse); var links = new Dictionary { { "next", nextPageUrl } }; @@ -39,7 +42,7 @@ namespace Octokit.Tests.Models public async Task WhenNoInformationSetReturnsNull() { var nextPageUrl = new Uri("https://example.com/page/2"); - var listResponse = new ApiResponse>(new Response(), new List { new object(), new object() }); + var listResponse = new ApiResponse>(CreateResponse(HttpStatusCode.OK), new List { new object(), new object() }); var nextPageResponse = Task.FromResult>>(listResponse); var links = new Dictionary(); @@ -65,7 +68,7 @@ namespace Octokit.Tests.Models public async Task WhenInlineFuncKillsPaginationReturnNull() { var nextPageUrl = new Uri("https://example.com/page/2"); - var listResponse = new ApiResponse>(new Response(), new List { new object(), new object() }); + var listResponse = new ApiResponse>(CreateResponse(HttpStatusCode.OK), new List { new object(), new object() }); var nextPageResponse = Task.FromResult>>(listResponse); var links = new Dictionary { { "next", nextPageUrl } }; diff --git a/Octokit.Tests/Reactive/ObservableIssueTimelineClientTests.cs b/Octokit.Tests/Reactive/ObservableIssueTimelineClientTests.cs index 836d9638..d1cd611b 100644 --- a/Octokit.Tests/Reactive/ObservableIssueTimelineClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableIssueTimelineClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -7,6 +8,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservableIssueTimelineClientTests @@ -33,10 +36,8 @@ namespace Octokit.Tests.Reactive var client = new ObservableIssueTimelineClient(gitHubClient); IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + CreateResponse(HttpStatusCode.OK), + result); gitHubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, "application/vnd.github.mockingbird-preview+json") .Returns(Task.FromResult(response)); @@ -58,11 +59,7 @@ namespace Octokit.Tests.Reactive var gitHubClient = new GitHubClient(connection); var client = new ObservableIssueTimelineClient(gitHubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 1), "application/vnd.github.mockingbird-preview+json") .Returns(Task.FromResult(response)); @@ -83,11 +80,7 @@ namespace Octokit.Tests.Reactive var githubClient = new GitHubClient(connection); var client = new ObservableIssueTimelineClient(githubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); githubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, "application/vnd.github.mockingbird-preview+json") .Returns(Task.FromResult(response)); @@ -108,11 +101,7 @@ namespace Octokit.Tests.Reactive var githubClient = new GitHubClient(connection); var client = new ObservableIssueTimelineClient(githubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); githubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 1), "application/vnd.github.mockingbird-preview+json") .Returns(Task.FromResult(response)); diff --git a/Octokit.Tests/Reactive/ObservableIssuesClientTests.cs b/Octokit.Tests/Reactive/ObservableIssuesClientTests.cs index af918574..10e1d6da 100644 --- a/Octokit.Tests/Reactive/ObservableIssuesClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableIssuesClientTests.cs @@ -1,13 +1,16 @@ -using NSubstitute; +using System; +using System.Collections.Generic; +using System.Net; +using System.Reactive.Linq; +using System.Threading.Tasks; +using NSubstitute; using Octokit; using Octokit.Internal; using Octokit.Reactive; -using System; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Threading.Tasks; using Xunit; +using static Octokit.Internal.TestSetup; + public class ObservableIssuesClientTests { public class TheGetMethod @@ -288,7 +291,7 @@ public class ObservableIssuesClientTests ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { CreateIssue(7) @@ -364,7 +367,7 @@ public class ObservableIssuesClientTests ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { CreateIssue(7) @@ -451,7 +454,7 @@ public class ObservableIssuesClientTests ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { CreateIssue(7) @@ -524,7 +527,7 @@ public class ObservableIssuesClientTests ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { CreateIssue(7) diff --git a/Octokit.Tests/Reactive/ObservableIssuesEventsClientTests.cs b/Octokit.Tests/Reactive/ObservableIssuesEventsClientTests.cs index f2f7d341..5674c454 100644 --- a/Octokit.Tests/Reactive/ObservableIssuesEventsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableIssuesEventsClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -7,6 +8,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservableIssuesEventsClientTests @@ -32,11 +35,7 @@ namespace Octokit.Tests.Reactive var gitHubClient = new GitHubClient(connection); var client = new ObservableIssuesEventsClient(gitHubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, null) .Returns(Task.FromResult(response)); @@ -55,11 +54,7 @@ namespace Octokit.Tests.Reactive var gitHubClient = new GitHubClient(connection); var client = new ObservableIssuesEventsClient(gitHubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, null) .Returns(Task.FromResult(response)); @@ -85,11 +80,7 @@ namespace Octokit.Tests.Reactive PageSize = 1 }; - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 2), null) .Returns(Task.FromResult(response)); @@ -116,10 +107,7 @@ namespace Octokit.Tests.Reactive }; IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()), - }, result); + CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 2), null) .Returns(Task.FromResult(response)); @@ -160,11 +148,7 @@ namespace Octokit.Tests.Reactive var gitHubClient = new GitHubClient(connection); var client = new ObservableIssuesEventsClient(gitHubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag1", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, null) .Returns(Task.FromResult(response)); @@ -183,11 +167,7 @@ namespace Octokit.Tests.Reactive var gitHubClient = new GitHubClient(connection); var client = new ObservableIssuesEventsClient(gitHubClient); - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag1", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Args.EmptyDictionary, null) .Returns(Task.FromResult(response)); @@ -213,11 +193,7 @@ namespace Octokit.Tests.Reactive PageSize = 1 }; - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag1", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 2), null) .Returns(Task.FromResult(response)); @@ -243,11 +219,7 @@ namespace Octokit.Tests.Reactive PageSize = 1 }; - IApiResponse> response = new ApiResponse>( - new Response - { - ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag1", new RateLimit()), - }, result); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); gitHubClient.Connection.Get>(Args.Uri, Arg.Is>(d => d.Count == 2), null) .Returns(Task.FromResult(response)); diff --git a/Octokit.Tests/Reactive/ObservableMilestonesClientTests.cs b/Octokit.Tests/Reactive/ObservableMilestonesClientTests.cs index ea9de926..e7c70f19 100644 --- a/Octokit.Tests/Reactive/ObservableMilestonesClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableMilestonesClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; using System.Threading.Tasks; @@ -8,6 +9,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservableMilestonesClientTests @@ -240,7 +243,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new Milestone(7) @@ -293,7 +296,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit(new Dictionary())) }, + CreateResponse(HttpStatusCode.OK), new List { new Milestone(7) diff --git a/Octokit.Tests/Reactive/ObservablePullRequestReviewCommentsClientTests.cs b/Octokit.Tests/Reactive/ObservablePullRequestReviewCommentsClientTests.cs index 9e4e2b80..4ae70bc8 100644 --- a/Octokit.Tests/Reactive/ObservablePullRequestReviewCommentsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservablePullRequestReviewCommentsClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -7,6 +8,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservablePullRequestReviewCommentsClientTests @@ -117,7 +120,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7) @@ -172,7 +175,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7) @@ -335,7 +338,7 @@ namespace Octokit.Tests.Reactive }); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7), @@ -419,7 +422,7 @@ namespace Octokit.Tests.Reactive }); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7), @@ -560,7 +563,7 @@ namespace Octokit.Tests.Reactive }); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7), @@ -632,7 +635,7 @@ namespace Octokit.Tests.Reactive }); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7), diff --git a/Octokit.Tests/Reactive/ObservablePullRequestReviewsClientTests.cs b/Octokit.Tests/Reactive/ObservablePullRequestReviewsClientTests.cs index 0ec6a946..89bee2f9 100644 --- a/Octokit.Tests/Reactive/ObservablePullRequestReviewsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservablePullRequestReviewsClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -7,6 +8,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservablePullRequestReviewsClientTests @@ -117,7 +120,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReview(7) @@ -172,7 +175,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReview(7) @@ -512,7 +515,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7) @@ -567,7 +570,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequestReviewComment(7) diff --git a/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs b/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs index 2366e4bd..d08c94a3 100644 --- a/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs @@ -1,14 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Reactive.Linq; -using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservablePullRequestsClientTests @@ -202,7 +204,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequest(7) @@ -255,7 +257,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequest(7) @@ -308,7 +310,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequest(7) @@ -377,7 +379,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new PullRequest(7) @@ -635,7 +637,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); IApiResponse> response = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { commit } ); connection.Get>(Args.Uri, null, null) @@ -659,7 +661,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); IApiResponse> response = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { commit } ); connection.Get>(Args.Uri, null, null) @@ -699,7 +701,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); IApiResponse> response = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { file } ); connection.Get>(Args.Uri, null, null) @@ -723,7 +725,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); IApiResponse> response = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { file } ); connection.Get>(Args.Uri, null, null) diff --git a/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs index da93eb32..ca4ffb13 100644 --- a/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -7,6 +8,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservableRepositoriesClientTests @@ -153,7 +156,7 @@ namespace Octokit.Tests.Reactive public async Task IsALukeWarmObservable() { var repository = new Repository(); - var response = Task.FromResult>(new ApiResponse(new Response(), repository)); + var response = Task.FromResult>(new ApiResponse(CreateResponse(HttpStatusCode.OK), repository)); var connection = Substitute.For(); connection.Get(Args.Uri, null, Args.AnyAcceptHeaders).Returns(response); var gitHubClient = new GitHubClient(connection); @@ -178,7 +181,7 @@ namespace Octokit.Tests.Reactive public async Task IsALukeWarmObservableWithRepositoryId() { var repository = new Repository(); - var response = Task.FromResult>(new ApiResponse(new Response(), repository)); + var response = Task.FromResult>(new ApiResponse(CreateResponse(HttpStatusCode.OK), repository)); var connection = Substitute.For(); connection.Get(Args.Uri, null, Args.AnyAcceptHeaders).Returns(response); var gitHubClient = new GitHubClient(connection); @@ -238,7 +241,7 @@ namespace Octokit.Tests.Reactive new Repository(6) }); var lastPageResponse = new ApiResponse>( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new Repository(7) @@ -301,7 +304,7 @@ namespace Octokit.Tests.Reactive ); var lastPageResponse = new ApiResponse> ( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new Repository(8) @@ -358,7 +361,7 @@ namespace Octokit.Tests.Reactive }); IApiResponse> lastPageResponse = new ApiResponse>( - new Response(), + CreateResponse(HttpStatusCode.OK), new List { new Repository(370) diff --git a/Octokit.Tests/Reactive/ObservableRepositoryContentsClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoryContentsClientTests.cs index 565034b4..adf1186d 100644 --- a/Octokit.Tests/Reactive/ObservableRepositoryContentsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableRepositoryContentsClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; @@ -8,6 +9,8 @@ using Octokit.Internal; using Octokit.Reactive; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Reactive { public class ObservableRepositoryContentsClientTests @@ -43,7 +46,7 @@ namespace Octokit.Tests.Reactive gitHubClient.Repository.Content.GetReadme("fake", "repo").Returns(Task.FromResult(readmeFake)); - IApiResponse apiResponse = new ApiResponse(new Response(), "README"); + IApiResponse apiResponse = new ApiResponse(CreateResponse(HttpStatusCode.OK), "README"); gitHubClient.Connection.GetHtml(Args.Uri, null) .Returns(Task.FromResult(apiResponse)); @@ -79,7 +82,7 @@ namespace Octokit.Tests.Reactive gitHubClient.Repository.Content.GetReadme(1).Returns(Task.FromResult(readmeFake)); - IApiResponse apiResponse = new ApiResponse(new Response(), "README"); + IApiResponse apiResponse = new ApiResponse(CreateResponse(HttpStatusCode.OK), "README"); gitHubClient.Connection.GetHtml(Args.Uri, null) .Returns(Task.FromResult(apiResponse)); @@ -118,7 +121,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse apiResponse = new ApiResponse(new Response(), "README"); + IApiResponse apiResponse = new ApiResponse(CreateResponse(HttpStatusCode.OK), "README"); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(apiResponse)); @@ -134,7 +137,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse apiResponse = new ApiResponse(new Response(), "README"); + IApiResponse apiResponse = new ApiResponse(CreateResponse(HttpStatusCode.OK), "README"); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(apiResponse)); @@ -168,11 +171,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -191,11 +190,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -214,11 +209,8 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); + connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -236,11 +228,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -284,11 +272,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse response = new ApiResponse - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), result); connection.GetRaw(Args.Uri, default).Returns(response); var rawContent = await contentsClient.GetRawContent("fake", "repo", "path/to/file.txt"); @@ -323,11 +307,8 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -345,11 +326,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -367,11 +344,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -389,11 +362,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse> response = new ApiResponse> - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse> response = new ApiResponse>(CreateResponse(HttpStatusCode.OK), result); connection.Get>(Args.Uri, null, null) .Returns(Task.FromResult(response)); @@ -445,11 +414,7 @@ namespace Octokit.Tests.Reactive var connection = Substitute.For(); var gitHubClient = new GitHubClient(connection); var contentsClient = new ObservableRepositoryContentsClient(gitHubClient); - IApiResponse response = new ApiResponse - ( - new Response { ApiInfo = new ApiInfo(new Dictionary(), new List(), new List(), "etag", new RateLimit()) }, - result - ); + IApiResponse response = new ApiResponse(CreateResponse(HttpStatusCode.OK), result); connection.GetRaw(Args.Uri, default).Returns(response); var rawContent = await contentsClient.GetRawContentByRef("fake", "repo", "path/to/file.txt", "reference"); @@ -977,4 +942,4 @@ namespace Octokit.Tests.Reactive } } } -} \ No newline at end of file +} diff --git a/Octokit/Http/Response.cs b/Octokit/Http/Response.cs index 41ff9d53..d023a34a 100644 --- a/Octokit/Http/Response.cs +++ b/Octokit/Http/Response.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Net; @@ -9,10 +10,12 @@ namespace Octokit.Internal /// internal class Response : IResponse { + [Obsolete("Use the constuctor with maximum parameters to avoid shortcuts")] public Response() : this(new Dictionary()) { } + [Obsolete("Use the constuctor with maximum parameters to avoid shortcuts")] public Response(IDictionary headers) { Ensure.ArgumentNotNull(headers, nameof(headers)); @@ -53,4 +56,4 @@ namespace Octokit.Internal /// public string ContentType { get; private set; } } -} \ No newline at end of file +}