Example #1
0
        public void GetRemainingRequestCount_NullHttpResponseHeaders()
        {
            //Arrange
            HttpResponseHeaders?nullHttpResponseHeaders = null;

            //Act

            //Assert
#pragma warning disable CS8604 // Possible null reference argument.
            Assert.Throws <GitHubApiStatusException>(() => GitHubApiStatusService.GetRemainingRequestCount(nullHttpResponseHeaders));
#pragma warning restore CS8604 // Possible null reference argument.
        }
Example #2
0
        public void HasReachedMaximumApiCallLimitTests_NullHttpResponseHeaders()
        {
            //Arrange
            HttpResponseHeaders?nullHttpResponseHeaders = null;

            //Act

            //Assert
#pragma warning disable CS8604 // Possible null reference argument.
            Assert.Throws <GitHubApiStatusException>(() => GitHubApiStatusService.HasReachedMaximimApiCallLimit(nullHttpResponseHeaders));
#pragma warning restore CS8604 // Possible null reference argument.
        }
Example #3
0
        public void NullNameTest()
        {
            //Arrange
            var gitHubApiStatusService = new GitHubApiStatusService();

            //Act

            //Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
            Assert.Throws <ArgumentException>(() => gitHubApiStatusService.AddProductHeaderValue(new ProductHeaderValue(null)));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
            Assert.IsFalse(gitHubApiStatusService.IsProductHeaderValueValid);
        }
Example #4
0
        public void NullAuthenticationHeaderValue()
        {
            //Arrange
            var gitHubApiStatusService = new GitHubApiStatusService();

            //Act

            //Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
            Assert.Throws <GitHubApiStatusException>(() => gitHubApiStatusService.SetAuthenticationHeaderValue(null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
            Assert.IsFalse(gitHubApiStatusService.IsAuthenticationHeaderValueSet);
        }
Example #5
0
        public void GetRateLimit_ValidHttpResponseHeaders()
        {
            //Act
            const int rateLimit_Expected = 5000;
            int       rateLimit_Actual;

            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit_Expected, DateTimeOffset.UtcNow, rateLimit_Expected - 5);

            //Act
            rateLimit_Actual = GitHubApiStatusService.GetRateLimit(validHttpResponseHeaders);

            //Assert
            Assert.AreEqual(rateLimit_Expected, rateLimit_Actual);
        }
Example #6
0
        public void GetRemainingRequestCount_ValidHttpResponseHeaders()
        {
            //Act
            const int rateLimit = 5000;
            const int remainingRequestCount_Expected = 100;

            int remainingRequestCount_Actual;

            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit, DateTimeOffset.UtcNow, remainingRequestCount_Expected);

            //Act
            remainingRequestCount_Actual = GitHubApiStatusService.GetRemainingRequestCount(validHttpResponseHeaders);

            //Assert
            Assert.AreEqual(remainingRequestCount_Expected, remainingRequestCount_Actual);
        }
Example #7
0
        public void HasReachedMaximumApiCallLimitTests_ValidHttpResponseHeaders_False()
        {
            //Act
            const int  rateLimit          = 5000;
            const int  rateLimitRemaining = 10;
            const bool hasReachedMaximumApiCallLimit_Expected = false;

            bool hasReachedMaximumApiCallLimit_Actual;

            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit, DateTimeOffset.UtcNow, rateLimitRemaining);

            //Act
            hasReachedMaximumApiCallLimit_Actual = GitHubApiStatusService.HasReachedMaximimApiCallLimit(validHttpResponseHeaders);

            //Assert
            Assert.AreEqual(hasReachedMaximumApiCallLimit_Expected, hasReachedMaximumApiCallLimit_Actual);
        }
Example #8
0
        public void GetApiRateLimits_InvalidBearerToken()
        {
            //Arrange
            var cancellationTokenSource = new CancellationTokenSource();

            GitHubApiStatusService.SetAuthenticationHeaderValue(new AuthenticationHeaderValue(GitHubConstants.AuthScheme, "abc 123"));

            //Act

            //Assert
            var httpRequestException = Assert.ThrowsAsync <HttpRequestException>(() => GitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token));

#if NET5_0
            Assert.AreEqual(HttpStatusCode.Unauthorized, httpRequestException?.StatusCode);
#else
            Assert.IsTrue(httpRequestException?.Message.Contains("Unauthorized"));
