Skip to content
代码片段 群组 项目
未验证 提交 4b1fb58a 编辑于 作者: BrennanConroy's avatar BrennanConroy 提交者: GitHub
浏览文件

Throw error on unexpected close (#2205)

上级 6f638279
No related branches found
No related tags found
无相关合并请求
......@@ -6,6 +6,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Channels;
......@@ -640,7 +641,8 @@ namespace Microsoft.AspNetCore.SignalR.Client
break;
}
}
else if (result.IsCompleted)
if (result.IsCompleted)
{
// Not enough data, and we won't be getting any more data.
throw new InvalidOperationException(
......@@ -717,8 +719,13 @@ namespace Microsoft.AspNetCore.SignalR.Client
break;
}
}
else if (result.IsCompleted)
if (result.IsCompleted)
{
if (!buffer.IsEmpty)
{
throw new InvalidDataException("Connection terminated while reading a message.");
}
break;
}
}
......
......@@ -381,12 +381,9 @@ namespace Microsoft.AspNetCore.SignalR
await WriteHandshakeResponseAsync(HandshakeResponseMessage.Empty);
return true;
}
else
{
_logger.LogInformation("Didn't parse the handshake");
}
}
else if (result.IsCompleted)
if (result.IsCompleted)
{
// connection was closed before we ever received a response
// can't send a handshake response because there is no longer a connection
......
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
......@@ -199,8 +200,13 @@ namespace Microsoft.AspNetCore.SignalR
await _dispatcher.DispatchMessageAsync(connection, message);
}
}
else if (result.IsCompleted)
if (result.IsCompleted)
{
if (!buffer.IsEmpty)
{
throw new InvalidDataException("Connection terminated while reading a message.");
}
break;
}
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
......@@ -34,7 +35,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
testConnection.StartAsync,
connection => ((TestConnection)connection).DisposeAsync());
builder.Services.AddSingleton<IConnectionFactory>(delegateConnectionFactory);
return builder.Build();
}
......@@ -450,6 +451,34 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}
}
[Fact]
public async Task HubConnectionClosesWithErrorIfTerminatedWithPartialMessage()
{
var builder = new HubConnectionBuilder();
var innerConnection = new TestConnection();
var delegateConnectionFactory = new DelegateConnectionFactory(
format => innerConnection.StartAsync(format),
connection => ((TestConnection)connection).DisposeAsync());
builder.Services.AddSingleton<IConnectionFactory>(delegateConnectionFactory);
var hubConnection = builder.Build();
var closedEventTcs = new TaskCompletionSource<Exception>();
hubConnection.Closed += e =>
{
closedEventTcs.SetResult(e);
return Task.CompletedTask;
};
await hubConnection.StartAsync().OrTimeout();
await innerConnection.Application.Output.WriteAsync(Encoding.UTF8.GetBytes(new[] { '{' })).OrTimeout();
innerConnection.Application.Output.Complete();
var exception = await closedEventTcs.Task.OrTimeout();
Assert.Equal("Connection terminated while reading a message.", exception.Message);
}
private static async Task ForceLastInvocationToComplete(TestConnection testConnection)
{
// We need to "complete" the invocation
......
......@@ -2184,6 +2184,38 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Fact]
public async Task ServerSendsCloseWithErrorWhenConnectionClosedWithPartialMessage()
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
{
services.AddSignalR(options => options.EnableDetailedErrors = true);
});
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<SimpleHub>>();
using (var client = new TestClient())
{
var connectionHandlerTask = await client.ConnectAsync(connectionHandler).OrTimeout();
await client.Connection.Application.Output.WriteAsync(Encoding.UTF8.GetBytes(new[] { '{' })).OrTimeout();
// Close connection
client.Connection.Application.Output.Complete();
// Ignore message from OnConnectedAsync
await client.ReadAsync().OrTimeout();
var closeMessage = Assert.IsType<CloseMessage>(await client.ReadAsync().OrTimeout());
Assert.Equal("Connection closed with an error. InvalidDataException: Connection terminated while reading a message.", closeMessage.Error);
// Shut down
client.Dispose();
await connectionHandlerTask.OrTimeout();
}
}
private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
{
public int ReleaseCount;
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册