Beispiel #1
0
            public void ParsesLinkHeader()
            {
                var response = new ApiResponse <string>
                {
                    Headers =
                    {
                        {
                            "Link",
                            "<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; rel=\"next\", " +
                            "<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5>; rel=\"last\", " +
                            "<https://api.github.com/repos/rails/rails/issues?page=1&per_page=5>; rel=\"first\", " +
                            "<https://api.github.com/repos/rails/rails/issues?page=2&per_page=5>; rel=\"prev\""
                        }
                    }
                };

                ApiInfoParser.ParseApiHttpHeaders(response);

                var apiInfo = response.ApiInfo;

                Assert.NotNull(apiInfo);
                Assert.Equal(4, apiInfo.Links.Count);
                Assert.Contains("next", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5"),
                             apiInfo.Links["next"]);
                Assert.Contains("prev", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5"),
                             apiInfo.Links["prev"]);
                Assert.Contains("first", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5"),
                             apiInfo.Links["first"]);
                Assert.Contains("last", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5"),
                             apiInfo.Links["last"]);
            }
Beispiel #2
0
            public void ParsesApiInfoFromHeaders()
            {
                var response = new ApiResponse <string>
                {
                    Headers =
                    {
                        { "X-Accepted-OAuth-Scopes", "user"                             },
                        { "X-OAuth-Scopes",          "user, public_repo, repo, gist"    },
                        { "X-RateLimit-Limit",       "5000"                             },
                        { "X-RateLimit-Remaining",   "4997"                             },
                        { "ETag",                    "5634b0b187fd2e91e3126a75006cc4fa" }
                    }
                };

                ApiInfoParser.ParseApiHttpHeaders(response);

                var apiInfo = response.ApiInfo;

                Assert.NotNull(apiInfo);
                Assert.Equal(new[] { "user" }, apiInfo.AcceptedOauthScopes.ToArray());
                Assert.Equal(new[] { "user", "public_repo", "repo", "gist" }, apiInfo.OauthScopes.ToArray());
                Assert.Equal(5000, apiInfo.RateLimit.Limit);
                Assert.Equal(4997, apiInfo.RateLimit.Remaining);
                Assert.Equal("5634b0b187fd2e91e3126a75006cc4fa", apiInfo.Etag);
            }