#endif
        }
        public void IsResponseFromAuthenticatedRequest_ValidHttpResponseHeaders_False()
        {
            //Act
            bool isUserAuthenticated_Actual;

            const int  rateLimit                    = 5000;
            const int  rateLimitRemaining           = 10;
            const bool isUserAuthenticated_Expected = false;


            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit, DateTimeOffset.UtcNow, rateLimitRemaining, isAuthenticated: isUserAuthenticated_Expected);

            //Act
            isUserAuthenticated_Actual = GitHubApiStatusService.IsResponseFromAuthenticatedRequest(validHttpResponseHeaders);

            //Assert
            Assert.AreEqual(isUserAuthenticated_Expected, isUserAuthenticated_Actual);
        }
Example #10
0
        public async Task GetApiRateLimits_ValidGraphQLApiRequest()
        {
            //Arrange
            RateLimitStatus     graphQLApiStatus_Initial, graphQLApiStatus_Final;
            GitHubApiRateLimits gitHubApiRateLimits_Initial, gitHubApiRateLimits_Final;

            var startTime = DateTimeOffset.UtcNow;
            var cancellationTokenSource = new CancellationTokenSource();

            //Act
            gitHubApiRateLimits_Initial = await GitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

            graphQLApiStatus_Initial = gitHubApiRateLimits_Initial.GraphQLApi;

            await SendValidGraphQLApiRequest().ConfigureAwait(false);

            gitHubApiRateLimits_Final = await GitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

            graphQLApiStatus_Final = gitHubApiRateLimits_Final.GraphQLApi;

            //Assert
            Assert.IsNotNull(graphQLApiStatus_Initial);
            Assert.AreEqual(5000, graphQLApiStatus_Initial.RateLimit);
            Assert.GreaterOrEqual(graphQLApiStatus_Initial.RemainingRequestCount, 0);
            Assert.LessOrEqual(graphQLApiStatus_Initial.RemainingRequestCount, graphQLApiStatus_Initial.RateLimit);
            Assert.AreEqual(graphQLApiStatus_Initial.RateLimitReset_DateTime.ToUnixTimeSeconds(), graphQLApiStatus_Initial.RateLimitReset_UnixEpochSeconds);
            Assert.GreaterOrEqual(graphQLApiStatus_Initial.RateLimitReset_DateTime, startTime);
            Assert.GreaterOrEqual(graphQLApiStatus_Initial.RateLimitReset_UnixEpochSeconds, startTime.ToUnixTimeSeconds());

            Assert.IsNotNull(graphQLApiStatus_Final);
            Assert.AreEqual(5000, graphQLApiStatus_Final.RateLimit);
            Assert.GreaterOrEqual(graphQLApiStatus_Final.RemainingRequestCount, 0);
            Assert.LessOrEqual(graphQLApiStatus_Final.RemainingRequestCount, graphQLApiStatus_Final.RateLimit);
            Assert.AreEqual(graphQLApiStatus_Final.RateLimitReset_DateTime.ToUnixTimeSeconds(), graphQLApiStatus_Final.RateLimitReset_UnixEpochSeconds);
            Assert.GreaterOrEqual(graphQLApiStatus_Final.RateLimitReset_DateTime, startTime);
            Assert.GreaterOrEqual(graphQLApiStatus_Final.RateLimitReset_UnixEpochSeconds, startTime.ToUnixTimeSeconds());

            Assert.AreEqual(graphQLApiStatus_Initial.RateLimit, graphQLApiStatus_Final.RateLimit);
            Assert.AreEqual(graphQLApiStatus_Initial.RateLimitReset_DateTime, graphQLApiStatus_Final.RateLimitReset_DateTime);
            Assert.GreaterOrEqual(graphQLApiStatus_Initial.RateLimitReset_TimeRemaining, graphQLApiStatus_Final.RateLimitReset_TimeRemaining);
            Assert.AreEqual(graphQLApiStatus_Initial.RateLimitReset_UnixEpochSeconds, graphQLApiStatus_Final.RateLimitReset_UnixEpochSeconds);
            Assert.Greater(graphQLApiStatus_Initial.RemainingRequestCount, graphQLApiStatus_Final.RemainingRequestCount);
        }
