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;
14 namespace MonoTests.System.Net.WebSockets
17 public class ClientWebSocketTest
19 const string EchoServerUrl = "ws://echo.websocket.org";
20 const int Port = 42123;
21 HttpListener listener;
22 ClientWebSocket socket;
23 MethodInfo headerSetMethod;
28 listener = new HttpListener ();
29 listener.Prefixes.Add ("http://localhost:" + Port + "/");
31 socket = new ClientWebSocket ();
35 public void Teardown ()
37 if (listener != null) {
42 if (socket.State == WebSocketState.Open)
43 socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (2000);
50 public void ServerHandshakeReturnCrapStatusCodeTest ()
53 #pragma warning disable 4014
54 HandleHttpRequestAsync ((req, resp) => resp.StatusCode = 418);
55 #pragma warning restore 4014
57 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
58 } catch (AggregateException e) {
59 AssertWebSocketException (e, WebSocketError.Success, typeof (WebException));
62 Assert.Fail ("Should have thrown");
66 public void ServerHandshakeReturnWrongUpgradeHeader ()
68 #pragma warning disable 4014
69 HandleHttpRequestAsync ((req, resp) => {
70 resp.StatusCode = 101;
71 resp.Headers["Upgrade"] = "gtfo";
73 #pragma warning restore 4014
75 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
76 } catch (AggregateException e) {
77 AssertWebSocketException (e, WebSocketError.Success);
80 Assert.Fail ("Should have thrown");
84 public void ServerHandshakeReturnWrongConnectionHeader ()
86 #pragma warning disable 4014
87 HandleHttpRequestAsync ((req, resp) => {
88 resp.StatusCode = 101;
89 resp.Headers["Upgrade"] = "websocket";
90 // Mono http request doesn't like the forcing, test still valid since the default connection header value is empty
91 //ForceSetHeader (resp.Headers, "Connection", "Foo");
93 #pragma warning restore 4014
95 Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
96 } catch (AggregateException e) {
97 AssertWebSocketException (e, WebSocketError.Success);
100 Assert.Fail ("Should have thrown");
104 [Ignore ("See bug #24340")]
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 [Ignore ("See bug #24340")]
130 public void CloseOutputAsyncTest ()
132 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
133 Assert.AreEqual (WebSocketState.Open, socket.State);
135 Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
136 Assert.AreEqual (WebSocketState.CloseSent, socket.State);
138 var resp = socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Result;
139 Assert.AreEqual (WebSocketState.Closed, socket.State);
140 Assert.AreEqual (WebSocketMessageType.Close, resp.MessageType);
141 Assert.AreEqual (WebSocketCloseStatus.NormalClosure, resp.CloseStatus);
142 Assert.AreEqual (string.Empty, resp.CloseStatusDescription);
146 public void CloseAsyncTest ()
148 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
149 Assert.AreEqual (WebSocketState.Open, socket.State);
151 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
152 Assert.AreEqual (WebSocketState.Closed, socket.State);
155 [Test, ExpectedException (typeof (InvalidOperationException))]
156 public void SendAsyncArgTest_NotConnected ()
158 socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None);
161 [Test, ExpectedException (typeof (ArgumentNullException))]
162 public void SendAsyncArgTest_NoArray ()
164 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
165 socket.SendAsync (new ArraySegment<byte> (), WebSocketMessageType.Text, true, CancellationToken.None);
168 [Test, ExpectedException (typeof (InvalidOperationException))]
169 public void ReceiveAsyncArgTest_NotConnected ()
171 socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None);
174 [Test, ExpectedException (typeof (ArgumentNullException))]
175 public void ReceiveAsyncArgTest_NoArray ()
177 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
178 socket.ReceiveAsync (new ArraySegment<byte> (), CancellationToken.None);
182 public void ReceiveAsyncWrongState_Closed ()
185 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
186 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
187 Assert.IsTrue (socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Wait (5000));
188 } catch (AggregateException e) {
189 AssertWebSocketException (e, WebSocketError.Success);
192 Assert.Fail ("Should have thrown");
196 public void SendAsyncWrongState_Closed ()
199 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
200 Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
201 Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
202 } catch (AggregateException e) {
203 AssertWebSocketException (e, WebSocketError.Success);
206 Assert.Fail ("Should have thrown");
210 public void SendAsyncWrongState_CloseSent ()
213 Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
214 Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
215 Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
216 } catch (AggregateException e) {
217 AssertWebSocketException (e, WebSocketError.Success);
220 Assert.Fail ("Should have thrown");
224 [Category ("NotWorking")] // FIXME: test relies on unimplemented HttpListenerContext.AcceptWebSocketAsync (), reenable it when the method is implemented
225 public void SendAsyncEndOfMessageTest ()
227 var cancellationToken = new CancellationTokenSource (TimeSpan.FromSeconds (30)).Token;
228 SendAsyncEndOfMessageTest (false, WebSocketMessageType.Text, cancellationToken).Wait (5000);
229 SendAsyncEndOfMessageTest (true, WebSocketMessageType.Text, cancellationToken).Wait (5000);
230 SendAsyncEndOfMessageTest (false, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
231 SendAsyncEndOfMessageTest (true, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
234 public async Task SendAsyncEndOfMessageTest (bool expectedEndOfMessage, WebSocketMessageType webSocketMessageType, CancellationToken cancellationToken)
236 using (var client = new ClientWebSocket ()) {
237 // Configure the listener.
238 var serverReceive = HandleHttpWebSocketRequestAsync<WebSocketReceiveResult> (async socket => await socket.ReceiveAsync (new ArraySegment<byte> (new byte[32]), cancellationToken), cancellationToken);
240 // Connect to the listener and make the request.
241 await client.ConnectAsync (new Uri ("ws://localhost:" + Port + "/"), cancellationToken);
242 await client.SendAsync (new ArraySegment<byte> (Encoding.UTF8.GetBytes ("test")), webSocketMessageType, expectedEndOfMessage, cancellationToken);
244 // Wait for the listener to handle the request and return its result.
245 var result = await serverReceive;
247 // Cleanup and check results.
248 await client.CloseAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
249 Assert.AreEqual (expectedEndOfMessage, result.EndOfMessage, "EndOfMessage should be " + expectedEndOfMessage);
253 async Task<T> HandleHttpWebSocketRequestAsync<T> (Func<WebSocket, Task<T>> action, CancellationToken cancellationToken)
255 var ctx = await this.listener.GetContextAsync ();
256 var wsContext = await ctx.AcceptWebSocketAsync (null);
257 var result = await action (wsContext.WebSocket);
258 await wsContext.WebSocket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
262 async Task HandleHttpRequestAsync (Action<HttpListenerRequest, HttpListenerResponse> handler)
264 var ctx = await listener.GetContextAsync ();
265 handler (ctx.Request, ctx.Response);
266 ctx.Response.Close ();
269 void AssertWebSocketException (AggregateException e, WebSocketError error, Type inner = null)
271 var wsEx = e.InnerException as WebSocketException;
272 Console.WriteLine (e.InnerException.ToString ());
273 Assert.IsNotNull (wsEx, "Not a websocketexception");
274 Assert.AreEqual (error, wsEx.WebSocketErrorCode);
276 Assert.IsNotNull (wsEx.InnerException);
277 Assert.IsTrue (inner.IsInstanceOfType (wsEx.InnerException));
281 void ForceSetHeader (WebHeaderCollection headers, string name, string value)
283 if (headerSetMethod == null)
284 headerSetMethod = typeof (WebHeaderCollection).GetMethod ("AddValue", BindingFlags.NonPublic);
285 headerSetMethod.Invoke (headers, new[] { name, value });