Example #1
0
        public void ExecuteResult() {
            // Arrange
            object data = _jsonData;
            string contentType = "Some content type.";
            Encoding contentEncoding = Encoding.UTF8;

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable();

            JsonResult result = new JsonResult {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
        public void OnException() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute() {
                View = "SomeView",
                Master = "SomeMaster",
                ExceptionType = typeof(ArgumentException) 
            };
            Exception exception = new ArgumentNullException();

            Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>(MockBehavior.Strict);
            mockHttpContext.Expect(c => c.IsCustomErrorEnabled).Returns(true);
            mockHttpContext.Expect(c => c.Session).Returns((HttpSessionStateBase)null);
            mockHttpContext.Expect(c => c.Response.Clear()).Verifiable();
            mockHttpContext.ExpectSet(c => c.Response.StatusCode, 500).Verifiable();
            mockHttpContext.ExpectSet(c => c.Response.TrySkipIisCustomErrors, true).Verifiable(); 
            
            TempDataDictionary tempData = new TempDataDictionary();
            IViewEngine viewEngine = new Mock<IViewEngine>().Object;
            Controller controller = new Mock<Controller>().Object;
            controller.TempData = tempData;

            ExceptionContext context = GetExceptionContext(mockHttpContext.Object, controller, exception);

            // Exception
            attr.OnException(context);

            // Assert
            mockHttpContext.Verify();
            ViewResult viewResult = context.Result as ViewResult;
            Assert.IsNotNull(viewResult, "The Result property should have been set to an instance of ViewResult.");
            Assert.AreEqual(tempData, viewResult.TempData);
            Assert.AreEqual("SomeView", viewResult.ViewName);
            Assert.AreEqual("SomeMaster", viewResult.MasterName);

            HandleErrorInfo viewData = viewResult.ViewData.Model as HandleErrorInfo;
            Assert.IsNotNull(viewData, "The ViewData model should have been set to an instance of ExceptionViewData.");
            Assert.AreSame(exception, viewData.Exception);
            Assert.AreEqual("SomeController", viewData.ControllerName);
            Assert.AreEqual("SomeAction", viewData.ActionName);
        }
        public void ExecuteResult() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.StatusCode, 401).Verifiable();

            HttpUnauthorizedResult result = new HttpUnauthorizedResult();

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Example #4
0
        public void EmptyContentTypeRendersDefault() {
            // Arrange
            object data = _jsonData;
            Encoding contentEncoding = Encoding.UTF8;

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/json").Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable();

            JsonResult result = new JsonResult {
                Data = data,
                ContentType = String.Empty,
                ContentEncoding = contentEncoding
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Example #5
0
        public void CreateClassMockTest()
        {
            var mock = new Mock<Mocked>();

            mock.ExpectCall(m => m.Method(2)).Returns("2").Once();
            mock.ExpectGet(m => m.Property).Gets(2).Never();
            mock.ExpectSet(m => m.Property).Sets(2).Never();

            var value = mock.Object.Method(2);

            mock.Verify();

            Assert.That(value, Is.EqualTo("2"));
        }
Example #6
0
        public void ExecuteResultDoesNotSetContentDispositionIfNotSpecified() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type");

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
        public void ConfigureSessionState_Default_DoesNothing() {
            // Arrange
            Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
            mockHttpContext.ExpectSet(o => o.Handler).Never();
            HttpContextBase httpContext = mockHttpContext.Object;

            DynamicSessionStateConfigurator35 configurator = new DynamicSessionStateConfigurator35(httpContext);

            // Act
            configurator.ConfigureSessionState(ControllerSessionState.Default);
            IHttpHandler newHandler = httpContext.Handler;

            // Assert
            mockHttpContext.Verify();
        }
        public void ExecuteResult() {
            // Arrange
            string content = "Some content.";
            string contentType = "Some content type.";
            Encoding contentEncoding = Encoding.UTF8;
            
            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.Write(content)).Verifiable();

            ContentResult result = new ContentResult {
                Content = content,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
        public void NullScriptIsNotOutput() {
            // Arrange
            string contentType = "application/x-javascript";

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();

            JavaScriptResult result = new JavaScriptResult();

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Example #10
0
        public void ContentDispositionHeaderIsEncodedCorrectlyForUnicodeCharacters() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", @"attachment; filename*=UTF-8''ABCXYZabcxyz012789!%40%23$%25%5E&%2A%28%29-%3D_+.:~%CE%94")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = "[email protected]#$%^&*()-=_+.:~Δ"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Example #11
0
        public void ExecuteResultSetsContentDispositionIfSpecified() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=filename.ext")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = "filename.ext"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Example #12
0
        public void ContentDispositionHeaderIsEncodedCorrectly() {
            // See comment in FileResult.cs detailing how the FileDownloadName should be encoded.

            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", @"attachment; filename=""some\\file""")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = @"some\file"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
        public void ExecuteResult() {
            // Arrange
            string script = "alert('foo');";
            string contentType = "application/x-javascript";

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.Write(script)).Verifiable();

            JavaScriptResult result = new JavaScriptResult {
                Script = script
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Example #14
0
        public void NullContentIsNotOutput() {
            // Arrange
            string contentType = "Some content type.";
            Encoding contentEncoding = Encoding.UTF8;

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable();

            JsonResult result = new JsonResult {
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
        public void OnResultExecuting_ItemsFlagSet_ReplacesOutputWriter() {
            // Arrange
            TextWriter originalWriter = TextWriter.Null;
            TextWriter newWriter = null;

            Mock<ResultExecutingContext> mockFilterContext = new Mock<ResultExecutingContext>();
            mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny<object>()]).Returns("theCacheKey");
            mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(originalWriter);
            mockFilterContext
                .ExpectSet(o => o.HttpContext.Response.Output)
                .Callback(w => { newWriter = w; });

            // Act
            ChildActionCacheAttribute attr = new ChildActionCacheAttribute() {
                Duration = 10
            };

            // Act
            attr.OnResultExecuting(mockFilterContext.Object);

            // Assert
            ChildActionCacheAttribute.WrappedStringWriter stringWriter = newWriter as ChildActionCacheAttribute.WrappedStringWriter;
            Assert.IsNotNull(stringWriter);
            Assert.AreEqual(originalWriter, stringWriter.OriginalWriter);
        }
Example #16
0
        public void NullContentEncodingIsNotOutput() {
            // Arrange
            object data = _jsonData;
            string contentType = "Some content type.";

            // Arrange expectations
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable();

            JsonResult result = new JsonResult {
                Data = data,
                ContentType = contentType,
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
        public void OnResultExecuted_ItemsFlagSet_NoException_DumpsContentAndAddsToCache() {
            // Arrange
            TextWriter originalWriter = TextWriter.Null;

            ChildActionCacheAttribute.WrappedStringWriter wrappedWriter = new ChildActionCacheAttribute.WrappedStringWriter(originalWriter);
            wrappedWriter.Write("captured text");

            Mock<ResultExecutedContext> mockFilterContext = new Mock<ResultExecutedContext>();
            mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny<object>()]).Returns("theCacheKey");
            mockFilterContext.Expect(o => o.Exception).Returns((Exception)null);

            TextWriter currentResponseWriter = wrappedWriter;
            mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(() => currentResponseWriter);
            mockFilterContext
                .ExpectSet(o => o.HttpContext.Response.Output)
                .Callback(w => { currentResponseWriter = w; });
            mockFilterContext
                .Expect(o => o.HttpContext.Response.Write("captured text"))
                .Callback(() => { Assert.AreEqual(originalWriter, currentResponseWriter, "Response.Write() called while wrong writer was active."); })
                .AtMostOnce()
                .Verifiable();

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache.Expect(o => o.SetCacheItem("theCacheKey", "captured text")).Verifiable();

            // Act
            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            attr.OnResultExecuted(mockFilterContext.Object);

            // Assert
            mockFilterContext.Verify();
            mockCache.Verify();
            Assert.AreEqual(originalWriter, currentResponseWriter);
        }