예제 #1
0
            private void Initialize(
                Mock <HttpContext> httpContext,
                string format      = null,
                FormatSource?place = null)
            {
                OptionsManager = new TestOptionsManager <MvcOptions>();

                // Setup options on mock service provider
                Options = OptionsManager.Value;

                // Set up default output formatters.
                Options.OutputFormatters.Add(new HttpNoContentOutputFormatter());
                Options.OutputFormatters.Add(new StringOutputFormatter());
                Options.OutputFormatters.Add(new JsonOutputFormatter());

                // Set up default mapping for json extensions to content type
                Options.FormatterMappings.SetMediaTypeMappingForFormat(
                    "json",
                    MediaTypeHeaderValue.Parse("application/json"));

                // Setup MVC services on mock service provider
                MockActionContext     = CreateMockActionContext(httpContext, format, place);
                ActionContextAccessor = new ActionContextAccessor()
                {
                    ActionContext = MockActionContext,
                };
            }
        public static HttpContextAccessor CreateHttpContextAccessor(RequestTelemetry requestTelemetry = null, ActionContext actionContext = null)
        {
            var services = new ServiceCollection();

            var request = new DefaultHttpContext().Request;

            request.Method = "GET";
            request.Path   = new PathString("/Test");
            var contextAccessor = new HttpContextAccessor()
            {
                HttpContext = request.HttpContext
            };

            services.AddSingleton <IHttpContextAccessor>(contextAccessor);

            if (actionContext != null)
            {
                var si = new ActionContextAccessor();
                si.ActionContext = actionContext;
                services.AddSingleton <IActionContextAccessor>(si);
            }

            if (requestTelemetry != null)
            {
                services.AddSingleton <RequestTelemetry>(requestTelemetry);
            }

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            contextAccessor.HttpContext.RequestServices = serviceProvider;

            return(contextAccessor);
        }
        public static HttpContextAccessor CreateHttpContextAccessor(RequestTelemetry requestTelemetry = null, ActionContext actionContext = null)
        {
            var services = new ServiceCollection();

            var request = new DefaultHttpContext().Request;
            request.Method = "GET";
            request.Path = new PathString("/Test");
            var contextAccessor = new HttpContextAccessor() { HttpContext = request.HttpContext };

            services.AddInstance<IHttpContextAccessor>(contextAccessor);

            if (actionContext != null)
            {
                var si = new ActionContextAccessor();
                si.ActionContext = actionContext;
                services.AddInstance<IActionContextAccessor>(si);
            }

            if (requestTelemetry != null)
            {
                services.AddInstance<RequestTelemetry>(requestTelemetry);
            }

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            contextAccessor.HttpContext.RequestServices = serviceProvider;

            return contextAccessor;
        }
예제 #4
0
        private MvcRouteHandler CreateMvcRouteHandler(
            ActionDescriptor actionDescriptor    = null,
            IActionSelector actionSelector       = null,
            IActionInvokerFactory invokerFactory = null,
            ILoggerFactory loggerFactory         = null,
            object diagnosticListener            = null)
        {
            var actionContextAccessor = new ActionContextAccessor();

            if (actionDescriptor == null)
            {
                var mockAction = new Mock <ActionDescriptor>();
                actionDescriptor = mockAction.Object;
            }

            if (actionSelector == null)
            {
                var mockActionSelector = new Mock <IActionSelector>();
                mockActionSelector
                .Setup(a => a.SelectCandidates(It.IsAny <RouteContext>()))
                .Returns(new ActionDescriptor[] { actionDescriptor });

                mockActionSelector
                .Setup(a => a.SelectBestCandidate(It.IsAny <RouteContext>(), It.IsAny <IReadOnlyList <ActionDescriptor> >()))
                .Returns(actionDescriptor);
                actionSelector = mockActionSelector.Object;
            }

            if (loggerFactory == null)
            {
                loggerFactory = NullLoggerFactory.Instance;
            }

            var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");

            if (diagnosticListener != null)
            {
                diagnosticSource.SubscribeWithAdapter(diagnosticListener);
            }

            if (invokerFactory == null)
            {
                var mockInvoker = new Mock <IActionInvoker>();
                mockInvoker.Setup(i => i.InvokeAsync())
                .Returns(Task.FromResult(true));

                var mockInvokerFactory = new Mock <IActionInvokerFactory>();
                mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny <ActionContext>()))
                .Returns(mockInvoker.Object);

                invokerFactory = mockInvokerFactory.Object;
            }

            return(new MvcRouteHandler(
                       invokerFactory,
                       actionSelector,
                       diagnosticSource,
                       loggerFactory,
                       actionContextAccessor));
        }
        public static HttpContextAccessor CreateHttpContextAccessor(RequestTelemetry requestTelemetry = null, ActionContext actionContext = null, string httpContextCorrelationId = null)
        {
            var services = new ServiceCollection();

            var request = new DefaultHttpContext().Request;

            request.Method = "GET";
            request.Path   = new PathString("/Test");
            if (httpContextCorrelationId != null)
            {
                HttpHeadersUtilities.SetRequestContextKeyValue(request.Headers, RequestResponseHeaders.RequestContextSourceKey, httpContextCorrelationId);
            }

            var contextAccessor = new HttpContextAccessor {
                HttpContext = request.HttpContext
            };

            services.AddSingleton <IHttpContextAccessor>(contextAccessor);

            if (actionContext != null)
            {
                var si = new ActionContextAccessor();
                si.ActionContext = actionContext;
                services.AddSingleton <IActionContextAccessor>(si);
            }

            if (requestTelemetry != null)
            {
                request.HttpContext.Features.Set(requestTelemetry);
            }

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            contextAccessor.HttpContext.RequestServices = serviceProvider;

            return(contextAccessor);
        }
