private StringWithQualityHeaderValue(StringWithQualityHeaderValue source)
        {
            Debug.Assert(source != null);

            _value = source._value;
            _quality = source._quality;
        }
        private StringWithQualityHeaderValue(StringWithQualityHeaderValue source)
        {
            Contract.Requires(source != null);

            _value = source._value;
            _quality = source._quality;
        }
		public static bool TryParse (string input, out StringWithQualityHeaderValue parsedValue)
		{
			var lexer = new Lexer (input);
			Token token;
			if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
				return true;

			parsedValue = null;
			return false;
		}
        public void Ctor_StringOnlyOverload_MatchExpectation()
        {
            StringWithQualityHeaderValue value = new StringWithQualityHeaderValue("token");
            Assert.Equal("token", value.Value);
            Assert.Null(value.Quality);

            Assert.Throws<ArgumentException>(() => { new StringWithQualityHeaderValue(null); });
            Assert.Throws<ArgumentException>(() => { new StringWithQualityHeaderValue(""); });
            Assert.Throws<FormatException>(() => { new StringWithQualityHeaderValue("in valid"); });
        }
 private void CheckValidParsedValue(string input, int startIndex, StringWithQualityHeaderValue expectedResult,
     int expectedIndex)
 {
     HttpHeaderParser parser = GenericHeaderParser.MultipleValueStringWithQualityParser;
     object result = null;
     Assert.True(parser.TryParseValue(input, null, ref startIndex, out result),
         string.Format("TryParse returned false: {0}", input));
     Assert.Equal(expectedIndex, startIndex);
     Assert.Equal(result, expectedResult);
 }
        public void Equals()
        {
            var value = new StringWithQualityHeaderValue ("ab");
            Assert.AreEqual (value, new StringWithQualityHeaderValue ("ab"), "#1");
            Assert.AreEqual (value, new StringWithQualityHeaderValue ("AB"), "#2");
            Assert.AreNotEqual (value, new StringWithQualityHeaderValue ("AA"), "#3");
            Assert.AreEqual ("ab", value.ToString (), "#33");

            value = new StringWithQualityHeaderValue ("ab", 1);
            Assert.AreEqual (value, new StringWithQualityHeaderValue ("Ab", 1), "#4");
            Assert.AreNotEqual (value, new StringWithQualityHeaderValue ("AB", 0), "#5");
            Assert.AreNotEqual (value, new StringWithQualityHeaderValue ("AA", 1), "#6");
        }
        public void Ctor_StringWithQualityOverload_MatchExpectation()
        {
            StringWithQualityHeaderValue value = new StringWithQualityHeaderValue("token", 0.5);
            Assert.Equal("token", value.Value);
            Assert.Equal(0.5, value.Quality);

            Assert.Throws<ArgumentException>(() => { new StringWithQualityHeaderValue(null, 0.1); });
            Assert.Throws<ArgumentException>(() => { new StringWithQualityHeaderValue("", 0.1); });
            Assert.Throws<FormatException>(() => { new StringWithQualityHeaderValue("in valid", 0.1); });

            Assert.Throws<ArgumentOutOfRangeException>(() => { new StringWithQualityHeaderValue("t", 1.1); });
            Assert.Throws<ArgumentOutOfRangeException>(() => { new StringWithQualityHeaderValue("t", -0.1); });
        }
		public static bool TryParse (string input, out StringWithQualityHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new StringWithQualityHeaderValue ();
			value.Value = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSemicolon) {
				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				var s = lexer.GetStringValue (t);
				if (s != "q" && s != "Q")
					return false;

				t = lexer.Scan ();
				if (t != Token.Type.SeparatorEqual)
					return false;

				t = lexer.Scan ();

				double d;
				if (!lexer.TryGetDoubleValue (t, out d))
					return false;

				if (d > 1)
					return false;

				value.Quality = d;

				t = lexer.Scan ();
			}

			if (t != Token.Type.End)
				return false;

			parsedValue = value;
			return true;
		}
        public void ToString_UseDifferentValues_AllSerializedCorrectly()
        {
            StringWithQualityHeaderValue value = new StringWithQualityHeaderValue("token");
            Assert.Equal("token", value.ToString());

            value = new StringWithQualityHeaderValue("token", 0.1);
            Assert.Equal("token; q=0.1", value.ToString());

            value = new StringWithQualityHeaderValue("token", 0);
            Assert.Equal("token; q=0.0", value.ToString());

            value = new StringWithQualityHeaderValue("token", 1);
            Assert.Equal("token; q=1.0", value.ToString());

            // Note that the quality value gets rounded
            value = new StringWithQualityHeaderValue("token", 0.56789);
            Assert.Equal("token; q=0.568", value.ToString());
        }
        public void GetHashCode_UseSameAndDifferentValues_SameOrDifferentHashCodes()
        {
            StringWithQualityHeaderValue value1 = new StringWithQualityHeaderValue("t", 0.123);
            StringWithQualityHeaderValue value2 = new StringWithQualityHeaderValue("t", 0.123);
            StringWithQualityHeaderValue value3 = new StringWithQualityHeaderValue("T", 0.123);
            StringWithQualityHeaderValue value4 = new StringWithQualityHeaderValue("t");
            StringWithQualityHeaderValue value5 = new StringWithQualityHeaderValue("x", 0.123);
            StringWithQualityHeaderValue value6 = new StringWithQualityHeaderValue("t", 0.5);
            StringWithQualityHeaderValue value7 = new StringWithQualityHeaderValue("t", 0.1234);
            StringWithQualityHeaderValue value8 = new StringWithQualityHeaderValue("T");
            StringWithQualityHeaderValue 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());
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            StringWithQualityHeaderValue value1 = new StringWithQualityHeaderValue("t", 0.123);
            StringWithQualityHeaderValue value2 = new StringWithQualityHeaderValue("t", 0.123);
            StringWithQualityHeaderValue value3 = new StringWithQualityHeaderValue("T", 0.123);
            StringWithQualityHeaderValue value4 = new StringWithQualityHeaderValue("t");
            StringWithQualityHeaderValue value5 = new StringWithQualityHeaderValue("x", 0.123);
            StringWithQualityHeaderValue value6 = new StringWithQualityHeaderValue("t", 0.5);
            StringWithQualityHeaderValue value7 = new StringWithQualityHeaderValue("t", 0.1234);
            StringWithQualityHeaderValue value8 = new StringWithQualityHeaderValue("T");
            StringWithQualityHeaderValue 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 static bool TryParse(string input, out StringWithQualityHeaderValue parsedValue)
 {
 }
