Skip to content

Commit

Permalink
Fix ClientWebSocket error message when endpoint is not a real websock…
Browse files Browse the repository at this point in the history
…et (dotnet/corefx#29159)

* fix clientwebsocket error message

* address feedback

* fix comment


Commit migrated from dotnet/corefx@0c15e75
  • Loading branch information
Caesar1995 authored Apr 18, 2018
1 parent c3a6cba commit dc7afff
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<data name="net_WebSockets_ArgumentOutOfRange_TooSmall" xml:space="preserve">
<value>The argument must be a value greater than {0}.</value>
</data>
<data name="net_WebSockets_Connect101Expected" xml:space="preserve">
<value>The server returned status code '{0}' when status code '101' was expected.</value>
</data>
<data name="net_WebSockets_InvalidState_ClosedOrAborted" xml:space="preserve">
<value>The '{0}' instance cannot be used for communication because it has been transitioned into the '{1}' state.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken,

if (response.StatusCode != HttpStatusCode.SwitchingProtocols)
{
throw new WebSocketException(SR.net_webstatus_ConnectFailure);
throw new WebSocketException(SR.Format(SR.net_WebSockets_Connect101Expected, (int) response.StatusCode));
}

// The Connection, Upgrade, and SecWebSocketAccept headers are required and with specific values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ public static IEnumerable<object[]> UnavailableWebSocketServers
get
{
Uri server;
string exceptionMessage;

// Unknown server.
{
server = new Uri(string.Format("ws://{0}", Guid.NewGuid().ToString()));
yield return new object[] { server };
exceptionMessage = ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure");

yield return new object[] { server, exceptionMessage };
}

// Known server but not a real websocket endpoint.
{
server = System.Net.Test.Common.Configuration.Http.RemoteEchoServer;
var ub = new UriBuilder("ws", server.Host, server.Port, server.PathAndQuery);
exceptionMessage = ResourceHelper.GetExceptionMessage("net_WebSockets_Connect101Expected", (int) HttpStatusCode.OK);

yield return new object[] { ub.Uri };
yield return new object[] { ub.Uri, exceptionMessage };
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/libraries/System.Net.WebSockets.Client/tests/ConnectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public class ConnectTest : ClientWebSocketTestBase
{
public ConnectTest(ITestOutputHelper output) : base(output) { }

[ActiveIssue(20360, TargetFrameworkMonikers.NetFramework)]
[OuterLoop] // TODO: Issue #11345
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(UnavailableWebSocketServers))]
public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMessage(Uri server)
public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMessage(Uri server, string exceptionMessage)
{
using (var cws = new ClientWebSocket())
{
Expand All @@ -29,7 +28,12 @@ public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMe

Assert.Equal(WebSocketError.Success, ex.WebSocketErrorCode);
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"), ex.Message);

// The .NET Native toolchain optimizes away exception messages.
if (!PlatformDetection.IsNetNative)
{
Assert.Equal(exceptionMessage, ex.Message);
}
}
}

Expand Down

0 comments on commit dc7afff

Please sign in to comment.