コード例 #1
0
        public void ContainsPrefix_GuardClauses()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(() => valueProvider.ContainsPrefix(null), "prefix");
        }
コード例 #2
0
        public void GetValue_GuardClauses()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(() => valueProvider.GetValue(null), "key");
        }
コード例 #3
0
        public void ContainsPrefix_WithNonEmptyCollection_ReturnsTrueForKnownPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & Assert
            Assert.True(valueProvider.ContainsPrefix("foo"));
            Assert.True(valueProvider.ContainsPrefix("bar"));
            Assert.True(valueProvider.ContainsPrefix("bar.baz"));
        }
コード例 #4
0
        public void ContainsPrefix_GuardClauses()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(
                () => valueProvider.ContainsPrefix(null),
                "prefix");
        }
コード例 #5
0
        public void ContainsPrefix_WithNonEmptyCollection_ReturnsTrueForKnownPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & Assert
            Assert.True(valueProvider.ContainsPrefix("foo"));
            Assert.True(valueProvider.ContainsPrefix("bar"));
            Assert.True(valueProvider.ContainsPrefix("bar.baz"));
        }
コード例 #6
0
        public void ContainsPrefix_WithNonEmptyCollection_ReturnsTrueForEmptyPrefix()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.True(result);
        }
コード例 #7
0
        public void ContainsPrefix_WithEmptyCollection_ReturnsFalseForEmptyPrefix()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(Enumerable.Empty<KeyValuePair<string, string>>(), null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.False(result);
        }
コード例 #8
0
        public void GetKeysFromPrefix_UnknownPrefix_ReturnsEmptyDictionary()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary <string, string> result = valueProvider.GetKeysFromPrefix("abc");

            // Assert
            Assert.Empty(result);
        }
コード例 #9
0
        public void GetValue_ReturnsNullIfKeyNotFound()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar");

            // Assert
            Assert.Null(vpResult);
        }
コード例 #10
0
        public void ContainsPrefix_WithEmptyCollection_ReturnsFalseForEmptyPrefix()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(Enumerable.Empty <KeyValuePair <string, string> >(), null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.False(result);
        }
コード例 #11
0
        public void ContainsPrefix_WithNonEmptyCollection_ReturnsFalseForUnknownPrefix()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            bool result = valueProvider.ContainsPrefix("biff");

            // Assert
            Assert.False(result);
        }
コード例 #12
0
        public void GetKeysFromPrefix_EmptyPrefix_ReturnsAllPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary <string, string> result = valueProvider.GetKeysFromPrefix("");

            // Assert
            Assert.Equal(2, result.Count);
            Assert.Equal("foo", result["foo"]);
            Assert.Equal("bar", result["bar"]);
        }
コード例 #13
0
        public void GetKeysFromPrefix_KnownPrefix_ReturnsMatchingItems()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary <string, string> result = valueProvider.GetKeysFromPrefix("bar");

            // Assert
            KeyValuePair <string, string> kvp = Assert.Single(result);

            Assert.Equal("baz", kvp.Key);
            Assert.Equal("bar.baz", kvp.Value);
        }
コード例 #14
0
        public void GetKeysFromPrefix_EmptyPrefix_ReturnsAllPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary <string, string> result = valueProvider.GetKeysFromPrefix("");

            // Assert
            Assert.Equal <KeyValuePair <string, string> >(
                result.OrderBy(kvp => kvp.Key),
                new Dictionary <string, string> {
                { "bar", "bar" }, { "foo", "foo" }, { "null_value", "null_value" }, { "prefix", "prefix" }
            });
        }
