示例#1
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);
        }
        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;
        }
示例#3
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);
        }
示例#4
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.Options;

                // 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,
                };
            }
        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;
        }
示例#6
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.Options;

                // 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,
                };
            }