From 32d72195806b807d15cb202b83b9b4258b00f8f3 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 17 Feb 2021 09:32:03 +0800 Subject: [PATCH] Add unit test for AbpSecurityHeadersMiddleware. --- .../AbpApplicationBuilderExtensions.cs | 4 +-- .../Volo.Abp.AspNetCore.csproj | 1 - .../Security/AbpSecurityHeadersMiddleware.cs | 31 +++++++------------ .../Authorization/AuthTestController_Tests.cs | 10 ------ .../Claims/ClaimsMapTestController_Tests.cs | 10 ------ .../Headers/SecurityHeadersTestController.cs | 12 +++++++ .../SecurityHeadersTestController_Tests.cs | 19 ++++++++++++ 7 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Security/Headers/SecurityHeadersTestController.cs create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Security/Headers/SecurityHeadersTestController_Tests.cs diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index fab4a4bb80..a409355ef0 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -84,9 +84,9 @@ namespace Microsoft.AspNetCore.Builder return app.UseMiddleware(); } - public static void UseAbpSecurityHeaders(this IApplicationBuilder app) + public static IApplicationBuilder UseAbpSecurityHeaders(this IApplicationBuilder app) { - app.UseMiddleware(); + return app.UseMiddleware(); } } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj b/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj index eaa053c91a..b191872339 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj +++ b/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj @@ -25,7 +25,6 @@ - diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs index 09dddfabe7..28b0f3a48e 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs @@ -1,37 +1,30 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Security { - public class AbpSecurityHeadersMiddleware + public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency { - private readonly RequestDelegate _next; - - public AbpSecurityHeadersMiddleware(RequestDelegate next) - { - _next = next; - } - - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { /*X-Content-Type-Options header tells the browser to not try and “guess” what a mimetype of a resource might be, and to just take what mimetype the server has returned as fact.*/ - AddHeaderIfNotExists(httpContext, "X-Content-Type-Options", "nosniff"); + AddHeaderIfNotExists(context, "X-Content-Type-Options", "nosniff"); /*X-XSS-Protection is a feature of Internet Explorer, Chrome and Safari that stops pages from loading when they detect reflected cross-site scripting (XSS) attacks*/ - AddHeaderIfNotExists(httpContext, "X-XSS-Protection", "1; mode=block"); + AddHeaderIfNotExists(context, "X-XSS-Protection", "1; mode=block"); /*The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a ,