Ejemplo n.º 1
0
        public void Count_AddSingleValueThenQueryCount_ReturnsValueCountWithSpecialValues()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(string)));
            HttpHeaderValueCollection<string> collection = new HttpHeaderValueCollection<string>(knownHeader, headers,
                "special");

            Assert.Equal(0, collection.Count);

            headers.Add(knownHeader, "value2");
            Assert.Equal(1, collection.Count);

            headers.Clear();
            headers.Add(knownHeader, "special");
            Assert.Equal(1, collection.Count);
            headers.Add(knownHeader, "special");
            headers.Add(knownHeader, "special");
            Assert.Equal(3, collection.Count);
        }
        public void Count_AddSingleValueThenQueryCount_ReturnsValueCountWithSpecialValues()
        {
            MockHeaders headers = new MockHeaders();
            HttpHeaderValueCollection <string> collection = new HttpHeaderValueCollection <string>(knownStringHeader, headers,
                                                                                                   "special");

            Assert.Equal(0, collection.Count);

            headers.Add(knownStringHeader, "value2");
            Assert.Equal(1, collection.Count);

            headers.Clear();
            headers.Add(knownStringHeader, "special");
            Assert.Equal(1, collection.Count);
            headers.Add(knownStringHeader, "special");
            headers.Add(knownStringHeader, "special");
            Assert.Equal(3, collection.Count);
        }
        public void CopyTo_AddSingleValue_ContainsSingleValue()
        {
            MockHeaders headers = new MockHeaders();
            HttpHeaderValueCollection <Uri> collection = new HttpHeaderValueCollection <Uri>(knownUriHeader, headers,
                                                                                             specialValue);

            collection.Add(new Uri("http://www.example.org/"));

            Uri[] array = new Uri[1];
            collection.CopyTo(array, 0);
            Assert.Equal(new Uri("http://www.example.org/"), array[0]);

            // Now only set the special value: nothing should be added to the array.
            headers.Clear();
            headers.Add(knownUriHeader, specialValue.ToString());
            array[0] = null;
            collection.CopyTo(array, 0);
            Assert.Equal(specialValue, array[0]);
        }
Ejemplo n.º 4
0
        public void CopyTo_AddSingleValue_ContainsSingleValue()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers,
                specialValue);

            collection.Add(new Uri("http://www.example.org/"));

            Uri[] array = new Uri[1];
            collection.CopyTo(array, 0);
            Assert.Equal(new Uri("http://www.example.org/"), array[0]);

            // Now only set the special value: nothing should be added to the array.
            headers.Clear();
            headers.Add(knownHeader, specialValue.ToString());
            array[0] = null;
            collection.CopyTo(array, 0);
            Assert.Equal(specialValue, array[0]);
        }
Ejemplo n.º 5
0
        public void Add_AddValueContainingNewLine_NewLineFollowedByWhitespaceIsOKButNewLineFollowedByNonWhitespaceIsRejected()
        {
            MockHeaders headers = new MockHeaders();

            headers.Clear();
            headers.Add("custom", "value\r");
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal("value\r", headers.First().Value.First());

            headers.Clear();
            Assert.Throws<FormatException>(() => { headers.Add("custom", new string[] { "valid\n", "invalid\r\nother" }); });
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal("valid\n", headers.First().Value.First());
        }
