public async Task Connect_Success() { using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); var webSocket = await context.WebSockets.AcceptWebSocketAsync(); })) { var client = new WebSocketClient(); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { } } }
public async Task ReceiveFragmentedData_Success() { var orriginalData = Encoding.UTF8.GetBytes("Hello World"); using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await webSocket.SendAsync(new ArraySegment <byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None); await webSocket.SendAsync(new ArraySegment <byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None); await webSocket.SendAsync(new ArraySegment <byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None); })) { var client = new WebSocketClient(); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { var clientBuffer = new byte[orriginalData.Length]; var result = await clientSocket.ReceiveAsync(new ArraySegment <byte>(clientBuffer), CancellationToken.None); Assert.False(result.EndOfMessage); Assert.Equal(2, result.Count); int totalReceived = result.Count; Assert.Equal(WebSocketMessageType.Binary, result.MessageType); result = await clientSocket.ReceiveAsync( new ArraySegment <byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None); Assert.False(result.EndOfMessage); Assert.Equal(2, result.Count); totalReceived += result.Count; Assert.Equal(WebSocketMessageType.Binary, result.MessageType); result = await clientSocket.ReceiveAsync( new ArraySegment <byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None); Assert.True(result.EndOfMessage); Assert.Equal(7, result.Count); totalReceived += result.Count; Assert.Equal(WebSocketMessageType.Binary, result.MessageType); Assert.Equal(orriginalData, clientBuffer); } } }
public async Task NegotiateSubProtocol_Success() { using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); Assert.Equal("alpha, bravo, charlie", context.Request.Headers["Sec-WebSocket-Protocol"]); var webSocket = await context.WebSockets.AcceptWebSocketAsync("Bravo"); })) { var client = new WebSocketClient(); client.SubProtocols.Add("alpha"); client.SubProtocols.Add("bravo"); client.SubProtocols.Add("charlie"); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { Assert.Equal("Bravo", clientSocket.SubProtocol); } } }
public async Task CloseFromCloseReceived_Success() { string closeDescription = "Test Closed"; using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None); var serverBuffer = new byte[1024]; var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(serverBuffer), CancellationToken.None); Assert.True(result.EndOfMessage); Assert.Equal(0, result.Count); Assert.Equal(WebSocketMessageType.Close, result.MessageType); Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus); Assert.Equal(closeDescription, result.CloseStatusDescription); })) { var client = new WebSocketClient(); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { var clientBuffer = new byte[1024]; var result = await clientSocket.ReceiveAsync(new ArraySegment <byte>(clientBuffer), CancellationToken.None); Assert.True(result.EndOfMessage); Assert.Equal(0, result.Count); Assert.Equal(WebSocketMessageType.Close, result.MessageType); Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus); Assert.Equal(closeDescription, result.CloseStatusDescription); Assert.Equal(WebSocketState.CloseReceived, clientSocket.State); await clientSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); Assert.Equal(WebSocketState.Closed, clientSocket.State); } } }
public async Task SendEmptyData_Success() { using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); var webSocket = await context.WebSockets.AcceptWebSocketAsync(); var serverBuffer = new byte[0]; var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(serverBuffer), CancellationToken.None); Assert.True(result.EndOfMessage); Assert.Equal(0, result.Count); Assert.Equal(WebSocketMessageType.Binary, result.MessageType); })) { var client = new WebSocketClient(); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { var orriginalData = new byte[0]; await clientSocket.SendAsync(new ArraySegment <byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None); } } }
public async Task SendLongData_Success() { var orriginalData = Encoding.UTF8.GetBytes(new string('a', 0x1FFFF)); using (var server = KestrelWebSocketHelpers.CreateServer(async context => { Assert.True(context.WebSockets.IsWebSocketRequest); var webSocket = await context.WebSockets.AcceptWebSocketAsync(); var serverBuffer = new byte[orriginalData.Length]; var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(serverBuffer), CancellationToken.None); int intermediateCount = result.Count; Assert.False(result.EndOfMessage); Assert.Equal(WebSocketMessageType.Text, result.MessageType); result = await webSocket.ReceiveAsync(new ArraySegment <byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None); intermediateCount += result.Count; Assert.False(result.EndOfMessage); Assert.Equal(WebSocketMessageType.Text, result.MessageType); result = await webSocket.ReceiveAsync(new ArraySegment <byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None); intermediateCount += result.Count; Assert.True(result.EndOfMessage); Assert.Equal(orriginalData.Length, intermediateCount); Assert.Equal(WebSocketMessageType.Text, result.MessageType); Assert.Equal(orriginalData, serverBuffer); })) { var client = new WebSocketClient(); using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None)) { await clientSocket.SendAsync(new ArraySegment <byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None); } } }