Ejemplo n.º 1
0
        /// <summary>
        /// Creates <see cref="LinkHeader"/> from string value/>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="FormatException"/>
        public static LinkHeader Parse(string value)
        {
            #region Check arguments
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            #endregion

            var result = new LinkHeader();

            var regex = new Regex("<(.+)> ?; ?(.+)", RegexOptions.IgnoreCase);
            var match = regex.Match(value);
            if (!match.Success)
            {
                throw new FormatException("Link doesn't match to regular expression");
            }

            result.Url = new Uri(match.Groups[1].Value, UriKind.RelativeOrAbsolute);
            var attributes = match.Groups[2].Value
                             .Split(';')
                             .Select(LinkHeaderItem.Parse)
                             .ToArray();

            result.Items.AddRange(attributes);

            return(result);
        }
Ejemplo n.º 2
0
        public void Serializae2()
        {
            var link = new LinkHeader("/TheBook/chapter2");

            link.Items.Add(new LinkHeaderItem("rel", "previous", true));
            link.Items.Add(new LinkHeaderItem("title*", "UTF-8'de'letztes%20Kapitel"));

            Assert.Equal("</TheBook/chapter2>; rel=\"previous\"; title*=UTF-8'de'letztes%20Kapitel", link.ToString());
        }
Ejemplo n.º 3
0
        public void Parse1()
        {
            var link = LinkHeader.Parse("</>;rel=\"index\"");

            Assert.Equal("/", link.Url.OriginalString);

            var rel = link.Items["REL"];

            Assert.NotNull(rel);
            Assert.Equal("rel", rel.Name);
            Assert.Equal("index", rel.Value);
            Assert.True(rel.Quoted);
        }
Ejemplo n.º 4
0
        public void Parse3()
        {
            var link = LinkHeader.Parse("</TheBook/chapter2>; rel=\"previous\"; title*=UTF-8'de'letztes%20Kapitel");

            Assert.Equal("/TheBook/chapter2", link.Url.OriginalString);

            var rel = link.Items["rel"];

            Assert.NotNull(rel);
            Assert.Equal("rel", rel.Name);
            Assert.Equal("previous", rel.Value);
            Assert.True(rel.Quoted);

            var title = link.Items["title*"];

            Assert.NotNull(title);
            Assert.Equal("title*", title.Name);
            Assert.Equal("UTF-8'de'letztes%20Kapitel", title.Value);
            Assert.True(rel.Quoted);
        }
Ejemplo n.º 5
0
        public void Parse2()
        {
            var link = LinkHeader.Parse("<http://example.com/TheBook/chapter2>; rel=\"previous\"; title=\"previous chapter\"");

            Assert.Equal("http://example.com/TheBook/chapter2", link.Url.OriginalString);

            var rel = link.Items["rel"];

            Assert.NotNull(rel);
            Assert.Equal("rel", rel.Name);
            Assert.Equal("previous", rel.Value);
            Assert.True(rel.Quoted);

            var title = link.Items["title"];

            Assert.NotNull(title);
            Assert.Equal("title", title.Name);
            Assert.Equal("previous chapter", title.Value);
            Assert.True(rel.Quoted);
        }
Ejemplo n.º 6
0
        public void Serializae1()
        {
            var link = new LinkHeader("/", new LinkHeaderItem("rel", "index", true));

            Assert.Equal("</>; rel=\"index\"", link.ToString());
        }