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 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);
        }
 public StubHttpRequest(StubHttpContext httpContext)
 {
     _httpContext = httpContext;
 }
 public StubHttpResponse(StubHttpContext httpContext)
 {
     _httpContext = httpContext;
 }
 public StubHttpRequest(StubHttpContext httpContext)
 {
     _httpContext = httpContext;
 }
 public StubHttpResponse(StubHttpContext httpContext)
 {
     _httpContext = httpContext;
 }
        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 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);
        }
        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 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>()));
        }