protected static bool VerifyHeaders(HttpHeaders headers, out Uri hubLocation)
        {
            IEnumerable <string> headerValues;

            if (!headers.TryGetValues("link", out headerValues))
            {
                hubLocation = null;
                return(false);
            }

            var valueList   = headerValues.ToList();
            var linkHeaders = new List <LinkHeader>();

            foreach (var value in valueList)
            {
                linkHeaders.AddRange(LinkHeader.Parse(value));
            }

            if (HasRequiredHeaders(linkHeaders))
            {
                var hub = linkHeaders.First(h => h.Relationship.Equals("hub", StringComparison.OrdinalIgnoreCase));
                hubLocation = hub.Link;
                return(true);
            }

            hubLocation = null;
            return(false);
        }
Example #2
0
    public void It_can_serialize_round_trip()
    {
        const string input =
            "</v2/colors?page=117&page_size=5>; rel=previous, </v2/colors?page=119&page_size=5>; rel=next, </v2/colors?page=118&page_size=5>; rel=self, </v2/colors?page=0&page_size=5>; rel=first, </v2/colors?page=119&page_size=5>; rel=last";
        var sut = LinkHeader.Parse(input);

        var actual = sut.ToString();

        Assert.Equal(input, actual);
    }
Example #3
0
        public void parse_no_links_exist_return_null_page_links()
        {
            var rsp = new HttpResponseMessage();
            var sut = new LinkHeader();

            var result = sut.Parse(rsp.Headers);

            Assert.Null(result.FirstPage);
            Assert.Null(result.PreviousPage);
            Assert.Null(result.NextPage);
            Assert.Null(result.LastPage);
        }
Example #4
0
    public void It_can_parse_link_headers()
    {
        const string input =
            "</v2/colors?page=117&page_size=5>; rel=previous, </v2/colors?page=119&page_size=5>; rel=next, </v2/colors?page=118&page_size=5>; rel=self, </v2/colors?page=0&page_size=5>; rel=first, </v2/colors?page=119&page_size=5>; rel=last";
        var actual = LinkHeader.Parse(input);

        Assert.Collection(
            actual.Links,
            next => LinkFact.IsLink(next, "previous", "/v2/colors?page=117&page_size=5"),
            next => LinkFact.IsLink(next, "next", "/v2/colors?page=119&page_size=5"),
            next => LinkFact.IsLink(next, "self", "/v2/colors?page=118&page_size=5"),
            next => LinkFact.IsLink(next, "first", "/v2/colors?page=0&page_size=5"),
            next => LinkFact.IsLink(next, "last", "/v2/colors?page=119&page_size=5")
            );
    }
Example #5
0
        public void parse_link_header_format_changed_return_null_page_links()
        {
            var linkHeader = $"{_first},{_prev}";
            var rsp        = new HttpResponseMessage();

            rsp.Headers.Add("link", new List <string> {
                linkHeader, linkHeader
            });
            var sut = new LinkHeader();

            var result = sut.Parse(rsp.Headers);

            Assert.Null(result.FirstPage);
            Assert.Null(result.PreviousPage);
            Assert.Null(result.NextPage);
            Assert.Null(result.LastPage);
        }
Example #6
0
        public void parse_last_page_contains_first_and_previous_links_parse_out_correctly()
        {
            var linkHeader = $"{_first},{_prev}";
            var rsp        = new HttpResponseMessage();

            rsp.Headers.Add("link", new List <string> {
                linkHeader
            });
            var sut = new LinkHeader();

            var result = sut.Parse(rsp.Headers);

            Assert.Equal(_firstUrl, result.FirstPage);
            Assert.Equal(_prevUrl, result.PreviousPage);
            Assert.Null(result.NextPage);
            Assert.Null(result.LastPage);
        }
Example #7
0
        public void parse_url_delimiter_changed_return_null_page_links()
        {
            var linkHeader = $"{_first.Replace(';', ':')}";
            var rsp        = new HttpResponseMessage();

            rsp.Headers.Add("link", new List <string> {
                linkHeader, linkHeader
            });
            var sut = new LinkHeader();

            var result = sut.Parse(rsp.Headers);

            Assert.Null(result.FirstPage);
            Assert.Null(result.PreviousPage);
            Assert.Null(result.NextPage);
            Assert.Null(result.LastPage);
        }