Ejemplo n.º 6
0
        public void TryAddWithoutValidation_AddValueContainingNewLine_NewLineFollowedByWhitespaceIsOKButNewLineFollowedByNonWhitespaceIsRejected()
        {
            MockHeaders headers = new MockHeaders();

            // The header parser rejects both of the following values. Both values contain new line chars. According
            // to the RFC, LWS supports newlines followed by whitespaces. I.e. the first value gets rejected by the
            // parser, but added to the list of invalid values.
            headers.TryAddWithoutValidation(knownHeader, invalidHeaderValue + "\r\n other: value"); // OK, LWS is allowed
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal(invalidHeaderValue + "\r\n other: value", headers.First().Value.First());
            Assert.Equal(1, headers.Parser.TryParseValueCallCount);

            // This value is considered invalid (newline char followed by non-whitepace). However, since
            // TryAddWithoutValidation() only causes the header value to be analyzed when it gets actually accessed, no
            // exception is thrown. Instead the value is discarded and a warning is logged.
            headers.Clear();
            headers.TryAddWithoutValidation(knownHeader, invalidHeaderValue + "\r\nother:value");
            Assert.False(headers.Contains(knownHeader));
            Assert.Equal(0, headers.Count());

            // Adding newline followed by whitespace to a custom header is OK.
            headers.Clear();
            headers.TryAddWithoutValidation("custom", "value\r\n other: value"); // OK, LWS is allowed
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal("value\r\n other: value", headers.First().Value.First());

            // Adding newline followed by non-whitespace chars is invalid. The value is discarded and a warning is
            // logged.
            headers.Clear();
            headers.TryAddWithoutValidation("custom", "value\r\nother: value");
            Assert.False(headers.Contains("custom"));
            Assert.Equal(0, headers.Count());

            // Also ending a value with newline is invalid. Verify that valid values are added.
            headers.Clear();
            headers.Parser.TryParseValueCallCount = 0;
            headers.TryAddWithoutValidation(knownHeader, rawPrefix + "\rvalid");
            headers.TryAddWithoutValidation(knownHeader, invalidHeaderValue + "\r\n");
            headers.TryAddWithoutValidation(knownHeader, rawPrefix + "\n," + invalidHeaderValue + "\r\nother");
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal(parsedPrefix + "\rvalid", headers.First().Value.First());
            Assert.Equal(4, headers.Parser.TryParseValueCallCount);

            headers.Clear();
            headers.TryAddWithoutValidation("custom", "value\r\ninvalid");
            headers.TryAddWithoutValidation("custom", "value\r\n valid");
            headers.TryAddWithoutValidation("custom", "validvalue, invalid\r\nvalue");
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal("value\r\n valid", headers.First().Value.First());
        }
Ejemplo n.º 7
0
        public void ContainsParsedValue_AddOneValueToKnownHeaderAndCompareWithValueThatDiffersInCase_CustomComparerUsedForComparison()
        {
            MockHeaders headers = new MockHeaders();
            headers.AddParsedValue(knownHeader, "value");

            // Our custom comparer (MockComparer) does case-insensitive value comparison. Verify that our custom
            // comparer is used to compare the header value.
            Assert.True(headers.ContainsParsedValue(knownHeader, "VALUE"));
            Assert.Equal(1, headers.Parser.MockComparer.EqualsCount);

            headers.Clear();
            headers.TryAddWithoutValidation(knownHeader, invalidHeaderValue);
            Assert.False(headers.ContainsParsedValue(knownHeader, invalidHeaderValue));
        }
Ejemplo n.º 8
0
        public void TryAddWithoutValidation_AddEmptyValueString_HeaderWithNoValueAfterParsing()
        {
            MockHeaders headers = new MockHeaders();

            // The parser returns 'true' to indicate that it could parse the value (empty values allowed) and an 
            // value of 'null'. HttpHeaders will remove the header from the collection since the known header doesn't
            // have a value.
            headers.TryAddWithoutValidation(knownHeader, string.Empty);
            Assert.Equal(0, headers.Parser.TryParseValueCallCount);
            Assert.Equal(0, headers.Count());

            headers.Clear();
            headers.TryAddWithoutValidation("custom", (string)null);
            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal(string.Empty, headers.GetValues("custom").First());
        }
Ejemplo n.º 9
0
        public void Clear_AddMultipleHeadersAndThenClear_NoHeadersInCollection()
        {
            MockHeaders headers = new MockHeaders();
            headers.Add(knownHeader, rawPrefix + "1");
            headers.TryAddWithoutValidation(knownHeader, rawPrefix + "2");
            headers.Add("custom1", "customValue1");
            headers.Add("custom2", "customValue2");
            headers.Add("custom3", "customValue3");

            // Only 1 value should get parsed (call to Add() with known header value).
            Assert.Equal(1, headers.Parser.TryParseValueCallCount);

            // We added 4 different headers
            Assert.Equal(4, headers.Count());

            headers.Clear();

            Assert.Equal(0, headers.Count());

            // The call to Count() triggers a TryParseValue for the TryAddWithoutValidation() value. Clear() should
            // not cause any additional parsing operations.
            Assert.Equal(2, headers.Parser.TryParseValueCallCount);
        }