Example #1
0
        public void Validate(NameValueCollection parameters)
        {
            ////appid验证
            string appid = parameters.ValidateRequired(TopClient.API_KEY);
            if (appid != APPID)
            {
                throw new TopException(102, TopClient.API_KEY + " error");
            }

            //允许客户端请求时间误差为6分钟。
            ulong timestamp = ulong.Parse(parameters.ValidateRequired(TopClient.TIMESTAMP));
            DateTime dt = TopUtils.UnixTimeStampToDateTime(timestamp);
            double totalMinutes = (DateTime.Now - dt).TotalMinutes;

            if (Math.Abs(totalMinutes) > 6)
            {
                throw new TopException(102, TopClient.TIMESTAMP + " error");
            }
            
            //签名验证
            string psign = parameters.ValidateRequired(TopClient.SIGN);
            var dic = parameters.ToDictionary();
            dic.Remove(TopClient.SIGN);
            string sign = TopUtils.SignTopRequest(dic, APPSECRET);

            if (!string.Equals(sign, psign, StringComparison.OrdinalIgnoreCase))
            {
                throw new TopException(102, TopClient.SIGN + " error");
            }
        }
Example #2
0
        public static void Returns_an_empty_dictionary_for_an_empty_name_value_collection()
        {
            var emptyCollection = new NameValueCollection();

            Dictionary<string, string> dictionary = emptyCollection.ToDictionary();

            dictionary.Should().BeEmpty();
        }
Example #3
0
 public static string BuildErrorJson(string type_name, string err_msg, NameValueCollection col)
 {
     var item = new
     {
         api_name = type_name,
         err_msg = err_msg,
         query = col.ToDictionary().ToJson(),
     };
     return item.ToJson();
 }
        public void ToDictionary()
        {
            // Type
            var @this = new NameValueCollection {{"Fizz", "Buzz"}};

            // Exemples
            IDictionary<string, object> result = @this.ToDictionary(); // return a Dictionary;

            // Unit Test
            Assert.AreEqual("Buzz", result["Fizz"]);
        }
        public void TestExtension_NameValueCollectionToDictionaryTest()
        {
            var nvc = new NameValueCollection()
            {
                { "key1", "val1" },
                { "key2", "val2" },
                { "key3", "val3" },
                { "key4", "val4" },
                { "key5", "val5" },
            };

            var dict = nvc.ToDictionary();
            Assert.IsTrue(dict.ContainsKey("key3"));
        }
        public void ToDictionary_can_convert_to_dictionary_from_NameValueCollection()
        {
            var nameValue = new NameValueCollection
            {
                {"KEY1", "Value1" },
                {"Key2", "VALUE2" },
                {"kEy3", "vALuE3" }
            };

            var dic = nameValue.ToDictionary();

            Assert.AreEqual("value1", dic["key1"]);
            Assert.AreEqual("value2", dic["key2"]);
            Assert.AreEqual("value3", dic["key3"]);
        }
Example #7
0
        public static void Returned_dictionary_contains_exactly_the_elements_from_the_name_value_collection()
        {
            var collection = new NameValueCollection
            {
                { "a", "1" },
                { "b", "2" },
                { "c", "3" }
            };

            Dictionary<string, string> dictionary = collection.ToDictionary();

            dictionary.Should().Equal(new Dictionary<string, string>
            {
                { "a", "1" },
                { "b", "2" },
                { "c", "3" }
            });
        }
Example #8
0
        public FacebookInstance GetInstance(FacebookConfigElement elem, NameValueCollection paramCollection)
        {
            string signed_request = paramCollection["signed_request"].Default().DecodeUrl();
            FacebookInstance instance = null;

            string json = null;
            if (!signed_request.isEmpty())
            {
                #region check the signature of the signed request
                string hash = signed_request.Part('.', 0).Decode64();
                json = signed_request.Part('.', 1).Decode64();

                string evidence = signed_request.Part('.', 1).HashForKey(elem.appSecret);
                if (hash.NotEquals(evidence))
                    throw new FormatException("Invalid Signature");
                #endregion
            }
            instance = new FacebookInstance(elem, json);
            instance.HttpParameters = paramCollection.ToDictionary();
            return instance;
        }
		public void ToDictionaryWithSkippedNullKey() {
			NameValueCollection nvc = new NameValueCollection();
			nvc[null] = "a";
			nvc["b"] = "c";
			var dictionary = nvc.ToDictionary(false);
			Assert.AreEqual(1, dictionary.Count);
			Assert.AreEqual(nvc["b"], dictionary["b"]);
		}
		public void ToDictionaryWithNullKey() {
			NameValueCollection nvc = new NameValueCollection();
			nvc[null] = "a";
			nvc["b"] = "c";
			nvc.ToDictionary(true);
		}
