示例#1
0
        public HttpContextBase PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution)
        {
            Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context");
            var urlParts         = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray()).Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray();
            var controllerName   = urlParts[0];
            var controllerAction = urlParts[1];
            var requestContext   = new RequestContext(
                XmlDbUpdateHttpContextHelpers.BuildHttpContextBase(recordedAction, backendUrl, userId, useGuidSubstitution),
                XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controllerName, controllerAction)
                );

            BackendActionContext.ResetCurrent();
            XmlDbUpdateHttpContextHelpers.BuildController(requestContext, controllerName, CultureHelpers.GetCultureByLcid(recordedAction.Lcid)).Execute(requestContext);
            return(requestContext.HttpContext);
        }
        public HttpContext PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution, IServiceProvider provider = null)
        {
            Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context");
            var urlParts = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray())
                           .Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray();
            var controller = urlParts[0];
            var action     = urlParts[1];

            BackendActionContext.ResetCurrent();

            var cultureInfo = CultureHelpers.GetCultureByLcid(recordedAction.Lcid);

            CultureInfo.CurrentCulture = cultureInfo;

            var httpContext = XmlDbUpdateHttpContextHelpers.BuildHttpContext(recordedAction, backendUrl, userId, useGuidSubstitution);
            var routeData   = XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controller, action);

            httpContext.Features[typeof(IRoutingFeature)] = new RoutingFeature {
                RouteData = routeData
            };
            var routeContext = new RouteContext(httpContext)
            {
                RouteData = routeData
            };

            var serviceProvider = provider ?? new HttpContextAccessor().HttpContext.RequestServices;

            httpContext.RequestServices = serviceProvider;
            var m       = typeof(DynamicRouteValueTransformer).Assembly.GetType("Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandler");
            var handler = (IRouter)serviceProvider.GetRequiredService(m);

            QPContext.CurrentUserId = userId;
            Task.Run(async() =>
            {
                await handler.RouteAsync(routeContext);
                await routeContext.Handler(routeContext.HttpContext);
            }).Wait();
            QPContext.CurrentUserId = 0;

            return(httpContext);
        }