// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Protocol; using Microsoft.AspNetCore.SignalR.Tests; using Microsoft.Extensions.Logging.Abstractions; using Xunit; namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests { public class DefaultHubProtocolResolverTests { [Theory] [MemberData(nameof(HubProtocolNames))] public void DefaultHubProtocolResolverTestsCanCreateAllProtocols(string protocolName) { var protocol = HubProtocolHelpers.GetHubProtocol(protocolName); var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger.Instance); Assert.IsType( protocol.GetType(), resolver.GetProtocol(protocol.Name, HubProtocolHelpers.AllProtocolNames)); } [Theory] [MemberData(nameof(HubProtocolNames))] public void DefaultHubProtocolResolverCreatesProtocolswhenSupoortedProtocolsIsNull(string protocolName) { var protocol = HubProtocolHelpers.GetHubProtocol(protocolName); List supportedProtocols = null; var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger.Instance); Assert.IsType( protocol.GetType(), resolver.GetProtocol(protocol.Name, supportedProtocols)); } [Theory] [MemberData(nameof(HubProtocolNames))] public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(string protocolName) { var protocol = HubProtocolHelpers.GetHubProtocol(protocolName); var supportedProtocols = new List { protocol.Name }; var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger.Instance); Assert.IsType( protocol.GetType(), resolver.GetProtocol(protocol.Name, supportedProtocols)); } [Fact] public void DefaultHubProtocolResolverThrowsForNullProtocol() { var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger.Instance); var exception = Assert.Throws( () => resolver.GetProtocol(null, HubProtocolHelpers.AllProtocolNames)); Assert.Equal("protocolName", exception.ParamName); } [Fact] public void DefaultHubProtocolResolverReturnsNullForNotSupportedProtocol() { var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger.Instance); Assert.Null(resolver.GetProtocol("notARealProtocol", HubProtocolHelpers.AllProtocolNames)); } [Fact] public void RegisteringMultipleHubProtocolsFails() { var exception = Assert.Throws(() => new DefaultHubProtocolResolver(new[] { new JsonHubProtocol(), new JsonHubProtocol() }, NullLogger.Instance)); Assert.Equal($"Multiple Hub Protocols with the name 'json' were registered.", exception.Message); } public static IEnumerable HubProtocolNames => HubProtocolHelpers.AllProtocols.Select(p => new object[] {p.Name}); } }