Exemplo n.º 1
0
        public void ErrorReturnsTheCorrectView_WhenStatusCodeIs404()
        {
            var controller = new ErrorController();
            var result     = (ViewResult)controller.Error(404);

            Assert.Equal("404", result.ViewName);
        }
Exemplo n.º 2
0
        public void ErrorController_NotFoundObjectResult()
        {
            // Arrange
            var mockEnv       = Mock.Of <IWebHostEnvironment>();
            var mockLog       = Mock.Of <ILogger <ErrorController> >();
            var mockException = Mock.Of <IExceptionHandlerFeature>(x => x.Error == new NotFoundException("Not found"));

            var context = new DefaultHttpContext();

            context.Features.Set <IExceptionHandlerFeature>(mockException);

            var controller = new ErrorController(mockEnv, mockLog)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = context
                }
            };

            // Act
            IActionResult actionResult = controller.Error();

            // Assert
            var notFoundResult = Assert.IsType <NotFoundObjectResult>(actionResult);
            var contentResult  = Assert.IsType <ErrorModel>(notFoundResult.Value);
            var expected       = "Not found";
            var actual         = contentResult.Error;

            Assert.Equal(expected, actual);
        }
Exemplo n.º 3
0
        public void ErrorReturnsTheCorrectView_WhenStatusCodeIsNull()
        {
            var controller = new ErrorController();
            var result     = (ViewResult)controller.Error(null);

            Assert.Equal("~/Views/Shared/Error.cshtml", result.ViewName);
        }
Exemplo n.º 4
0
        public async Task ShouldRedirectIfPageNotFoundButMatchedALegacyRedirect()
        {
            const string url = "/a-url";
            var          redirectedToLocation = @"/redirected-to-location-from-the-rule";

            _legacyRedirects.Setup(o => o.RedirectUrl(url)).ReturnsAsync(redirectedToLocation);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path        = "/test";
            httpContext.Response.StatusCode = 404;
            var mockHttpContext = new ControllerContext
            {
                HttpContext = httpContext
            };
            var controller = new ErrorController(_legacyRedirects.Object, _logger.Object)
            {
                ControllerContext = mockHttpContext
            };

            controller.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
            {
                OriginalPath = url
            });

            var result = await controller.Error() as RedirectResult;

            result.Url.Should().Be(redirectedToLocation);
            result.Permanent.Should().BeTrue();
        }
Exemplo n.º 5
0
        public void ErrorHandlerFilter_BadRequestObjectResult_Production()
        {
            // Arrange
            var mockEnv       = Mock.Of <IWebHostEnvironment>(x => x.EnvironmentName == "Production");
            var mockLog       = Mock.Of <ILogger <ErrorController> >();
            var mockException = Mock.Of <IExceptionHandlerFeature>(x => x.Error == new ArgumentException("Should not be displayed"));

            var context = new DefaultHttpContext();

            context.Features.Set <IExceptionHandlerFeature>(mockException);

            var controller = new ErrorController(mockEnv, mockLog)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = context
                }
            };

            // Act
            IActionResult actionResult = controller.Error();

            // Assert
            var notFoundResult = Assert.IsType <BadRequestObjectResult>(actionResult);
            var contentResult  = Assert.IsType <ErrorModel>(notFoundResult.Value);
            var expected       = "An error occured. Please try again later.";
            var actual         = contentResult.Error;

            Assert.Equal(expected, actual);
        }
Exemplo n.º 6
0
        public async Task ShouldTellUsSomethingsMissingIfAPageWasNotFound()
        {
            const string url = "/a-url";

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path        = "/pathThatDoesntExist";
            httpContext.Response.StatusCode = 404;
            var mockHttpContext = new ControllerContext
            {
                HttpContext = httpContext
            };
            var controller = new ErrorController(_legacyRedirects.Object, _logger.Object)
            {
                ControllerContext = mockHttpContext
            };

            controller.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
            {
                OriginalPath = "/OriginalPath"
            });

            _legacyRedirects.Setup(o => o.RedirectUrl(url)).ReturnsAsync(string.Empty);
            var result = await controller.Error() as ViewResult;

            result.ViewData[@"ErrorHeading"].Should().Be("Something's missing");
        }
