Skip to content

Commit

Permalink
Throw exception if same hub has same RoutePattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 13, 2023
1 parent d95ea2e commit a2113f5
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Connections;
Expand Down Expand Up @@ -54,10 +55,17 @@ public override void ConfigureServices(ServiceConfigurationContext context)
.GetRequiredService<IOptions<AbpSignalROptions>>()
.Value;
var hubWithRoutePatterns = new List<KeyValuePair<Type, string>>();
foreach (var hubConfig in signalROptions.Hubs)
{
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(
hubConfig.HubType,
endpointContext.Endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.SignalR\Volo.Abp.AspNetCore.SignalR.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>

</Project>
Original file line number Diff line number Diff line change
@@ -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;

namespace Volo.Abp.AspNetCore.SignalR;

[DependsOn(
typeof(AbpAspNetCoreSignalRModule),
typeof(AbpTestBaseModule),
typeof(AbpAspNetCoreTestBaseModule),
typeof(AbpAutofacModule)
)]
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.Options;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Shouldly;
using Volo.Abp.AspNetCore.SignalR.SampleHubs;
using Xunit;

namespace Volo.Abp.AspNetCore.SignalR;

public class AbpSignalROptions_Tests : AbpAspNetCoreSignalRTestBase
public class AbpSignalROptions_Tests : AbpAspNetCoreTestBase<Program>
{
private readonly AbpSignalROptions _options;

Expand All @@ -14,12 +16,74 @@ public AbpSignalROptions_Tests()
_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()
{
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHub));
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularAbpHub));
_options.Hubs.ShouldNotContain(h => h.HubType == typeof(DisableConventionalRegistrationHub));
_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();
}
}
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
@@ -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
{

}

0 comments on commit a2113f5

Please sign in to comment.