Remove HubConnectionContext parameter from IHubProtocolResolver (#1735)

This commit is contained in:
David Fowler 2018-03-27 10:25:00 -07:00 committed by GitHub
parent b7e2678592
commit 185453908f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 26 deletions

View File

@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.SignalR
{
if (HandshakeProtocol.TryParseRequestMessage(buffer, out var handshakeRequestMessage, out consumed, out examined))
{
Protocol = protocolResolver.GetProtocol(handshakeRequestMessage.Protocol, supportedProtocols, this);
Protocol = protocolResolver.GetProtocol(handshakeRequestMessage.Protocol, supportedProtocols);
if (Protocol == null)
{
Log.HandshakeFailed(_logger, null);

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal
}
}
public IHubProtocol GetProtocol(string protocolName, IList<string> supportedProtocols, HubConnectionContext connection)
public IHubProtocol GetProtocol(string protocolName, IList<string> supportedProtocols)
{
protocolName = protocolName ?? throw new ArgumentNullException(nameof(protocolName));

View File

@ -8,6 +8,6 @@ namespace Microsoft.AspNetCore.SignalR.Internal
{
public interface IHubProtocolResolver
{
IHubProtocol GetProtocol(string protocolName, IList<string> supportedProtocols, HubConnectionContext connection);
IHubProtocol GetProtocol(string protocolName, IList<string> supportedProtocols);
}
}

View File

@ -28,52 +28,40 @@ namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverTestsCanCreateAllProtocols(IHubProtocol protocol)
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, AllProtocolNames, mockConnection.Object));
resolver.GetProtocol(protocol.Name, AllProtocolNames));
}
[Theory]
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverCreatesProtocolswhenSupoortedProtocolsIsNull(IHubProtocol protocol)
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
List<string> supportedProtocols = null;
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, supportedProtocols, mockConnection.Object));
resolver.GetProtocol(protocol.Name, supportedProtocols));
}
[Theory]
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(IHubProtocol protocol)
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
var supportedProtocols = new List<string> { protocol.Name };
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, supportedProtocols, mockConnection.Object));
resolver.GetProtocol(protocol.Name, supportedProtocols));
}
[Fact]
public void DefaultHubProtocolResolverThrowsForNullProtocol()
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var exception = Assert.Throws<ArgumentNullException>(
() => resolver.GetProtocol(null, AllProtocolNames, mockConnection.Object));
() => resolver.GetProtocol(null, AllProtocolNames));
Assert.Equal("protocolName", exception.ParamName);
}
@ -81,19 +69,13 @@ namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests
[Fact]
public void DefaultHubProtocolResolverReturnsNullForNotSupportedProtocol()
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.Null(resolver.GetProtocol("notARealProtocol", AllProtocolNames, mockConnection.Object));
Assert.Null(resolver.GetProtocol("notARealProtocol", AllProtocolNames));
}
[Fact]
public void RegisteringMultipleHubProtocolsFails()
{
var connection = new Mock<ConnectionContext>();
connection.Setup(m => m.Features).Returns(new FeatureCollection());
var mockConnection = new Mock<HubConnectionContext>(connection.Object, TimeSpan.FromSeconds(30), NullLoggerFactory.Instance) { CallBase = true };
var exception = Assert.Throws<InvalidOperationException>(() => new DefaultHubProtocolResolver(new[] {
new JsonHubProtocol(),
new JsonHubProtocol()