コード例 #1
0
        public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
        {
            NameValueWithParametersHeaderValue expected = new NameValueWithParametersHeaderValue("custom");
            CheckValidParsedValue("\r\n custom  ", 0, expected, 11);
            CheckValidParsedValue("custom", 0, expected, 6);
            CheckValidParsedValue(",, ,\r\n custom  , chunked", 0, expected, 17);

            // Note that even if the whole string is invalid, the first "Expect" value is valid. When the parser
            // gets called again using the result-index (9), then it fails: I.e. we have 1 valid "Expect" value
            // and an invalid one.
            CheckValidParsedValue("custom , 会", 0, expected, 9);

            // We don't have to test all possible input strings, since most of the pieces are handled by other parsers.
            // The purpose of this test is to verify that these other parsers are combined correctly to build a
            // transfer-coding parser.
            expected.Parameters.Add(new NameValueHeaderValue("name", "value"));
            CheckValidParsedValue("\r\n custom ;  name =   value ", 0, expected, 28);
            CheckValidParsedValue("  custom;name=value", 2, expected, 19);
            CheckValidParsedValue("  custom ; name=value", 2, expected, 21);

            CheckValidParsedValue(null, 0, null, 0);
            CheckValidParsedValue(string.Empty, 0, null, 0);
            CheckValidParsedValue("  ", 0, null, 2);
            CheckValidParsedValue("  ,,", 0, null, 4);
        }
コード例 #2
0
		protected NameValueWithParametersHeaderValue (NameValueWithParametersHeaderValue source)
			: base (source)
		{
			if (source.parameters != null) {
				foreach (var item in source.parameters)
					Parameters.Add (item);
			}
		}
コード例 #3
0
        public void ToString_WithAndWithoutParameters_SerializedCorrectly()
        {
            NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue("text", "token");
            Assert.Equal("text=token", nameValue.ToString());

            nameValue.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
            nameValue.Parameters.Add(new NameValueHeaderValue("param2", "value2"));
            Assert.Equal("text=token; param1=value1; param2=value2", nameValue.ToString());
        }
コード例 #4
0
        public void Parse_ValidValue_ReturnsNameValueWithParametersHeaderValue()
        {
            // This test verifies that Parse() correctly calls TryParse().
            HttpHeaderParser parser = GenericHeaderParser.MultipleValueNameValueWithParametersParser;
            int index = 2;

            NameValueWithParametersHeaderValue expected = new NameValueWithParametersHeaderValue("custom");
            expected.Parameters.Add(new NameValueHeaderValue("name", "value"));
            Assert.True(expected.Equals(parser.ParseValue("   custom ; name = value ", null, ref index)));
            Assert.Equal(25, index);
        }
コード例 #5
0
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
     : base(source)
 {
     if (source.parameters != null)
     {
         foreach (var item in source.parameters)
         {
             Parameters.Add(item);
         }
     }
 }
コード例 #6
0
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
     : base(source)
 {
     if (source._parameters != null)
     {
         foreach (var parameter in source._parameters)
         {
             this.Parameters.Add((NameValueHeaderValue)((ICloneable)parameter).Clone());
         }
     }
 }
コード例 #7
0
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
     : base(source)
 {
     if (source.parameters != null)
     {
         foreach (var parameter in source.parameters)
         {
             this.Parameters.Add((NameValueHeaderValue)((ICloneable)parameter).Clone());
         }
     }
 }
コード例 #8
0
        public void Ctor_CallBaseCtor_Success()
        {
            // Just make sure the base ctor gets called correctly. Validation of input parameters is done in the base
            // class.
            NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue("name");
            Assert.Equal("name", nameValue.Name);
            Assert.Null(nameValue.Value);

            nameValue = new NameValueWithParametersHeaderValue("name", "value");
            Assert.Equal("name", nameValue.Name);
            Assert.Equal("value", nameValue.Value);
        }
コード例 #9
0
        public static bool TryParse(string input, out NameValueWithParametersHeaderValue 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);
        }