Example #11
0
        public async Task BEARERSchemeHttpClient()
        {
            //Arrange
            var httpClient             = CreateGitHubHttpClient(new AuthenticationHeaderValue(GitHubConstants.AuthScheme.ToUpper(), GitHubConstants.PersonalAccessToken), new ProductHeaderValue(nameof(GitHubApiStatus)));
            var gitHubApiStatusService = new GitHubApiStatusService(httpClient);

            //Act
            var apiRateLimits = await gitHubApiStatusService.GetApiRateLimits().ConfigureAwait(false);

            //Assert
            Assert.IsNotNull(gitHubApiStatusService);
            Assert.IsNotNull(apiRateLimits);
            Assert.IsNotNull(apiRateLimits.AppManifestConfiguration);
            Assert.IsNotNull(apiRateLimits.CodeScanningUpload);
            Assert.IsNotNull(apiRateLimits.GraphQLApi);
            Assert.IsNotNull(apiRateLimits.RestApi);
            Assert.IsNotNull(apiRateLimits.SearchApi);
            Assert.IsNotNull(apiRateLimits.SourceImport);
        }
        public void GetRemainingRequestCount_ValidHttpResponseHeaders()
        {
            //Act
            const int rateLimit = 5000;
            TimeSpan  rateLimitTimeRemaining_Actual;

            var rateLimitTimeRemaining_Expected = TimeSpan.FromMinutes(45);
            var rateLimitResetDateTime          = DateTimeOffset.UtcNow.Add(rateLimitTimeRemaining_Expected);


            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit, rateLimitResetDateTime, rateLimit - 5);

            //Act
            rateLimitTimeRemaining_Actual = GitHubApiStatusService.GetRateLimitTimeRemaining(validHttpResponseHeaders);

            //Assert
            Assert.Greater(rateLimitTimeRemaining_Expected, rateLimitTimeRemaining_Actual);
            Assert.Less(rateLimitTimeRemaining_Expected.Subtract(TimeSpan.FromSeconds(2)), rateLimitTimeRemaining_Actual);
        }
Example #13
0
        public void GetRateLimitResetDateTime_ValidHttpResponseHeaders()
        {
            //Act
            DateTimeOffset rateLimitResetDateTime_Actual;

            const int rateLimit = 5000;
            var       rateLimitResetDateTime_Expected = DateTime.UtcNow.Add(TimeSpan.FromMinutes(20));

            var validHttpResponseHeaders = CreateHttpResponseHeaders(rateLimit, rateLimitResetDateTime_Expected, rateLimit - 5);

            //Act
            rateLimitResetDateTime_Actual = GitHubApiStatusService.GetRateLimitResetDateTime(validHttpResponseHeaders);

            //Assert Test all values except Milliseconds, because GitHub API
            Assert.AreEqual(rateLimitResetDateTime_Expected.Second, rateLimitResetDateTime_Actual.Second);
            Assert.AreEqual(rateLimitResetDateTime_Expected.Minute, rateLimitResetDateTime_Actual.Minute);
            Assert.AreEqual(rateLimitResetDateTime_Expected.Hour, rateLimitResetDateTime_Actual.Hour);
            Assert.AreEqual(rateLimitResetDateTime_Expected.DayOfYear, rateLimitResetDateTime_Actual.DayOfYear);
            Assert.AreEqual(rateLimitResetDateTime_Expected.Year, rateLimitResetDateTime_Actual.Year);
        }
Example #14
0
        public async Task ValidProductHeaderValueTest()
        {
            //Arrange
            var gitHubApiStatusService = new GitHubApiStatusService();

            gitHubApiStatusService.AddProductHeaderValue(new ProductHeaderValue(nameof(GitHubApiStatus)));
            gitHubApiStatusService.SetAuthenticationHeaderValue(new AuthenticationHeaderValue(GitHubConstants.AuthScheme, GitHubConstants.PersonalAccessToken));

            //Act
            var apiRateLimits = await gitHubApiStatusService.GetApiRateLimits().ConfigureAwait(false);

            //Assert
            Assert.IsNotNull(gitHubApiStatusService);
            Assert.IsTrue(gitHubApiStatusService.IsProductHeaderValueValid);

            Assert.IsNotNull(apiRateLimits);
            Assert.IsNotNull(apiRateLimits.AppManifestConfiguration);
            Assert.IsNotNull(apiRateLimits.CodeScanningUpload);
            Assert.IsNotNull(apiRateLimits.GraphQLApi);
            Assert.IsNotNull(apiRateLimits.RestApi);
            Assert.IsNotNull(apiRateLimits.SearchApi);
            Assert.IsNotNull(apiRateLimits.SourceImport);
        }