Example #1
0
        public async void ThrowsForNullEndpointLocation()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = null
                    }
                }
            };


            try
            {
                var processed = await context.ProcessHttpEndpointInitialization().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                ex.Message.Should().Be("Routing item's controller type is null");
                return;
            }

            Assert.False(true);
        }
Example #2
0
        public async void ReturnsTrueAndRetrivesModelsForUriAndBodyParameterWhenEndpointHasExtraParametersAfter(string method)
        {
            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(m => m.GetService(It.IsAny <Type>())).Returns(new InjectionController());

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(InjectionController),
                            methodInfo: typeof(InjectionController).GetMethod(nameof(InjectionController.DefaultEndpointWithUriAndBodyAndOthersAfter)),
                            httpMethod: method)
                    }
                },
                RequestServices = mockServiceProvider.Object
            };

            var processed = await context.ProcessHttpEndpointInitialization().ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Request.InvocationContext.ControllerInstance.Should().NotBeNull();
            context.Request.InvocationContext.ControllerInstance.Should().BeOfType <InjectionController>();
            context.Request.InvocationContext.UriModel.Should().BeNull();
            context.Request.InvocationContext.BodyModel.Should().BeNull();

            context.Routing.Route.Location.BodyParameterType.Should().Be(typeof(StandardNullableModel));
            context.Routing.Route.Location.UriParameterType.Should().Be(typeof(StandardModel));
            context.Routing.Route.Location.MethodInfo.Should().NotBeNull();
        }
Example #3
0
        public async void ReturnsTrueAndRetrivesActivatesControlleWhenServiceProviderIsNull()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(InjectionController),
                            methodInfo: typeof(InjectionController).GetMethod(nameof(InjectionController.DefaultEndpoint)),
                            httpMethod: null)
                    }
                }
            };

            var processed = await context.ProcessHttpEndpointInitialization().ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Request.InvocationContext.ControllerInstance.Should().NotBeNull();
            context.Request.InvocationContext.ControllerInstance.Should().BeOfType <InjectionController>();
            context.Request.InvocationContext.UriModel.Should().BeNull();
            context.Request.InvocationContext.BodyModel.Should().BeNull();
            context.Routing.Route.Location.BodyParameterType.Should().BeNull();
            context.Routing.Route.Location.UriParameterType.Should().BeNull();
            context.Routing.Route.Location.MethodInfo.Should().NotBeNull();
            context.Routing.Route.Location.MethodInfo.Name.Should().Be(context.Routing.Route.Location.MethodInfo.Name);
        }
Example #4
0
        public async void ReturnsTrueAndRetrivesControllerEndpointForProtectedMethod()
        {
            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(m => m.GetService(It.IsAny <Type>())).Returns(null);

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(InjectionController),
                            methodInfo: typeof(InjectionController).GetMethod(name: "DefaultEndpointProtected", bindingAttr: BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
                            httpMethod: null)
                    }
                },
                RequestServices = mockServiceProvider.Object
            };

            var processed = await context.ProcessHttpEndpointInitialization().ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Request.InvocationContext.ControllerInstance.Should().NotBeNull();
            context.Request.InvocationContext.ControllerInstance.Should().BeOfType <InjectionController>();
            context.Request.InvocationContext.UriModel.Should().BeNull();
            context.Request.InvocationContext.BodyModel.Should().BeNull();
            context.Routing.Route.Location.BodyParameterType.Should().BeNull();
            context.Routing.Route.Location.UriParameterType.Should().BeNull();
            context.Routing.Route.Location.MethodInfo.Should().NotBeNull();
            context.Routing.Route.Location.MethodInfo.Name.Should().Be(context.Routing.Route.Location.MethodInfo.Name);
        }
Example #5
0
        public async void ReturnsFalseForCancelledRequest()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(true)
            };

            var processed = await context.ProcessHttpEndpointInitialization().ConfigureAwait(false);

            processed.Should().BeFalse();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
        }