Пример #1
0
    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());
    }
Пример #2
0
    private HttpContext GetHttpContext(IHttpResponseFeature responseFeature = null)
    {
        if (responseFeature == null)
        {
            responseFeature = new TestResponseFeature();
        }
        var httpContext = new DefaultHttpContext();

        httpContext.Features.Set <IHttpResponseFeature>(responseFeature);
        return(httpContext);
    }
Пример #3
0
        public async Task OnResultExecuting_SavesTempData_WhenTempData_NotSavedAlready(IActionResult result)
        {
            // Arrange
            var tempDataDictionary = GetTempDataDictionary();
            var filter             = GetFilter(tempDataDictionary.Object);
            var responseFeature    = new TestResponseFeature();
            var actionContext      = GetActionContext(GetHttpContext(responseFeature));
            var context            = GetResultExecutingContext(actionContext, result);

            filter.OnResultExecuting(context); // registers callback

            // Act
            await responseFeature.FireOnSendingHeadersAsync();

            // Assert
            tempDataDictionary.Verify(tdd => tdd.Save(), Times.Once());
        }
Пример #4
0
    public async Task OnResultExecuting_DoesNotKeepTempData_ForNonIKeepTempDataResult()
    {
        // Arrange
        var tempDataDictionary = GetTempDataDictionary();
        var filter             = GetFilter(tempDataDictionary.Object);
        var responseFeature    = new TestResponseFeature();
        var actionContext      = GetHttpContext(responseFeature);
        var context            = GetResourceExecutingContext(actionContext);

        filter.OnResourceExecuting(context); // registers callback

        // Act
        await responseFeature.FireOnSendingHeadersAsync();

        // Assert
        tempDataDictionary.Verify(tdf => tdf.Keep(), Times.Never());
        tempDataDictionary.Verify(tdf => tdf.Save(), Times.Once());
    }
Пример #5
0
    public async Task OnResultExecuting_DoesntThrowIfResponseStarted()
    {
        // Arrange
        var responseFeature = new TestResponseFeature(hasStarted: true);
        var httpContext     = GetHttpContext(responseFeature);
        var tempDataFactory = new Mock <ITempDataDictionaryFactory>(MockBehavior.Loose);

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

        filter.OnResultExecuting(context);

        // Act
        // Checking it doesn't throw
        await responseFeature.FireOnSendingHeadersAsync();
    }
Пример #6
0
    public void OnResultExecuted_CanBeCalledTwice()
    {
        // Arrange
        var responseFeature = new TestResponseFeature();
        var httpContext     = GetHttpContext(responseFeature);
        var tempData        = GetTempDataDictionary();
        var tempDataFactory = new Mock <ITempDataDictionaryFactory>(MockBehavior.Strict);

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

        // Act (No Assert)
        filter.OnResultExecuted(context);
        // Shouldn't have thrown
        filter.OnResultExecuted(context);
    }
        public HttpRequestActivityTests()
        {
            _httpContext = new DefaultHttpContext();
            _httpContext.Features.Set <IHttpResponseFeature>(_responseFeature = new TestResponseFeature());

            Logger serilog = new LoggerConfiguration()
                             .MinimumLevel.Verbose()
                             .WriteTo.TestCorrelator()
                             .CreateLogger();

            var serilogProvider = new SerilogLoggerProvider(serilog);

            _logger = serilogProvider.CreateLogger("");

            _sut = new HttpRequestActivity(_logger, _httpContext, CorrelationHttpHeaders.CorrelationId);
            _correlationContext = new CorrelationContext
            {
                CorrelationId = Guid.NewGuid().ToString()
            };
        }
Пример #8
0
        public async Task OnResultExecuting_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 = GetResultExecutingContext(httpContext);

            filter.OnResultExecuting(context); // registers callback

            // Act
            await responseFeature.FireOnSendingHeadersAsync();

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