Beispiel #1
0
        public void DoesntCacheNonReusableHandlers()
        {
            MockRepository     mocks = new MockRepository();
            TestHandlerFactory f     = (TestHandlerFactory)mocks.PartialMock(typeof(TestHandlerFactory));
            IHttpHandler       nonReusableHandler = (IHttpHandler)mocks.DynamicMock(typeof(IHttpHandler));

            Expect.Call(nonReusableHandler.IsReusable).Return(false);
            IHttpHandler nonReusableHandler2 = (IHttpHandler)mocks.DynamicMock(typeof(IHttpHandler));

            Expect.Call(nonReusableHandler2.IsReusable).Return(false);
            IConfigurableApplicationContext appCtx = (IConfigurableApplicationContext)mocks.DynamicMock(typeof(IConfigurableApplicationContext));

            // if (IHttpHandler.IsReusable == false) => always create new handler instance
            // - CreateHandlerInstance() is called for each request
            using (Record(mocks))
            {
                Expect.Call(f.GetContextStub("notreusable")).Return(appCtx);
                Expect.Call(f.CreateHandlerInstanceStub(appCtx, null, null, "notreusable", null)).Return(nonReusableHandler);
                Expect.Call(f.GetContextStub("notreusable")).Return(appCtx);
                Expect.Call(f.CreateHandlerInstanceStub(appCtx, null, null, "notreusable", null)).Return(nonReusableHandler2);
            }
            using (Playback(mocks))
            {
                Assert.AreSame(nonReusableHandler, f.GetHandler(null, null, "notreusable", null));
                Assert.AreSame(nonReusableHandler2, f.GetHandler(null, null, "notreusable", null));
            }
        }
Beispiel #2
0
        public void GetCheckedApplicationContextThrowsExceptionsOnNonConfigurableContexts()
        {
            MockRepository                  mocks             = new MockRepository();
            TestHandlerFactory              f                 = (TestHandlerFactory)mocks.PartialMock(typeof(TestHandlerFactory));
            IApplicationContext             simpleAppContext  = (IApplicationContext)mocks.DynamicMock(typeof(IApplicationContext));
            IConfigurableApplicationContext allowedAppContext = (IConfigurableApplicationContext)mocks.DynamicMock(typeof(IConfigurableApplicationContext));

            using (Record(mocks))
            {
                Expect.Call(f.GetContextStub("/NullContext")).Return(null);
                Expect.Call(f.GetContextStub("/NonConfigurableContext")).Return(simpleAppContext);
                Expect.Call(f.GetContextStub("/AllowedContext")).Return(allowedAppContext);
            }

            // (context == null) -> ArgumentException
            try
            {
                f.GetCheckedApplicationContext("/NullContext");
                Assert.Fail("should throw ArgumentException");
            }
            catch (ArgumentException)
            {}

            // !(context is IConfigurableApplicationContext) -> InvalidOperationException
            try
            {
                f.GetCheckedApplicationContext("/NonConfigurableContext");
                Assert.Fail("should throw InvalidOperationException");
            }
            catch (InvalidOperationException)
            {}

            // (context is IConfigurableApplicationContext) -> OK
            Assert.AreSame(allowedAppContext, f.GetCheckedApplicationContext("/AllowedContext"));
        }
        public void GetCheckedApplicationContextThrowsExceptionsOnNonConfigurableContexts()
        {
            TestHandlerFactory              f = A.Fake <TestHandlerFactory>(options => options.CallsBaseMethods());
            IApplicationContext             simpleAppContext  = A.Fake <IApplicationContext>();
            IConfigurableApplicationContext allowedAppContext = A.Fake <IConfigurableApplicationContext>();

            A.CallTo(() => f.GetContextStub("/NullContext")).Returns(null);
            A.CallTo(() => f.GetContextStub("/NonConfigurableContext")).Returns(simpleAppContext);
            A.CallTo(() => f.GetContextStub("/AllowedContext")).Returns(allowedAppContext);

            // (context == null) -> ArgumentException
            try
            {
                f.GetCheckedApplicationContext("/NullContext");
                Assert.Fail("should throw ArgumentException");
            }
            catch (ArgumentException)
            {
            }

            // !(context is IConfigurableApplicationContext) -> InvalidOperationException
            try
            {
                f.GetCheckedApplicationContext("/NonConfigurableContext");
                Assert.Fail("should throw InvalidOperationException");
            }
            catch (InvalidOperationException)
            {
            }

            // (context is IConfigurableApplicationContext) -> OK
            Assert.AreSame(allowedAppContext, f.GetCheckedApplicationContext("/AllowedContext"));
        }
        public void CachesReusableHandlers()
        {
            TestHandlerFactory f = A.Fake <TestHandlerFactory>(options => options.CallsBaseMethods());
            IHttpHandler       reusableHandler     = A.Fake <IHttpHandler>();
            IConfigurableApplicationContext appCtx = A.Fake <IConfigurableApplicationContext>();

            // if (IHttpHandler.IsReusable == true) => always returns the same handler instance
            // - CreateHandlerInstance() is only called once
            A.CallTo(() => reusableHandler.IsReusable).Returns(true);
            A.CallTo(() => f.GetContextStub("reusable")).Returns(appCtx);
            A.CallTo(() => f.CreateHandlerInstanceStub(appCtx, null, null, "reusable", null)).Returns(reusableHandler);

            Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
            Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
        }
Beispiel #5
0
        public void CachesReusableHandlers()
        {
            MockRepository     mocks               = new MockRepository();
            TestHandlerFactory f                   = (TestHandlerFactory)mocks.PartialMock(typeof(TestHandlerFactory));
            IHttpHandler       reusableHandler     = (IHttpHandler)mocks.DynamicMock(typeof(IHttpHandler));
            IConfigurableApplicationContext appCtx = (IConfigurableApplicationContext)mocks.DynamicMock(typeof(IConfigurableApplicationContext));

            // if (IHttpHandler.IsReusable == true) => always returns the same handler instance
            // - CreateHandlerInstance() is only called once
            using (Record(mocks))
            {
                Expect.Call(reusableHandler.IsReusable).Return(true);
                Expect.Call(f.GetContextStub("reusable")).Return(appCtx);
                Expect.Call(f.CreateHandlerInstanceStub(appCtx, null, null, "reusable", null)).Return(reusableHandler);
            }
            using (Playback(mocks))
            {
                Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
                Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
            }
        }
        public void DoesntCacheNonReusableHandlers()
        {
            TestHandlerFactory f = A.Fake <TestHandlerFactory>(options => options.CallsBaseMethods());
            IHttpHandler       nonReusableHandler = A.Fake <IHttpHandler>();

            A.CallTo(() => nonReusableHandler.IsReusable).Returns(false);
            IHttpHandler nonReusableHandler2 = A.Fake <IHttpHandler>();

            A.CallTo(() => nonReusableHandler2.IsReusable).Returns(false);
            IConfigurableApplicationContext appCtx = A.Fake <IConfigurableApplicationContext>();

            // if (IHttpHandler.IsReusable == false) => always create new handler instance
            // - CreateHandlerInstance() is called for each request

            A.CallTo(() => f.GetContextStub("notreusable")).Returns(appCtx);
            A.CallTo(() => f.CreateHandlerInstanceStub(appCtx, null, null, "notreusable", null))
            .Returns(nonReusableHandler).Once()
            .Then.Returns(nonReusableHandler2).Once();

            Assert.AreSame(nonReusableHandler, f.GetHandler(null, null, "notreusable", null));
            Assert.AreSame(nonReusableHandler2, f.GetHandler(null, null, "notreusable", null));
        }