Exemplo n.º 1
0
        private void SetupProjectAPIForTesting()
        {
            var httpRequest  = new HttpRequest("", "http://localhost:62081/", "");
            var stringWriter = new System.IO.StringWriter();
            var httpResponce = new HttpResponse(stringWriter);
            var httpContext  = new HttpContext(httpRequest, httpResponce);

            var sessionContainer = new System.Web.SessionState.HttpSessionStateContainer("id",
                                                                                         new System.Web.SessionState.SessionStateItemCollection(),
                                                                                         new HttpStaticObjectsCollection(), 10, true,
                                                                                         HttpCookieMode.AutoDetect,
                                                                                         System.Web.SessionState.SessionStateMode.InProc, false);

            httpContext.Items["AspSession"] = typeof(System.Web.SessionState.HttpSessionState).GetConstructor(
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance,
                null, System.Reflection.CallingConventions.Standard,
                new[] { typeof(System.Web.SessionState.HttpSessionStateContainer) },
                null)
                                              .Invoke(new object[] { sessionContainer });

            //setup the User Pricinpal for HttpContext
            httpContext.User = new System.Security.Principal.GenericPrincipal(
                new System.Security.Principal.GenericIdentity(user.Email.ToString()),
                new string[0]);

            //set up the User information for ProjectAPI
            projectApi.CurrentUser = user;

            HttpContext.Current = httpContext;
        }
Exemplo n.º 2
0
            /// <summary>
            /// 创建一个模拟的 MVC 控制器。
            /// </summary>
            /// <typeparam name="TController">控制的数据类型。</typeparam>
            /// <param name="user">当前已授权的登录用户。</param>
            /// <param name="mockFactoryCallback">模拟的执行器工厂回调函数。</param>
            /// <returns>返回一个控制器的实例。</returns>
            public static TController Create <TController>(object user, Action <MockExecutorFactory> mockFactoryCallback = null)
                where TController : System.Web.Mvc.Controller, new()
            {
                var httpRequest  = new HttpRequest(string.Empty, "http://www.aoite.com/", string.Empty);
                var stringWriter = new System.IO.StringWriter();
                var httpResponce = new HttpResponse(stringWriter);
                var httpContext  = new HttpContext(httpRequest, httpResponce);

                var sessionContainer = new System.Web.SessionState.HttpSessionStateContainer("id", new System.Web.SessionState.SessionStateItemCollection(),
                                                                                             new HttpStaticObjectsCollection(), 10, true,
                                                                                             HttpCookieMode.AutoDetect,
                                                                                             System.Web.SessionState.SessionStateMode.InProc, false);

                httpContext.Items["AspSession"] = typeof(System.Web.SessionState.HttpSessionState).GetConstructor(
                    BindingFlags.NonPublic | BindingFlags.Instance,
                    null, CallingConventions.Standard,
                    new[] { typeof(System.Web.SessionState.HttpSessionStateContainer) },
                    null)
                                                  .Invoke(new object[] { sessionContainer });
                HttpContext.Current = httpContext;

                var    appFactoryType = typeof(HttpContext).Assembly.GetType("System.Web.HttpApplicationFactory");
                object appFactory     = Aoite.Reflection.FieldExtensions.DelegateForGetFieldValue(appFactoryType, "_theApplicationFactory")(null);

                Aoite.Reflection.FieldExtensions.DelegateForSetFieldValue(appFactoryType, "_state")(appFactory, HttpContext.Current.Application);

                var identityStore = new SessionIdentityStore(user);
                var container     = ServiceFactory.CreateContainer(identityStore, mockFactoryCallback);

                container.AddService <IIdentityStore>(identityStore);
                Webx.Container = container;
                TController c = new TController();

                c.ControllerContext = new Web.Mvc.ControllerContext(new Routing.RequestContext(new HttpContextWrapper(httpContext), new Routing.RouteData()), c);
                return(c);
            }