Esempio n. 1
0
        public void ProcessRequestWithNormalControlFlowForAsynchronousController()
        {
            // Arrange
            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Response.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();

            RequestContext  requestContext = new RequestContext(mockHttpContext.Object, GetRouteData());
            MvcAsyncHandler handler        = new MvcAsyncHandler(requestContext);

            MockAsyncResult         asyncResult    = new MockAsyncResult();
            Mock <IAsyncController> mockController = new Mock <IAsyncController>();

            mockController.Expect(c => c.BeginExecute(requestContext, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(asyncResult).Verifiable();
            mockController.Expect(c => c.EndExecute(asyncResult)).Verifiable();
            mockController.As <IDisposable>().Expect(c => c.Dispose()).AtMostOnce().Verifiable();

            ControllerBuilder builder = new ControllerBuilder();

            builder.SetControllerFactory(new SimpleControllerFactory(mockController.Object));
            handler.ControllerBuilder = builder;

            // Act
            IAsyncResult returnedAsyncResult = handler.BeginProcessRequest(mockHttpContext.Object, null, null);

            handler.EndProcessRequest(returnedAsyncResult);

            mockHttpContext.Verify();
            mockController.Verify();
        }
Esempio n. 2
0
        public void ProcessRequestWhereControllerBeginExecuteThrows()
        {
            // Arrange
            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Response.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();

            RequestContext  requestContext = new RequestContext(mockHttpContext.Object, GetRouteData());
            MvcAsyncHandler handler        = new MvcAsyncHandler(requestContext);

            Mock <IAsyncController> mockController = new Mock <IAsyncController>();

            mockController.Expect(c => c.BeginExecute(requestContext, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Throws(new InvalidOperationException("Some exception text.")).Verifiable();
            mockController.As <IDisposable>().Expect(c => c.Dispose()).AtMostOnce().Verifiable();

            ControllerBuilder builder = new ControllerBuilder();

            builder.SetControllerFactory(new SimpleControllerFactory(mockController.Object));
            handler.ControllerBuilder = builder;

            // Act
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                handler.BeginProcessRequest(mockHttpContext.Object, null, null);
            },
                @"Some exception text.");

            mockHttpContext.Verify();
            mockController.Verify();
        }
Esempio n. 3
0
        public void ProcessRequestWithRouteWithoutControllerThrows()
        {
            // Arrange
            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Response.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();

            RequestContext  requestContext = new RequestContext(mockHttpContext.Object, new RouteData());
            MvcAsyncHandler handler        = new MvcAsyncHandler(requestContext);

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                handler.BeginProcessRequest(mockHttpContext.Object, null, null);
            },
                @"The RouteData must contain an item named 'controller' with a non-empty string value.");

            mockHttpContext.Verify();
        }
Esempio n. 4
0
        public void ProcessRequestThrowsIfControllerNotFound()
        {
            // Arrange
            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Response.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();

            RequestContext  requestContext = new RequestContext(mockHttpContext.Object, GetRouteData());
            MvcAsyncHandler handler        = new MvcAsyncHandler(requestContext);

            ControllerBuilder builder = new ControllerBuilder();

            builder.SetControllerFactory(new SimpleControllerFactory(null));
            handler.ControllerBuilder = builder;

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                handler.BeginProcessRequest(mockHttpContext.Object, null, null);
            },
                @"The IControllerFactory 'Microsoft.Web.Mvc.Test.MvcAsyncHandlerTest+SimpleControllerFactory' did not return a controller for a controller named 'Foo'.");

            mockHttpContext.Verify();
        }