1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using MonoTests.Helpers;
namespace MonoTests.System.Net.WebSockets
{
[TestFixture]
public class ClientWebSocketTest
{
const string EchoServerUrl = "ws://corefx-net-http11.azurewebsites.net/WebSocket/EchoWebSocket.ashx";
ClientWebSocket socket;
MethodInfo headerSetMethod;
int Port;
[SetUp]
public void Setup ()
{
socket = new ClientWebSocket ();
}
HttpListener _listener;
HttpListener listener {
get {
if (_listener != null)
return _listener;
return NetworkHelpers.CreateAndStartHttpListener ("http://localhost:", out Port, "/");
}
}
[TearDown]
public void Teardown ()
{
if (_listener != null) {
_listener.Stop ();
_listener = null;
}
if (socket != null) {
if (socket.State == WebSocketState.Open)
socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (2000);
socket.Dispose ();
}
}
[Test]
[Category ("NotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ServerHandshakeReturnCrapStatusCodeTest ()
{
// On purpose,
#pragma warning disable 4014
HandleHttpRequestAsync ((req, resp) => resp.StatusCode = 418);
#pragma warning restore 4014
try {
Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.Success, typeof (WebException));
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ServerHandshakeReturnWrongUpgradeHeader ()
{
#pragma warning disable 4014
HandleHttpRequestAsync ((req, resp) => {
resp.StatusCode = 101;
resp.Headers["Upgrade"] = "gtfo";
});
#pragma warning restore 4014
try {
Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.Success);
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ServerHandshakeReturnWrongConnectionHeader ()
{
#pragma warning disable 4014
HandleHttpRequestAsync ((req, resp) => {
resp.StatusCode = 101;
resp.Headers["Upgrade"] = "websocket";
// Mono http request doesn't like the forcing, test still valid since the default connection header value is empty
//ForceSetHeader (resp.Headers, "Connection", "Foo");
});
#pragma warning restore 4014
try {
Assert.IsTrue (socket.ConnectAsync (new Uri ("ws://localhost:" + Port), CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.Success);
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("MobileNotWorking")] // The test hangs when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void EchoTest ()
{
const string Payload = "This is a websocket test";
Assert.AreEqual (WebSocketState.None, socket.State);
socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait ();
Assert.AreEqual (WebSocketState.Open, socket.State);
var sendBuffer = Encoding.ASCII.GetBytes (Payload);
Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
var receiveBuffer = new byte[Payload.Length];
var resp = socket.ReceiveAsync (new ArraySegment<byte> (receiveBuffer), CancellationToken.None).Result;
Assert.AreEqual (Payload.Length, resp.Count);
Assert.IsTrue (resp.EndOfMessage);
Assert.AreEqual (WebSocketMessageType.Text, resp.MessageType);
Assert.AreEqual (Payload, Encoding.ASCII.GetString (receiveBuffer, 0, resp.Count));
Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.AreEqual (WebSocketState.Closed, socket.State);
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void CloseOutputAsyncTest ()
{
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
Assert.AreEqual (WebSocketState.Open, socket.State);
Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.AreEqual (WebSocketState.CloseSent, socket.State);
var resp = socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Result;
Assert.AreEqual (WebSocketState.CloseReceived, socket.State);
Assert.AreEqual (WebSocketMessageType.Close, resp.MessageType);
Assert.AreEqual (WebSocketCloseStatus.NormalClosure, resp.CloseStatus);
Assert.AreEqual (string.Empty, resp.CloseStatusDescription);
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void CloseAsyncTest ()
{
if (!socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000)) {
Assert.Inconclusive (socket.State.ToString ());
return;
}
Assert.AreEqual (WebSocketState.Open, socket.State);
Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.AreEqual (WebSocketState.Closed, socket.State);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void SendAsyncArgTest_NotConnected ()
{
socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void SendAsyncArgTest_NoArray ()
{
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
socket.SendAsync (new ArraySegment<byte> (), WebSocketMessageType.Text, true, CancellationToken.None);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ReceiveAsyncArgTest_NotConnected ()
{
socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ReceiveAsyncArgTest_NoArray ()
{
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
socket.ReceiveAsync (new ArraySegment<byte> (), CancellationToken.None);
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void ReceiveAsyncWrongState_Closed ()
{
try {
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.ReceiveAsync (new ArraySegment<byte> (new byte[0]), CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.InvalidState);
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void SendAsyncWrongState_Closed ()
{
try {
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.InvalidState);
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("MobileNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
public void SendAsyncWrongState_CloseSent ()
{
try {
Assert.IsTrue (socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (new byte[0]), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));
} catch (AggregateException e) {
AssertWebSocketException (e, WebSocketError.InvalidState);
return;
}
Assert.Fail ("Should have thrown");
}
[Test]
[Category ("NotWorking")] // FIXME: test relies on unimplemented HttpListenerContext.AcceptWebSocketAsync (), reenable it when the method is implemented
public void SendAsyncEndOfMessageTest ()
{
var cancellationToken = new CancellationTokenSource (TimeSpan.FromSeconds (30)).Token;
SendAsyncEndOfMessageTest (false, WebSocketMessageType.Text, cancellationToken).Wait (5000);
SendAsyncEndOfMessageTest (true, WebSocketMessageType.Text, cancellationToken).Wait (5000);
SendAsyncEndOfMessageTest (false, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
SendAsyncEndOfMessageTest (true, WebSocketMessageType.Binary, cancellationToken).Wait (5000);
}
public async Task SendAsyncEndOfMessageTest (bool expectedEndOfMessage, WebSocketMessageType webSocketMessageType, CancellationToken cancellationToken)
{
using (var client = new ClientWebSocket ()) {
// Configure the listener.
var serverReceive = HandleHttpWebSocketRequestAsync<WebSocketReceiveResult> (async socket => await socket.ReceiveAsync (new ArraySegment<byte> (new byte[32]), cancellationToken), cancellationToken);
// Connect to the listener and make the request.
await client.ConnectAsync (new Uri ("ws://localhost:" + Port + "/"), cancellationToken);
await client.SendAsync (new ArraySegment<byte> (Encoding.UTF8.GetBytes ("test")), webSocketMessageType, expectedEndOfMessage, cancellationToken);
// Wait for the listener to handle the request and return its result.
var result = await serverReceive;
// Cleanup and check results.
await client.CloseAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
Assert.AreEqual (expectedEndOfMessage, result.EndOfMessage, "EndOfMessage should be " + expectedEndOfMessage);
}
}
async Task<T> HandleHttpWebSocketRequestAsync<T> (Func<WebSocket, Task<T>> action, CancellationToken cancellationToken)
{
var ctx = await this.listener.GetContextAsync ();
var wsContext = await ctx.AcceptWebSocketAsync (null);
var result = await action (wsContext.WebSocket);
await wsContext.WebSocket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, "Finished", cancellationToken);
return result;
}
async Task HandleHttpRequestAsync (Action<HttpListenerRequest, HttpListenerResponse> handler)
{
var ctx = await listener.GetContextAsync ();
handler (ctx.Request, ctx.Response);
ctx.Response.Close ();
}
void AssertWebSocketException (AggregateException e, WebSocketError error, Type inner = null)
{
var wsEx = e.InnerException as WebSocketException;
Assert.IsNotNull (wsEx, "Not a websocketexception");
Assert.AreEqual (error, wsEx.WebSocketErrorCode);
if (inner != null) {
Assert.IsNotNull (wsEx.InnerException);
Assert.IsTrue (inner.IsInstanceOfType (wsEx.InnerException));
}
}
void ForceSetHeader (WebHeaderCollection headers, string name, string value)
{
if (headerSetMethod == null)
headerSetMethod = typeof (WebHeaderCollection).GetMethod ("AddValue", BindingFlags.NonPublic);
headerSetMethod.Invoke (headers, new[] { name, value });
}
}
}
|