-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathConnectTest.cs
More file actions
369 lines (305 loc) · 17.1 KB
/
ConnectTest.cs
File metadata and controls
369 lines (305 loc) · 17.1 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net.Http;
using System.Net.Test.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
using Xunit.Abstractions;
using EchoQueryKey = System.Net.Test.Common.WebSocketEchoOptions.EchoQueryKey;
namespace System.Net.WebSockets.Client.Tests
{
//
// Class hierarchy:
//
// - ConnectTestBase → file:ConnectTest.cs
// ├─ ConnectTest_External
// │ ├─ [*]ConnectTest_SharedHandler_External
// │ ├─ [*]ConnectTest_Invoker_External
// │ └─ [*]ConnectTest_HttpClient_External
// └─ ConnectTest_LoopbackBase → file:ConnectTest.Loopback.cs
// ├─ ConnectTest_Loopback
// │ ├─ [*]ConnectTest_SharedHandler_Loopback → file:ConnectTest.Loopback.cs, ConnectTest.SharedHandler.cs
// │ ├─ [*]ConnectTest_Invoker_Loopback → file:ConnectTest.Loopback.cs, ConnectTest.Invoker.cs
// │ └─ [*]ConnectTest_HttpClient_Loopback
// └─ ConnectTest_Http2Loopback → file:ConnectTest.Loopback.cs, ConnectTest.Http2.cs
// ├─ [*]ConnectTest_Invoker_Http2Loopback
// └─ [*]ConnectTest_HttpClient_Http2Loopback
//
// ---
// `[*]` - concrete runnable test classes
// `→ file:` - file containing the class and its concrete subclasses
public abstract class ConnectTestBase(ITestOutputHelper output) : ClientWebSocketTestBase(output)
{
#region Common (Echo Server) tests
protected Task RunClient_EchoBinaryMessage_Success(Uri server)
=> RunClientAsync(server,
(cws, ct) => WebSocketHelper.TestEcho(cws, WebSocketMessageType.Binary, ct));
protected Task RunClient_EchoTextMessage_Success(Uri server)
=> RunClientAsync(server,
(cws, ct) => WebSocketHelper.TestEcho(cws, WebSocketMessageType.Text, ct));
protected async Task RunClient_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 = new ArraySegment<byte>(buffer, 0, recvResult.Count).Utf8ToString();
Assert.Contains("X-CustomHeader1:Value1", headers, StringComparison.OrdinalIgnoreCase);
Assert.Contains("X-CustomHeader2:Value2", headers, StringComparison.OrdinalIgnoreCase);
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
}
protected async Task RunClient_ConnectAsync_CookieHeaders_Success(Uri server)
{
using (var cws = new ClientWebSocket())
{
Assert.Null(cws.Options.Cookies);
var cookies = new CookieContainer();
Cookie cookie1 = new Cookie("Cookies", "Are Yummy");
Cookie cookie2 = new Cookie("Especially", "Chocolate Chip");
Cookie secureCookie = new Cookie("Occasionally", "Raisin") { Secure = true };
cookies.Add(server, cookie1);
cookies.Add(server, cookie2);
cookies.Add(server, secureCookie);
if (UseSharedHandler)
{
cws.Options.Cookies = cookies;
}
else
{
ConfigureCustomHandler = handler => handler.CookieContainer = cookies;
}
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 = new ArraySegment<byte>(buffer, 0, recvResult.Count).Utf8ToString();
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);
}
}
protected async Task RunClient_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) { Query = $"{EchoQueryKey.SubProtocol}={AcceptedProtocol}" };
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() =>
ConnectAsync(cws, ub.Uri, cts.Token));
_output.WriteLine(ex.Message);
Assert.True(ex.WebSocketErrorCode == WebSocketError.UnsupportedProtocol ||
ex.WebSocketErrorCode == WebSocketError.Faulted ||
ex.WebSocketErrorCode == WebSocketError.NotAWebSocket, $"Actual WebSocketErrorCode {ex.WebSocketErrorCode} {ex.InnerException?.Message} \n {ex}");
Assert.Equal(WebSocketState.Closed, cws.State);
}
}
protected async Task RunClient_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) { Query = $"{EchoQueryKey.SubProtocol}={AcceptedProtocol}" };
await ConnectAsync(cws, ub.Uri, cts.Token);
Assert.Equal(WebSocketState.Open, cws.State);
Assert.Equal(AcceptedProtocol, cws.SubProtocol);
}
}
protected async Task RunClient_ConnectAndCloseAsync_UseProxyServer_ExpectedClosedState(Uri server)
{
if (HttpVersion != Net.HttpVersion.Version11)
{
throw new SkipTestException("LoopbackProxyServer is HTTP/1.1 only");
}
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
using (LoopbackProxyServer proxyServer = LoopbackProxyServer.Create())
{
if (UseSharedHandler)
{
cws.Options.Proxy = new WebProxy(proxyServer.Uri);
}
else
{
ConfigureCustomHandler = handler => handler.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);
}
}
#endregion
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalClass(typeof(ClientWebSocketTestBase), nameof(WebSocketsSupported))]
public abstract class ConnectTest_External(ITestOutputHelper output) : ConnectTestBase(output)
{
#region Common (Echo Server) tests
[Theory, MemberData(nameof(EchoServers))]
public Task EchoBinaryMessage_Success(Uri server)
=> RunClient_EchoBinaryMessage_Success(server);
[Theory, MemberData(nameof(EchoServers))]
public Task EchoTextMessage_Success(Uri server)
=> RunClient_EchoTextMessage_Success(server);
[SkipOnPlatform(TestPlatforms.Browser, "SetRequestHeader not supported on browser")]
[Theory, MemberData(nameof(EchoHeadersServers))]
public Task ConnectAsync_AddCustomHeaders_Success(Uri server)
=> RunClient_ConnectAsync_AddCustomHeaders_Success(server);
[SkipOnPlatform(TestPlatforms.Browser, "Cookies not supported on browser")]
[Theory, MemberData(nameof(EchoHeadersServers))]
public Task ConnectAsync_CookieHeaders_Success(Uri server)
=> RunClient_ConnectAsync_CookieHeaders_Success(server);
[ActiveIssue("https://github.com/dotnet/runtime/issues/101115", typeof(PlatformDetection), nameof(PlatformDetection.IsFirefox))]
[Theory, MemberData(nameof(EchoServers))]
public Task ConnectAsync_PassNoSubProtocol_ServerRequires_ThrowsWebSocketException(Uri server)
=> RunClient_ConnectAsync_PassNoSubProtocol_ServerRequires_ThrowsWebSocketException(server);
[Theory, MemberData(nameof(EchoServers))]
public Task ConnectAsync_PassMultipleSubProtocols_ServerRequires_ConnectionUsesAgreedSubProtocol(Uri server)
=> RunClient_ConnectAsync_PassMultipleSubProtocols_ServerRequires_ConnectionUsesAgreedSubProtocol(server);
[SkipOnPlatform(TestPlatforms.Browser, "Proxy not supported on Browser")]
[Theory, MemberData(nameof(EchoServers))]
public Task ConnectAndCloseAsync_UseProxyServer_ExpectedClosedState(Uri server)
=> RunClient_ConnectAndCloseAsync_UseProxyServer_ExpectedClosedState(server);
#endregion
#region External-only tests
[ActiveIssue("https://github.com/dotnet/runtime/issues/1895")]
[Theory]
[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));
}
}
[SkipOnPlatform(TestPlatforms.Browser, "HTTP/2 WebSockets are not supported on this platform")]
[ConditionalFact] // Uses SkipTestException
public async Task ConnectAsync_Http11Server_DowngradeFail()
{
if (UseSharedHandler)
{
throw new SkipTestException("HTTP/2 is not supported with SharedHandler");
}
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
cws.Options.HttpVersion = Net.HttpVersion.Version20;
cws.Options.HttpVersionPolicy = HttpVersionPolicy.RequestVersionExact;
Task t = cws.ConnectAsync(Test.Common.Configuration.WebSockets.SecureRemoteEchoServer, GetInvoker(), cts.Token);
var ex = await Assert.ThrowsAnyAsync<WebSocketException>(() => t);
Assert.True(ex.InnerException.Data.Contains("HTTP2_ENABLED"));
HttpRequestException inner = Assert.IsType<HttpRequestException>(ex.InnerException);
HttpRequestError expectedError = PlatformDetection.SupportsAlpn ?
HttpRequestError.SecureConnectionError :
HttpRequestError.VersionNegotiationError;
Assert.Equal(expectedError, inner.HttpRequestError);
Assert.Equal(WebSocketState.Closed, cws.State);
}
}
[SkipOnPlatform(TestPlatforms.Browser, "HTTP/2 WebSockets are not supported on this platform")]
[ConditionalTheory] // Uses SkipTestException
[MemberData(nameof(EchoServers))]
public async Task ConnectAsync_Http11Server_DowngradeSuccess(Uri server)
{
if (UseSharedHandler)
{
throw new SkipTestException("HTTP/2 is not supported with SharedHandler");
}
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
cws.Options.HttpVersion = Net.HttpVersion.Version20;
cws.Options.HttpVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
await cws.ConnectAsync(server, GetInvoker(), cts.Token);
Assert.Equal(WebSocketState.Open, cws.State);
}
}
[SkipOnPlatform(TestPlatforms.Browser, "HTTP/2 WebSockets are not supported on this platform")]
[ConditionalTheory] // Uses SkipTestException
[MemberData(nameof(EchoServers))]
public async Task ConnectAsync_Http11WithRequestVersionOrHigher_DowngradeSuccess(Uri server)
{
if (UseSharedHandler)
{
throw new SkipTestException("HTTP/2 is not supported with SharedHandler");
}
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
cws.Options.HttpVersion = Net.HttpVersion.Version11;
cws.Options.HttpVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
await cws.ConnectAsync(server, GetInvoker(), cts.Token);
Assert.Equal(WebSocketState.Open, cws.State);
}
}
#endregion
}
#region Runnable test classes: External/Outerloop
public sealed class ConnectTest_SharedHandler_External(ITestOutputHelper output) : ConnectTest_External(output) { }
public sealed class ConnectTest_Invoker_External(ITestOutputHelper output) : ConnectTest_External(output)
{
protected override bool UseCustomInvoker => true;
}
public sealed class ConnectTest_HttpClient_External(ITestOutputHelper output) : ConnectTest_External(output)
{
protected override bool UseHttpClient => true;
}
#endregion
}