public void TryParseStrictList_WithSomeInvalidValues_ReturnsFalse()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text 1",
                "text2",
                "\"text 2\",",
                "text3;q=0.5",
                "text4;q=0.5, extra stuff",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.False(StringWithQualityHeaderValue.TryParseStrictList(inputs, out results));
        }
        public void GetHashCode_UseSameAndDifferentValues_SameOrDifferentHashCodes()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.Equal(value1.GetHashCode(), value2.GetHashCode());
            Assert.Equal(value1.GetHashCode(), value3.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value4.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value5.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value6.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value7.GetHashCode());
            Assert.Equal(value4.GetHashCode(), value8.GetHashCode());
            Assert.NotEqual(value4.GetHashCode(), value9.GetHashCode());
        }
Пример #3
0
        private static bool TryReadQuality(StringSegment input, StringWithQualityHeaderValue result, ref int index)
        {
            var current = index;

            // See if we have a quality value by looking for "q"
            if ((current == input.Length) || ((input[current] != 'q') && (input[current] != 'Q')))
            {
                return(false);
            }

            current++; // skip 'q' identifier
            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            // If we found "q" it must be followed by "="
            if ((current == input.Length) || (input[current] != '='))
            {
                return(false);
            }

            current++; // skip '=' separator
            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            if (current == input.Length)
            {
                return(false);
            }

            if (!HeaderUtilities.TryParseQualityDouble(input, current, out var quality, out var qualityLength))
            {
                return(false);
            }

            result._quality = quality;

            current = current + qualityLength;
            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            index = current;
            return(true);
        }
        public void TryParseList_WithSomeInvalidValues_IgnoresInvalidValues()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text 1",
                "text2",
                "\"text 2\",",
                "text3;q=0.5",
                "text4;q=0.5, extra stuff",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.True(StringWithQualityHeaderValue.TryParseList(inputs, out results));

            var expectedResults = new[]
            {
                new StringWithQualityHeaderValue("text1"),
                new StringWithQualityHeaderValue("1"),
                new StringWithQualityHeaderValue("text2"),
                new StringWithQualityHeaderValue("text3", 0.5),
                new StringWithQualityHeaderValue("text4", 0.5),
                new StringWithQualityHeaderValue("stuff"),
                new StringWithQualityHeaderValue("text5", 0.5),
                new StringWithQualityHeaderValue("text6", 0.05),
                new StringWithQualityHeaderValue("text7"),
                new StringWithQualityHeaderValue("text8", 0.5),
                new StringWithQualityHeaderValue("text9"),
                new StringWithQualityHeaderValue("text10", 0.5),
            }.ToList();

            Assert.Equal(expectedResults, results);
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.False(value1.Equals(null), "t; q=0.123 vs. <null>");
            Assert.True(value1.Equals(value2), "t; q=0.123 vs. t; q=0.123");
            Assert.True(value1.Equals(value3), "t; q=0.123 vs. T; q=0.123");
            Assert.False(value1.Equals(value4), "t; q=0.123 vs. t");
            Assert.False(value4.Equals(value1), "t vs. t; q=0.123");
            Assert.False(value1.Equals(value5), "t; q=0.123 vs. x; q=0.123");
            Assert.False(value1.Equals(value6), "t; q=0.123 vs. t; q=0.5");
            Assert.False(value1.Equals(value7), "t; q=0.123 vs. t; q=0.1234");
            Assert.True(value4.Equals(value8), "t vs. T");
            Assert.False(value4.Equals(value9), "t vs. T");
        }
        public void TryParseStrictList_SetOfValidValueStrings_ParsedCorrectly()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text2,",
                "textA,textB",
                "text3;q=0.5",
                "text4;q=0.5,",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.True(StringWithQualityHeaderValue.TryParseStrictList(inputs, out results));

            var expectedResults = new[]
            {
                new StringWithQualityHeaderValue("text1"),
                new StringWithQualityHeaderValue("text2"),
                new StringWithQualityHeaderValue("textA"),
                new StringWithQualityHeaderValue("textB"),
                new StringWithQualityHeaderValue("text3", 0.5),
                new StringWithQualityHeaderValue("text4", 0.5),
                new StringWithQualityHeaderValue("text5", 0.5),
                new StringWithQualityHeaderValue("text6", 0.05),
                new StringWithQualityHeaderValue("text7"),
                new StringWithQualityHeaderValue("text8", 0.5),
                new StringWithQualityHeaderValue("text9"),
                new StringWithQualityHeaderValue("text10", 0.5),
            }.ToList();

            Assert.Equal(expectedResults, results);
        }
        private void CheckValidParse(string input, StringWithQualityHeaderValue expectedResult)
        {
            var result = StringWithQualityHeaderValue.Parse(input);

            Assert.Equal(expectedResult, result);
        }
 public void Parse_SetOfInvalidValueStrings_Throws(string input)
 {
     Assert.Throws <FormatException>(() => StringWithQualityHeaderValue.Parse(input));
 }
Пример #9
0
        private static int GetStringWithQualityLength(StringSegment input, int startIndex, out StringWithQualityHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Parse the value string: <value> in '<value>; q=<quality>'
            var valueLength = ProtoRuleParser.GetTokenLength(input, startIndex);

            if (valueLength == 0)
            {
                return(0);
            }

            StringWithQualityHeaderValue result = new StringWithQualityHeaderValue();

            result._value = input.Subsegment(startIndex, valueLength);
            var current = startIndex + valueLength;

            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            if ((current == input.Length) || (input[current] != ';'))
            {
                parsedValue = result;
                return(current - startIndex); // we have a valid token, but no quality.
            }

            current++; // skip ';' separator
            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            // If we found a ';' separator, it must be followed by a quality information
            if (!TryReadQuality(input, result, ref current))
            {
                return(0);
            }

            parsedValue = result;
            return(current - startIndex);
        }
Пример #10
0
        public static bool TryParse(StringSegment input, out StringWithQualityHeaderValue parsedValue)
        {
            var index = 0;

            return(SingleValueParser.TryParseValue(input, ref index, out parsedValue));
        }