|
16 | 16 | using Microsoft.AspNetCore.Testing;
|
17 | 17 | using Microsoft.Extensions.DependencyInjection;
|
18 | 18 | using Microsoft.Extensions.Logging;
|
| 19 | +using Microsoft.Net.Http.Headers; |
19 | 20 | using Xunit;
|
20 | 21 |
|
21 | 22 | namespace Microsoft.AspNetCore.TestHost
|
@@ -172,6 +173,7 @@ public async Task WebSocketWorks()
|
172 | 173 | {
|
173 | 174 | if (ctx.WebSockets.IsWebSocketRequest)
|
174 | 175 | {
|
| 176 | + Assert.False(ctx.Request.Headers.ContainsKey(HeaderNames.SecWebSocketProtocol)); |
175 | 177 | var websocket = await ctx.WebSockets.AcceptWebSocketAsync();
|
176 | 178 | var receiveArray = new byte[1024];
|
177 | 179 | while (true)
|
@@ -232,6 +234,58 @@ public async Task WebSocketWorks()
|
232 | 234 | clientSocket.Dispose();
|
233 | 235 | }
|
234 | 236 |
|
| 237 | + [Fact] |
| 238 | + public async Task WebSocketSubProtocolsWorks() |
| 239 | + { |
| 240 | + // Arrange |
| 241 | + RequestDelegate appDelegate = async ctx => |
| 242 | + { |
| 243 | + if (ctx.WebSockets.IsWebSocketRequest) |
| 244 | + { |
| 245 | + if (ctx.WebSockets.WebSocketRequestedProtocols.Contains("alpha") && |
| 246 | + ctx.WebSockets.WebSocketRequestedProtocols.Contains("bravo")) |
| 247 | + { |
| 248 | + // according to rfc6455, the "server needs to include the same field and one of the selected subprotocol values" |
| 249 | + // however, this isn't enforced by either our server or client so it's possible to accept an arbitrary protocol. |
| 250 | + // Done here to demonstrate not "correct" behaviour, simply to show it's possible. Other clients may not allow this. |
| 251 | + var websocket = await ctx.WebSockets.AcceptWebSocketAsync("charlie"); |
| 252 | + await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None); |
| 253 | + } |
| 254 | + else |
| 255 | + { |
| 256 | + var subprotocols = ctx.WebSockets.WebSocketRequestedProtocols.Any() |
| 257 | + ? string.Join(", ", ctx.WebSockets.WebSocketRequestedProtocols) |
| 258 | + : "<none>"; |
| 259 | + var closeReason = "Unexpected subprotocols: " + subprotocols; |
| 260 | + var websocket = await ctx.WebSockets.AcceptWebSocketAsync(); |
| 261 | + await websocket.CloseAsync(WebSocketCloseStatus.InternalServerError, closeReason, CancellationToken.None); |
| 262 | + } |
| 263 | + } |
| 264 | + }; |
| 265 | + var builder = new WebHostBuilder() |
| 266 | + .Configure(app => |
| 267 | + { |
| 268 | + app.Run(appDelegate); |
| 269 | + }); |
| 270 | + var server = new TestServer(builder); |
| 271 | + |
| 272 | + // Act |
| 273 | + var client = server.CreateWebSocketClient(); |
| 274 | + client.SubProtocols.Add("alpha"); |
| 275 | + client.SubProtocols.Add("bravo"); |
| 276 | + var clientSocket = await client.ConnectAsync(new Uri("wss://localhost"), CancellationToken.None); |
| 277 | + var buffer = new byte[1024]; |
| 278 | + var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 279 | + |
| 280 | + // Assert |
| 281 | + Assert.Equal(WebSocketMessageType.Close, result.MessageType); |
| 282 | + Assert.Equal("Normal Closure", result.CloseStatusDescription); |
| 283 | + Assert.Equal(WebSocketState.CloseReceived, clientSocket.State); |
| 284 | + Assert.Equal("charlie", clientSocket.SubProtocol); |
| 285 | + |
| 286 | + clientSocket.Dispose(); |
| 287 | + } |
| 288 | + |
235 | 289 | [ConditionalFact]
|
236 | 290 | public async Task WebSocketAcceptThrowsWhenCancelled()
|
237 | 291 | {
|
|
0 commit comments