-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add IHttpUpgradeFeature to TestServer for SignalR WebSocket support #33595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
namespace Microsoft.AspNetCore.TestHost | ||
{ | ||
internal class UpgradeFeature : IHttpUpgradeFeature | ||
{ | ||
public bool IsUpgradableRequest => false; | ||
|
||
// TestHost provides an IHttpWebSocketFeature so it wont call UpgradeAsync() | ||
public Task<Stream> UpgradeAsync() | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.TestHost.Tests | ||
|
@@ -54,5 +55,77 @@ await client.ConnectAsync( | |
Assert.Equal(expectedHost, capturedHost); | ||
Assert.Equal("/connect", capturedPath); | ||
} | ||
|
||
[Fact] | ||
public async Task CanAcceptWebSocket() | ||
{ | ||
using (var testServer = new TestServer(new WebHostBuilder() | ||
.Configure(app => | ||
{ | ||
app.UseWebSockets(); | ||
app.Run(async ctx => | ||
{ | ||
if (ctx.Request.Path.StartsWithSegments("/connect")) | ||
{ | ||
if (ctx.WebSockets.IsWebSocketRequest) | ||
{ | ||
using var websocket = await ctx.WebSockets.AcceptWebSocketAsync(); | ||
var buffer = new byte[1000]; | ||
var res = await websocket.ReceiveAsync(buffer, default); | ||
await websocket.SendAsync(buffer.AsMemory(0, res.Count), System.Net.WebSockets.WebSocketMessageType.Binary, true, default); | ||
await websocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default); | ||
} | ||
} | ||
}); | ||
}))) | ||
{ | ||
var client = testServer.CreateWebSocketClient(); | ||
|
||
using var socket = await client.ConnectAsync( | ||
uri: new Uri("http://localhost/connect"), | ||
cancellationToken: default); | ||
|
||
await socket.SendAsync(new byte[10], System.Net.WebSockets.WebSocketMessageType.Binary, true, default); | ||
var res = await socket.ReceiveAsync(new byte[100], default); | ||
Assert.Equal(10, res.Count); | ||
Assert.True(res.EndOfMessage); | ||
|
||
await socket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task VerifyWebSocketAndUpgradeFeatures() | ||
{ | ||
using (var testServer = new TestServer(new WebHostBuilder() | ||
.Configure(app => | ||
{ | ||
app.Run(async c => | ||
{ | ||
var upgradeFeature = c.Features.Get<IHttpUpgradeFeature>(); | ||
Assert.NotNull(upgradeFeature); | ||
Assert.False(upgradeFeature.IsUpgradableRequest); | ||
await Assert.ThrowsAsync<NotSupportedException>(() => upgradeFeature.UpgradeAsync()); | ||
|
||
var webSocketFeature = c.Features.Get<IHttpWebSocketFeature>(); | ||
Assert.NotNull(webSocketFeature); | ||
Assert.True(webSocketFeature.IsWebSocketRequest); | ||
}); | ||
}))) | ||
{ | ||
var client = testServer.CreateWebSocketClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant that you'd send a non-websocket request here like you do in the signalr negotiation scenario. Including |
||
|
||
try | ||
{ | ||
using var socket = await client.ConnectAsync( | ||
uri: new Uri("http://localhost/connect"), | ||
cancellationToken: default); | ||
} | ||
catch | ||
{ | ||
// An exception will be thrown because our endpoint does not accept the websocket | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/SignalR/clients/csharp/Client/test/UnitTests/TestServerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// 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.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.SignalR.Tests; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Client.Tests | ||
{ | ||
public class TestServerTests : VerifiableLoggedTest | ||
{ | ||
[Fact] | ||
public async Task WebSocketsWorks() | ||
{ | ||
using (StartVerifiableLog()) | ||
{ | ||
var builder = new WebHostBuilder().ConfigureServices(s => | ||
{ | ||
s.AddLogging(); | ||
s.AddSingleton(LoggerFactory); | ||
s.AddSignalR(); | ||
}).Configure(app => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapHub<EchoHub>("/echo"); | ||
}); | ||
}); | ||
var server = new TestServer(builder); | ||
|
||
var webSocketFactoryCalled = false; | ||
var connectionBuilder = new HubConnectionBuilder() | ||
.WithUrl(server.BaseAddress + "echo", options => | ||
{ | ||
options.Transports = Http.Connections.HttpTransportType.WebSockets; | ||
options.HttpMessageHandlerFactory = _ => | ||
{ | ||
return server.CreateHandler(); | ||
}; | ||
options.WebSocketFactory = async (context, token) => | ||
{ | ||
webSocketFactoryCalled = true; | ||
var wsClient = server.CreateWebSocketClient(); | ||
return await wsClient.ConnectAsync(context.Uri, default); | ||
}; | ||
}); | ||
connectionBuilder.Services.AddLogging(); | ||
connectionBuilder.Services.AddSingleton(LoggerFactory); | ||
var connection = connectionBuilder.Build(); | ||
|
||
var originalMessage = "message"; | ||
connection.On<string>("Echo", (receivedMessage) => | ||
{ | ||
Assert.Equal(originalMessage, receivedMessage); | ||
}); | ||
|
||
await connection.StartAsync(); | ||
await connection.InvokeAsync("Echo", originalMessage); | ||
Assert.True(webSocketFactoryCalled); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task LongPollingWorks() | ||
{ | ||
using (StartVerifiableLog()) | ||
{ | ||
var builder = new WebHostBuilder().ConfigureServices(s => | ||
{ | ||
s.AddLogging(); | ||
s.AddSingleton(LoggerFactory); | ||
s.AddSignalR(); | ||
}).Configure(app => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapHub<EchoHub>("/echo"); | ||
}); | ||
}); | ||
var server = new TestServer(builder); | ||
|
||
var connectionBuilder = new HubConnectionBuilder() | ||
.WithUrl(server.BaseAddress + "echo", options => | ||
{ | ||
options.Transports = Http.Connections.HttpTransportType.LongPolling; | ||
options.HttpMessageHandlerFactory = _ => | ||
{ | ||
return server.CreateHandler(); | ||
}; | ||
}); | ||
connectionBuilder.Services.AddLogging(); | ||
connectionBuilder.Services.AddSingleton(LoggerFactory); | ||
var connection = connectionBuilder.Build(); | ||
|
||
var originalMessage = "message"; | ||
connection.On<string>("Echo", (receivedMessage) => | ||
{ | ||
Assert.Equal(originalMessage, receivedMessage); | ||
}); | ||
|
||
await connection.StartAsync(); | ||
await connection.InvokeAsync("Echo", originalMessage); | ||
} | ||
} | ||
} | ||
|
||
class EchoHub : Hub | ||
{ | ||
public Task Echo(string message) | ||
{ | ||
return Clients.All.SendAsync("Echo", message); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want a different test for the normal TestServer scenario that shows the upgrade and WebSocket features are present, but an upgrade isn't possible.