private async Task AssertCanInvokeActionWithRouteParamOfType <T>(bool dateOnly = false)
        {
            Type type = typeof(T);

            object value = "";

            if (typeof(T) == typeof(DateTimeOffset))
            {
                value = "2017-10-28T09:51:17+07:00";
            }
            if (typeof(T) != typeof(string) && typeof(T) != typeof(DateTimeOffset))
            {
                value = Activator.CreateInstance(type);
            }
            if (typeof(T) == typeof(DateTime))
            {
                value = default(DateTime).ToString("yyyy-MM-dd HH:mm:ss");
                if (dateOnly)
                {
                    value = default(DateTime).ToString("yyyy-MM-dd");
                }
            }

            string actionName = "Action_" + type.Name;

            var discoverer = new Fakes.FakeLimitedControllerDiscoverer(typeof(Controllers.RouteSupportedParametersController));
            var ctrl       = discoverer.GetControllers(null).Single();
            var action     = ctrl.Actions.Single(x => x.Name == actionName.ToLower());
            var serializer = new JsonSerializer();
            var invoker    = new ActionInvoker(new ControllerBuilder((new Moq.Mock <IServiceProvider>()).Object),
                                               new Services.ModelBinders.ModelBinderCollection(
                                                   serializer, Fakes.FakeServiceProvider.GetServiceProvider(), new Fakes.FakeDefaultLiteApiOptionsRetriever()), new JsonSerializer());
            var httpCtx = new Fakes.FakeHttpContext();

            httpCtx.SetActionContext(action);
            httpCtx.Request.Path = "/api/RouteSupportedParameters/" + actionName + "/" + value;
            await invoker.Invoke(httpCtx, action);

            string jsonResult = httpCtx.Response.ReadBody();
            object result     = serializer.Deserialize(jsonResult, type);

            if (typeof(T) == typeof(DateTime))
            {
                value = default(DateTime);
            }
            if (typeof(T) == typeof(DateTimeOffset))
            {
                value = DateTimeOffset.Parse(value as string);
            }
            Assert.Equal(value, result);
        }
Exemple #2
0
        private void AssertCanParseSimpleParam <T>()
        {
            Type type;
            Type nullableArg;
            bool isNullable = false;

            if (typeof(T).GetTypeInfo().IsNullable(out nullableArg))
            {
                type       = nullableArg;
                isNullable = true;
            }
            else
            {
                type = typeof(T);
            }

            object value = "";

            if (typeof(T) == typeof(TestEnum))
            {
                value = TestEnum.TestValueTest;
            }
            else if (typeof(T) != typeof(string))
            {
                value = Activator.CreateInstance(type);
            }

            string      actionName = "Action_" + type.Name + (isNullable ? "_Nullable" : "");
            string      url        = "/ParameterParsing/" + actionName;
            HttpContext ctx        = new Fakes.FakeHttpContext();

            ctx.SetLiteApiOptions(LiteApiOptions.Default);
            var request = ctx.Request as Fakes.FakeHttpRequest;

            request.Path = url;
            request.AddQuery("p", value.ToString());
            var discoverer = new Fakes.FakeLimitedControllerDiscoverer(typeof(Controllers.ParameterParsingController));
            var ctrl       = discoverer.GetControllers(null).Single();
            var action     = ctrl.Actions.Single(x => x.Name == actionName.ToLower());

            ctx.SetActionContext(action);

            ModelBinderCollection mb = new ModelBinderCollection(new JsonSerializer(), Fakes.FakeServiceProvider.GetServiceProvider(), new Fakes.FakeDefaultLiteApiOptionsRetriever());

            object[] parameters = mb.GetParameterValues(request, action);
            Assert.Equal(value, parameters.Single());
        }