예제 #6
0
        public static HttpContextAccessor CreateHttpContextAccessor(RequestTelemetry requestTelemetry = null, ActionContext actionContext = null, string sourceInstrumentationKey = null)
        {
            var services = new ServiceCollection();

            var request = new DefaultHttpContext().Request;

            request.Method = "GET";
            request.Path   = new PathString("/Test");
            if (sourceInstrumentationKey != null)
            {
                request.Headers.Add(RequestResponseHeaders.SourceInstrumentationKeyHeader, new StringValues(sourceInstrumentationKey));
            }

            var contextAccessor = new HttpContextAccessor {
                HttpContext = request.HttpContext
            };

            services.AddSingleton <IHttpContextAccessor>(contextAccessor);

            if (actionContext != null)
            {
                var si = new ActionContextAccessor();
                si.ActionContext = actionContext;
                services.AddSingleton <IActionContextAccessor>(si);
            }

            if (requestTelemetry != null)
            {
                request.HttpContext.Features.Set(requestTelemetry);
            }

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            contextAccessor.HttpContext.RequestServices = serviceProvider;

            return(contextAccessor);
        }
예제 #7
0
        private static HttpContext GetHttpContext(string appRoot,
                                                  string contentPath,
                                                  string expectedPath,
                                                  HttpResponse response)
        {
            var httpContext           = new Mock <HttpContext>();
            var actionContext         = GetActionContext(httpContext.Object);
            var actionContextAccessor = new ActionContextAccessor()
            {
                ActionContext = actionContext
            };
            var mockActionSelector = new Mock <IActionSelector>();
            var urlHelper          = new UrlHelper(actionContextAccessor, mockActionSelector.Object);
            var serviceProvider    = GetServiceProvider(urlHelper);

            httpContext.Setup(o => o.Response)
            .Returns(response);
            httpContext.SetupGet(o => o.RequestServices)
            .Returns(serviceProvider);
            httpContext.Setup(o => o.Request.PathBase)
            .Returns(new PathString(appRoot));

            return(httpContext.Object);
        }
예제 #8
0
        private static HttpContext GetHttpContext(
            string appRoot,
            string contentPath,
            string expectedPath,
            HttpResponse response)
        {
            var httpContext = new Mock<HttpContext>();
            var actionContext = GetActionContext(httpContext.Object);
            var actionContextAccessor = new ActionContextAccessor() { ActionContext = actionContext };
            var mockActionSelector = new Mock<IActionSelector>();
            var urlHelper = new UrlHelper(actionContextAccessor, mockActionSelector.Object);
            var serviceProvider = GetServiceProvider(urlHelper);

            httpContext.Setup(o => o.Response)
                       .Returns(response);
            httpContext.SetupGet(o => o.RequestServices)
                       .Returns(serviceProvider);
            httpContext.Setup(o => o.Request.PathBase)
                       .Returns(new PathString(appRoot));

            return httpContext.Object;
        }
예제 #9
0
        private static IActionContextAccessor GetContextAccessor(
            IServiceProvider serviceProvider,
            RouteData routeData = null)
        {
            // Set IServiceProvider properties because TemplateRoute gets services (e.g. an ILoggerFactory instance)
            // through the HttpContext.
            var httpContext = new DefaultHttpContext
            {
                ApplicationServices = serviceProvider,
                RequestServices = serviceProvider,
            };

            if (routeData == null)
            {
                routeData = new RouteData
                {
                    Routers = { Mock.Of<IRouter>(), },
                };
            }

            var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
            var contextAccessor = new ActionContextAccessor()
            {
                ActionContext = actionContext,
            };

            return contextAccessor;
        }