|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.AspNetCore.SignalR.Internal; |
| 8 | +using Microsoft.AspNetCore.SignalR.Protocol; |
| 9 | +using Microsoft.Extensions.Logging.Abstractions; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.SignalR.Tests.Internal |
| 14 | +{ |
| 15 | + public class DefaultHubMessageSerializerTests |
| 16 | + { |
| 17 | + [Theory] |
| 18 | + [MemberData(nameof(InvocationTestData))] |
| 19 | + public void SerializeMessages(string testName) |
| 20 | + { |
| 21 | + var testData = _invocationTestData[testName]; |
| 22 | + |
| 23 | + var resolver = CreateHubProtocolResolver(new List<IHubProtocol> { new MessagePackHubProtocol(), new JsonHubProtocol() }); |
| 24 | + var protocolNames = new List<string>(); |
| 25 | + foreach (var protocol in testData.SupportedHubProtocols) |
| 26 | + { |
| 27 | + protocolNames.Add(protocol.Name); |
| 28 | + } |
| 29 | + var serializer = new DefaultHubMessageSerializer<Hub>(resolver, Options.Create(new HubOptions() { SupportedProtocols = protocolNames, AdditionalHubProtocols = testData.AdditionalHubProtocols }), Options.Create(new HubOptions<Hub>())); |
| 30 | + var serializedHubMessage = serializer.SerializeMessage(_testMessage); |
| 31 | + |
| 32 | + var serializedMessages = serializedHubMessage.GetAllSerializations().ToList(); |
| 33 | + |
| 34 | + var allBytes = new List<byte>(); |
| 35 | + Assert.Equal(testData.SupportedHubProtocols.Count + testData.AdditionalHubProtocols.Count, serializedMessages.Count); |
| 36 | + foreach (var message in serializedMessages) |
| 37 | + { |
| 38 | + allBytes.AddRange(message.Serialized.ToArray()); |
| 39 | + } |
| 40 | + |
| 41 | + Assert.Equal(testData.Encoded, allBytes); |
| 42 | + } |
| 43 | + |
| 44 | + private IHubProtocolResolver CreateHubProtocolResolver(List<IHubProtocol> hubProtocols) |
| 45 | + { |
| 46 | + return new DefaultHubProtocolResolver(hubProtocols, NullLogger<DefaultHubProtocolResolver>.Instance); |
| 47 | + } |
| 48 | + |
| 49 | + // We use a func so we are guaranteed to get a new SerializedHubMessage for each test |
| 50 | + private static Dictionary<string, ProtocolTestData> _invocationTestData = new[] |
| 51 | + { |
| 52 | + new ProtocolTestData( |
| 53 | + "Single supported protocol", |
| 54 | + new List<IHubProtocol>() { new MessagePackHubProtocol() }, |
| 55 | + new List<IHubProtocol>(), |
| 56 | + 0x0D, |
| 57 | + 0x96, |
| 58 | + 0x01, |
| 59 | + 0x80, |
| 60 | + 0xC0, |
| 61 | + 0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', |
| 62 | + 0x90, |
| 63 | + 0x90), |
| 64 | + new ProtocolTestData( |
| 65 | + "Supported protocol with same additional protocol", |
| 66 | + new List<IHubProtocol>() { new MessagePackHubProtocol() }, |
| 67 | + new List<IHubProtocol>() { new MessagePackHubProtocol() }, |
| 68 | + 0x0D, |
| 69 | + 0x96, |
| 70 | + 0x01, |
| 71 | + 0x80, |
| 72 | + 0xC0, |
| 73 | + 0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', |
| 74 | + 0x90, |
| 75 | + 0x90, |
| 76 | + 0x0D, |
| 77 | + 0x96, |
| 78 | + 0x01, |
| 79 | + 0x80, |
| 80 | + 0xC0, |
| 81 | + 0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', |
| 82 | + 0x90, |
| 83 | + 0x90), |
| 84 | + new ProtocolTestData( |
| 85 | + "Supported protocol with different additional protocol", |
| 86 | + new List<IHubProtocol>() { new MessagePackHubProtocol() }, |
| 87 | + new List<IHubProtocol>() { new JsonHubProtocol() }, |
| 88 | + 0x0D, |
| 89 | + 0x96, |
| 90 | + 0x01, |
| 91 | + 0x80, |
| 92 | + 0xC0, |
| 93 | + 0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', |
| 94 | + 0x90, |
| 95 | + 0x90, |
| 96 | + (byte)'{', (byte)'"', (byte)'t', (byte)'y', (byte)'p', (byte)'e', (byte)'"', (byte)':', (byte)'1', |
| 97 | + (byte)',',(byte)'"', (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', (byte)'"', (byte)':', |
| 98 | + (byte)'"', (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', (byte)'"', |
| 99 | + (byte)',', (byte)'"', (byte)'a', (byte)'r', (byte)'g', (byte)'u', (byte)'m', (byte)'e', (byte)'n', (byte)'t', (byte)'s', (byte)'"', |
| 100 | + (byte)':', (byte)'[', (byte)']', (byte)'}', 0x1e), |
| 101 | + new ProtocolTestData( |
| 102 | + "No supported protocol with additional protocol", |
| 103 | + new List<IHubProtocol>(), |
| 104 | + new List<IHubProtocol>() { new MessagePackHubProtocol() }, |
| 105 | + 0x0D, |
| 106 | + 0x96, |
| 107 | + 0x01, |
| 108 | + 0x80, |
| 109 | + 0xC0, |
| 110 | + 0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', |
| 111 | + 0x90, |
| 112 | + 0x90), |
| 113 | + new ProtocolTestData( |
| 114 | + "No protocols", |
| 115 | + new List<IHubProtocol>(), |
| 116 | + new List<IHubProtocol>()), |
| 117 | + }.ToDictionary(t => t.Name); |
| 118 | + |
| 119 | + public static IEnumerable<object[]> InvocationTestData = _invocationTestData.Keys.Select(k => new object[] { k }); |
| 120 | + |
| 121 | + public class ProtocolTestData |
| 122 | + { |
| 123 | + public string Name { get; } |
| 124 | + public byte[] Encoded { get; } |
| 125 | + public List<IHubProtocol> AdditionalHubProtocols { get; } |
| 126 | + public List<IHubProtocol> SupportedHubProtocols { get; } |
| 127 | + |
| 128 | + public ProtocolTestData(string name, List<IHubProtocol> supportedHubProtocols, List<IHubProtocol> additionalHubProtocols, params byte[] encoded) |
| 129 | + { |
| 130 | + Name = name; |
| 131 | + Encoded = encoded; |
| 132 | + AdditionalHubProtocols = additionalHubProtocols; |
| 133 | + SupportedHubProtocols = supportedHubProtocols; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // The actual invocation message doesn't matter |
| 138 | + private static InvocationMessage _testMessage = new InvocationMessage("target", Array.Empty<object>()); |
| 139 | + } |
| 140 | +} |
0 commit comments