rewrite the setup of responses to use a standard helper function (#2177)

This commit is contained in:
Brendan Forster
2020-04-14 11:30:15 -03:00
committed by GitHub
parent 69d118230d
commit 287861e4a7
41 changed files with 390 additions and 448 deletions
+12 -15
View File
@@ -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<string, string>(),
"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<string, string>(), "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<string, string> { { "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<string, string> { { "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<string, string> { { "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<string, string>();
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<string, string> { { "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<string, string> { { "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<string, string> { { "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);
+20 -42
View File
@@ -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<string, string>(),
"application/json"
);
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}");
var exception = new ApiException(response);
@@ -70,11 +65,9 @@ namespace Octokit.Tests.Exceptions
[InlineData("<html><body><h1>502 Bad Gateway</h1>The server returned an invalid or incomplete response.</body></html>")]
public void CreatesGitHubErrorIfResponseMessageIsNotValidJson(string responseContent)
{
var response = new Response(
var response = CreateResponse(
HttpStatusCode.GatewayTimeout,
responseContent,
new Dictionary<string, string>(),
"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<string, string>(), "application/json"));
var thirdException = new ApiException(new Response(HttpStatusCode.ServiceUnavailable, "message2", new Dictionary<string, string>(), "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<string, string>(),
"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<string, string>(),
"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<string, string>(),
"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<string, string>(),
"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<string, string>(),
"application/json"
);
responseBody);
var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
@@ -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<string, string>(),
"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<string, string>(), "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<string, string>(),
"application/json");
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}");
var exception = new ApiValidationException(response);
@@ -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<string, string>(),
"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<string, string>(), "application/json");
var response = CreateResponse(HttpStatusCode.Forbidden);
var forbiddenException = new ForbiddenException(response);
Assert.Equal("Request Forbidden", forbiddenException.Message);
@@ -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<string, string>(), "application/json");
var response = CreateResponse((HttpStatusCode)451);
var legalRestrictionException = new LegalRestrictionException(response);
Assert.Equal("Resource taken down due to a DMCA notice.", legalRestrictionException.Message);
}
}
}
}
@@ -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<string, string>(), "application/json");
var response = CreateResponse(HttpStatusCode.Forbidden);
var exception = new LoginAttemptsExceededException(response);
@@ -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<string, string>(), "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);
@@ -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<string, string>(), "application/json");
var response = CreateResponse(HttpStatusCode.Unauthorized);
var exception = new TwoFactorRequiredException(response, TwoFactorType.Sms);