Browse Source

Merge pull request #18451 from abpframework/AbpSignalROptions

Throw exception if same hub has same `RoutePattern`.
pull/18465/head
liangshiwei 3 years ago
committed by GitHub
parent
commit
ff9754a288
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs
  2. 2
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo.Abp.AspNetCore.SignalR.Tests.csproj
  3. 24
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRTestModule.cs
  4. 70
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions_Tests.cs
  5. 10
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/Program.cs
  6. 37
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/SampleHubs/RegularHubBase.cs

8
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Connections;
@ -54,10 +55,17 @@ public class AbpAspNetCoreSignalRModule : AbpModule
.GetRequiredService<IOptions<AbpSignalROptions>>() .GetRequiredService<IOptions<AbpSignalROptions>>()
.Value; .Value;
var hubWithRoutePatterns = new List<KeyValuePair<Type, string>>();
foreach (var hubConfig in signalROptions.Hubs) foreach (var hubConfig in signalROptions.Hubs)
{ {
routePatterns.AddIfNotContains(hubConfig.RoutePattern); routePatterns.AddIfNotContains(hubConfig.RoutePattern);
if (hubWithRoutePatterns.Any(x => x.Key == hubConfig.HubType && x.Value == hubConfig.RoutePattern))
{
throw new AbpException($"The hub type {hubConfig.HubType.FullName} is already registered with route pattern {hubConfig.RoutePattern}");
}
hubWithRoutePatterns.Add(new KeyValuePair<Type, string>(hubConfig.HubType, hubConfig.RoutePattern));
MapHubType( MapHubType(
hubConfig.HubType, hubConfig.HubType,
endpointContext.Endpoints, endpointContext.Endpoints,

2
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo.Abp.AspNetCore.SignalR.Tests.csproj

@ -14,7 +14,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.SignalR\Volo.Abp.AspNetCore.SignalR.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.SignalR\Volo.Abp.AspNetCore.SignalR.csproj" />
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> <ProjectReference Include="..\Volo.Abp.AspNetCore.Tests\Volo.Abp.AspNetCore.Tests.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

24
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRTestModule.cs

@ -1,14 +1,34 @@
using Volo.Abp.Autofac; using System;
using Microsoft.AspNetCore.Builder;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace Volo.Abp.AspNetCore.SignalR; namespace Volo.Abp.AspNetCore.SignalR;
[DependsOn( [DependsOn(
typeof(AbpAspNetCoreSignalRModule), typeof(AbpAspNetCoreSignalRModule),
typeof(AbpTestBaseModule), typeof(AbpAspNetCoreTestBaseModule),
typeof(AbpAutofacModule) typeof(AbpAutofacModule)
)] )]
public class AbpAspNetCoreSignalRTestModule : AbpModule public class AbpAspNetCoreSignalRTestModule : AbpModule
{ {
public static Exception UseConfiguredEndpointsException { get; set; }
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
app.UseRouting();
UseConfiguredEndpointsException = null;
try
{
app.UseConfiguredEndpoints();
}
catch (Exception e)
{
UseConfiguredEndpointsException = e;
}
}
} }

70
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions_Tests.cs

@ -1,11 +1,13 @@
using Microsoft.Extensions.Options; using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Shouldly; using Shouldly;
using Volo.Abp.AspNetCore.SignalR.SampleHubs; using Volo.Abp.AspNetCore.SignalR.SampleHubs;
using Xunit; using Xunit;
namespace Volo.Abp.AspNetCore.SignalR; namespace Volo.Abp.AspNetCore.SignalR;
public class AbpSignalROptions_Tests : AbpAspNetCoreSignalRTestBase public class AbpSignalROptions_Tests : AbpAspNetCoreTestBase<Program>
{ {
private readonly AbpSignalROptions _options; private readonly AbpSignalROptions _options;
@ -14,12 +16,74 @@ public class AbpSignalROptions_Tests : AbpAspNetCoreSignalRTestBase
_options = GetRequiredService<IOptions<AbpSignalROptions>>().Value; _options = GetRequiredService<IOptions<AbpSignalROptions>>().Value;
} }
[Fact(Skip = "Can not run this test since AspNet Core environment has not been properly set!")] protected override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RegularHubClass1>();
services.AddTransient<RegularHubClass12>();
services.AddTransient<RegularHubClass2>();
services.AddTransient<RegularHubClass22>();
}
[Fact]
public void Should_Auto_Add_Maps() public void Should_Auto_Add_Maps()
{ {
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHub)); _options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHub));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularAbpHub)); _options.Hubs.ShouldContain(h => h.HubType == typeof(RegularAbpHub));
_options.Hubs.ShouldNotContain(h => h.HubType == typeof(DisableConventionalRegistrationHub)); _options.Hubs.ShouldNotContain(h => h.HubType == typeof(DisableConventionalRegistrationHub));
_options.Hubs.ShouldNotContain(h => h.HubType == typeof(DisableAutoHubMapHub)); _options.Hubs.ShouldNotContain(h => h.HubType == typeof(DisableAutoHubMapHub));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHubClass1));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHubClass12));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHubClass2));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHubClass22));
}
}
[Collection("AbpAspNetCoreSignalR")]
public class AbpSignalSameRroutePattern_Tests : AbpAspNetCoreTestBase<Program>
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RegularHubClass1>();
services.AddTransient<RegularHubClass12>();
services.AddTransient<RegularHubClass1, RegularHubClass12>();
}
[Fact]
public void Should_Throw_Exception_If_HubType_Has_Same_RoutePattern()
{
AbpAspNetCoreSignalRTestModule.UseConfiguredEndpointsException.ShouldNotBeNull();
AbpAspNetCoreSignalRTestModule.UseConfiguredEndpointsException.Message.ShouldBe($"The hub type {typeof(RegularHubClass12).FullName} is already registered with route pattern {HubRouteAttribute.GetRoutePattern(typeof(RegularHubClass12))}");
}
}
[Collection("AbpAspNetCoreSignalR")]
public class AbpSignalDifferentRroutePattern_Tests : AbpAspNetCoreTestBase<Program>
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RegularHubClass2>();
services.AddTransient<RegularHubClass22>();
services.AddTransient<RegularHubClass2, RegularHubClass22>();
services.Configure<AbpSignalROptions>(options =>
{
var firstHub = options.Hubs.FirstOrDefault(x => x.HubType == typeof(RegularHubClass22));
if (firstHub != null)
{
firstHub.RoutePattern = "/signalr-hubs/regular-hub-class-22";
}
var lastHub = options.Hubs.LastOrDefault(x => x.HubType == typeof(RegularHubClass22));
if (lastHub != null)
{
lastHub.RoutePattern = "/signalr-hubs/regular-hub-class-22-1";
}
});
}
[Fact]
public void Should_Work_If_Same_HubType_Has_Different_RoutePattern()
{
AbpAspNetCoreSignalRTestModule.UseConfiguredEndpointsException.ShouldBeNull();
} }
} }

10
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/Program.cs

@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Builder;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.AspNetCore.TestBase;
var builder = WebApplication.CreateBuilder();
await builder.RunAbpModuleAsync<AbpAspNetCoreSignalRTestModule>();
public partial class Program
{
}

37
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/SampleHubs/RegularHubBase.cs

@ -0,0 +1,37 @@
using Microsoft.AspNetCore.SignalR;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.SignalR.SampleHubs;
public abstract class RegularHubBase<THub> : Hub<THub> where THub : class
{
}
[DisableConventionalRegistration]
[ExposeServices(typeof(RegularHubClass1))]
public class RegularHubClass1 : RegularHubBase<RegularHubClass1>
{
}
[DisableConventionalRegistration]
[ExposeServices(typeof(RegularHubClass12), typeof(RegularHubClass1))]
public class RegularHubClass12 : RegularHubClass1
{
}
[DisableConventionalRegistration]
[ExposeServices(typeof(RegularHubClass2))]
public class RegularHubClass2 : RegularHubBase<RegularHubClass2>
{
}
[DisableConventionalRegistration]
[ExposeServices(typeof(RegularHubClass22), typeof(RegularHubClass2))]
public class RegularHubClass22 : RegularHubClass2
{
}
Loading…
Cancel
Save