|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Linq;
|
| 7 | +using System.Runtime.Serialization; |
7 | 8 | using System.Security.Claims;
|
8 | 9 | using System.Threading;
|
9 | 10 | using System.Threading.Tasks;
|
10 | 11 | using System.Threading.Tasks.Channels;
|
11 | 12 | using Microsoft.AspNetCore.Authorization;
|
| 13 | +using Microsoft.AspNetCore.Hosting; |
12 | 14 | using Microsoft.AspNetCore.Http;
|
13 | 15 | using Microsoft.AspNetCore.SignalR.Internal.Protocol;
|
14 | 16 | using Microsoft.AspNetCore.SignalR.Tests.Common;
|
|
21 | 23 | using Newtonsoft.Json;
|
22 | 24 | using Newtonsoft.Json.Serialization;
|
23 | 25 | using Xunit;
|
24 |
| -using Microsoft.AspNetCore.Hosting; |
25 | 26 |
|
26 | 27 | namespace Microsoft.AspNetCore.SignalR.Tests
|
27 | 28 | {
|
@@ -1149,6 +1150,32 @@ public async Task GetHttpContextFromHubConnectionContextHandlesNull()
|
1149 | 1150 | }
|
1150 | 1151 | }
|
1151 | 1152 |
|
| 1153 | + [Fact] |
| 1154 | + public async Task ConnectionClosedIfWritingToTransportFails() |
| 1155 | + { |
| 1156 | + // MessagePack does not support serializing objects or private types (including anonymous types) |
| 1157 | + // and throws. In this test we make sure that this exception closes the connection and bubbles up. |
| 1158 | + |
| 1159 | + var serviceProvider = CreateServiceProvider(); |
| 1160 | + |
| 1161 | + var endPoint = serviceProvider.GetService<HubEndPoint<MethodHub>>(); |
| 1162 | + |
| 1163 | + using (var client = new TestClient(false, new MessagePackHubProtocol())) |
| 1164 | + { |
| 1165 | + var transportFeature = new Mock<IConnectionTransportFeature>(); |
| 1166 | + transportFeature.SetupGet(f => f.TransportCapabilities).Returns(TransferMode.Binary); |
| 1167 | + client.Connection.Features.Set(transportFeature.Object); |
| 1168 | + |
| 1169 | + var endPointLifetime = endPoint.OnConnectedAsync(client.Connection); |
| 1170 | + |
| 1171 | + await client.Connected.OrTimeout(); |
| 1172 | + |
| 1173 | + await client.SendInvocationAsync(nameof(MethodHub.SendAnonymousObject)).OrTimeout(); |
| 1174 | + |
| 1175 | + await Assert.ThrowsAsync<SerializationException>(() => endPointLifetime.OrTimeout()); |
| 1176 | + } |
| 1177 | + } |
| 1178 | + |
1152 | 1179 | private static void AssertHubMessage(HubMessage expected, HubMessage actual)
|
1153 | 1180 | {
|
1154 | 1181 | // We aren't testing InvocationIds here
|
@@ -1567,6 +1594,11 @@ public string ConcatString(byte b, int i, char c, string s)
|
1567 | 1594 | return $"{b}, {i}, {c}, {s}";
|
1568 | 1595 | }
|
1569 | 1596 |
|
| 1597 | + public Task SendAnonymousObject() |
| 1598 | + { |
| 1599 | + return Clients.Client(Context.ConnectionId).InvokeAsync("Send", new { }); |
| 1600 | + } |
| 1601 | + |
1570 | 1602 | public override Task OnDisconnectedAsync(Exception e)
|
1571 | 1603 | {
|
1572 | 1604 | return Task.CompletedTask;
|
|
0 commit comments