Makes MapHub and MapEndpoint consistent with other ASP.NET Core APIs using PathString. #1188

This commit is contained in:
Geovanny Alzate Sandoval 2017-12-11 22:58:02 -05:00 committed by Andrew Stanton-Nurse
parent 588d9cdb11
commit 31ef3c49df
12 changed files with 45 additions and 30 deletions

View File

@ -77,10 +77,10 @@ namespace Microsoft.AspNetCore.SignalR.Test.Server
} }
app.UseFileServer(); app.UseFileServer();
app.UseSockets(options => options.MapEndPoint<EchoEndPoint>("echo")); app.UseSockets(options => options.MapEndPoint<EchoEndPoint>("/echo"));
app.UseSignalR(options => options.MapHub<TestHub>("testhub")); app.UseSignalR(options => options.MapHub<TestHub>("/testhub"));
app.UseSignalR(options => options.MapHub<UncreatableHub>("uncreatable")); app.UseSignalR(options => options.MapHub<UncreatableHub>("/uncreatable"));
app.UseSignalR(options => options.MapHub<HubWithAuthorization>("authorizedhub")); app.UseSignalR(options => options.MapHub<HubWithAuthorization>("/authorizedhub"));
app.Use(next => async (context) => app.Use(next => async (context) =>
{ {

View File

@ -88,7 +88,7 @@ namespace ChatSample
app.UseSignalR(routes => app.UseSignalR(routes =>
{ {
routes.MapHub<Chat>("chat"); routes.MapHub<Chat>("/chat");
}); });
app.UseMvc(routes => app.UseMvc(routes =>

View File

@ -66,7 +66,7 @@ namespace JwtSample
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
app.UseFileServer(); app.UseFileServer();
app.UseSignalR(options => options.MapHub<Broadcaster>("broadcast")); app.UseSignalR(options => options.MapHub<Broadcaster>("/broadcast"));
var routeBuilder = new RouteBuilder(app); var routeBuilder = new RouteBuilder(app);
routeBuilder.MapGet("generatetoken", c => c.Response.WriteAsync(GenerateToken(c))); routeBuilder.MapGet("generatetoken", c => c.Response.WriteAsync(GenerateToken(c)));

View File

@ -32,7 +32,7 @@ namespace SocialWeather
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseSockets(o => { o.MapEndPoint<SocialWeatherEndPoint>("weather"); }); app.UseSockets(o => { o.MapEndPoint<SocialWeatherEndPoint>("/weather"); });
app.UseFileServer(); app.UseFileServer();
var formatterResolver = app.ApplicationServices.GetRequiredService<FormatterResolver>(); var formatterResolver = app.ApplicationServices.GetRequiredService<FormatterResolver>();

View File

@ -52,15 +52,15 @@ namespace SocketsSample
app.UseSignalR(routes => app.UseSignalR(routes =>
{ {
routes.MapHub<DynamicChat>("dynamic"); routes.MapHub<DynamicChat>("/dynamic");
routes.MapHub<Chat>("default"); routes.MapHub<Chat>("/default");
routes.MapHub<Streaming>("streaming"); routes.MapHub<Streaming>("/streaming");
routes.MapHub<HubTChat>("hubT"); routes.MapHub<HubTChat>("/hubT");
}); });
app.UseSockets(routes => app.UseSockets(routes =>
{ {
routes.MapEndPoint<MessagesEndPoint>("chat"); routes.MapEndPoint<MessagesEndPoint>("/chat");
}); });
} }
} }

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
@ -18,11 +19,16 @@ namespace Microsoft.AspNetCore.SignalR
} }
public void MapHub<THub>(string path) where THub : Hub public void MapHub<THub>(string path) where THub : Hub
{
MapHub<THub>(new PathString(path), socketOptions: null);
}
public void MapHub<THub>(PathString path) where THub : Hub
{ {
MapHub<THub>(path, socketOptions: null); MapHub<THub>(path, socketOptions: null);
} }
public void MapHub<THub>(string path, Action<HttpSocketOptions> socketOptions) where THub : Hub public void MapHub<THub>(PathString path, Action<HttpSocketOptions> socketOptions) where THub : Hub
{ {
// find auth attributes // find auth attributes
var authorizeAttributes = typeof(THub).GetCustomAttributes<AuthorizeAttribute>(inherit: true); var authorizeAttributes = typeof(THub).GetCustomAttributes<AuthorizeAttribute>(inherit: true);

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Sockets namespace Microsoft.AspNetCore.Sockets
@ -20,9 +21,12 @@ namespace Microsoft.AspNetCore.Sockets
} }
public void MapSocket(string path, Action<ISocketBuilder> socketConfig) => public void MapSocket(string path, Action<ISocketBuilder> socketConfig) =>
MapSocket(new PathString(path), new HttpSocketOptions(), socketConfig);
public void MapSocket(PathString path, Action<ISocketBuilder> socketConfig) =>
MapSocket(path, new HttpSocketOptions(), socketConfig); MapSocket(path, new HttpSocketOptions(), socketConfig);
public void MapSocket(string path, HttpSocketOptions options, Action<ISocketBuilder> socketConfig) public void MapSocket(PathString path, HttpSocketOptions options, Action<ISocketBuilder> socketConfig)
{ {
var socketBuilder = new SocketBuilder(_routes.ServiceProvider); var socketBuilder = new SocketBuilder(_routes.ServiceProvider);
socketConfig(socketBuilder); socketConfig(socketBuilder);
@ -32,11 +36,16 @@ namespace Microsoft.AspNetCore.Sockets
} }
public void MapEndPoint<TEndPoint>(string path) where TEndPoint : EndPoint public void MapEndPoint<TEndPoint>(string path) where TEndPoint : EndPoint
{
MapEndPoint<TEndPoint>(new PathString(path), socketOptions: null);
}
public void MapEndPoint<TEndPoint>(PathString path) where TEndPoint : EndPoint
{ {
MapEndPoint<TEndPoint>(path, socketOptions: null); MapEndPoint<TEndPoint>(path, socketOptions: null);
} }
public void MapEndPoint<TEndPoint>(string path, Action<HttpSocketOptions> socketOptions) where TEndPoint : EndPoint public void MapEndPoint<TEndPoint>(PathString path, Action<HttpSocketOptions> socketOptions) where TEndPoint : EndPoint
{ {
var authorizeAttributes = typeof(TEndPoint).GetCustomAttributes<AuthorizeAttribute>(inherit: true); var authorizeAttributes = typeof(TEndPoint).GetCustomAttributes<AuthorizeAttribute>(inherit: true);
var options = new HttpSocketOptions(); var options = new HttpSocketOptions();

View File

@ -49,10 +49,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
app.UseSignalR(routes => app.UseSignalR(routes =>
{ {
routes.MapHub<TestHub>("default"); routes.MapHub<TestHub>("/default");
routes.MapHub<DynamicTestHub>("dynamic"); routes.MapHub<DynamicTestHub>("/dynamic");
routes.MapHub<TestHubT>("hubT"); routes.MapHub<TestHubT>("/hubT");
routes.MapHub<HubWithAuthorization>("authorizedhub"); routes.MapHub<HubWithAuthorization>("/authorizedhub");
}); });
app.Run(async (context) => app.Run(async (context) =>

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
app.UseSignalR(options => options.MapHub<EchoHub>("echo")); app.UseSignalR(options => options.MapHub<EchoHub>("/echo"));
} }
} }
} }

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
{ {
var ex = Assert.Throws<NotSupportedException>(() => var ex = Assert.Throws<NotSupportedException>(() =>
{ {
using (var builder = BuildWebHost(options => options.MapHub<InvalidHub>("overloads"))) using (var builder = BuildWebHost(options => options.MapHub<InvalidHub>("/overloads")))
{ {
builder.Start(); builder.Start();
} }
@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public void MapHubFindsAuthAttributeOnHub() public void MapHubFindsAuthAttributeOnHub()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost(options => options.MapHub<AuthHub>("path", httpSocketOptions => using (var builder = BuildWebHost(options => options.MapHub<AuthHub>("/path", httpSocketOptions =>
{ {
authCount += httpSocketOptions.AuthorizationData.Count; authCount += httpSocketOptions.AuthorizationData.Count;
}))) })))
@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public void MapHubFindsAuthAttributeOnInheritedHub() public void MapHubFindsAuthAttributeOnInheritedHub()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost(options => options.MapHub<InheritedAuthHub>("path", httpSocketOptions => using (var builder = BuildWebHost(options => options.MapHub<InheritedAuthHub>("/path", httpSocketOptions =>
{ {
authCount += httpSocketOptions.AuthorizationData.Count; authCount += httpSocketOptions.AuthorizationData.Count;
}))) })))
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public void MapHubFindsMultipleAuthAttributesOnDoubleAuthHub() public void MapHubFindsMultipleAuthAttributesOnDoubleAuthHub()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost(options => options.MapHub<DoubleAuthHub>("path", httpSocketOptions => using (var builder = BuildWebHost(options => options.MapHub<DoubleAuthHub>("/path", httpSocketOptions =>
{ {
authCount += httpSocketOptions.AuthorizationData.Count; authCount += httpSocketOptions.AuthorizationData.Count;
}))) })))

View File

@ -18,8 +18,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
app.UseSockets(options => options.MapEndPoint<EchoEndPoint>("echo")); app.UseSockets(options => options.MapEndPoint<EchoEndPoint>("/echo"));
app.UseSignalR(options => options.MapHub<UncreatableHub>("uncreatable")); app.UseSignalR(options => options.MapHub<UncreatableHub>("/uncreatable"));
} }
} }
} }

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Sockets.Tests
public void MapEndPointFindsAuthAttributeOnEndPoint() public void MapEndPointFindsAuthAttributeOnEndPoint()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost<AuthEndPoint>("auth", using (var builder = BuildWebHost<AuthEndPoint>("/auth",
options => authCount += options.AuthorizationData.Count)) options => authCount += options.AuthorizationData.Count))
{ {
builder.Start(); builder.Start();
@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Sockets.Tests
public void MapEndPointFindsAuthAttributeOnInheritedEndPoint() public void MapEndPointFindsAuthAttributeOnInheritedEndPoint()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost<InheritedAuthEndPoint>("auth", using (var builder = BuildWebHost<InheritedAuthEndPoint>("/auth",
options => authCount += options.AuthorizationData.Count)) options => authCount += options.AuthorizationData.Count))
{ {
builder.Start(); builder.Start();
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Sockets.Tests
public void MapEndPointFindsAuthAttributesOnDoubleAuthEndPoint() public void MapEndPointFindsAuthAttributesOnDoubleAuthEndPoint()
{ {
var authCount = 0; var authCount = 0;
using (var builder = BuildWebHost<DoubleAuthEndPoint>("auth", using (var builder = BuildWebHost<DoubleAuthEndPoint>("/auth",
options => authCount += options.AuthorizationData.Count)) options => authCount += options.AuthorizationData.Count))
{ {
builder.Start(); builder.Start();
@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.Sockets.Tests
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2, SkipReason = "No WebSockets Client for this platform")] [OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2, SkipReason = "No WebSockets Client for this platform")]
public async Task MapEndPointWithWebSocketSubProtocolSetsProtocol() public async Task MapEndPointWithWebSocketSubProtocolSetsProtocol()
{ {
var host = BuildWebHost<MyEndPoint>("socket", var host = BuildWebHost<MyEndPoint>("/socket",
options => options.WebSockets.SubProtocol = "protocol1"); options => options.WebSockets.SubProtocol = "protocol1");
await host.StartAsync(); await host.StartAsync();