Skip to content

Commit

Permalink
cleaned up code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
karpikpl committed Nov 13, 2024
1 parent 03d3bd8 commit 4542be3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,7 @@ public static class WebPubSubEndpointRouteBuilderExtensions
public static IEndpointConventionBuilder MapWebPubSubHub<THub>(
this IEndpointRouteBuilder endpoints,
string path) where THub: WebPubSubHub
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

var marker = endpoints.ServiceProvider.GetService<WebPubSubMarkerService>() ?? throw new InvalidOperationException(
"Unable to find the required services. Please add all the required services by calling " +
"'IServiceCollection.AddWebPubSub' inside the call to 'ConfigureServices(...)' in the application startup code.");

var adaptor = endpoints.ServiceProvider.GetService<ServiceRequestHandlerAdapter>();
adaptor.RegisterHub<THub>();

var app = endpoints.CreateApplicationBuilder();
app.UseMiddleware<WebPubSubMiddleware>();

return endpoints.Map(path, app.Build());
}
=> MapWebPubSubHub<THub>(endpoints, path, typeof(THub).Name);

/// <summary>
/// Maps the <see cref="WebPubSubHub"/> to the path "/client" with the specified hub name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ public void TestMapWebPubSubHubConfigureCustomHub()
Assert.NotNull(adaptor.GetHub(customHub), "Custom hub should be registered");
}

[Test]
public void TestMapWebPubSubHubConfigureHubByTypeName()
{
var testHost = "webpubsub.azure.net";
WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.Services
.AddWebPubSub(o => o.ServiceEndpoint = new WebPubSubServiceEndpoint($"Endpoint=https://{testHost};AccessKey=7aab239577fd4f24bc919802fb629f5f;Version=1.0;"));

using var app = builder.Build();
app.MapWebPubSubHub<TestHub>("/testhub");

var validator = app.Services.GetRequiredService<RequestValidator>();
var adaptor = app.Services.GetRequiredService<ServiceRequestHandlerAdapter>();

Assert.NotNull(validator);
Assert.NotNull(adaptor);
Assert.True(validator.IsValidOrigin(new List<string> { testHost }));
Assert.NotNull(adaptor.GetHub(nameof(TestHub)), "Custom hub should be registered");
}

private sealed class TestHub : WebPubSubHub
{ }

Expand Down

0 comments on commit 4542be3

Please sign in to comment.