Exemplo n.º 7
0
        public async Task ShouldLogInformationIfLegacyUrlfound()
        {
            const string url = "/a-url";
            var          redirectedToLocation = @"/redirected-to-location-from-the-rule";

            _legacyRedirects.Setup(o => o.RedirectUrl(url)).ReturnsAsync(redirectedToLocation);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path        = "/test";
            httpContext.Response.StatusCode = 404;
            var mockHttpContext = new ControllerContext
            {
                HttpContext = httpContext
            };
            var controller = new ErrorController(_legacyRedirects.Object, _logger.Object)
            {
                ControllerContext = mockHttpContext
            };

            controller.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
            {
                OriginalPath = url
            });

            await controller.Error();

            LogTesting.Assert(_logger, LogLevel.Information,
                              $"A legacy redirect was found - redirecting to {redirectedToLocation}");
        }
Exemplo n.º 8
0
        public async Task ShouldLogInformationIfLegacyUrlNotfound()
        {
            const string url = "/a-url";

            _legacyRedirects.Setup(o => o.RedirectUrl(url)).ReturnsAsync(string.Empty);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path        = "/test";
            httpContext.Response.StatusCode = 404;
            var mockHttpContext = new ControllerContext
            {
                HttpContext = httpContext
            };
            var controller = new ErrorController(_legacyRedirects.Object, _logger.Object)
            {
                ControllerContext = mockHttpContext
            };

            controller.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
            {
                OriginalPath = url
            });

            await controller.Error();

            LogTesting.Assert(_logger, LogLevel.Information,
                              $"No legacy url matching current url ({url}) found");
        }
Exemplo n.º 9
0
        public void Error_Returns_View()
        {
            var controller = new ErrorController();

            var result = controller.Error();

            Assert.IsType <ViewResult>(result);
        }
        public void Error404ReturnsPageNotFound()
        {
            var controller = new ErrorController(_logger, _httpContextAccessor);

            // Act and Assert
            controller.Error(404).Should()
            .BeViewResult().WithViewName("PageNotFound");
        }
        public void ErrorReturnsErrorPage()
        {
            //Arrange
            var controller = new ErrorController(_logger, _httpContextAccessor);

            // Act and Assert
            controller.Error(500).Should()
            .BeViewResult().WithViewName("Error");
        }
Exemplo n.º 12
0
        public void Error_ReturnsAViewResult_WithAnErrorViewModel()
        {
            ViewResult result = (ViewResult)controller.Error();

            // Assert
            Assert.IsInstanceOf <ViewResult>(result);
            Assert.IsAssignableFrom <ErrorViewModel>(result.ViewData.Model);
            Assert.IsNotEmpty(((ErrorViewModel)result.ViewData.Model).RequestId);
        }
