|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.AspNetCore.TestHost; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | + |
| 7 | +namespace ByteGuard.SecurityHeaders.Tests; |
| 8 | + |
| 9 | +public class SecurityHeadersMiddlewareTests |
| 10 | +{ |
| 11 | + [Fact(DisplayName = "UseDefaultApiSecurityHeaders add the expected headers")] |
| 12 | + public async Task UseDefaultApiSecurityHeaders_AddsExpectedHeaders() |
| 13 | + { |
| 14 | + using var host = await new HostBuilder() |
| 15 | + .ConfigureWebHost(builder => |
| 16 | + { |
| 17 | + builder.UseTestServer(); |
| 18 | + builder.Configure(app => |
| 19 | + { |
| 20 | + app.UseDefaultApiSecurityHeaders(); |
| 21 | + app.Run(async context => await context.Response.WriteAsync("response")); |
| 22 | + }); |
| 23 | + }).StartAsync(); |
| 24 | + |
| 25 | + using var client = host.GetTestClient(); |
| 26 | + using var response = await client.GetAsync("/"); |
| 27 | + |
| 28 | + AssertHeader(response, "Cache-Control", "no-store"); |
| 29 | + AssertHeader(response, "Content-Security-Policy", "frame-ancestors 'none'"); |
| 30 | + AssertHeader(response, "X-Content-Type-Options", "nosniff"); |
| 31 | + AssertHeader(response, "X-Frame-Options", "DENY"); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task EnforceFalse_DoesNotOverrideExistingHeaderValues() |
| 36 | + { |
| 37 | + using var host = await new HostBuilder() |
| 38 | + .ConfigureWebHost(builder => |
| 39 | + { |
| 40 | + builder.UseTestServer(); |
| 41 | + builder.Configure(app => |
| 42 | + { |
| 43 | + app.UseDefaultApiSecurityHeaders(options => options.Enforce = false); |
| 44 | + |
| 45 | + app.Use(async (ctx, next) => |
| 46 | + { |
| 47 | + ctx.Response.Headers["Cache-Control"] = "public, max-age=60"; |
| 48 | + await next(); |
| 49 | + }); |
| 50 | + |
| 51 | + app.Run(async context => await context.Response.WriteAsync("response")); |
| 52 | + }); |
| 53 | + }).StartAsync(); |
| 54 | + |
| 55 | + using var client = host.GetTestClient(); |
| 56 | + using var response = await client.GetAsync("/"); |
| 57 | + |
| 58 | + AssertHeader(response, "Cache-Control", "public, max-age=60"); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task EnforceTrue_OverridesExistingHeaderValues() |
| 63 | + { |
| 64 | + using var host = await new HostBuilder() |
| 65 | + .ConfigureWebHost(builder => |
| 66 | + { |
| 67 | + builder.UseTestServer(); |
| 68 | + builder.Configure(app => |
| 69 | + { |
| 70 | + app.UseDefaultApiSecurityHeaders(options => options.Enforce = true); |
| 71 | + |
| 72 | + app.Use(async (ctx, next) => |
| 73 | + { |
| 74 | + ctx.Response.Headers["Cache-Control"] = "public, max-age=60"; |
| 75 | + await next(); |
| 76 | + }); |
| 77 | + |
| 78 | + app.Run(async context => await context.Response.WriteAsync("response")); |
| 79 | + }); |
| 80 | + }).StartAsync(); |
| 81 | + |
| 82 | + using HttpClient client = host.GetTestClient(); |
| 83 | + using var response = await client.GetAsync("/"); |
| 84 | + |
| 85 | + AssertHeader(response, "Cache-Control", "no-store"); |
| 86 | + } |
| 87 | + |
| 88 | + private static void AssertHeader(HttpResponseMessage response, string name, string expectedValue) |
| 89 | + { |
| 90 | + Assert.True(response.Headers.TryGetValues(name, out var values), $"Missing header: {name}"); |
| 91 | + Assert.Equal(expectedValue, values.Single()); |
| 92 | + } |
| 93 | +} |
0 commit comments