OnResourceExecuting() public method

public OnResourceExecuting ( ResourceExecutingContext context ) : void
context ResourceExecutingContext
return void
        public async Task OnResourceExecuting_DoesNotSaveTempData_WhenUnhandledExceptionOccurs()
        {
            // Arrange
            var responseFeature = new TestResponseFeature();
            var httpContext     = GetHttpContext(responseFeature);

            httpContext.Items[SaveTempDataFilter.SaveTempDataFilterContextKey] = new SaveTempDataFilter.SaveTempDataContext()
            {
                RequestHasUnhandledException = true
            };
            var tempDataFactory = new Mock <ITempDataDictionaryFactory>(MockBehavior.Strict);

            tempDataFactory
            .Setup(f => f.GetTempData(It.IsAny <HttpContext>()))
            .Verifiable();
            var filter  = new SaveTempDataFilter(tempDataFactory.Object);
            var context = GetResourceExecutingContext(httpContext);

            filter.OnResourceExecuting(context); // registers callback

            // Act
            await responseFeature.FireOnSendingHeadersAsync();

            // Assert
            tempDataFactory.Verify(tdf => tdf.GetTempData(It.IsAny <HttpContext>()), Times.Never());
        }
        public void OnResourceExecuting_RegistersOnStartingCallback()
        {
            // Arrange
            var responseFeature = new Mock <IHttpResponseFeature>(MockBehavior.Strict);

            responseFeature
            .Setup(rf => rf.OnStarting(It.IsAny <Func <object, Task> >(), It.IsAny <object>()))
            .Verifiable();
            responseFeature
            .SetupGet(rf => rf.HasStarted)
            .Returns(false);

            var tempDataFactory = new Mock <ITempDataDictionaryFactory>(MockBehavior.Strict);

            tempDataFactory
            .Setup(f => f.GetTempData(It.IsAny <HttpContext>()))
            .Verifiable();
            var filter      = new SaveTempDataFilter(tempDataFactory.Object);
            var httpContext = GetHttpContext(responseFeature.Object);
            var context     = GetResourceExecutingContext(httpContext);

            // Act
            filter.OnResourceExecuting(context);

            // Assert
            responseFeature.Verify();
            tempDataFactory.Verify(tdf => tdf.GetTempData(It.IsAny <HttpContext>()), Times.Never());
        }
Example #3
0
        public async Task OnResourceExecuting_DoesNotSaveTempData_WhenTempDataAlreadySaved()
        {
            // Arrange
            var responseFeature = new TestResponseFeature();
            var httpContext     = GetHttpContext(responseFeature);

            httpContext.Items[SaveTempDataFilter.TempDataSavedKey] = true; // indicate that tempdata was already saved
            var tempDataFactory = new Mock <ITempDataDictionaryFactory>(MockBehavior.Strict);

            tempDataFactory
            .Setup(f => f.GetTempData(It.IsAny <HttpContext>()))
            .Verifiable();
            var filter  = new SaveTempDataFilter(tempDataFactory.Object);
            var context = GetResourceExecutingContext(httpContext);

            filter.OnResourceExecuting(context); // registers callback

            // Act
            await responseFeature.FireOnSendingHeadersAsync();

            // Assert
            tempDataFactory.Verify(tdf => tdf.GetTempData(It.IsAny <HttpContext>()), Times.Never());
        }