コード例 #10
0
        internal static int GetNameValueWithParametersLength(string input, int startIndex, out object parsedValue)
        {
            Contract.Requires(input != null);
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

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

            NameValueHeaderValue nameValue = null;
            int nameValueLength            = NameValueHeaderValue.GetNameValueLength(input, startIndex,
                                                                                     nameValueCreator, out nameValue);

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

            int current = startIndex + nameValueLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            NameValueWithParametersHeaderValue nameValueWithParameters =
                nameValue as NameValueWithParametersHeaderValue;

            Contract.Assert(nameValueWithParameters != null);

            // So far we have a valid name/value pair. Check if we have also parameters for the name/value pair. If
            // yes, parse parameters. E.g. something like "name=value; param1=value1; param2=value2".
            if ((current < input.Length) && (input[current] == ';'))
            {
                current++; // skip delimiter.
                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
                                                                                  nameValueWithParameters.Parameters);

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

                parsedValue = nameValueWithParameters;
                return(current + parameterLength - startIndex);
            }

            // We have a name/value pair without parameters.
            parsedValue = nameValueWithParameters;
            return(current - startIndex);
        }
コード例 #11
0
        public static bool TryParse(string input, out NameValueWithParametersHeaderValue parsedValue)
        {
            int    index = 0;
            object output;

            parsedValue = null;

            if (GenericHeaderParser.SingleValueNameValueWithParametersParser.TryParseValue(input,
                                                                                           null, ref index, out output))
            {
                parsedValue = (NameValueWithParametersHeaderValue)output;
                return(true);
            }
            return(false);
        }
コード例 #12
0
        public static bool TryParse(string input, out NameValueWithParametersHeaderValue parsedValue)
        {
            List <NameValueHeaderValue> values;

            if (!ParseParameters(new Lexer(input), out values))
            {
                parsedValue = null;
                return(false);
            }

            parsedValue = new NameValueWithParametersHeaderValue(values[0]);
            values.RemoveAt(0);
            parsedValue.parameters = values;
            return(true);
        }
コード例 #13
0
        public void GetHashCode_ValuesUseDifferentValues_HashDiffersAccordingToRfc()
        {
            NameValueWithParametersHeaderValue nameValue1 = new NameValueWithParametersHeaderValue("text");
            NameValueWithParametersHeaderValue nameValue2 = new NameValueWithParametersHeaderValue("text");

            // NameValueWithParametersHeaderValue just calls methods of the base class. Just verify Parameters is used.
            Assert.Equal(nameValue1.GetHashCode(), nameValue2.GetHashCode());

            nameValue1.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
            nameValue2.Value = null;
            Assert.NotEqual(nameValue1.GetHashCode(), nameValue2.GetHashCode());

            nameValue2.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
            Assert.Equal(nameValue1.GetHashCode(), nameValue2.GetHashCode());
        }
        public void Equals()
        {
            var value = new NameValueWithParametersHeaderValue ("ab");
            Assert.AreEqual (value, new NameValueWithParametersHeaderValue ("ab"), "#1");
            Assert.AreEqual (value, new NameValueWithParametersHeaderValue ("AB"), "#2");
            Assert.AreNotEqual (value, new NameValueWithParametersHeaderValue ("AA"), "#3");

            var second = new NameValueWithParametersHeaderValue ("AB");
            second.Parameters.Add (new NameValueHeaderValue ("pv"));

            Assert.AreNotEqual (value, second, "#4");

            value.Parameters.Add (new NameValueHeaderValue ("pv"));
            Assert.AreEqual (value, second, "#5");
        }
コード例 #15
0
        public void Equals_ValuesUseDifferentValues_ValuesAreEqualOrDifferentAccordingToRfc()
        {
            NameValueWithParametersHeaderValue nameValue1 = new NameValueWithParametersHeaderValue("text", "value");
            NameValueWithParametersHeaderValue nameValue2 = new NameValueWithParametersHeaderValue("text", "value");
            NameValueHeaderValue nameValue3 = new NameValueHeaderValue("text", "value");

            // NameValueWithParametersHeaderValue just calls methods of the base class. Just verify Parameters is used.
            Assert.True(nameValue1.Equals(nameValue2), "No parameters.");
            Assert.False(nameValue1.Equals(null), "Compare to null.");
            Assert.False(nameValue1.Equals(nameValue3), "Compare to base class instance.");

            nameValue1.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
            Assert.False(nameValue1.Equals(nameValue2), "none vs. 1 parameter.");

            nameValue2.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
            Assert.True(nameValue1.Equals(nameValue2), "1 parameter vs. 1 parameter.");
        }
コード例 #16
0
        public override bool Equals(object obj)
        {
            bool result = base.Equals(obj);

            if (result)
            {
                NameValueWithParametersHeaderValue other = obj as NameValueWithParametersHeaderValue;

                if (other == null)
                {
                    return(false);
                }
                return(HeaderUtilities.AreEqualCollections(parameters, other.parameters));
            }

            return(false);
        }
