Example #1
0
        public void Should_add_model_error_for_invalid_id()
        {
            var idParser = new Mock<IRestIdParser>();
            idParser.Setup(parser => parser.ParseId("5")).Throws(new InvalidIdException("5"));
            idParser.SetupGet(parser => parser.IdKey).Returns("id");
            var binder = new RestRequestModelBinder(null, null);

            var values = new NameValueCollection {
                { "id", "5" }
            };

            var bindingContext = new ModelBindingContext {
                ValueProvider = new NameValueCollectionValueProvider(values, CultureInfo.InvariantCulture)
            };

            var controllerContext = CreateControllerContext();
            binder.BindModel(controllerContext, bindingContext);
            Assert.That(controllerContext.IsValid(), Is.False);
        }
Example #2
0
        public void Should_bind_RestRequest_with_single_id()
        {
            var idParser = new AlwaysTrueIdParser();
            var binder = new RestRequestModelBinder(null, null);

            var values = new NameValueCollection {
                { "id", "5" }
            };

            var bindingContext = new ModelBindingContext {
                ValueProvider = new NameValueCollectionValueProvider(values, CultureInfo.InvariantCulture)
            };

            var model = binder.BindModel(CreateControllerContext(), bindingContext);
            Assert.That(model, Is.TypeOf<RestRequest>());
            var request = (RestRequest)model;

            Assert.That(request.Criteria, Has.Count.EqualTo(1));
            Assert.That(request.Criteria["id"], Is.Not.Null);
            Assert.That(request.Criteria["id"].Values.Count(), Is.EqualTo(1));
            Assert.That(request.Criteria["id"].Values.First().RawValue, Is.EqualTo("5"));
        }
Example #3
0
        public void Should_add_model_error_for_invalid_sort_value()
        {
            var idParser = new AlwaysTrueIdParser();
            var binder = new RestRequestModelBinder(null, null);

            var valueProvider = new NameValueCollectionValueProvider(
                new NameValueCollection {
                    { ":sort", "foo|asdf" },
                    { "id", idParser.FetchAllIdValue }
                },
                CultureInfo.InvariantCulture
            );

            var bindingContext = new ModelBindingContext {
                ValueProvider = valueProvider
            };

            var controllerContext = CreateControllerContext();
            var model = binder.BindModel(controllerContext, bindingContext);
            Assert.That(model, Is.TypeOf<RestRequest>());

            Assert.That(controllerContext.IsValid(), Is.False);
        }
Example #4
0
        public void Should_parse_sort_info()
        {
            var idParser = new AlwaysTrueIdParser();
            var binder = new RestRequestModelBinder(null, null);

            var valueProvider = new NameValueCollectionValueProvider(
                new NameValueCollection {
                    { ":sort", "foo|asc,bar|DeSC,baz|ascending,bat|descending" },
                    { "id", idParser.FetchAllIdValue }
                },
                CultureInfo.InvariantCulture
            );

            var bindingContext = new ModelBindingContext {
                ValueProvider = valueProvider
            };

            var model = binder.BindModel(CreateControllerContext(), bindingContext);
            Assert.That(model, Is.TypeOf<RestRequest>());
            var request = (RestRequest)model;

            Assert.That(request.SortInfo, Has.Count.EqualTo(4));
            Assert.That(request.SortInfo[0].Field, Is.EqualTo("foo"));
            Assert.That(request.SortInfo[0].Order, Is.EqualTo(SortOrder.Ascending));
            Assert.That(request.SortInfo[1].Field, Is.EqualTo("bar"));
            Assert.That(request.SortInfo[1].Order, Is.EqualTo(SortOrder.Descending));
            Assert.That(request.SortInfo[2].Field, Is.EqualTo("baz"));
            Assert.That(request.SortInfo[2].Order, Is.EqualTo(SortOrder.Ascending));
            Assert.That(request.SortInfo[3].Field, Is.EqualTo("bat"));
            Assert.That(request.SortInfo[3].Order, Is.EqualTo(SortOrder.Descending));
        }