Example #11
0
 public static EntityRecord CreateRecord(
     this Entity entity,
     NameValueCollection request,
     string prefix = "",
     Func<object, object> valueMutator = null)
 {
     return entity.CreateRecord(
         request.ToDictionary().ToDictionary(x => x.Key, x => (object)x.Value),
         prefix,
         valueMutator);
 }
 public void ToDictionary_NameValueCollection_CaseInsensitiveKeys()
 {
     NameValueCollection nameValueCollection = new NameValueCollection();
     // Add the values using mixed case and upper case keys even though NameValueCollections are case insensitive
     nameValueCollection.Set("Keyname", Random.Next().ToString(CultureInfo.InvariantCulture));
     nameValueCollection.Set("KEYNAME", Random.Next().ToString(CultureInfo.InvariantCulture));
     Dictionary<string, string> dict = nameValueCollection.ToDictionary();
     Assert.IsTrue(
         dict.ContainsKey("keyname"),
         "After converting a NameValueCollection with ToDictionary, the keys should be case insensitive.");
     Assert.AreEqual(
         nameValueCollection.Get("keyname"),
         dict["keyname"],
         "After converting a nameValueCollection with ToDictionary, the keys should be case insensitive.");
     Assert.IsTrue(
         dict.ContainsKey("Keyname"),
         "After converting a NameValueCollection with ToDictionary, the keys should be case insensitive.");
     Assert.IsTrue(
         dict.ContainsKey("KEYNAME"),
         "After converting a NameValueCollection with ToDictionary, the keys should be case insensitive.");
 }
 public void ToDictionary_NameValueCollection_ValuesMatch()
 {
     NameValueCollection nameValueCollection = new NameValueCollection();
     int count = Random.Next(5, 10);
     for (int i = 0; i < count; i++)
         nameValueCollection.Set(
             String.Format("{1} {0}", i, Random.Next()),
             Random.Next().ToString(CultureInfo.InvariantCulture));
     Dictionary<string, string> dict = nameValueCollection.ToDictionary();
     foreach (string key in nameValueCollection)
         Assert.AreEqual(
             nameValueCollection.Get(key),
             dict[key],
             "After converting a nameValueCollection with ToDictionary, the values of each key should be preserved.");
 }
 public void ToDictionary_NameValueCollection_HasSameItemCount()
 {
     NameValueCollection nameValueCollection = new NameValueCollection();
     int count = Random.Next(1, 20);
     for (int i = 0; i < count; i++)
         nameValueCollection.Set(
             String.Format("{1} {0}", i, Random.Next()),
             Random.Next().ToString(CultureInfo.InvariantCulture));
     Dictionary<string, string> dict = nameValueCollection.ToDictionary();
     Assert.AreEqual(
         nameValueCollection.Count,
         dict.Count,
         "Converting a nameValueCollection with ToDictionary should preserve item count");
 }
 public void ToDictionary_EmptyNameValueCollection_GivesEmptyDict()
 {
     NameValueCollection nameValueCollection = new NameValueCollection();
     Dictionary<string, string> dict = nameValueCollection.ToDictionary();
     Assert.AreEqual(
         0,
         dict.Count,
         "Converting an empty nameValueCollection with ToDictionary should result in an empty dictionary");
 }
 public void CollectionIsEmpty()
 {
     NameValueCollection nullCollection = new NameValueCollection();
     Dictionary<string, string> dict = nullCollection.ToDictionary();
     Assert.AreEqual(0, dict.Count);
 }