public bool Equals(KeyValuePair <string, IEnumerable <string> > x, KeyValuePair <string, IEnumerable <string> > y)
        {
            if (x.Key != y.Key)
            {
                return(false);
            }

            if (_isOnlyMatchingHeaderName)
            {
                return(true);
            }

            if (y.Value.Any(
                    yValue => x.Value
                    .SelectMany(HttpHeadersCollection.ParseHttpHeaderValue)
                    .All(xValue =>
            {
                string[] headerValues = HttpHeadersCollection.ParseHttpHeaderValue(yValue).ToArray();
                return(headerValues.Contains(xValue) || _valuePatternMatcher != null && headerValues.Any(_valuePatternMatcher.IsMatch));
            })
                    ))
            {
                return(true);
            }

            return(!x.Value.Any());
        }
        public void Given_empty_header_when_parsing_should_return_empty_dictionary()
        {
            // Act
            HttpHeaders actual = HttpHeadersCollection.Parse(string.Empty);

            // Assert
            actual.Should().BeEmpty();
        }
        public void Given_single_invalid_header_string_with_when_parsing_should_throw(string invalidHeaderString, string exceptionMessage)
        {
            // Act
            Action act = () => HttpHeadersCollection.Parse(invalidHeaderString);

            // Assert
            act.Should().Throw <Exception>()
            .WithMessage(exceptionMessage);
        }
        public void Given_null_header_when_parsing_should_throw()
        {
            // Act
            Action act = () => HttpHeadersCollection.Parse(null);

            // Assert
            act.Should()
            .Throw <ArgumentNullException>()
            .WithParamName("headers");
        }
        public void Given_multiple_empty_newlines_when_parsing_should_ignore_newlines()
        {
            string headers = Environment.NewLine + Environment.NewLine + "Header: Value";

            // Act
            HttpHeaders actual = HttpHeadersCollection.Parse(headers);

            // Assert
            actual.Should().HaveCount(1);
            actual.Should().Contain(h => h.Key == "Header");
        }
        public void Given_single_header_string_when_parsing_should_return_key_with_one_or_more_values(string headerString, string expectedHeaderKey, params string[] expectedValues)
        {
            // Act
            HttpHeaders headers = HttpHeadersCollection.Parse(headerString);

            // Assert
            headers.TryGetValues(expectedHeaderKey, out IEnumerable <string> values)
            .Should()
            .BeTrue();
            values
            .Should()
            .BeEquivalentTo(expectedValues);
        }
        public static HttpHeaders Parse(string headers)
        {
            if (headers is null)
            {
                throw new ArgumentNullException(nameof(headers));
            }

            var httpHeaders = new HttpHeadersCollection();

            if (headers.Length == 0)
            {
                return(httpHeaders);
            }

            using (var sr = new StringReader(headers))
            {
                while (true)
                {
                    string header = sr.ReadLine();
                    if (header is null)
                    {
                        break;
                    }

                    if (string.IsNullOrWhiteSpace(header))
                    {
                        continue;
                    }

                    string[] hvp = header.Split(new[] { ':' }, 2, StringSplitOptions.None);

                    string fieldName  = hvp.Length > 0 ? hvp[0] : null;
                    string fieldValue = hvp.Length > 1 ? hvp[1] : null;
                    httpHeaders.Add(fieldName, ParseHttpHeaderValue(fieldValue));
                }
            }

            return(httpHeaders);
        }