Exemple #13
0
        public static bool TryParse(string input, out StringWithQualityHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new StringWithQualityHeaderValue();

            value.Value = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.SeparatorSemicolon)
            {
                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }

                var s = lexer.GetStringValue(t);
                if (s != "q" && s != "Q")
                {
                    return(false);
                }

                t = lexer.Scan();
                if (t != Token.Type.SeparatorEqual)
                {
                    return(false);
                }

                t = lexer.Scan();

                double d;
                if (!lexer.TryGetDoubleValue(t, out d))
                {
                    return(false);
                }

                if (d > 1)
                {
                    return(false);
                }

                value.Quality = d;

                t = lexer.Scan();
            }

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;
            return(true);
        }
 private static void CallGetStringWithQualityLength(string input, int startIndex, int expectedLength,
     out StringWithQualityHeaderValue result)
 {
     object temp = null;
     Assert.Equal(expectedLength, StringWithQualityHeaderValue.GetStringWithQualityLength(input,
         startIndex, out temp));
     result = temp as StringWithQualityHeaderValue;
 }
        public void Properties()
        {
            var value = new StringWithQualityHeaderValue ("s", 1);
            Assert.AreEqual ("s", value.Value, "#1");
            Assert.AreEqual (1, value.Quality, "#2");

            value = new StringWithQualityHeaderValue ("ss");
            Assert.AreEqual ("ss", value.Value, "#3");
            Assert.IsNull (value.Quality, "#4");
        }
 public void Ctor()
 {
     var a = new StringWithQualityHeaderValue ("s", 0.123456);
     Assert.AreEqual ("s; q=0.123", a.ToString ());
 }
 private void CheckValidTryParse(string input, StringWithQualityHeaderValue expectedResult)
 {
     StringWithQualityHeaderValue result = null;
     Assert.True(StringWithQualityHeaderValue.TryParse(input, out result));
     Assert.Equal(expectedResult, result);
 }
		static bool TryParseElement (Lexer lexer, out StringWithQualityHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;
			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new StringWithQualityHeaderValue ();
			value.Value = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSemicolon) {
				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				var s = lexer.GetStringValue (t);
				if (s != "q" && s != "Q")
					return false;

				t = lexer.Scan ();
				if (t != Token.Type.SeparatorEqual)
					return false;

				t = lexer.Scan ();

				double d;
				if (!lexer.TryGetDoubleValue (t, out d))
					return false;

				if (d > 1)
					return false;

				value.Quality = d;

				t = lexer.Scan ();
			}

			parsedValue = value;
			return true;
		}
 private void CheckValidParse(string input, StringWithQualityHeaderValue expectedResult)
 {
     StringWithQualityHeaderValue result = StringWithQualityHeaderValue.Parse(input);
     Assert.Equal(expectedResult, result);
 }
 public void WhenAcceptLanguageIsCalledThenRequestAcceptLanguageIsSet()
 {
     var language = new StringWithQualityHeaderValue("en");
     _builder.AcceptLanguage(language);
     Assert.AreEqual(language, _request.Headers.AcceptLanguage.First());
 }
        public void Clone_Call_CloneFieldsMatchSourceFields()
        {
            StringWithQualityHeaderValue source = new StringWithQualityHeaderValue("token", 0.123);
            StringWithQualityHeaderValue clone = (StringWithQualityHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.Value, clone.Value);
            Assert.Equal(source.Quality, clone.Quality);

            source = new StringWithQualityHeaderValue("token");
            clone = (StringWithQualityHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.Value, clone.Value);
            Assert.Null(source.Quality);
        }
 public void WhenAcceptEncodingIsCalledThenRequestAcceptEncodingIsSet()
 {
     var encoding = new StringWithQualityHeaderValue("gzip");
     _builder.AcceptEncoding(encoding);
     Assert.AreEqual(encoding, _request.Headers.AcceptEncoding.First());
 }
 public void WhenAcceptCharsetIsCalledThenRequestAcceptCharsetIsSet()
 {
     var charset = new StringWithQualityHeaderValue("unicode-1-1");
     _builder.AcceptCharset(charset);
     Assert.AreEqual(charset, _request.Headers.AcceptCharset.First());
 }
        public static bool TryParse(string input, out StringWithQualityHeaderValue parsedValue)
        {
            int index = 0;
            object output;
            parsedValue = null;

            if (GenericHeaderParser.SingleValueStringWithQualityParser.TryParseValue(
                input, null, ref index, out output))
            {
                parsedValue = (StringWithQualityHeaderValue)output;
                return true;
            }
            return false;
        }
        internal static int GetStringWithQualityLength(string input, int startIndex, out object parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

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

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

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

            StringWithQualityHeaderValue result = new StringWithQualityHeaderValue();
            result._value = input.Substring(startIndex, valueLength);
            int current = startIndex + valueLength;
            current = current + HttpRuleParser.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 + HttpRuleParser.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;
        }
        private static bool TryReadQuality(string input, StringWithQualityHeaderValue result, ref int index)
        {
            int 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 + HttpRuleParser.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 + HttpRuleParser.GetWhitespaceLength(input, current);

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

            int qualityLength = HttpRuleParser.GetNumberLength(input, current, true);

            if (qualityLength == 0)
            {
                return false;
            }

            double quality = 0;
            if (!double.TryParse(input.Substring(current, qualityLength), NumberStyles.AllowDecimalPoint,
                NumberFormatInfo.InvariantInfo, out quality))
            {
                return false;
            }

            if ((quality < 0) || (quality > 1))
            {
                return false;
            }

            result._quality = quality;

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

            index = current;
            return true;
        }