Example #1
0
        public async void ReturnsTrueForUriModelTypeFoundInServiceProvider()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: null,
                            httpMethod: null,
                            methodInfo: null,
                            uriParameterType: typeof(StandardModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Request.InvocationContext.UriModel.Should().NotBeNull();
        }
Example #2
0
        public async void ReturnsTrueForNullUriModelType()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(TestController),
                            methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)),
                            httpMethod: "GET")
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
        }
Example #3
0
        public async void DoesNotThrowExceptionForNonDefaultConstructorType()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: null,
                            methodInfo: null,
                            httpMethod: null,
                            uriParameterType: typeof(NonDefaultConstructorModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().Be(true);
            context.Request.InvocationContext.UriModel.Should().NotBeNull();
        }
Example #4
0
        public async void ReturnsFalseAndErrorsWhenBindingFails()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                        //UriModelType = typeof(StandardModel)
                    },
                    QueryVariables = new Dictionary <string, string>
                    {
                        { "LongProp", "def" },
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        RouteVariables = new Dictionary <string, string>
                        {
                            { "IntProp", "abc" }
                        },
                        Location = new ApiEndpointLocation(
                            controller: null,
                            httpMethod: null,
                            methodInfo: null,
                            uriParameterType: typeof(StandardModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                },
                Configuration = new DeepSleepRequestConfiguration
                {
                    ValidationErrorConfiguration = new ApiValidationErrorConfiguration
                    {
                        UriBindingError = "400.000001|'LongProp' is in an incorrect format and could not be bound."
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().BeFalse();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Response.StatusCode.Should().Be(400);
            context.Routing.Should().NotBeNull();
            context.Request.InvocationContext.UriModel.Should().BeNull();

            context.Runtime.Should().NotBeNull();
            context.Validation.Errors.Should().NotBeNull();
            context.Validation.Errors.Should().HaveCount(1);
            context.Validation.Errors[0].Should().Be("400.000001|'LongProp' is in an incorrect format and could not be bound.");
        }
Example #5
0
        public async void ReturnsTrueAndCorrectlyBindsBoolRouteVariables(string boolValue)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                        //UriModelType = typeof(StandardModel)
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        RouteVariables = new Dictionary <string, string>
                        {
                            { "BoolProp", boolValue }
                        },
                        Location = new ApiEndpointLocation(
                            controller: null,
                            httpMethod: null,
                            methodInfo: null,
                            uriParameterType: typeof(StandardModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Routing.Should().NotBeNull();
            context.Request.InvocationContext.UriModel.Should().NotBeNull();

            var model = context.Request.InvocationContext.UriModel as StandardModel;

            model.Should().NotBeNull();

            if (boolValue.ToString().ToLower() == "true" || boolValue == "1")
            {
                model.BoolProp.ToString().ToLower().Should().Be("true");
            }
            else
            {
                model.BoolProp.ToString().ToLower().Should().Be("false");
            }
        }
Example #6
0
        public async void ReturnsFalseForCancelledRequest()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(true)
            };

            var processed = await context.ProcessHttpRequestUriBinding(null).ConfigureAwait(false);

            processed.Should().BeFalse();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
        }
Example #7
0
        public async void ReturnsTrueAndCorrectlyBindsUtcDateTimeRouteVariables(string date)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        RouteVariables = new Dictionary <string, string>
                        {
                            { "DateTimeProp", date }
                        },
                        Location = new ApiEndpointLocation(
                            controller: null,
                            httpMethod: null,
                            methodInfo: null,
                            uriParameterType: typeof(StandardModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Routing.Should().NotBeNull();
            context.Request.InvocationContext.UriModel.Should().NotBeNull();

            var model = context.Request.InvocationContext.UriModel as StandardModel;

            model.Should().NotBeNull();
            model.DateTimeProp.ToString("r").Should().Be("Mon, 15 Jun 2009 20:45:30 GMT");
        }
Example #8
0
        public async void ReturnsTrueForNullInvocationContext()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = null
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
        }
Example #9
0
        public async void ReturnsTrueAndCorrectlyBindsRouteAndQueryVariables()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    InvocationContext = new ApiInvocationContext
                    {
                    },
                    QueryVariables = new Dictionary <string, string>
                    {
                        { "LongProp", "-4" },
                        { "ULongProp", "34" },
                        { "DoubleProp", "-8.45" },
                        { "FloatProp", "5.9" },
                        { "DecimalProp", "3.9098" },
                        { "ObjectProp", "1" },
                        { "DateTimeProp", "4/2/2007 7:23:57 PM" },
                        { "DateTimeOfsetProp", "4/2/2007 7:23:57 PM -01:00" },
                        { "TimeSpanProp", "10:00:00" },
                        { "GuidProp", "0F6AD742-3248-4229-B9A3-DC20EFA074D1" },
                        { "EnumProp", "Item1" }
                    }
                },
                Routing = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        RouteVariables = new Dictionary <string, string>
                        {
                            { "StringProp", "StringProp1" },
                            { "CharProp", "a" },
                            { "ByteProp", "1" },
                            { "SByteProp", "1" },
                            { "BoolProp", "true" },
                            { "IntProp", "-23" },
                            { "UIntProp", "23" },
                            { "ShortProp", "-2" },
                            { "UShortProp", "2" },
                        },
                        Location = new ApiEndpointLocation(
                            controller: null,
                            methodInfo: null,
                            httpMethod: null,
                            uriParameterType: typeof(StandardModel),
                            bodyParameterType: null,
                            simpleParameters: null,
                            methodReturnType: null)
                    }
                }
            };

            var processed = await context.ProcessHttpRequestUriBinding(new FormUrlEncodedObjectSerializer()).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Routing.Should().NotBeNull();
            context.Request.InvocationContext.UriModel.Should().NotBeNull();

            var model = context.Request.InvocationContext.UriModel as StandardModel;

            model.Should().NotBeNull();
            model.StringProp.Should().Be("StringProp1");
            model.CharProp.Should().Be('a');
            model.ByteProp.Should().Be(1);
            model.SByteProp.Should().Be(1);
            model.BoolProp.Should().Be(true);
            model.IntProp.Should().Be(-23);
            model.UIntProp.Should().Be(23);
            model.ShortProp.Should().Be(-2);
            model.UShortProp.Should().Be(2);
            model.LongProp.Should().Be(-4);
            model.ULongProp.Should().Be(34);
            model.DoubleProp.Should().Be(-8.45);
            model.FloatProp.Should().Be(5.9f);
            model.DecimalProp.Should().Be(3.9098m);
            model.ObjectProp.ToString().Should().Be("1");
            model.DateTimeProp.ToLocalTime().ToString(CultureInfo.InvariantCulture).Should().Be("04/02/2007 19:23:57");
            model.DateTimeOfsetProp.ToString(CultureInfo.InvariantCulture).Should().Be("04/02/2007 19:23:57 -01:00");
            model.TimeSpanProp.ToString().Should().Be("10:00:00");
            model.GuidProp.ToString().Should().Be("0f6ad742-3248-4229-b9a3-dc20efa074d1");
            model.EnumProp.Should().Be(StandardEnum.Item1);
        }