public void Init() { var builder = new ContainerBuilder(); builder.RegisterType<DefaultProcessingEngine>().As<IProcessingEngine>(); builder.RegisterModule(new WorkContextModule()); builder.RegisterType<WorkContextAccessor>().As<IWorkContextAccessor>(); builder.RegisterAutoMocking(MockBehavior.Loose); _container = builder.Build(); _shellContext = new ShellContext { Descriptor = new ShellDescriptor(), Settings = new ShellSettings(), LifetimeScope = _container.BeginLifetimeScope(), }; var httpContext = new StubHttpContext(); _container.Mock<IShellContextFactory>() .Setup(x => x.CreateDescribedContext(_shellContext.Settings, _shellContext.Descriptor)) .Returns(_shellContext); _container.Mock<IHttpContextAccessor>() .Setup(x=>x.Current()) .Returns(httpContext); _container.Mock<IHttpContextAccessor>() .Setup(x => x.CreateContext(It.IsAny<ILifetimeScope>())) .Returns(httpContext); }
public void DifferentChildScopesWillNotCollideInTheSameHttpContext() { var shell1 = _container.BeginLifetimeScope(); var accessor1 = shell1.Resolve<IWorkContextAccessor>(); var shell2 = _container.BeginLifetimeScope(); var accessor2 = shell2.Resolve<IWorkContextAccessor>(); var httpContext = new StubHttpContext(); Assert.That(accessor1.GetContext(httpContext), Is.Null); Assert.That(accessor2.GetContext(httpContext), Is.Null); var scope1 = accessor1.CreateWorkContextScope(httpContext); Assert.That(accessor1.GetContext(httpContext), Is.Not.Null); Assert.That(accessor2.GetContext(httpContext), Is.Null); var scope2 = accessor2.CreateWorkContextScope(httpContext); Assert.That(accessor1.GetContext(httpContext), Is.Not.Null); Assert.That(accessor2.GetContext(httpContext), Is.Not.Null); scope1.Dispose(); Assert.That(accessor1.GetContext(httpContext), Is.Null); Assert.That(accessor2.GetContext(httpContext), Is.Not.Null); scope2.Dispose(); Assert.That(accessor1.GetContext(httpContext), Is.Null); Assert.That(accessor2.GetContext(httpContext), Is.Null); }
private static ActionExecutedContext BuildContext() { var httpContext = new StubHttpContext(); var routeData = new RouteData(); var controllerContext = new ControllerContext(httpContext, routeData, new Mock<ControllerBase>().Object); var actionDescriptor = new Mock<ActionDescriptor>().Object; return new ActionExecutedContext(controllerContext, actionDescriptor, false/*cancelled*/, null/*exception*/); }
public void ScopeIsCreatedAndCanBeRetrievedFromHttpContextBase() { var accessor = _container.Resolve<IWorkContextAccessor>(); var httpContext = new StubHttpContext(); var workContextScope = accessor.CreateWorkContextScope(httpContext); Assert.That(workContextScope.WorkContext, Is.Not.Null); var workContext = accessor.GetContext(httpContext); Assert.That(workContext, Is.SameAs(workContextScope.WorkContext)); }
public void ContextIsNullAfterDisposingScope() { var accessor = _container.Resolve<IWorkContextAccessor>(); var httpContext = new StubHttpContext(); Assert.That(accessor.GetContext(httpContext), Is.Null); var scope = accessor.CreateWorkContextScope(httpContext); Assert.That(accessor.GetContext(httpContext), Is.Not.Null); scope.Dispose(); Assert.That(accessor.GetContext(httpContext), Is.Null); }
private static RequestContext GetRequestContext(StubContainerProvider containerProvider) { var handler = new MvcRouteHandler(); var route = new Route("yadda", handler) { DataTokens = new RouteValueDictionary {{"IContainerProvider", containerProvider}} }; var httpContext = new StubHttpContext(); var routeData = route.GetRouteData(httpContext); return new RequestContext(httpContext, routeData); }
public void DifferentHttpContextWillHoldDifferentWorkContext() { var accessor = _container.Resolve<IWorkContextAccessor>(); var httpContext1 = new StubHttpContext(); var workContextScope1 = accessor.CreateWorkContextScope(httpContext1); var workContext1 = accessor.GetContext(httpContext1); var httpContext2 = new StubHttpContext(); var workContextScope2 = accessor.CreateWorkContextScope(httpContext2); var workContext2 = accessor.GetContext(httpContext2); Assert.That(workContext1, Is.Not.Null); Assert.That(workContext1, Is.SameAs(workContextScope1.WorkContext)); Assert.That(workContext2, Is.Not.Null); Assert.That(workContext2, Is.SameAs(workContextScope2.WorkContext)); Assert.That(workContext1, Is.Not.SameAs(workContext2)); }
public void NormalExecutionReturnsExpectedObjects() { var settings = new ShellSettings { Name = ShellSettings.DefaultName }; var descriptor = new ShellDescriptor { SerialNumber = 6655321 }; var blueprint = new ShellBlueprint(); var shellLifetimeScope = _container.BeginLifetimeScope("shell"); var httpContext = new StubHttpContext(); _container.Mock<IShellDescriptorCache>() .Setup(x => x.Fetch(ShellSettings.DefaultName)) .Returns(descriptor); _container.Mock<ICompositionStrategy>() .Setup(x => x.Compose(settings, descriptor)) .Returns(blueprint); _container.Mock<IShellContainerFactory>() .Setup(x => x.CreateContainer(settings, blueprint)) .Returns(shellLifetimeScope); _container.Mock<IShellDescriptorManager>() .Setup(x => x.GetShellDescriptor()) .Returns(descriptor); _container.Mock<IWorkContextEvents>() .Setup(x => x.Started()); _container.Mock<IHttpContextAccessor>() .Setup(x => x.Current()) .Returns(httpContext); _container.Mock<IHttpContextAccessor>() .Setup(x => x.CreateContext(It.IsAny<ILifetimeScope>())) .Returns(httpContext); var factory = _container.Resolve<IShellContextFactory>(); var context = factory.CreateShellContext(settings); Assert.That(context.Settings, Is.SameAs(settings)); Assert.That(context.Descriptor, Is.SameAs(descriptor)); Assert.That(context.Blueprint, Is.SameAs(blueprint)); Assert.That(context.LifetimeScope, Is.SameAs(shellLifetimeScope)); Assert.That(context.Shell, Is.SameAs(shellLifetimeScope.Resolve<IOrchardShell>())); }
public void MatchingRouteToActiveShellTableWillLimitTheAbilityToMatchRoutes() { var routeFoo = new Route("foo", new MvcRouteHandler()); _settingsA.RequestUrlHost = "a.example.com"; _containerA.Resolve<IRoutePublisher>().Publish( new[] { new RouteDescriptor { Priority = 0, Route = routeFoo } }); _rootContainer.Resolve<IRunningShellTable>().Add(_settingsA); _settingsB.RequestUrlHost = "b.example.com"; _containerB.Resolve<IRoutePublisher>().Publish( new[] { new RouteDescriptor { Priority = 0, Route = routeFoo } }); _rootContainer.Resolve<IRunningShellTable>().Add(_settingsB); var httpContext = new StubHttpContext("~/foo"); var routeData = _routes.GetRouteData(httpContext); Assert.That(routeData, Is.Null); var httpContextA = new StubHttpContext("~/foo", "a.example.com"); var routeDataA = _routes.GetRouteData(httpContextA); Assert.That(routeDataA, Is.Not.Null); Assert.That(routeDataA.DataTokens.ContainsKey("IWorkContextAccessor"), Is.True); var workContextAccessorA = (IWorkContextAccessor)routeDataA.DataTokens["IWorkContextAccessor"]; var workContextScopeA = workContextAccessorA.CreateWorkContextScope(httpContextA); Assert.That(workContextScopeA.Resolve<IRoutePublisher>(), Is.SameAs(_containerA.Resolve<IRoutePublisher>())); Assert.That(workContextScopeA.Resolve<IRoutePublisher>(), Is.Not.SameAs(_containerB.Resolve<IRoutePublisher>())); var httpContextB = new StubHttpContext("~/foo", "b.example.com"); var routeDataB = _routes.GetRouteData(httpContextB); Assert.That(routeDataB, Is.Not.Null); Assert.That(routeDataB.DataTokens.ContainsKey("IWorkContextAccessor"), Is.True); var workContextAccessorB = (IWorkContextAccessor)routeDataB.DataTokens["IWorkContextAccessor"]; var workContextScopeB = workContextAccessorB.CreateWorkContextScope(httpContextB); Assert.That(workContextScopeB.Resolve<IRoutePublisher>(), Is.SameAs(_containerB.Resolve<IRoutePublisher>())); Assert.That(workContextScopeB.Resolve<IRoutePublisher>(), Is.Not.SameAs(_containerA.Resolve<IRoutePublisher>())); }
public StubHttpResponse(StubHttpContext httpContext) { _httpContext = httpContext; }
public StubHttpRequest(StubHttpContext httpContext) { _httpContext = httpContext; }
public void FunctionsByDefaultAgainstAmbientHttpContext() { var accessor = _container.Resolve<IWorkContextAccessor>(); var explicitHttpContext = new StubHttpContext(); var ambientHttpContext = new StubHttpContext(); _httpContextCurrent = ambientHttpContext; Assert.That(accessor.GetContext(), Is.Null); Assert.That(accessor.GetContext(ambientHttpContext), Is.Null); Assert.That(accessor.GetContext(explicitHttpContext), Is.Null); var scope = accessor.CreateWorkContextScope(); Assert.That(accessor.GetContext(), Is.Not.Null); Assert.That(accessor.GetContext(ambientHttpContext), Is.Not.Null); Assert.That(accessor.GetContext(explicitHttpContext), Is.Null); Assert.That(accessor.GetContext(), Is.SameAs(accessor.GetContext(ambientHttpContext))); _httpContextCurrent = explicitHttpContext; Assert.That(accessor.GetContext(), Is.Null); _httpContextCurrent = ambientHttpContext; Assert.That(accessor.GetContext(), Is.Not.Null); scope.Dispose(); Assert.That(accessor.GetContext(), Is.Null); }