Ejemplo n.º 1
0
            public void HandlesInvalidHeaderValues()
            {
                var headers = new Dictionary <string, string>
                {
                    { "X-RateLimit-Limit", "XXX" },
                    { "X-RateLimit-Remaining", "XXXX" },
                    { "X-RateLimit-Reset", "XXXX" },
                    { "Date", "XXX" },
                    { ApiInfoParser.ReceivedTimeHeaderName, "XXX" },
                };
                var response = CreateResponse(HttpStatusCode.Forbidden, headers);

                var exception = new RateLimitExceededException(response);

                Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode);
                Assert.Equal(0, exception.Limit);
                Assert.Equal(0, exception.Remaining);
                var expectedReset = DateTimeOffset.ParseExact(
                    "Thu 01 Jan 1970 0:00:00 AM -00:00",
                    "ddd dd MMM yyyy h:mm:ss tt zzz",
                    CultureInfo.InvariantCulture);

                Assert.Equal(expectedReset, exception.Reset);
                Assert.Equal(TimeSpan.Zero, exception.GetRetryAfterTimeSpan());
            }
Ejemplo n.º 2
0
            public void ReturnsSkewedDistanceFromReset()
            {
                // One hour into the future is hopefully long enough to still be
                // in the future at the end of this method
                var resetTime = DateTimeOffset.Now + TimeSpan.FromHours(1);
                var recvDate  = new DateTimeOffset(2020, 06, 07, 12, 00, 00, TimeSpan.Zero);
                var skew      = TimeSpan.FromSeconds(-5);

                var response = CreateResponse(HttpStatusCode.Forbidden,
                                              new Dictionary <string, string>
                {
                    ["X-RateLimit-Limit"]     = "100",
                    ["X-RateLimit-Remaining"] = "0",
                    ["X-RateLimit-Reset"]     = resetTime.ToUnixTimeSeconds()
                                                .ToString(CultureInfo.InvariantCulture),
                    ["Date"] = (recvDate + skew).ToString("r"),
                    [ApiInfoParser.ReceivedTimeHeaderName] = recvDate.ToString("r"),
                });

                Assert.Equal(skew, response.ApiInfo.ServerTimeDifference);
                var except = new RateLimitExceededException(response);

                var timeToReset = except.GetRetryAfterTimeSpan();

                Assert.NotEqual(TimeSpan.Zero, timeToReset);
                Assert.InRange(timeToReset, TimeSpan.Zero, TimeSpan.FromHours(1));
            }
Ejemplo n.º 3
0
            public void HandlesMissingHeaderValues()
            {
                var response  = CreateResponse(HttpStatusCode.Forbidden);
                var exception = new RateLimitExceededException(response);

                Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode);
                Assert.Equal(0, exception.Limit);
                Assert.Equal(0, exception.Remaining);
                var expectedReset = DateTimeOffset.ParseExact(
                    "Thu 01 Jan 1970 0:00:00 AM -00:00",
                    "ddd dd MMM yyyy h:mm:ss tt zzz",
                    CultureInfo.InvariantCulture);

                Assert.Equal(expectedReset, exception.Reset);
                Assert.Equal(TimeSpan.Zero, exception.GetRetryAfterTimeSpan());
            }
Ejemplo n.º 4
0
            public void ReturnsZeroIfSkewedResetInPast()
            {
                var beginTime = DateTimeOffset.Now;
                var resetTime = beginTime - TimeSpan.FromHours(1);

                var response = CreateResponse(HttpStatusCode.Forbidden,
                                              new Dictionary <string, string>
                {
                    ["X-RateLimit-Limit"]     = "100",
                    ["X-RateLimit-Remaining"] = "0",
                    ["X-RateLimit-Reset"]     = resetTime.ToUnixTimeSeconds()
                                                .ToString(CultureInfo.InvariantCulture),
                    ["Date"] = beginTime.ToString("r"),
                    [ApiInfoParser.ReceivedTimeHeaderName] = beginTime.ToString("r"),
                });

                Assert.Equal(TimeSpan.Zero, response.ApiInfo.ServerTimeDifference);
                var except = new RateLimitExceededException(response);

                var timeToReset = except.GetRetryAfterTimeSpan();

                Assert.Equal(TimeSpan.Zero, timeToReset);
            }