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()); }
private HttpContext GetHttpContext(IHttpResponseFeature responseFeature = null) { if (responseFeature == null) { responseFeature = new TestResponseFeature(); } var httpContext = new DefaultHttpContext(); httpContext.Features.Set <IHttpResponseFeature>(responseFeature); return(httpContext); }
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()); }
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()); }
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(); }
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() }; }
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()); }