Beispiel #3
0
            public void ParsesLinkHeader()
            {
                var headers = new Dictionary <string, string>
                {
                    {
                        "Link",
                        "<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; rel=\"next\", " +
                        "<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5>; rel=\"last\", " +
                        "<https://api.github.com/repos/rails/rails/issues?page=1&per_page=5>; rel=\"first\", " +
                        "<https://api.github.com/repos/rails/rails/issues?page=2&per_page=5>; rel=\"prev\""
                    }
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(4, apiInfo.Links.Count);
                Assert.Contains("next", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5"),
                             apiInfo.Links["next"]);
                Assert.Contains("prev", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5"),
                             apiInfo.Links["prev"]);
                Assert.Contains("first", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5"),
                             apiInfo.Links["first"]);
                Assert.Contains("last", apiInfo.Links.Keys);
                Assert.Equal(new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5"),
                             apiInfo.Links["last"]);
            }
Beispiel #4
0
        // THIS IS THE METHOD THAT EVERY REQUEST MUST GO THROUGH!
        async Task <IResponse <T> > RunRequest <T>(IRequest request)
        {
            request.Headers.Add("User-Agent", UserAgent);
            await _authenticator.Apply(request);

            var response = await _httpClient.Send <T>(request);

            ApiInfoParser.ParseApiHttpHeaders(response);
            HandleErrors(response);
            return(response);
        }
Beispiel #5
0
        // THIS IS THE METHOD THAT EVERY REQUEST MUST GO THROUGH!
        async Task <IResponse <T> > RunRequest <T>(IRequest request, CancellationToken cancellationToken)
        {
            request.Headers.Add("User-Agent", UserAgent);
            await _authenticator.Apply(request).ConfigureAwait(false);

            var response = await _httpClient.Send <T>(request, cancellationToken).ConfigureAwait(false);

            ApiInfoParser.ParseApiHttpHeaders(response);
            HandleErrors(response);
            return(response);
        }
Beispiel #6
0
            public void ParsesServerTimeDifferenceAsZeroWhenReceiveDateHeaderIsMissing()
            {
                var serverDate = new DateTimeOffset(2020, 06, 07, 12, 00, 00, TimeSpan.Zero);
                var headers    = new Dictionary <string, string>
                {
                    ["Date"] = serverDate.ToString("r"), // Format string r: RFC1123 HTTP Round-tripping time format
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(TimeSpan.Zero, apiInfo.ServerTimeDifference);
            }
Beispiel #7
0
            public void ParsesServerTimeDifferenceAsZeroWhenReceiveDateHeaderIsNonesense()
            {
                var headers = new Dictionary <string, string>
                {
                    // Format string r: RFC1123 HTTP Round-tripping time format
                    ["Date"] = DateTimeOffset.Now.ToString("r"),
                    [ApiInfoParser.ReceivedTimeHeaderName] = "abfhjsdkhfjkldhf"
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(TimeSpan.Zero, apiInfo.ServerTimeDifference);
            }
Beispiel #8
0
            public void BadHeadersAreIgnored()
            {
                var headers = new Dictionary <string, string>
                {
                    {
                        "Link",
                        "<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; , " +
                        "<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5; rel=\"last\""
                    }
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(0, apiInfo.Links.Count);
            }
Beispiel #9
0
            public void ParsesServerTimeDifference()
            {
                var serverDate   = new DateTimeOffset(2020, 06, 07, 12, 00, 00, TimeSpan.Zero);
                var receivedDate = new DateTimeOffset(2020, 06, 07, 14, 00, 00, TimeSpan.Zero);
                var diff         = serverDate - receivedDate;

                var headers = new Dictionary <string, string>
                {
                    ["Date"] = serverDate.ToString("r"), // Format string r: RFC1123 HTTP Round-tripping time format
                    [ApiInfoParser.ReceivedTimeHeaderName] = receivedDate.ToString("r")
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(diff, apiInfo.ServerTimeDifference);
            }
            public void ParsesApiInfoFromHeaders()
            {
                var headers = new Dictionary <string, string>
                {
                    { "X-Accepted-OAuth-Scopes", "user" },
                    { "X-OAuth-Scopes", "user, public_repo, repo, gist" },
                    { "X-RateLimit-Limit", "5000" },
                    { "X-RateLimit-Remaining", "4997" },
                    { "ETag", "5634b0b187fd2e91e3126a75006cc4fa" }
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(5000, apiInfo.RateLimit.Limit);
                Assert.Equal(4997, apiInfo.RateLimit.Remaining);
                Assert.Equal("5634b0b187fd2e91e3126a75006cc4fa", apiInfo.Etag);
            }
Beispiel #11
0
            public void ParsesApiInfoFromCaseInsensitiveHeaders()
            {
                var headers = new Dictionary <string, string>
                {
                    { "x-accepted-oauth-scopes", "user" },
                    { "x-oauth-scopes", "user, public_repo, repo, gist" },
                    { "x-ratelimit-limit", "5000" },
                    { "x-ratelimit-remaining", "4997" },
                    { "etag", "5634b0b187fd2e91e3126a75006cc4fa" }
                };

                var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);

                Assert.NotNull(apiInfo);
                Assert.Equal(new[] { "user" }, apiInfo.AcceptedOauthScopes.ToArray());
                Assert.Equal(new[] { "user", "public_repo", "repo", "gist" }, apiInfo.OauthScopes.ToArray());
                Assert.Equal(5000, apiInfo.RateLimit.Limit);
                Assert.Equal(4997, apiInfo.RateLimit.Remaining);
                Assert.Equal("5634b0b187fd2e91e3126a75006cc4fa", apiInfo.Etag);
            }
Beispiel #12
0
            public void BadHeadersAreIgnored()
            {
                var response = new ApiResponse <string>
                {
                    Headers =
                    {
                        {
                            "Link",
                            "<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; , " +
                            "<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5; rel=\"last\""
                        }
                    }
                };

                ApiInfoParser.ParseApiHttpHeaders(response);

                var apiInfo = response.ApiInfo;

                Assert.NotNull(apiInfo);
                Assert.Equal(0, apiInfo.Links.Count);
            }