public void Parse_PrevLinkOnly()
        {
            var res = LinkHeaderParser.Parse <Product>("<https://test.myshopify.com/admin/api/2019-07/products.json?page_info=vwxyzab&limit=6>; rel=\"previous\"");

            Assert.Null(res.NextLink);
            Assert.NotNull(res.PreviousLink);
            Assert.Equal(res.PreviousLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?page_info=vwxyzab&limit=6");
            Assert.Equal(res.PreviousLink.PageInfo, "vwxyzab");
        }
        public void ParseLinkWithoutRelation()
        {
            var headerValue = "<https://my-server.dev/api/v1/users?limit=25>; rel=\"self\"";
            var links       = LinkHeaderParser.Parse(headerValue);

            links.Count().Should().Be(1);
            links.Single().Target.Should().Be("https://my-server.dev/api/v1/users?limit=25");
            links.Single().Relation.Should().Be("self");
        }
        public void IgnoreBadInlineLinkValues()
        {
            var headerValue = @"<https://my-server.dev/api/v1/users?after=abc123>;
; foo=""bar""; rel=""next""";
            var links       = LinkHeaderParser.Parse(headerValue);

            links.Count().Should().Be(1);
            links.Single().Target.Should().Be("https://my-server.dev/api/v1/users?after=abc123");
            links.Single().Relation.Should().Be("next");
        }
        public void IgnoreBadInlineLinks()
        {
            var headerValue = @"
<https://my-server.dev/api/v1/users?after=abc123>; rel=""next"",,
  definitelyinvalid!";
            var links       = LinkHeaderParser.Parse(headerValue);

            links.Count().Should().Be(1);
            links.Single().Target.Should().Be("https://my-server.dev/api/v1/users?after=abc123");
        }
        public void Parse_PrevThenNext_PageInfoIsNotFirstQueryParam()
        {
            var res = LinkHeaderParser.Parse <Product>("<https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=abcdefg>; rel=\"previous\", <https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=opqrstu>; rel=\"next\"");

            Assert.NotNull(res.PreviousLink);
            Assert.NotNull(res.NextLink);
            Assert.Equal(res.PreviousLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=abcdefg");
            Assert.Equal(res.PreviousLink.PageInfo, "abcdefg");
            Assert.Equal(res.NextLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=opqrstu");
            Assert.Equal(res.NextLink.PageInfo, "opqrstu");
        }
        public void Parse_PrevThenNext_WithFieldsParam()
        {
            var res = LinkHeaderParser.Parse <Product>("<https://test.myshopify.com/admin/api/2019-07/products.json?page_info=abcdefg&limit=3&fields=id,images,title>; rel=\"previous\", <https://test.myshopify.com/admin/api/2019-07/products.json?page_info=opqrstu&limit=3&fields=id,images,title>; rel=\"next\"");

            Assert.NotNull(res.PreviousLink);
            Assert.NotNull(res.NextLink);
            Assert.Equal(res.PreviousLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?page_info=abcdefg&limit=3&fields=id,images,title");
            Assert.Equal(res.PreviousLink.PageInfo, "abcdefg");
            Assert.Equal(res.NextLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?page_info=opqrstu&limit=3&fields=id,images,title");
            Assert.Equal(res.NextLink.PageInfo, "opqrstu");
        }
        public void Parse_NextThenPrev()
        {
            var res = LinkHeaderParser.Parse <Product>("<https://test.myshopify.com/admin/api/2019-07/products.json?page_info=opqrstu&limit=3>; rel=\"next\", <https://test.myshopify.com/admin/api/2019-07/products.json?page_info=abcdefg&limit=3>; rel=\"previous\"");

            Assert.NotNull(res.PreviousLink);
            Assert.NotNull(res.NextLink);
            Assert.Equal(res.PreviousLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?page_info=abcdefg&limit=3");
            Assert.Equal(res.PreviousLink.PageInfo, "abcdefg");
            Assert.Equal(res.NextLink.Url, "https://test.myshopify.com/admin/api/2019-07/products.json?page_info=opqrstu&limit=3");
            Assert.Equal(res.NextLink.PageInfo, "opqrstu");
        }
Exemple #8
0
        public static List <ILink> ParseLinkHeaders(this HttpHeaders headers, Uri baseUri, ILinkFactory linkRegistry)
        {
            var list        = new List <ILink>();
            var parser      = new LinkHeaderParser(linkRegistry);
            var linkHeaders = headers.GetValues("Link");

            foreach (var linkHeader in linkHeaders)
            {
                list.AddRange(parser.Parse(baseUri, linkHeader));
            }
            return(list);
        }
        public void ParseMultipleLinksInSingleValue()
        {
            var headerValue = @"
<https://my-server.dev/api/v1/users?after=abc123>; rel=""next"",
  <https://my-server.dev/api/v1/users?after=xyz987>; rel=""self""";
            var links       = LinkHeaderParser.Parse(headerValue);

            links.ElementAt(0).Target.Should().Be("https://my-server.dev/api/v1/users?after=abc123");
            links.ElementAt(0).Relation.Should().Be("next");

            links.ElementAt(1).Target.Should().Be("https://my-server.dev/api/v1/users?after=xyz987");
            links.ElementAt(1).Relation.Should().Be("self");
        }
        public void IgnoreBadLinks()
        {
            var headerValues = new List <string>()
            {
                "barbaz",
                "nope!",
                "   ",
                "<https://foo.bar>; rel=\"prev\"",
            };
            var links = LinkHeaderParser.Parse(headerValues);

            links.Count().Should().Be(1);
            links.Single().Target.Should().Be("https://foo.bar");
            links.Single().Relation.Should().Be("prev");
        }
        public void ParseMultipleLinks()
        {
            var headerValues = new List <string>()
            {
                "<https://my-server.dev/api/v1/users?limit=25>; rel=\"self\"",
                "<https://my-server.dev/api/v1/users?after=asdf123&limit=25>; rel=\"next\"",
            };
            var links = LinkHeaderParser.Parse(headerValues);

            links.Count().Should().Be(2);

            links.ElementAt(0).Target.Should().Be("https://my-server.dev/api/v1/users?limit=25");
            links.ElementAt(0).Relation.Should().Be("self");

            links.ElementAt(1).Target.Should().Be("https://my-server.dev/api/v1/users?after=asdf123&limit=25");
            links.ElementAt(1).Relation.Should().Be("next");
        }
        public void IgnoreEmptyValues()
        {
            var headerValues = new List <string>()
            {
                #pragma warning disable SA1122
                "",
                #pragma warning restore SA1122
                string.Empty,
                null,
                "<https://foo.bar>; rel=\"self\"",
            };
            var links = LinkHeaderParser.Parse(headerValues);

            links.Count().Should().Be(1);
            links.Single().Target.Should().Be("https://foo.bar");
            links.Single().Relation.Should().Be("self");
        }
Exemple #13
0
        private static WebLink GetNextLink(HttpResponse response)
        {
            if (response?.Headers == null)
            {
                return(null);
            }

            var linkHeaders = response
                              .Headers
                              .Where(kvp => kvp.Key.Equals("Link", StringComparison.OrdinalIgnoreCase))
                              .Select(kvp => kvp.Value);

            var nextLink = LinkHeaderParser
                           .Parse(linkHeaders.SelectMany(x => x))
                           .Where(x => x.Relation == "next")
                           .FirstOrDefault();

            return(nextLink);
        }
        public void Parses_Fields_Parameter()
        {
            var res = LinkHeaderParser.Parse <Product>("<https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=abcdefg&fields=id%2Ccustomer%2Ccreated_at>; rel=\"previous\", <https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=opqrstu&fields=id%2Ccustomer%2Ccreated_at>; rel=\"next\"");

            Assert.NotNull(res.PreviousLink);
            Assert.NotNull(res.NextLink);
            Assert.Equal("https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=abcdefg&fields=id%2Ccustomer%2Ccreated_at", res.PreviousLink.Url);
            Assert.Equal("id,customer,created_at", res.PreviousLink.Fields);
            Assert.Equal("https://test.myshopify.com/admin/api/2019-07/products.json?limit=3&page_info=opqrstu&fields=id%2Ccustomer%2Ccreated_at", res.NextLink.Url);
            Assert.Equal("id,customer,created_at", res.NextLink.Fields);

            var nextPageFilter = res.NextLink.GetFollowingPageFilter();

            Assert.Equal("id,customer,created_at", nextPageFilter.Fields);

            var previousPageFilter = res.PreviousLink.GetFollowingPageFilter();

            Assert.Equal("id,customer,created_at", previousPageFilter.Fields);
        }
        public void IgnoreEmptyArgs()
        {
            var links = LinkHeaderParser.Parse();

            links.Count().Should().Be(0);
        }
 /// <summary>
 /// Parses a link header value into a ListResult<T>. The Items property will need to be manually set.
 /// </summary>
 protected ListResult <T> ParseLinkHeaderToListResult <T>(RequestResult <List <T> > requestResult)
 {
     return(new ListResult <T>(requestResult.Result, requestResult.RawLinkHeaderValue == null ? null : LinkHeaderParser.Parse <T>(requestResult.RawLinkHeaderValue)));
 }
        public void ParseNull()
        {
            var links = LinkHeaderParser.Parse((IEnumerable <string>)null);

            links.Count().Should().Be(0);
        }
        public void ParseEmptyString()
        {
            var links = LinkHeaderParser.Parse(string.Empty);

            links.Count().Should().Be(0);
        }
        public void IgnoreNullAndEmptyArgs()
        {
            var links = LinkHeaderParser.Parse(string.Empty, null);

            links.Count().Should().Be(0);
        }
        public void ParseEmptyArray()
        {
            var links = LinkHeaderParser.Parse(new string[0]);

            links.Count().Should().Be(0);
        }
Exemple #21
0
        public static IList <ILink> ParseLinkHeader(this ILink link, string linkHeader, ILinkFactory linkRegistry)
        {
            var parser = new LinkHeaderParser(linkRegistry);

            return(parser.Parse(link.Target, linkHeader));
        }