コード例 #17
0
        static bool TryParseElement(Lexer lexer, out NameValueWithParametersHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

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

            parsedValue = new NameValueWithParametersHeaderValue()
            {
                Name = lexer.GetStringValue(t),
            };

            t = lexer.Scan();
            if (t == Token.Type.SeparatorEqual)
            {
                t = lexer.Scan();

                if (t == Token.Type.Token || t == Token.Type.QuotedString)
                {
                    parsedValue.value = lexer.GetStringValue(t);
                    t = lexer.Scan();
                }
                else
                {
                    return(false);
                }
            }

            if (t == Token.Type.SeparatorSemicolon)
            {
                List <NameValueHeaderValue> result;
                if (!TryParseParameters(lexer, out result, out t))
                {
                    return(false);
                }

                parsedValue.parameters = result;
            }

            return(true);
        }
コード例 #18
0
		public static bool TryParse (string input, out NameValueWithParametersHeaderValue parsedValue)
		{
			List<NameValueHeaderValue> values;
			if (!ParseParameters (new Lexer (input), out values)) {
				parsedValue = null;
				return false;
			}

			parsedValue = new NameValueWithParametersHeaderValue (values[0]);
			values.RemoveAt (0);
			parsedValue.parameters = values;
			return true;
		}
コード例 #19
0
 public void WhenExpectIsCalledThenRequestExpectIsSet()
 {
     var expect = new NameValueWithParametersHeaderValue("Foo", "bar");
     _builder.Expect(expect);
     Assert.AreEqual(expect, _request.Headers.Expect.First());
 }
コード例 #20
0
		public static bool TryParse (string input, out NameValueWithParametersHeaderValue parsedValue)
		{
			throw new NotImplementedException ();
		}
コード例 #21
0
 private void CheckValidParsedValue(string input, int startIndex,
     NameValueWithParametersHeaderValue expectedResult, int expectedIndex)
 {
     HttpHeaderParser parser = GenericHeaderParser.MultipleValueNameValueWithParametersParser;
     object result = null;
     Assert.True(parser.TryParseValue(input, null, ref startIndex, out result),
         string.Format("TryParse returned false. Input: '{0}', Index: {1}", input, startIndex));
     Assert.Equal(expectedIndex, startIndex);
     Assert.Equal(result, expectedResult);
 }
コード例 #22
0
 private static void CallGetNameValueWithParametersLength(string input, int startIndex, int expectedLength,
     out NameValueWithParametersHeaderValue result)
 {
     object temp = null;
     Assert.Equal(expectedLength, NameValueWithParametersHeaderValue.GetNameValueWithParametersLength(input,
         startIndex, out temp));
     result = temp as NameValueWithParametersHeaderValue;
 }
コード例 #23
0
 public void Ctor_NameNull_Throw()
 {
     Assert.Throws<ArgumentException>(() => { NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue(null); });
 }
コード例 #24
0
 private void CheckValidTryParse(string input, NameValueWithParametersHeaderValue expectedResult)
 {
     NameValueWithParametersHeaderValue result = null;
     Assert.True(NameValueWithParametersHeaderValue.TryParse(input, out result));
     Assert.Equal(expectedResult, result);
 }
 public void Properties_Invalid()
 {
     var value = new NameValueWithParametersHeaderValue ("s");
     try {
         value.Value = "   ";
         Assert.Fail ("#1");
     } catch (FormatException) {
     }
 }
コード例 #26
0
		static bool TryParseElement (Lexer lexer, out NameValueWithParametersHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

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

			parsedValue = new NameValueWithParametersHeaderValue () {
				Name = lexer.GetStringValue (t),
			};

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorEqual) {
				t = lexer.Scan ();

				if (t == Token.Type.Token || t == Token.Type.QuotedString) {
					parsedValue.value = lexer.GetStringValue (t);
					t = lexer.Scan ();
				} else {
					return false;
				}
			}

			if (t == Token.Type.SeparatorSemicolon) {
				List<NameValueHeaderValue> result;
				if (!TryParseParameters (lexer,  out result, out t))
					return false;

				parsedValue.parameters = result;
			}

			return true;
		}
コード例 #27
0
		public static bool TryParse (string input, out NameValueWithParametersHeaderValue 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;
		}
コード例 #28
0
 public void Clone_Call_CloneFieldsMatchSourceFields()
 {
     NameValueWithParametersHeaderValue source = new NameValueWithParametersHeaderValue("name", "value");
     source.Parameters.Add(new NameValueHeaderValue("param1", "value1"));
     NameValueWithParametersHeaderValue clone = (NameValueWithParametersHeaderValue)((ICloneable)source).Clone();
     Assert.Equal(source.Name, clone.Name);
     Assert.Equal(source.Value, clone.Value);
     Assert.Equal(1, clone.Parameters.Count);
     Assert.Equal("param1", clone.Parameters.First().Name);
     Assert.Equal("value1", clone.Parameters.First().Value);
 }
