Browse Source

Add unit tests.

pull/18638/head
maliming 3 years ago
parent
commit
2823596497
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs
  2. 2
      framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs
  3. 74
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs
  4. 31
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs
  5. 11
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs
  6. 41
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs

2
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs

@ -12,7 +12,7 @@ public abstract class AbpMiddlewareBase : IMiddleware
var endpoint = context.GetEndpoint(); var endpoint = context.GetEndpoint();
var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>(); var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();
var disableAbpFeaturesAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute<DisableAbpFeaturesAttribute>(); var disableAbpFeaturesAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute<DisableAbpFeaturesAttribute>();
return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddlewares); return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddleware);
} }
public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); public abstract Task InvokeAsync(HttpContext context, RequestDelegate next);

2
framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs

@ -15,7 +15,7 @@ public class DisableAbpFeaturesAttribute : Attribute
/// The framework middleware will skip the class. /// The framework middleware will skip the class.
/// This will cause the all features that depend on middleware to not work. /// This will cause the all features that depend on middleware to not work.
/// </summary> /// </summary>
public bool DisableMiddlewares { get; set; } = true; public bool DisableMiddleware { get; set; } = true;
/// <summary> /// <summary>
/// The framework will not remove all built-in filters for the class. /// The framework will not remove all built-in filters for the class.

74
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Shouldly;
using Volo.Abp.AspNetCore.Mvc.Auditing;
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc.Features;
using Volo.Abp.AspNetCore.Mvc.GlobalFeatures;
using Volo.Abp.AspNetCore.Mvc.Response;
using Volo.Abp.AspNetCore.Mvc.Uow;
using Volo.Abp.AspNetCore.Mvc.Validation;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.Filters;
[Route("api/enabled-features-test")]
public class EnabledAbpFeaturesController : AbpController, IRemoteService
{
[HttpGet]
public Task<List<string>> GetAsync()
{
var filters = HttpContext.GetEndpoint().Metadata.GetMetadata<ControllerActionDescriptor>()
.FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute)
.Select(x => x.Filter.As<ServiceFilterAttribute>().ServiceType.FullName).ToList();
return Task.FromResult(filters);
}
}
[Route("api/disabled-features-test")]
[DisableAbpFeatures]
public class DisabledAbpFeaturesController : AbpController, IRemoteService
{
[HttpGet]
public Task<List<string>> GetAsync()
{
var filters = HttpContext.GetEndpoint().Metadata.GetMetadata<ControllerActionDescriptor>()
.FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute)
.Select(x => x.Filter.As<ServiceFilterAttribute>().ServiceType.FullName).ToList();
return Task.FromResult(filters);
}
}
public class MvcDisableAbpFeaturesAttribute_Tests : AspNetCoreMvcTestBase
{
[Fact]
public async Task Should_Disable_MVC_Filters()
{
var filters = await GetResponseAsObjectAsync<List<string>>("/api/enabled-features-test");
filters.ShouldContain(typeof(GlobalFeatureActionFilter).FullName);
filters.ShouldContain(typeof(AbpAuditActionFilter).FullName);
filters.ShouldContain(typeof(AbpNoContentActionFilter).FullName);
filters.ShouldContain(typeof(AbpFeatureActionFilter).FullName);
filters.ShouldContain(typeof(AbpValidationActionFilter).FullName);
filters.ShouldContain(typeof(AbpUowActionFilter).FullName);
filters.ShouldContain(typeof(AbpExceptionFilter).FullName);
filters = await GetResponseAsObjectAsync<List<string>>("/api/disabled-features-test");
filters.ShouldNotContain(typeof(GlobalFeatureActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpAuditActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpNoContentActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpFeatureActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpValidationActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpUowActionFilter).FullName);
filters.ShouldNotContain(typeof(AbpExceptionFilter).FullName);
}
}

31
framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs

