Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up HttpClient for ClientWebSocket.ConnectAsync #73387

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public async Task ConnectAsync(Uri uri, HttpMessageInvoker? invoker, Cancellatio

using (linkedCancellation)
{
response = await invoker.SendAsync(request, externalAndAbortCancellation.Token).ConfigureAwait(false);
Task<HttpResponseMessage> sendTask = invoker is HttpClient client
? client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, externalAndAbortCancellation.Token)
: invoker.SendAsync(request, externalAndAbortCancellation.Token);
response = await sendTask.ConfigureAwait(false);
externalAndAbortCancellation.Token.ThrowIfCancellationRequested(); // poll in case sends/receives in request/response didn't observe cancellation
}

Expand Down
18 changes: 17 additions & 1 deletion src/libraries/System.Net.WebSockets.Client/tests/AbortTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Net.Http;
using System.Net.Test.Common;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,10 +12,25 @@

namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerAbortTest : AbortTest
{
public InvokerAbortTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpMessageInvoker(new SocketsHttpHandler());
}

public sealed class HttpClientAbortTest : AbortTest
{
public HttpClientAbortTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpClient(new HttpClientHandler());
}

public class AbortTest : ClientWebSocketTestBase
{
public AbortTest(ITestOutputHelper output) : base(output) { }


[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task Abort_ConnectAndAbort_ThrowsWebSocketExceptionWithmessage(Uri server)
Expand All @@ -26,7 +42,7 @@ public async Task Abort_ConnectAndAbort_ThrowsWebSocketExceptionWithmessage(Uri
var ub = new UriBuilder(server);
ub.Query = "delay10sec";

Task t = cws.ConnectAsync(ub.Uri, cts.Token);
Task t = ConnectAsync(cws, ub.Uri, cts.Token);
cws.Abort();
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() => t);

Expand Down
23 changes: 19 additions & 4 deletions src/libraries/System.Net.WebSockets.Client/tests/CancelTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Threading;
using System.Threading.Tasks;

Expand All @@ -9,6 +10,20 @@

namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerCancelTest : CancelTest
{
public InvokerCancelTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpMessageInvoker(new SocketsHttpHandler());
}

public sealed class HttpClientCancelTest : CancelTest
{
public HttpClientCancelTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpClient(new HttpClientHandler());
}

public class CancelTest : ClientWebSocketTestBase
{
public CancelTest(ITestOutputHelper output) : base(output) { }
Expand All @@ -24,7 +39,7 @@ public async Task ConnectAsync_Cancel_ThrowsCancellationException(Uri server)
var ub = new UriBuilder(server);
ub.Query = PlatformDetection.IsBrowser ? "delay20sec" : "delay10sec";

var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => cws.ConnectAsync(ub.Uri, cts.Token));
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => ConnectAsync(cws, ub.Uri, cts.Token));
Assert.True(WebSocketState.Closed == cws.State, $"Actual {cws.State} when {ex}");
}
}
Expand Down Expand Up @@ -116,7 +131,7 @@ await cws.SendAsync(
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task ReceiveAsync_CancelThenReceive_ThrowsOperationCanceledException(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Expand All @@ -132,7 +147,7 @@ public async Task ReceiveAsync_CancelThenReceive_ThrowsOperationCanceledExceptio
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task ReceiveAsync_ReceiveThenCancel_ThrowsOperationCanceledException(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Expand All @@ -148,7 +163,7 @@ public async Task ReceiveAsync_ReceiveThenCancel_ThrowsOperationCanceledExceptio
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task ReceiveAsync_AfterCancellationDoReceiveAsync_ThrowsWebSocketException(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

using Xunit;
using Xunit.Abstractions;
using System.Net.Http;
using System.Net.WebSockets.Client.Tests;

namespace System.Net.WebSockets.Client.Tests
{
Expand Down Expand Up @@ -105,6 +107,17 @@ protected static async Task<WebSocketReceiveResult> ReceiveEntireMessageAsync(We
}
}

protected virtual HttpMessageInvoker? GetInvoker() => null;

protected Task<ClientWebSocket> GetConnectedWebSocket(Uri uri, int TimeOutMilliseconds, ITestOutputHelper output) =>
WebSocketHelper.GetConnectedWebSocket(uri, TimeOutMilliseconds, output, invoker: GetInvoker());

protected Task ConnectAsync(ClientWebSocket cws, Uri uri, CancellationToken cancellationToken) =>
cws.ConnectAsync(uri, GetInvoker(), cancellationToken);

protected Task TestEcho(Uri uri, WebSocketMessageType type, int timeOutMilliseconds, ITestOutputHelper output) =>
WebSocketHelper.TestEcho(uri, WebSocketMessageType.Text, TimeOutMilliseconds, _output, GetInvoker());

public static bool WebSocketsSupported { get { return WebSocketHelper.WebSocketsSupported; } }
}
}
44 changes: 30 additions & 14 deletions src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
Expand All @@ -13,18 +14,33 @@

namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerCloseTest : CloseTest
{
public InvokerCloseTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpMessageInvoker(new SocketsHttpHandler());
}

public sealed class HttpClientCloseTest : CloseTest
{
public HttpClientCloseTest(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpClient(new HttpClientHandler());
}

public class CloseTest : ClientWebSocketTestBase
{
public CloseTest(ITestOutputHelper output) : base(output) { }


[ActiveIssue("https://github.com/dotnet/runtime/issues/28957")]
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServersAndBoolean))]
public async Task CloseAsync_ServerInitiatedClose_Success(Uri server, bool useCloseOutputAsync)
{
const string closeWebSocketMetaCommand = ".close";

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand Down Expand Up @@ -70,7 +86,7 @@ await cws.SendAsync(
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_ClientInitiatedClose_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
Assert.Equal(WebSocketState.Open, cws.State);
Expand All @@ -94,7 +110,7 @@ public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server)
{
string closeDescription = new string('C', CloseDescriptionMaxLength);

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -108,7 +124,7 @@ public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentEx
{
string closeDescription = new string('C', CloseDescriptionMaxLength + 1);

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -131,7 +147,7 @@ public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentEx
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionHasUnicode_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -150,7 +166,7 @@ public async Task CloseAsync_CloseDescriptionHasUnicode_Success(Uri server)
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionIsNull_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -166,7 +182,7 @@ public async Task CloseAsync_CloseDescriptionIsNull_Success(Uri server)
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ExpectedStates(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -185,7 +201,7 @@ public async Task CloseOutputAsync_ExpectedStates(Uri server)
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseOutputAsync_Throws(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -212,7 +228,7 @@ public async Task CloseOutputAsync_ClientInitiated_CanReceive_CanClose(Uri serve
{
string message = "Hello WebSockets!";

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand Down Expand Up @@ -254,7 +270,7 @@ public async Task CloseOutputAsync_ServerInitiated_CanSend(Uri server)
var expectedCloseStatus = WebSocketCloseStatus.NormalClosure;
var expectedCloseDescription = ".shutdown";

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand Down Expand Up @@ -297,7 +313,7 @@ await cws.SendAsync(
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_CloseDescriptionIsNull_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);

Expand All @@ -314,7 +330,7 @@ public async Task CloseOutputAsync_CloseDescriptionIsNull_Success(Uri server)
public async Task CloseOutputAsync_DuringConcurrentReceiveAsync_ExpectedStates(Uri server)
{
var receiveBuffer = new byte[1024];
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
// Issue a receive but don't wait for it.
var t = cws.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
Expand Down Expand Up @@ -351,7 +367,7 @@ public async Task CloseOutputAsync_DuringConcurrentReceiveAsync_ExpectedStates(U
public async Task CloseAsync_DuringConcurrentReceiveAsync_ExpectedStates(Uri server)
{
var receiveBuffer = new byte[1024];
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var t = cws.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
Assert.False(t.IsCompleted);
Expand Down Expand Up @@ -387,7 +403,7 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
using (var cws = new ClientWebSocket())
using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
{
await cws.ConnectAsync(uri, cts.Token);
await ConnectAsync(cws, uri, cts.Token);

Task receiveTask = cws.ReceiveAsync(new byte[1], CancellationToken.None);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@

namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerConnectTest_Http2 : ConnectTest_Http2
{
public InvokerConnectTest_Http2(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpClient(new HttpClientHandler());
}

public sealed class HttpClientConnectTest_Http2 : ConnectTest_Http2
{
public HttpClientConnectTest_Http2(ITestOutputHelper output) : base(output) { }

protected override HttpMessageInvoker? GetInvoker() => new HttpClient(new HttpClientHandler());
}

public class ConnectTest_Http2 : ClientWebSocketTestBase
{
public ConnectTest_Http2(ITestOutputHelper output) : base(output) { }


[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down
Loading