コード例 #29
0
        public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
        {
            NameValueWithParametersHeaderValue expected = new NameValueWithParametersHeaderValue("custom");
            CheckValidTryParse("\r\n custom  ", expected);
            CheckValidTryParse("custom", expected);

            // We don't have to test all possible input strings, since most of the pieces are handled by other parsers.
            // The purpose of this test is to verify that these other parsers are combined correctly to build a
            // transfer-coding parser.
            expected.Parameters.Add(new NameValueHeaderValue("name", "value"));
            CheckValidTryParse("\r\n custom ;  name =   value ", expected);
            CheckValidTryParse("  custom;name=value", expected);
            CheckValidTryParse("  custom ; name=value", expected);
        }
コード例 #30
0
 public static bool TryParse(string input, out NameValueWithParametersHeaderValue parsedValue)
 {
 }
コード例 #31
0
 public void Ctor_NameEmpty_Throw()
 {
     // null and empty should be treated the same. So we also throw for empty strings.
     Assert.Throws<ArgumentException>(() => { NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue(string.Empty); });
 }
コード例 #32
0
 public void Parameters_AddNull_Throw()
 {
     NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue("name");
     
     Assert.Throws<ArgumentNullException>(() => { nameValue.Parameters.Add(null); });
 }
コード例 #33
0
        public static bool TryParse(string input, out NameValueWithParametersHeaderValue parsedValue)
        {
            int index = 0;
            object output;
            parsedValue = null;

            if (GenericHeaderParser.SingleValueNameValueWithParametersParser.TryParseValue(input,
                null, ref index, out output))
            {
                parsedValue = (NameValueWithParametersHeaderValue)output;
                return true;
            }
            return false;
        }
コード例 #34
0
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
     : base(source)
 {
     _parameters = source._parameters.Clone();
 }
        public void Properties()
        {
            var value = new NameValueWithParametersHeaderValue ("s", "p");
            Assert.AreEqual ("s", value.Name, "#1");
            Assert.AreEqual ("p", value.Value, "#2");

            value = new NameValueWithParametersHeaderValue ("s");
            Assert.AreEqual ("s", value.Name, "#3");
            Assert.IsNull (value.Value, "#4");

            value.Value = "bb";
            Assert.AreEqual ("bb", value.Value, "#5");
        }
コード例 #36
0
 private void CheckValidParse(string input, NameValueWithParametersHeaderValue expectedResult)
 {
     NameValueWithParametersHeaderValue result = NameValueWithParametersHeaderValue.Parse(input);
     Assert.Equal(expectedResult, result);
 }
コード例 #37
0
        public void Expect_UseAddMethod_AddedValueCanBeRetrievedUsingProperty()
        {
            headers.TryAddWithoutValidation("Expect",
                ", , 100-continue, name1 = value1, name2; param2=paramValue2, name3=value3; param3 ,");

            // Connection collection has 3 values plus '100-continue'
            Assert.Equal(4, headers.Expect.Count);
            Assert.Equal(4, headers.GetValues("Expect").Count());
            Assert.True(headers.ExpectContinue == true, "ExpectContinue expected to be true.");

            Assert.Equal(new NameValueWithParametersHeaderValue("100-continue"),
                headers.Expect.ElementAt(0));

            Assert.Equal(new NameValueWithParametersHeaderValue("name1", "value1"),
                headers.Expect.ElementAt(1));

            NameValueWithParametersHeaderValue expected2 = new NameValueWithParametersHeaderValue("name2");
            expected2.Parameters.Add(new NameValueHeaderValue("param2", "paramValue2"));
            Assert.Equal(expected2, headers.Expect.ElementAt(2));

            NameValueWithParametersHeaderValue expected3 = new NameValueWithParametersHeaderValue("name3", "value3");
            expected3.Parameters.Add(new NameValueHeaderValue("param3"));
            Assert.Equal(expected3, headers.Expect.ElementAt(3));

            headers.Expect.Clear();
            Assert.Null(headers.ExpectContinue);
            Assert.Equal(0, headers.Expect.Count);
            IEnumerable<string> dummyArray;
            Assert.False(headers.TryGetValues("Expect", out dummyArray), "Expect header count after Expect.Clear().");
        }
コード例 #38
0
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
 {
 }