Describes an HTTP header.
示例#1
0
        public void it_should_serialize_the_header_to_its_string_representation()
        {
            string givenName = "Name";
            string givenParameterName = "Parameter";
            string givenParameterValue = "ParameterValue";
            HeaderValue[] givenValue = new[] { new HeaderValue("Value1"), new HeaderValue("Value2") };
            givenValue[0].Parameters.Add(new HeaderParameter(givenParameterName, givenParameterValue));
            Header header = new Header(givenName, givenValue);

            string expected = String.Format("{0}:{1};{3}=\"{4}\",{2}", givenName, givenValue[0].Value, givenValue[1].Value, givenParameterName, givenParameterValue);
            header.ToString().Should().Be(expected);
        }
示例#2
0
        public void it_should_initialize_the_instance_properly()
        {
            string givenName = "Name";
            string givenParameterName = "ParameterName";
            HeaderValue[] givenValue = new[] { new HeaderValue("Value1"), new HeaderValue("Value2") };
            givenValue[0].Parameters.Add(new HeaderParameter(givenParameterName));
            Header header = new Header(givenName, givenValue);

            header.Name.Should().Be(givenName);
            header.Values.Count.Should().Be(givenValue.Length);
            header.Values.First().Should().Be(givenValue[0]);
            header.Values.First().Parameters.Should().HaveCount(1);
            header.Values.Last().Should().Be(givenValue[1]);
        }
示例#3
0
        public void it_should_throw_when_last_parsed_value_is_removed()
        {
            var header = new Header<int>("Content-Length", 0);

            header.Values.Invoking(values => values.Remove(header.Values.First(item => item.Value == 0))).ShouldThrow<InvalidOperationException>();
        }
示例#4
0
        public void it_should_maintain_parsed_header_values_correctly()
        {
            var header = new Header<int>("Content-Length", 0);

            header.Values.Add(new HeaderValue<int>(10));
            header.Values.Remove(header.Values.First(item => item.Value == 0));

            header.Values.Should().HaveCount(1).And.Subject.First().Value.Should().Be(10);
        }
示例#5
0
 public void it_should_consider_same_instance_as_equal()
 {
     var instance = new Header("test");
     instance.Equals(instance).Should().BeTrue();
 }
示例#6
0
文件: Header.cs 项目: alien-mcl/URSA
        /// <summary>Tries to parse a given string as an <see cref="Header" />.</summary>
        /// <param name="header">String to be parsed.</param>
        /// <param name="httpHeader">Resulting HTTP header if parsing was successful; otherwise <b>null</b>.</param>
        /// <returns><b>true</b> if parsing was successful; otherwise <b>false</b>.</returns>
        public static bool TryParse(string header, out Header httpHeader)
        {
            bool result = false;
            httpHeader = null;
            try
            {
                httpHeader = Parse(header);
                result = true;
            }
            catch
            {
            }

            return result;
        }
示例#7
0
        /// <summary>Sets a new header.</summary>
        /// <remarks>If the header already exists, it is replaced with the new one.</remarks>
        /// <param name="header">Header to be set.</param>
        /// <returns>Instance of the <see cref="Header" /> set.</returns>
        public Header Set(Header header)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            return _headers[header.Name] = header;
        }
示例#8
0
        /// <summary>Adds a new header.</summary>
        /// <remarks>If the header already exists, method will merge new value and parameters with the existing ones.</remarks>
        /// <param name="header">Header to be added.</param>
        /// <returns>Instance of the <see cref="Header" /> either updated or added.</returns>
        public Header Add(Header header)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            Header result;
            if (!_headers.TryGetValue(header.Name, out result))
            {
                _headers[header.Name] = result = header;
            }
            else
            {
                header.Values.ForEach(value => result.Values.AddUnique(value));
            }

            return result;
        }