コード例 #15
0
        public void GetValue_NullValue(string key)
        {
            // Arrange
            var culture       = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue(key);

            // Assert
            Assert.NotNull(vpResult);
            Assert.Null(vpResult.RawValue);
            Assert.Null(vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #16
0
        public void GetValue_SingleValue()
        {
            // Arrange
            var culture       = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar.baz");

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal("someOtherValue", vpResult.RawValue);
            Assert.Equal("someOtherValue", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #17
0
        static void Main(string[] args)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("contacts[0].Name", "张三");
            dictionary.Add("contacts[0].PhoneNo", "123456789");
            dictionary.Add("contacts[0].EmailAddress", "*****@*****.**");
            dictionary.Add("contacts[1].Name", "李四");
            dictionary.Add("contacts[1].PhoneNo", "987654321");
            dictionary.Add("contacts[1].EmailAddress", "*****@*****.**");

            NameValuePairsValueProvider valueProvider = new NameValuePairsValueProvider(dictionary.ToArray(), null);

            //Prefix=""
            Console.WriteLine("Prefix: <Empty>");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            IDictionary<string, string> keys = valueProvider.GetKeysFromPrefix(string.Empty);
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }

            //Prefix="contact"
            Console.WriteLine("\nPrefix: contact");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            keys = valueProvider.GetKeysFromPrefix("contact");
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }

            //Prefix="contacts[0]"
            Console.WriteLine("\nPrefix: contacts[0]");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            keys = valueProvider.GetKeysFromPrefix("contacts[0]");
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }

            //Prefix="contacts[1]"
            Console.WriteLine("\nPrefix: contacts[1]");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            keys = valueProvider.GetKeysFromPrefix("contacts[1]");
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }
        }
コード例 #18
0
            // Read the body upfront , add as a ValueProvider
            public override Task ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
            {
                HttpRequestMessage request = actionContext.ControllerContext.Request;
                HttpContent content = request.Content;
                if (content != null)
                {
                    FormDataCollection fd = content.ReadAsAsync<FormDataCollection>().Result;
                    if (fd != null)
                    {
                        IValueProvider vp = new NameValuePairsValueProvider(fd, CultureInfo.InvariantCulture);
                        request.Properties.Add(Key, vp);
                    }
                }

                return base.ExecuteBindingAsync(actionContext, cancellationToken);
            }
コード例 #19
0
        static void Main(string[] args)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            list.Add(new KeyValuePair<string, string>("foobar", "1"));
            list.Add(new KeyValuePair<string, string>("foobar", "2"));
            list.Add(new KeyValuePair<string, string>("foobar", "3"));

            NameValuePairsValueProvider valueProvider = new NameValuePairsValueProvider(list, null);
            var result = valueProvider.GetValue("foobar");

            int[] value1 = (int[])result.ConvertTo(typeof(int[]));
            int value2 = (int)result.ConvertTo(typeof(int));

            Console.WriteLine("{0,-16}{1}", "RawValue", "NewValue");
            Console.WriteLine("{0,-16}{1}", result.RawValue.ConvertToString(), value1.ConvertToString());
            Console.WriteLine("{0,-16}{1}", result.RawValue.ConvertToString(), value2.ConvertToString());
        }
コード例 #20
0
        public void GetValue_MultiValue()
        {
            // Arrange
            var culture       = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo");

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(new List <string>()
            {
                "fooValue1", "fooValue2"
            }, (List <string>)vpResult.RawValue);
            Assert.Equal("fooValue1,fooValue2", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #21
0
        public void GetValue_NullMultipleValue()
        {
            // Arrange
            var backingStore = new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>("key", null),
                new KeyValuePair <string, string>("key", null),
                new KeyValuePair <string, string>("key", "value")
            };
            var culture       = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("key");

            // Assert
            Assert.Equal(new[] { null, null, "value" }, vpResult.RawValue as IEnumerable <string>);
            Assert.Equal(",,value", vpResult.AttemptedValue);
        }
コード例 #22
0
        static void Main(string[] args)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("contact.Name", "张三");
            dictionary.Add("contact.PhoneNo", "123456789");
            dictionary.Add("contact.EmailAddress", "*****@*****.**");
            dictionary.Add("contact.Address.Province", "江苏");
            dictionary.Add("contact.Address.City", "苏州");
            dictionary.Add("contact.Address.District", "工业园区");
            dictionary.Add("contact.Address.Street", "星湖街328号");
            NameValuePairsValueProvider valueProvider = new NameValuePairsValueProvider(dictionary.ToArray(), null);

            //Prefix=""
            Console.WriteLine("Prefix: <Empty>");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            IDictionary<string, string> keys = valueProvider.GetKeysFromPrefix(string.Empty);
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }

            //Prefix="contact"
            Console.WriteLine("\nPrefix: contact");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            keys = valueProvider.GetKeysFromPrefix("contact");
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }

            //Prefix="contact.Address"
            Console.WriteLine("\nPrefix: contact.Address");
            Console.WriteLine("{0,-14}{1}", "Key", "Value");
            keys = valueProvider.GetKeysFromPrefix("contact.Address");
            foreach (var item in keys)
            {
                Console.WriteLine("{0,-14}{1}", item.Key, item.Value);
            }
        }
