public void ConvertToNameValueCollection_EmptyDic_EmptyCollection()
        {
            Dictionary<int, string> d = new Dictionary<int, string>();

            NameValueCollection c = d.ToNameValueCollection();

            Assert.Empty(c);
        }
        public void ConvertToNameValue_SomeRefTypeAsValue_ToStringUsedToConvertTheValue()
        {
            Dictionary<int, MyType> d = new Dictionary<int, MyType>
                {
                    {1, new MyType("Some value")}
                };

            NameValueCollection c = d.ToNameValueCollection();

            Assert.Equal("Some value", c[0]);
        }
        public void ConvertToNameValue_ConvertFunction_ConvertFunctionUsedToConvertTheValue()
        {
            Dictionary<int, MyType> d = new Dictionary<int, MyType>
                {
                    {1, new MyType("Some value")}
                };

            NameValueCollection c = d.ToNameValueCollection(type => "converted value");

            Assert.Equal("converted value", c[0]);
        }
        public void ConvertToNameValueCollection_NullForValue_ConvertToEmptyString()
        {
            Dictionary<int, string> d = new Dictionary<int, string>
                {
                    {1, null}
                };

            NameValueCollection c = d.ToNameValueCollection();

            Assert.Equal(string.Empty, c[0]);
        }
        public void ConvertToNameValueCollection_NotStringForKey_ToStringForTKeyUsed()
        {
            Dictionary<int, string> d = new Dictionary<int, string>
                {
                    {1, "Value1"}
                };

            NameValueCollection c = d.ToNameValueCollection();

            Assert.Equal("Value1", c["1"]);
        }
        public void ToNameValueCollection()
        {
            // Type
            var @this = new Dictionary<string, string> {{"Fizz", "Buzz"}};

            // Exemples
            NameValueCollection result = @this.ToNameValueCollection(); // return a NameValueCollection

            // Unit Test
            Assert.AreEqual("Buzz", result["Fizz"]);
        }
        public void ConvertToNameValueCollection_MoreElements_SameOrderInCollection()
        {
            Dictionary<int, string> d = new Dictionary<int, string>
                {
                    {1, "Value1"},
                    {2, "Value2"}
                };

            NameValueCollection c = d.ToNameValueCollection();

            Assert.Equal("Value1", c[0]);
            Assert.Equal("Value2", c[1]);
        }
    public void ToNameValueCollection_creates_equivalent_collection()
    {
      Dictionary<string,string> test = new Dictionary<string, string>();
      test.Add("foo", "bar");
      test.Add("spong", "wibble");

      NameValueCollection output = test.ToNameValueCollection();

      Assert.IsNotNull(output, "Null");
      Assert.AreEqual(2, output.Count, "Count");
      Assert.AreEqual("bar", output["foo"], "foo");
      Assert.AreEqual("wibble", output["spong"], "spong");
    }
        public void TestExtentsion_DictionaryToNameValueCollection()
        {
            var dict = new Dictionary<string,string>()
            {
                { "key1", "val1" },
                { "key2", "val2" },
                { "key3", "val3" },
                { "key4", "val4" },
                { "key5", "val5" },
            };

            var nvc = dict.ToNameValueCollection();
            Assert.IsTrue((nvc["key3"] == "val3"));
        }
        public string Get(string url, Dictionary<string, string> headers)
        {
            var req = (HttpWebRequest)WebRequest.Create(url);
            req.Headers.Add(headers.ToNameValueCollection());

            Debug.WriteLine(url);

            using(var response = (HttpWebResponse)req.GetResponse())
            using(var responseStream = response.GetResponseStream())
            using(var sr = new StreamReader(responseStream))
            {
                return sr.ReadToEnd().Trim();
            }
        }
 public void Test_DictionaryExtensions_ToNameValueCollection()
 {
     var dict = new Dictionary<string, string> { { "name", "value" } };
     var coll = dict.ToNameValueCollection();
     Assert.AreEqual(dict.Count, coll.Count);
     foreach (KeyValuePair<string, string> pair in dict)
     {
         bool keyExists = false;
         for (int i = 0; i < coll.Keys.Count; i++)
         {
             if (coll.Keys[i] == pair.Key)
             {
                 keyExists = true;
                 break;
             }
         }
         Assert.IsTrue(keyExists);
         Assert.AreEqual(pair.Value, coll[pair.Key]);
     }
 }
        private NameValueCollection ParseHeader(string headerLines) {
            var items = new Dictionary<string, string>();
            var parser = new EslStringReader(headerLines);
            string name = parser.Read(':', true);
            while (!parser.EOF
                   && name != string.Empty) {
                string value = parser.ReadLine(true);
                if (!items.ContainsKey(name.Trim())) {
                    try { items.Add(name.Trim(), Uri.UnescapeDataString(value.Trim())); }
                    catch (UriFormatException) {
                        // add the value unformatted
                        items.Add(name.Trim(), value.Trim());
                    }
                }

                name = parser.Read(':', true);
            }

            return items.ToNameValueCollection();
        }
Example #13
0
		public void ReadFromRequestAuthorizationScattered() {
			// Start by creating a standard POST HTTP request.
			var postedFields = new Dictionary<string, string> {
				{ "age", "15" },
			};

			// Now add another field to the request URL
			var builder = new UriBuilder(MessagingTestBase.DefaultUrlForHttpRequestInfo);
			builder.Query = "Name=Andrew";

			// Finally, add an Authorization header
			var authHeaderFields = new Dictionary<string, string> {
				{ "Location", "http://hostb/pathB" },
				{ "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
			};
			var headers = new NameValueCollection();
			headers.Add(HttpRequestHeaders.Authorization, CreateAuthorizationHeader(authHeaderFields));
			headers.Add(HttpRequestHeaders.ContentType, Channel.HttpFormUrlEncoded);

			var requestInfo = new HttpRequestInfo("POST", builder.Uri, form: postedFields.ToNameValueCollection(), headers: headers);

			IDirectedProtocolMessage requestMessage = this.channel.ReadFromRequest(requestInfo);

			Assert.IsNotNull(requestMessage);
			Assert.IsInstanceOf<TestMessage>(requestMessage);
			TestMessage testMessage = (TestMessage)requestMessage;
			Assert.AreEqual(15, testMessage.Age);
			Assert.AreEqual("Andrew", testMessage.Name);
			Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
		}
        private NameValueCollection ParseBody(string bodyLines) {
            var items = new Dictionary<string, string>();
            var parser = new EslStringReader(bodyLines);
            string name = parser.Read(':', true);
            while (!parser.EOF
                   && !string.IsNullOrEmpty(name)) {
                string value = parser.ReadLine(true);
                if (!items.ContainsKey(name.Trim())) {
                    try { items.Add(name.Trim(), Uri.UnescapeDataString(value.Trim())); }
                    catch (UriFormatException) {
                        // add the value unformatted
                        items.Add(name.Trim(), value.Trim());
                    }
                }
                name = parser.Read(':', true);
            }

            var extraContents = new List<string>();
            while (!parser.EOF) {
                string line = parser.ReadLine(true);
                if (!string.IsNullOrEmpty(line)) extraContents.Add(line.Trim());
            }

            if (extraContents.Count <= 0) return items.ToNameValueCollection();
            if (!items.ContainsKey("__CONTENT__")) items.Add("__CONTENT__", String.Join("|", extraContents));
            return items.ToNameValueCollection();
        }