#region ========================================================================= USING =====================================================================================
using Nemera.Presentation.Web.Common.Http;
using Nemera.Presentation.Web.Common.Middlewares;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
#endregion
namespace Nemera.Presentation.Web.UnitTests.Common.Middlewares;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class CacheControlHeaderMiddlewareTests
{
[Fact]
public async Task InvokeAsync_WhenTheEndpointStoredACacheControlValue_ShouldRestoreItAfterLaterCallbacksRun()
{
// Arrange
DefaultHttpContext context = new();
TestHttpResponseFeature responseFeature = new();
context.Features.Set(responseFeature);
context.Items[HttpContextItemKeys.CACHE_CONTROL] = "private, no-store";
CacheControlHeaderMiddleware sut = new(nextContext =>
{
// The session middleware registers its cache header write after this middleware, so it runs first when the response starts.
nextContext.Response.OnStarting(() =>
{
nextContext.Response.Headers.CacheControl = "no-cache,no-store";
return Task.CompletedTask;
});
return Task.CompletedTask;
});
// Act
await sut.InvokeAsync(context);
await responseFeature.StartAsync();
// Assert
Assert.Equal("private, no-store", context.Response.Headers.CacheControl.ToString());
}
[Fact]
public async Task InvokeAsync_WhenNoCacheControlValueWasStored_ShouldLeaveTheHeaderWrittenByLaterCallbacks()
{
// Arrange
DefaultHttpContext context = new();
TestHttpResponseFeature responseFeature = new();
context.Features.Set(responseFeature);
CacheControlHeaderMiddleware sut = new(nextContext =>
{
nextContext.Response.OnStarting(() =>
{
nextContext.Response.Headers.CacheControl = "no-cache,no-store";
return Task.CompletedTask;
});
return Task.CompletedTask;
});
// Act
await sut.InvokeAsync(context);
await responseFeature.StartAsync();
// Assert
Assert.Equal("no-cache,no-store", context.Response.Headers.CacheControl.ToString());
}
[Fact]
public async Task InvokeAsync_WhenTheStoredValueIsEmpty_ShouldLeaveTheHeaderWrittenByLaterCallbacks()
{
// Arrange
DefaultHttpContext context = new();
TestHttpResponseFeature responseFeature = new();
context.Features.Set(responseFeature);
context.Items[HttpContextItemKeys.CACHE_CONTROL] = string.Empty;
CacheControlHeaderMiddleware sut = new(nextContext =>
{
nextContext.Response.OnStarting(() =>
{
nextContext.Response.Headers.CacheControl = "no-cache,no-store";
return Task.CompletedTask;
});
return Task.CompletedTask;
});
// Act
await sut.InvokeAsync(context);
await responseFeature.StartAsync();
// Assert
Assert.Equal("no-cache,no-store", context.Response.Headers.CacheControl.ToString());
}
///
/// Minimal response feature that stores the response-start callbacks and invokes them in reverse registration order, like the server does.
///
[ExcludeFromCodeCoverage]
private sealed class TestHttpResponseFeature : IHttpResponseFeature
{
private readonly List<(Func