Exemplo n.º 13
0
        public void ErrorOk()
        {
            // setup
            var controller = new ErrorController();

            // action
            var result = controller.Error() as ViewResult;

            // assert
            Assert.IsType(typeof(ViewResult), result);
        }
        public void Error()
        {
            // Arrange
            var controller = new ErrorController();

            // Act
            var result = controller.Error() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 15
0
        public void ErrorControllerTest_Error()
        {
            var controller = new ErrorController(new AppSettings())
            {
                ControllerContext = { HttpContext = new DefaultHttpContext() }
            };

            var actionResult = controller.Error(404) as PhysicalFileResult;

            Assert.AreEqual("text/html", actionResult.ContentType);
        }
Exemplo n.º 16
0
        public void ExceptionHandler()
        {
            var ctl = new ErrorController(_loggerMock.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            var result = ctl.Error();

            Assert.IsInstanceOf(typeof(ViewResult), result);
        }
Exemplo n.º 17
0
        public int TestUnknownStatusCodes(int statusCode)
        {
            var ctl = new ErrorController(_loggerMock.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            var result = ctl.Error(statusCode);

            Assert.IsInstanceOf(typeof(StatusCodeResult), result);
            return(ctl.ControllerContext.HttpContext.Response.StatusCode);
        }
Exemplo n.º 18
0
        public void Error(int?code)
        {
            // Act
            var result = _controller.Error(code);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            var model = Assert.IsAssignableFrom <ErrorViewModel>(
                viewResult.ViewData.Model
                );

            Assert.Equal(code ?? 404, model.Code);
        }
        public void When_InvokingError_Expect_ProblemDetails()
        {
            // Arrange
            ErrorController controller = new ErrorController
            {
                ProblemDetailsFactory = new MockProblemDetailsFactory(),
            };

            // Act
            ActionResult result = controller.Error();

            // Assert
            ObjectResult objectResult = Assert.IsType <ObjectResult>(result);

            Assert.IsType <ProblemDetails>(objectResult.Value);
        }
Exemplo n.º 20
0
        public void Error()
        {
            var routes = new RouteCollection();

            Console.WriteLine("Error View.\n");
            MRPEntities db = new MRPEntities();

            HttpContext.Current = DataHelper.SetUserAndPermission();
            ErrorController objErrorController = new ErrorController();

            objErrorController.ControllerContext = new ControllerContext(MockHelpers.FakeUrlHelper.FakeHttpContext(), new RouteData(), objErrorController);
            objErrorController.Url = MockHelpers.FakeUrlHelper.UrlHelper();
            var result = objErrorController.Error() as ViewResult;

            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name + " \n The Assert Value result.ViewName:  " + result.ViewName);
            Assert.IsNotNull(result.ViewName);
        }
Exemplo n.º 21
0
        public void ErrorControllerErrorActionReturnsSuccess()
        {
            // Arrange
            using var errorController = new ErrorController(fakeLogger, fakeVersionedFiles, fakeConfiguration)
                  {
                      ControllerContext = new ControllerContext()
                      {
                          HttpContext = new DefaultHttpContext(),
                      },
                  };

            // Act
            var result = errorController.Error();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <PageViewModel>(viewResult.ViewData.Model);

            Assert.Contains("Error", model.PageTitle, System.StringComparison.OrdinalIgnoreCase);
        }
Exemplo n.º 22
0
        public void ErrorControllerErrorActionReturnsSuccess()
        {
            // Arrange
            string expectedUrl     = $"/{ApplicationController.AlertPathName}/500";
            var    errorController = new ErrorController(fakeLogger)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext(),
                },
            };

            // Act
            var result = errorController.Error();

            // Assert
            var statusResult = Assert.IsType <RedirectResult>(result);

            statusResult.Url.Should().Be(expectedUrl);
            A.Equals(false, statusResult.Permanent);
            errorController.Dispose();
        }
        public void ErrorShouldReturnAValidViewResult()
        {
            var controller = new ErrorController();

            Mock <HttpContextBase>  httpContextMock  = new Mock <HttpContextBase>();
            Mock <HttpResponseBase> httpResponseMock = new Mock <HttpResponseBase>();

            httpContextMock.SetupGet(c => c.Response).Returns(httpResponseMock.Object);

            var urlHelperMock = new Mock <UrlHelper>();

            urlHelperMock.Setup(m => m.Action(It.IsAny <string>(), It.IsAny <string>())).Returns(string.Empty);
            controller.Url = urlHelperMock.Object;
            controller.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), controller);

            var result = controller.Error();

            result.Should().BeOfType <ViewResult>();

            var responseResult = (ViewResult)result;

            responseResult.Should().NotBe(null);
        }
Exemplo n.º 24
0
        public async Task ShouldTellUsSomethingIsWrongIfADifferentErrorOccurred()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path        = "/test";
            httpContext.Response.StatusCode = 500;
            var mockHttpContext = new ControllerContext
            {
                HttpContext = httpContext
            };
            var controller = new ErrorController(_legacyRedirects.Object, _logger.Object)
            {
                ControllerContext = mockHttpContext
            };

            controller.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
            {
                OriginalPath = "/OriginalPath"
            });

            var result = await controller.Error() as ViewResult;;

            result.ViewData[@"ErrorHeading"].Should().Be("Something went wrong");
        }
Exemplo n.º 25
0
 public void returns_ok_if_no_exception_in_pipeline()
 {
     _errorController.ControllerContext.HttpContext = new DefaultHttpContext();
     var response = Assert.IsType <OkResult>(_errorController.Error());
 }