Пример #1
0
        public void ToQueryString_ArrayWithNoItems()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "arr", new object[0] }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);

            // Then
            Assert.Empty(queryString);
        }
Пример #2
0
        public void ToQueryString_Array()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "ids", new object[] { 1, 2, 3 } }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?ids[]=1&ids[]=2&ids[]=3";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #3
0
        public void ToQueryString_ArrayWithOneItem()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "ten", new object[] { 10 } }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?ten[]=10";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #4
0
        public void ToQueryString_TimeSpan()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "delay", new TimeSpan(10, 23, 59, 59) }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?delay=10.23:59:59";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #5
0
        public void ToQueryString_DateTime()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "at", new DateTime(2002, 12, 31, 23, 59, 59) },
                { "on", new DateTime(2002, 12, 31) },
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var pattern = "at=2002-12-31T23:59:59-0.:00&on=2002-12-31T00:00:00-0.:00";

            Assert.Matches(pattern, queryString);
        }
Пример #6
0
        public void ToQueryString_Range()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "num", new { Min = -20, Max = 20 } },
                { "age", new { min = new DateTime(2002, 12, 31) } },
                { "top", new { max = 100.001M } },
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var pattern = @"num\.min=-20&num\.max=20&age\.min=2002-12-31T00:00:00-0.:00&top\.max=100\.001";

            Assert.Matches(pattern, queryString);
        }
Пример #7
0
        public void ToQueryString_Decimal()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "debit", -20.02M },
                { "credit", 15.51M }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?debit=-20.02&credit=15.51";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #8
0
        public void ToQueryString_Boolean()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "up", true },
                { "down", false }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?up=true&down=false";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #9
0
        public void ToQueryString_String()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "empty", null },
                { "text", "hello, world!" }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?text=hello, world!";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #10
0
        public void ToQueryString_FromObject()
        {
            // Given
            var args = new
            {
                empty  = (string)null,
                text   = "hello, world!",
                up     = true,
                down   = false,
                zero   = 0,
                plus   = 10,
                minus  = -20,
                debit  = -20.02M,
                credit = 15.51M,
                at     = new DateTime(2002, 12, 31, 23, 59, 59),
                on     = new DateTime(2002, 12, 31),
                delay  = new TimeSpan(10, 23, 59, 59),
                arr    = new object[0],
                ten    = new object[] { 10 },
                ids    = new object[] { 1, 2, 3 },
                num    = new { Min = -20, Max = 20 },
                age    = new { min = new DateTime(2002, 12, 31) },
                top    = new { max = 100.001M }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var pattern =
                @"text=hello, world!"
                + @"&up=true"
                + @"&down=false"
                + @"&zero=0"
                + @"&plus=10"
                + @"&minus=-20"
                + @"&debit=-20\.02"
                + @"&credit=15\.51"
                + @"&at=2002-12-31T23:59:59-0.:00"
                + @"&on=2002-12-31T00:00:00-0.:00"
                + @"&delay=10\.23:59:59"
                + @"&ten\[\]=10"
                + @"&ids\[\]=1&ids\[\]=2&ids\[\]=3"
                + @"&num\.min=-20&num\.max=20"
                + @"&age\.min=2002-12-31T00:00:00-0.:00"
                + @"&top\.max=100\.001";

            Assert.Matches(pattern, queryString);
        }
Пример #11
0
        public void ToQueryString_Integer()
        {
            // Given
            IDictionary args = new Dictionary <string, object>
            {
                { "zero", 0 },
                { "plus", 10 },
                { "minus", -20 }
            };
            // When
            var queryString = UriUtil.ToQueryString(args);
            // Then
            var expected = "?zero=0&plus=10&minus=-20";
            var obtained = queryString;

            Assert.Equal(expected, obtained);
        }
Пример #12
0
        public static string BuildPath(
            RenderContext ctx, object query, string pathTemplate,
            IEnumerable <KeyValuePair <string, object> > pathArgs   = null,
            IEnumerable <KeyValuePair <string, object> > filterArgs = null)
        {
            if (!pathTemplate.StartsWith("/"))
            {
                pathTemplate = $"/{pathTemplate}";
            }

            if (pathArgs == null)
            {
                pathArgs = SatisfyArgs(query, ctx.PathArgs.Keys);
            }

            if (filterArgs == null)
            {
                var filter = query.Get("Filter");
                if (filter != null)
                {
                    var type           = filter.GetType();
                    var filterArgNames = type.GetProperties().Select(p => p.Name).ToArray();
                    filterArgs = SatisfyArgs(query, filterArgNames);
                }
                else
                {
                    filterArgs = new KeyValuePair <string, object> [0];
                }
            }

            var pagination = query.Get <Pagination>("Pagination");

            if (pagination != null)
            {
                filterArgs =
                    new[] {
                    new KeyValuePair <string, object>("offset", pagination.Offset),
                    new KeyValuePair <string, object>("limit", pagination.Limit)
                }.Concat(filterArgs);
            }

            var sort = query.Get <Sort>("Sort");

            if (sort != null)
            {
                var fields = (
                    from field in sort.SortedFields
                    let key = field.FieldName.ChangeCase(TextCase.CamelCase)
                              select(field.Order == Order.Descending) ? $"{key}:desc" : key
                    ).ToArray();

                filterArgs = filterArgs.Append(new KeyValuePair <string, object>("sort", fields));
            }

            var root        = GetUriPrefix(ctx.HttpContext.Request);
            var path        = UriUtil.BuildPath(pathTemplate, pathArgs);
            var queryString = UriUtil.ToQueryString(filterArgs);

            path = $"{root}{path}{queryString}";
            return(path);
        }