@ -15,22 +15,34 @@ public abstract class AbpInterceptionTestBase<TStartupModule> : AbpAsyncIntegrat
services.AddTransient<SimpleAsyncInterceptor>(); services.AddTransient<SimpleAsyncInterceptor>();
services.AddTransient<SimpleAsyncInterceptor2>(); services.AddTransient<SimpleAsyncInterceptor2>();
services.AddTransient<SimpleInterceptionTargetClass>(); services.AddTransient<SimpleInterceptionTargetClass>();
services.AddTransient<DisableInterceptionTargetClass>();
services.AddTransient<SimpleResultCacheTestInterceptor>(); services.AddTransient<SimpleResultCacheTestInterceptor>();
services.AddTransient<CachedTestObject>(); services.AddTransient<CachedTestObject>();
services.AddTransient<AlwaysExceptionAsyncInterceptor>();
services.OnRegistered(registration => services.OnRegistered(registration =>
{ {
if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType)
{ {
registration.Interceptors.Add<SimpleAsyncInterceptor>(); registration.Interceptors.Add<SimpleAsyncInterceptor>();
registration.Interceptors.Add<SimpleAsyncInterceptor2>(); registration.Interceptors.Add<SimpleAsyncInterceptor2>();
registration.Interceptors.Add<SimpleResultCacheTestInterceptor>();
} }
if (typeof(CachedTestObject) == registration.ImplementationType) if (typeof(CachedTestObject) == registration.ImplementationType)
{ {
registration.Interceptors.Add<SimpleResultCacheTestInterceptor>(); registration.Interceptors.Add<SimpleResultCacheTestInterceptor>();
} }
if (typeof(DisableInterceptionTargetClass) == registration.ImplementationType)
{
registration.Interceptors.Add<SimpleAsyncInterceptor>();
registration.Interceptors.Add<SimpleAsyncInterceptor2>();
registration.Interceptors.Add<SimpleResultCacheTestInterceptor>();
registration.Interceptors.Add<AlwaysExceptionAsyncInterceptor>();
}
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -96,4 +108,23 @@ public abstract class AbpInterceptionTestBase<TStartupModule> : AbpAsyncIntegrat
(await target.GetValueAsync(43)).ShouldBe(42); //First run, cached previous value (await target.GetValueAsync(43)).ShouldBe(42); //First run, cached previous value
(await target.GetValueAsync(44)).ShouldBe(42); //First run, cached previous value (await target.GetValueAsync(44)).ShouldBe(42); //First run, cached previous value
} }
[Fact]
public async Task Should_Disable_Interceptors()
{
//Arrange
var target = ServiceProvider.GetService<DisableInterceptionTargetClass>();
//Act
await target.DoItAsync();
//Assert
target.Logs.Count.ShouldBe(3);
target.Logs[0].ShouldBe("EnterDoItAsync");
target.Logs[1].ShouldBe("MiddleDoItAsync");
target.Logs[2].ShouldBe("ExitDoItAsync");
}
} }

11
framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs

@ -0,0 +1,11 @@
using System.Threading.Tasks;
namespace Volo.Abp.DynamicProxy;
public class AlwaysExceptionAsyncInterceptor : AbpInterceptor
{
public override Task InterceptAsync(IAbpMethodInvocation invocation)
{
throw new AbpException("This interceptor should not be executed!");
}
}

41
framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs

@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.TestBase.Logging;
namespace Volo.Abp.DynamicProxy;
[DisableAbpFeatures]
public class DisableInterceptionTargetClass : ICanLogOnObject
{
public List<string> Logs { get; } = new List<string>();
public virtual void DoIt()
{
Logs.Add("ExecutingDoIt");
}
public virtual int GetValue()
{
Logs.Add("ExecutingGetValue");
return 42;
}
public virtual async Task<int> GetValueAsync()
{
Logs.Add("EnterGetValueAsync");
await Task.Delay(5);
Logs.Add("MiddleGetValueAsync");
await Task.Delay(5);
Logs.Add("ExitGetValueAsync");
return 42;
}
public virtual async Task DoItAsync()
{
Logs.Add("EnterDoItAsync");
await Task.Delay(5);
Logs.Add("MiddleDoItAsync");
await Task.Delay(5);
Logs.Add("ExitDoItAsync");
}
}
Loading…
Cancel
Save