コード例 #23
0
        public void GetKeysFromPrefix_EmptyPrefix_ReturnsAllPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary<string, string> result = valueProvider.GetKeysFromPrefix("");

            // Assert
            Assert.Equal<KeyValuePair<string, string>>(
                result.OrderBy(kvp => kvp.Key),
                new Dictionary<string, string> { { "bar", "bar" }, { "foo", "foo" }, { "null_value", "null_value" }, { "prefix", "prefix" } });
        }
コード例 #24
0
        public void GetValue_NullValue(string key)
        {
            // Arrange
            var culture = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue(key);

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(null, vpResult.RawValue);
            Assert.Equal(null, vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #25
0
        public void GetValue_NullMultipleValue()
        {
            // Arrange
            var backingStore = new KeyValuePair<string, string>[] 
            { 
                new KeyValuePair<string, string>("key", null),
                new KeyValuePair<string, string>("key", null),
                new KeyValuePair<string, string>("key", "value")
            };
            var culture = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("key");

            // Assert
            Assert.Equal(new[] { null, null, "value" }, vpResult.RawValue as IEnumerable<string>);
            Assert.Equal(",,value", vpResult.AttemptedValue);
        }
コード例 #26
0
        public void GetKeysFromPrefix_EmptyPrefix_ReturnsAllPrefixes()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary<string, string> result = valueProvider.GetKeysFromPrefix("");

            // Assert
            Assert.Equal(2, result.Count);
            Assert.Equal("foo", result["foo"]);
            Assert.Equal("bar", result["bar"]);
        }
コード例 #27
0
        public void GetKeysFromPrefix_UnknownPrefix_ReturnsEmptyDictionary()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary<string, string> result = valueProvider.GetKeysFromPrefix("abc");

            // Assert
            Assert.Empty(result);
        }
コード例 #28
0
        public void GetKeysFromPrefix_KnownPrefix_ReturnsMatchingItems()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            IDictionary<string, string> result = valueProvider.GetKeysFromPrefix("bar");

            // Assert
            KeyValuePair<string, string> kvp = Assert.Single(result);
            Assert.Equal("baz", kvp.Key);
            Assert.Equal("bar.baz", kvp.Value);
        }
コード例 #29
0
        public void GetValue_GuardClauses()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(
                () => valueProvider.GetValue(null),
                "key");
        }
コード例 #30
0
        public void GetValue_SingleValue()
        {
            // Arrange
            var culture = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar.baz");

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal("someOtherValue", vpResult.RawValue);
            Assert.Equal("someOtherValue", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #31
0
        public void GetValue_MultiValue()
        {
            // Arrange
            var culture = CultureInfo.GetCultureInfo("fr-FR");
            var valueProvider = new NameValuePairsValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo");

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(new List<string>() { "fooValue1", "fooValue2" }, (List<string>)vpResult.RawValue);
            Assert.Equal("fooValue1,fooValue2", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
コード例 #32
0
        public void GetValue_ReturnsNullIfKeyNotFound()
        {
            // Arrange
            var valueProvider = new NameValuePairsValueProvider(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar");

            // Assert
            Assert.Null(vpResult);
        }