-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ConnectTest.cs
467 lines (407 loc) · 23.8 KB
/
ConnectTest.cs
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Test.Common;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerConnectTest : ConnectTest
{
public InvokerConnectTest(ITestOutputHelper output) : base(output) { }
protected override bool UseCustomInvoker => true;
public static IEnumerable<object[]> ConnectAsync_CustomInvokerWithIncompatibleWebSocketOptions_ThrowsArgumentException_MemberData()
{
yield return Throw(options => options.UseDefaultCredentials = true);
yield return NoThrow(options => options.UseDefaultCredentials = false);
yield return Throw(options => options.Credentials = new NetworkCredential());
yield return Throw(options => options.Proxy = new WebProxy());
// Will result in an exception on apple mobile platforms
// and crash the test.
if (PlatformDetection.IsNotAppleMobile)
{
yield return Throw(options => options.ClientCertificates.Add(Test.Common.Configuration.Certificates.GetClientCertificate()));
}
yield return NoThrow(options => options.ClientCertificates = new X509CertificateCollection());
yield return Throw(options => options.RemoteCertificateValidationCallback = delegate { return true; });
yield return Throw(options => options.Cookies = new CookieContainer());
// We allow no proxy or the default proxy to be used
yield return NoThrow(options => { });
yield return NoThrow(options => options.Proxy = null);
// These options don't conflict with the custom invoker
yield return NoThrow(options => options.HttpVersion = new Version(2, 0));
yield return NoThrow(options => options.HttpVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher);
yield return NoThrow(options => options.SetRequestHeader("foo", "bar"));
yield return NoThrow(options => options.AddSubProtocol("foo"));
yield return NoThrow(options => options.KeepAliveInterval = TimeSpan.FromSeconds(42));
yield return NoThrow(options => options.DangerousDeflateOptions = new WebSocketDeflateOptions());
yield return NoThrow(options => options.CollectHttpResponseDetails = true);
static object[] Throw(Action<ClientWebSocketOptions> configureOptions) =>
new object[] { configureOptions, true };
static object[] NoThrow(Action<ClientWebSocketOptions> configureOptions) =>
new object[] { configureOptions, false };
}
[Theory]
[MemberData(nameof(ConnectAsync_CustomInvokerWithIncompatibleWebSocketOptions_ThrowsArgumentException_MemberData))]
[SkipOnPlatform(TestPlatforms.Browser, "Custom invoker is ignored on Browser")]
public async Task ConnectAsync_CustomInvokerWithIncompatibleWebSocketOptions_ThrowsArgumentException(Action<ClientWebSocketOptions> configureOptions, bool shouldThrow)
{
using var invoker = new HttpMessageInvoker(new SocketsHttpHandler
{
ConnectCallback = (_, _) => ValueTask.FromException<Stream>(new Exception("ConnectCallback"))
});
using var ws = new ClientWebSocket();
configureOptions(ws.Options);
Task connectTask = ws.ConnectAsync(new Uri("wss://dummy"), invoker, CancellationToken.None);
if (shouldThrow)
{
Assert.Equal(TaskStatus.Faulted, connectTask.Status);
await Assert.ThrowsAsync<ArgumentException>("options", () => connectTask);
}
else
{
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() => connectTask);
Assert.NotNull(ex.InnerException);
Assert.Contains("ConnectCallback", ex.InnerException.Message);
}
foreach (X509Certificate cert in ws.Options.ClientCertificates)
{
cert.Dispose();
}
}
}
public sealed class HttpClientConnectTest : ConnectTest
{
public HttpClientConnectTest(ITestOutputHelper output) : base(output) { }
protected override bool UseHttpClient => true;
}
public class ConnectTest : ClientWebSocketTestBase
{
public ConnectTest(ITestOutputHelper output) : base(output) { }
[ActiveIssue("https://github.com/dotnet/runtime/issues/1895")]
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(UnavailableWebSocketServers))]
public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMessage(Uri server, string exceptionMessage, WebSocketError errorCode)
{
using (var cws = new ClientWebSocket())
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() =>
ConnectAsync(cws, server, cts.Token));
if (!PlatformDetection.IsInAppContainer) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
{
Assert.Equal(errorCode, ex.WebSocketErrorCode);
}
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(exceptionMessage, ex.Message);
// Other operations throw after failed connect
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.ReceiveAsync(new byte[1], default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.SendAsync(new byte[1], WebSocketMessageType.Binary, true, default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default));
}
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task EchoBinaryMessage_Success(Uri server)
{
await TestEcho(server, WebSocketMessageType.Binary, TimeOutMilliseconds, _output);
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task EchoTextMessage_Success(Uri server)
{
await TestEcho(server, WebSocketMessageType.Text, TimeOutMilliseconds, _output);
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoHeadersServers))]
[SkipOnPlatform(TestPlatforms.Browser, "SetRequestHeader not supported on browser")]
public async Task ConnectAsync_AddCustomHeaders_Success(Uri server)
{
using (var cws = new ClientWebSocket())
{
cws.Options.SetRequestHeader("X-CustomHeader1", "Value1");
cws.Options.SetRequestHeader("X-CustomHeader2", "Value2");
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
Task taskConnect = ConnectAsync(cws, server, cts.Token);
Assert.True(
(cws.State == WebSocketState.None) ||
(cws.State == WebSocketState.Connecting) ||
(cws.State == WebSocketState.Open),
"State immediately after ConnectAsync incorrect: " + cws.State);
await taskConnect;
}
Assert.Equal(WebSocketState.Open, cws.State);
byte[] buffer = new byte[65536];
WebSocketReceiveResult recvResult;
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
recvResult = await ReceiveEntireMessageAsync(cws, new ArraySegment<byte>(buffer), cts.Token);
}
Assert.Equal(WebSocketMessageType.Text, recvResult.MessageType);
string headers = WebSocketData.GetTextFromBuffer(new ArraySegment<byte>(buffer, 0, recvResult.Count));
Assert.Contains("X-CustomHeader1:Value1", headers);
Assert.Contains("X-CustomHeader2:Value2", headers);
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
}
[ConditionalFact(nameof(WebSocketsSupported))]
[SkipOnPlatform(TestPlatforms.Browser, "SetRequestHeader not supported on browser")]
public async Task ConnectAsync_AddHostHeader_Success()
{
string expectedHost = null;
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
expectedHost = "subdomain." + uri.Host;
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
cws.Options.SetRequestHeader("Host", expectedHost);
await ConnectAsync(cws, uri, cts.Token);
}
}, server => server.AcceptConnectionAsync(async connection =>
{
Dictionary<string, string> headers = await LoopbackHelper.WebSocketHandshakeAsync(connection);
Assert.NotNull(headers);
Assert.True(headers.TryGetValue("Host", out string host));
Assert.Equal(expectedHost, host);
}), new LoopbackServer.Options { WebSocketEndpoint = true });
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoHeadersServers))]
[SkipOnPlatform(TestPlatforms.Browser, "Cookies not supported on browser")]
public async Task ConnectAsync_CookieHeaders_Success(Uri server)
{
using (var cws = new ClientWebSocket())
{
Assert.Null(cws.Options.Cookies);
cws.Options.Cookies = new CookieContainer();
Cookie cookie1 = new Cookie("Cookies", "Are Yummy");
Cookie cookie2 = new Cookie("Especially", "Chocolate Chip");
Cookie secureCookie = new Cookie("Occasionally", "Raisin");
secureCookie.Secure = true;
cws.Options.Cookies.Add(server, cookie1);
cws.Options.Cookies.Add(server, cookie2);
cws.Options.Cookies.Add(server, secureCookie);
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
Task taskConnect = cws.ConnectAsync(server, cts.Token);
Assert.True(
cws.State == WebSocketState.None ||
cws.State == WebSocketState.Connecting ||
cws.State == WebSocketState.Open,
"State immediately after ConnectAsync incorrect: " + cws.State);
await taskConnect;
}
Assert.Equal(WebSocketState.Open, cws.State);
byte[] buffer = new byte[65536];
WebSocketReceiveResult recvResult;
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
recvResult = await ReceiveEntireMessageAsync(cws, new ArraySegment<byte>(buffer), cts.Token);
}
Assert.Equal(WebSocketMessageType.Text, recvResult.MessageType);
string headers = WebSocketData.GetTextFromBuffer(new ArraySegment<byte>(buffer, 0, recvResult.Count));
Assert.Contains("Cookies=Are Yummy", headers);
Assert.Contains("Especially=Chocolate Chip", headers);
Assert.Equal(server.Scheme == "wss", headers.Contains("Occasionally=Raisin"));
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/101115", typeof(PlatformDetection), nameof(PlatformDetection.IsFirefox))]
public async Task ConnectAsync_PassNoSubProtocol_ServerRequires_ThrowsWebSocketException(Uri server)
{
const string AcceptedProtocol = "CustomProtocol";
using (var cws = new ClientWebSocket())
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var ub = new UriBuilder(server);
ub.Query = "subprotocol=" + AcceptedProtocol;
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() =>
ConnectAsync(cws, ub.Uri, cts.Token));
_output.WriteLine(ex.Message);
Assert.True(ex.WebSocketErrorCode == WebSocketError.Faulted ||
ex.WebSocketErrorCode == WebSocketError.NotAWebSocket, $"Actual WebSocketErrorCode {ex.WebSocketErrorCode} {ex.InnerException?.Message} \n {ex}");
Assert.Equal(WebSocketState.Closed, cws.State);
}
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task ConnectAsync_PassMultipleSubProtocols_ServerRequires_ConnectionUsesAgreedSubProtocol(Uri server)
{
const string AcceptedProtocol = "AcceptedProtocol";
const string OtherProtocol = "OtherProtocol";
using (var cws = new ClientWebSocket())
{
cws.Options.AddSubProtocol(AcceptedProtocol);
cws.Options.AddSubProtocol(OtherProtocol);
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var ub = new UriBuilder(server);
ub.Query = "subprotocol=" + AcceptedProtocol;
await ConnectAsync(cws, ub.Uri, cts.Token);
Assert.Equal(WebSocketState.Open, cws.State);
Assert.Equal(AcceptedProtocol, cws.SubProtocol);
}
}
[ConditionalFact(nameof(WebSocketsSupported))]
[SkipOnPlatform(TestPlatforms.Browser, "SetRequestHeader not supported on Browser")]
public async Task ConnectAsync_NonStandardRequestHeaders_HeadersAddedWithoutValidation()
{
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using (var clientSocket = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
clientSocket.Options.SetRequestHeader("Authorization", "AWS4-HMAC-SHA256 Credential=PLACEHOLDER /20190301/us-east-2/neptune-db/aws4_request, SignedHeaders=host;x-amz-date, Signature=b8155de54d9faab00000000000000000000000000a07e0d7dda49902e4d9202");
await ConnectAsync(clientSocket, uri, cts.Token);
}
}, server => server.AcceptConnectionAsync(async connection =>
{
Assert.NotNull(await LoopbackHelper.WebSocketHandshakeAsync(connection));
}), new LoopbackServer.Options { WebSocketEndpoint = true });
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
[SkipOnPlatform(TestPlatforms.Browser, "Proxy not supported on Browser")]
public async Task ConnectAndCloseAsync_UseProxyServer_ExpectedClosedState(Uri server)
{
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
using (LoopbackProxyServer proxyServer = LoopbackProxyServer.Create())
{
ConfigureCustomHandler = handler => handler.Proxy = new WebProxy(proxyServer.Uri);
if (UseSharedHandler)
{
cws.Options.Proxy = new WebProxy(proxyServer.Uri);
}
await ConnectAsync(cws, server, cts.Token);
string expectedCloseStatusDescription = "Client close status";
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, expectedCloseStatusDescription, cts.Token);
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(WebSocketCloseStatus.NormalClosure, cws.CloseStatus);
Assert.Equal(expectedCloseStatusDescription, cws.CloseStatusDescription);
Assert.Equal(1, proxyServer.Connections);
}
}
[ConditionalFact(nameof(WebSocketsSupported))]
public async Task ConnectAsync_CancellationRequestedBeforeConnect_ThrowsOperationCanceledException()
{
using (var clientSocket = new ClientWebSocket())
{
var cts = new CancellationTokenSource();
cts.Cancel();
Task t = ConnectAsync(clientSocket, new Uri($"ws://{Guid.NewGuid():N}"), cts.Token);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
}
}
[ConditionalFact(nameof(WebSocketsSupported))]
public async Task ConnectAsync_CancellationRequestedInflightConnect_ThrowsOperationCanceledException()
{
using (var clientSocket = new ClientWebSocket())
{
var cts = new CancellationTokenSource();
Task t = ConnectAsync(clientSocket, new Uri($"ws://{Guid.NewGuid():N}"), cts.Token);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
}
}
[ConditionalFact(nameof(WebSocketsSupported))]
public async Task ConnectAsync_CancellationRequestedAfterConnect_ThrowsOperationCanceledException()
{
var releaseServer = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
var clientSocket = new ClientWebSocket();
try
{
var cts = new CancellationTokenSource();
Task t = ConnectAsync(clientSocket, uri, cts.Token);
Assert.False(t.IsCompleted);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
}
finally
{
releaseServer.SetResult();
clientSocket.Dispose();
}
}, async server =>
{
try
{
await server.AcceptConnectionAsync(async connection =>
{
await releaseServer.Task;
});
}
// Ignore IO exception on server as there are race conditions when client is cancelling.
catch (IOException) { }
}, new LoopbackServer.Options { WebSocketEndpoint = true });
}
[ConditionalFact(nameof(WebSocketsSupported))]
[SkipOnPlatform(TestPlatforms.Browser, "CollectHttpResponseDetails not supported on Browser")]
public async Task ConnectAsync_HttpResponseDetailsCollectedOnFailure()
{
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using (var clientWebSocket = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
clientWebSocket.Options.CollectHttpResponseDetails = true;
Task t = ConnectAsync(clientWebSocket, uri, cts.Token);
await Assert.ThrowsAnyAsync<WebSocketException>(() => t);
Assert.Equal(HttpStatusCode.Unauthorized, clientWebSocket.HttpStatusCode);
Assert.NotEmpty(clientWebSocket.HttpResponseHeaders);
}
}, server => server.AcceptConnectionSendResponseAndCloseAsync(HttpStatusCode.Unauthorized), new LoopbackServer.Options { WebSocketEndpoint = true });
}
[ConditionalFact(nameof(WebSocketsSupported))]
[SkipOnPlatform(TestPlatforms.Browser, "CollectHttpResponseDetails not supported on Browser")]
public async Task ConnectAsync_HttpResponseDetailsCollectedOnFailure_CustomHeader()
{
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using (var clientWebSocket = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
clientWebSocket.Options.CollectHttpResponseDetails = true;
Task t = ConnectAsync(clientWebSocket, uri, cts.Token);
await Assert.ThrowsAnyAsync<WebSocketException>(() => t);
Assert.Equal(HttpStatusCode.Unauthorized, clientWebSocket.HttpStatusCode);
Assert.NotEmpty(clientWebSocket.HttpResponseHeaders);
Assert.Contains("X-CustomHeader1", clientWebSocket.HttpResponseHeaders);
Assert.Contains("X-CustomHeader2", clientWebSocket.HttpResponseHeaders);
Assert.NotNull(clientWebSocket.HttpResponseHeaders.Values);
}
}, server => server.AcceptConnectionSendResponseAndCloseAsync(HttpStatusCode.Unauthorized, "X-CustomHeader1: Value1\r\nX-CustomHeader2: Value2\r\n"), new LoopbackServer.Options { WebSocketEndpoint = true });
}
[ConditionalFact(nameof(WebSocketsSupported))]
[SkipOnPlatform(TestPlatforms.Browser, "CollectHttpResponseDetails not supported on Browser")]
public async Task ConnectAsync_HttpResponseDetailsCollectedOnSuccess_Extensions()
{
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using (var clientWebSocket = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
clientWebSocket.Options.CollectHttpResponseDetails = true;
await ConnectAsync(clientWebSocket, uri, cts.Token);
Assert.Equal(HttpStatusCode.SwitchingProtocols, clientWebSocket.HttpStatusCode);
Assert.NotEmpty(clientWebSocket.HttpResponseHeaders);
Assert.Contains("Sec-WebSocket-Extensions", clientWebSocket.HttpResponseHeaders);
}
}, server => server.AcceptConnectionAsync(async connection =>
{
Dictionary<string, string> headers = await LoopbackHelper.WebSocketHandshakeAsync(connection, "X-CustomHeader1");
}), new LoopbackServer.Options { WebSocketEndpoint = true });
}
}
}