4 using System.Threading;
5 using System.Threading.Tasks;
6 using System.Collections.Generic;
7 using System.Net.WebSockets;
8 using System.Reflection;
11 using NUnit.Framework;
13 using MonoTests.Helpers;
15 namespace MonoTests.System.Net.WebSockets
18 public class ClientWebSocketTest
20 const string EchoServerUrl = "ws://echo.websocket.org";
21 int Port = NetworkHelpers.FindFreePort ();
22 HttpListener listener;
23 ClientWebSocket socket;
24 MethodInfo headerSetMethod;
29 listener = new HttpListener ();
30 listener.Prefixes.Add ("http://localhost:" + Port + "/");
32 socket = new ClientWebSocket ();
36 public void Teardown ()
38 if (listener != null) {
43 if (socket.State == WebSocketState.Open)
44 socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (2000);
51 public void ServerHandshakeReturnCrapStatusCodeTest ()
54 #pragma warning disable 4014
55 HandleHttpRequestAsync ((req, resp) => resp.StatusCode = 418);
56 #pragma warning restore 4014
58 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
59 } catch (AggregateException e) {
60 AssertWebSocketException (e, WebSocketError.Success, typeof (WebException));
63 Assert.Fail ("Should have thrown");
67 public void ServerHandshakeReturnWrongUpgradeHeader ()
69 #pragma warning disable 4014
70 HandleHttpRequestAsync ((req, resp) => {
71 resp.StatusCode = 101;
72 resp.Headers["Upgrade"] = "gtfo";
74 #pragma warning restore 4014
76 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
77 } catch (AggregateException e) {
78 AssertWebSocketException (e, WebSocketError.Success);
81 Assert.Fail ("Should have thrown");
85 public void ServerHandshakeReturnWrongConnectionHeader ()
87 #pragma warning disable 4014
88 HandleHttpRequestAsync ((req, resp) => {
89 resp.StatusCode = 101;
90 resp.Headers["Upgrade"] = "websocket";
91 // Mono http request doesn't like the forcing, test still valid since the default connection header value is empty
92 //ForceSetHeader (resp.Headers, "Connection", "Foo");
94 #pragma warning restore 4014
96 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
97 } catch (AggregateException e) {
98 AssertWebSocketException (e, WebSocketError.Success);
101 Assert.Fail ("Should have thrown");
105 public void EchoTest ()
107 const string Payload = "This is a websocket test";
109 Assert.AreEqual (WebSocketState.None, socket.State);
110 socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait ();
111 Assert.AreEqual (WebSocketState.Open, socket.State);
113 var sendBuffer = Encoding.ASCII.GetBytes (Payload);
114 Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
116 var receiveBuffer = new byte[Payload.Length];
117 var resp = socket.ReceiveAsync (new ArraySegment<byte> (receiveBuffer), CancellationToken.None).Result;
119 Assert.AreEqual (Payload.Length, resp.Count);
120 Assert.IsTrue (resp.EndOfMessage);
121 Assert.AreEqual (WebSocketMessageType.Text, resp.MessageType);
122 Assert.AreEqual (Payload, Encoding.ASCII.GetString (receiveBuffer, 0, resp.Count));
124 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
125 Assert.AreEqual (WebSocketState.Closed, socket.State);
129 public void CloseOutputAsyncTest ()
131 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
132 Assert.AreEqual (WebSocketState.Open, socket.State);
134 Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
135 Assert.AreEqual (WebSocketState.CloseSent, socket.State);
137 var resp = socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Result;
138 Assert.AreEqual (WebSocketState.Closed, socket.State);
139 Assert.AreEqual (WebSocketMessageType.Close, resp.MessageType);
140 Assert.AreEqual (WebSocketCloseStatus.NormalClosure, resp.CloseStatus);
141 Assert.AreEqual (string.Empty, resp.CloseStatusDescription);
145 public void CloseAsyncTest ()
147 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
148 Assert.AreEqual (WebSocketState.Open, socket.State);
150 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
151 Assert.AreEqual (WebSocketState.Closed, socket.State);
154 [Test, ExpectedException (typeof (InvalidOperationException))]
155 public void SendAsyncArgTest_NotConnected ()
157 socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None);
160 [Test, ExpectedException (typeof (ArgumentNullException))]
161 public void SendAsyncArgTest_NoArray ()
163 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
164 socket.SendAsync (new ArraySegment<byte> (), WebSocketMessageType.Text, true, CancellationToken.None);
167 [Test, ExpectedException (typeof (InvalidOperationException))]
168 public void ReceiveAsyncArgTest_NotConnected ()
170 socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None);
173 [Test, ExpectedException (typeof (ArgumentNullException))]
174 public void ReceiveAsyncArgTest_NoArray ()
176 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
177 socket.ReceiveAsync (new ArraySegment<byte> (), CancellationToken.None);
181 public void ReceiveAsyncWrongState_Closed ()
184 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
185 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
186 Assert.IsTrue (socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Wait (5000));
187 } catch (AggregateException e) {
188 AssertWebSocketException (e, WebSocketError.Success);
191 Assert.Fail ("Should have thrown");
195 public void SendAsyncWrongState_Closed ()
198 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
199 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
200 Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
201 } catch (AggregateException e) {
202 AssertWebSocketException (e, WebSocketError.Success);
205 Assert.Fail ("Should have thrown");
209 public void SendAsyncWrongState_CloseSent ()
212 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
213 Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
214 Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
215 } catch (AggregateException e) {
216 AssertWebSocketException (e, WebSocketError.Success);
219 Assert.Fail ("Should have thrown");
223 [Category ("NotWorking")] // FIXME: test relies on unimplemented HttpListenerContext.AcceptWebSocketAsync (), reenable it when the method is implemented
224 public void SendAsyncEndOfMessageTest ()
226 var cancellationToken = new CancellationTokenSource (TimeSpan.FromSeconds (30)).Token;
227 SendAsyncEndOfMessageTest (false, WebSocketMessageType.Text, cancellationToken).Wait (5000);
228 SendAsyncEndOfMessageTest (true, WebSocketMessageType.Text, cancellationToken).Wait (5000);
229 SendAsyncEndOfMessageTest (false, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
230 SendAsyncEndOfMessageTest (true, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
233 public async Task SendAsyncEndOfMessageTest (bool expectedEndOfMessage, WebSocketMessageType webSocketMessageType, CancellationToken cancellationToken)
235 using (var client = new ClientWebSocket ()) {
236 // Configure the listener.
237 var serverReceive = HandleHttpWebSocketRequestAsync<WebSocketReceiveResult> (async socket => await socket.ReceiveAsync (new ArraySegment<byte> (new byte[32]), cancellationToken), cancellationToken);
239 // Connect to the listener and make the request.
240 await client.ConnectAsync (new Uri ("ws://localhost:" + Port + "/"), cancellationToken);
241 await client.SendAsync (new ArraySegment<byte> (Encoding.UTF8.GetBytes ("test")), webSocketMessageType, expectedEndOfMessage, cancellationToken);
243 // Wait for the listener to handle the request and return its result.
244 var result = await serverReceive;
246 // Cleanup and check results.
247 await client.CloseAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
248 Assert.AreEqual (expectedEndOfMessage, result.EndOfMessage, "EndOfMessage should be " + expectedEndOfMessage);
252 async Task<T> HandleHttpWebSocketRequestAsync<T> (Func<WebSocket, Task<T>> action, CancellationToken cancellationToken)
254 var ctx = await this.listener.GetContextAsync ();
255 var wsContext = await ctx.AcceptWebSocketAsync (null);
256 var result = await action (wsContext.WebSocket);
257 await wsContext.WebSocket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
261 async Task HandleHttpRequestAsync (Action<HttpListenerRequest, HttpListenerResponse> handler)
263 var ctx = await listener.GetContextAsync ();
264 handler (ctx.Request, ctx.Response);
265 ctx.Response.Close ();
268 void AssertWebSocketException (AggregateException e, WebSocketError error, Type inner = null)
270 var wsEx = e.InnerException as WebSocketException;
271 Console.WriteLine (e.InnerException.ToString ());
272 Assert.IsNotNull (wsEx, "Not a websocketexception");
273 Assert.AreEqual (error, wsEx.WebSocketErrorCode);
275 Assert.IsNotNull (wsEx.InnerException);
276 Assert.IsTrue (inner.IsInstanceOfType (wsEx.InnerException));
280 void ForceSetHeader (WebHeaderCollection headers, string name, string value)
282 if (headerSetMethod == null)
283 headerSetMethod = typeof (WebHeaderCollection).GetMethod ("AddValue", BindingFlags.NonPublic);
284 headerSetMethod.Invoke (headers, new[] { name, value });