|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace System.Net.WebSockets.Tests |
| 11 | +{ |
| 12 | + public class WebSocketCloseTests |
| 13 | + { |
| 14 | + private readonly CancellationTokenSource? _cancellation; |
| 15 | + |
| 16 | + public WebSocketCloseTests() |
| 17 | + { |
| 18 | + if (!Debugger.IsAttached) |
| 19 | + { |
| 20 | + _cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public CancellationToken CancellationToken => _cancellation?.Token ?? default; |
| 25 | + |
| 26 | + public static object[][] CloseStatuses = { |
| 27 | + new object[] { WebSocketCloseStatus.EndpointUnavailable }, |
| 28 | + new object[] { WebSocketCloseStatus.InternalServerError }, |
| 29 | + new object[] { WebSocketCloseStatus.InvalidMessageType}, |
| 30 | + new object[] { WebSocketCloseStatus.InvalidPayloadData }, |
| 31 | + new object[] { WebSocketCloseStatus.MandatoryExtension }, |
| 32 | + new object[] { WebSocketCloseStatus.MessageTooBig }, |
| 33 | + new object[] { WebSocketCloseStatus.NormalClosure }, |
| 34 | + new object[] { WebSocketCloseStatus.PolicyViolation }, |
| 35 | + new object[] { WebSocketCloseStatus.ProtocolError }, |
| 36 | + new object[] { (WebSocketCloseStatus)1012 }, // ServiceRestart indicates that the server / service is restarting. |
| 37 | + new object[] { (WebSocketCloseStatus)1013 }, // TryAgainLater indicates that a temporary server condition forced blocking the client's request. |
| 38 | + new object[] { (WebSocketCloseStatus)1014 }, // BadGateway indicates that the server acting as gateway received an invalid response |
| 39 | + }; |
| 40 | + |
| 41 | + [Theory] |
| 42 | + [MemberData(nameof(CloseStatuses))] |
| 43 | + public void WebSocketReceiveResult_WebSocketCloseStatus_Roundtrip(WebSocketCloseStatus closeStatus) |
| 44 | + { |
| 45 | + string closeStatusDescription = "closeStatus " + closeStatus.ToString(); |
| 46 | + WebSocketReceiveResult wsrr = new WebSocketReceiveResult(42, WebSocketMessageType.Close, endOfMessage: true, closeStatus, closeStatusDescription); |
| 47 | + Assert.Equal(42, wsrr.Count); |
| 48 | + Assert.Equal(closeStatus, wsrr.CloseStatus); |
| 49 | + Assert.Equal(closeStatusDescription, wsrr.CloseStatusDescription); |
| 50 | + } |
| 51 | + |
| 52 | + [Theory] |
| 53 | + [MemberData(nameof(CloseStatuses))] |
| 54 | + public async Task ReceiveAsync_ValidCloseStatus_Success(WebSocketCloseStatus closeStatus) |
| 55 | + { |
| 56 | + byte[] receiveBuffer = new byte[1024]; |
| 57 | + WebSocketTestStream stream = new(); |
| 58 | + Encoding encoding = Encoding.UTF8; |
| 59 | + |
| 60 | + using (WebSocket server = WebSocket.CreateFromStream(stream, isServer: true, subProtocol: null, TimeSpan.FromSeconds(3))) |
| 61 | + using (WebSocket client = WebSocket.CreateFromStream(stream.Remote, isServer: false, subProtocol: null, TimeSpan.FromSeconds(3))) |
| 62 | + { |
| 63 | + Assert.NotNull(server); |
| 64 | + Assert.NotNull(client); |
| 65 | + |
| 66 | + // send something |
| 67 | + string hello = "Testing " + closeStatus.ToString(); |
| 68 | + byte[] sendBytes = encoding.GetBytes(hello); |
| 69 | + await server.SendAsync(sendBytes.AsMemory(), WebSocketMessageType.Text, WebSocketMessageFlags.None, CancellationToken); |
| 70 | + |
| 71 | + // and then server-side close with the test status |
| 72 | + string closeStatusDescription = "CloseStatus " + closeStatus.ToString(); |
| 73 | + await server.CloseOutputAsync(closeStatus, closeStatusDescription, CancellationToken); |
| 74 | + |
| 75 | + // get the hello from the client (after the close message was sent) |
| 76 | + WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken); |
| 77 | + Assert.Equal(WebSocketMessageType.Text, result.MessageType); |
| 78 | + string response = encoding.GetString(receiveBuffer.AsSpan(0, result.Count)); |
| 79 | + Assert.Equal(hello, response); |
| 80 | + |
| 81 | + // now look for the expected close status |
| 82 | + WebSocketReceiveResult closing = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken); |
| 83 | + Assert.Equal(WebSocketMessageType.Close, closing.MessageType); |
| 84 | + Assert.Equal(closeStatus, closing.CloseStatus); |
| 85 | + Assert.Equal(closeStatusDescription, closing.CloseStatusDescription); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments