Include correct error code in WebSocket.ThrowOnInvalidState (dotnet/corefx#35960)
authorStephen Toub <stoub@microsoft.com>
Tue, 12 Mar 2019 15:05:04 +0000 (11:05 -0400)
committerGitHub <noreply@github.com>
Tue, 12 Mar 2019 15:05:04 +0000 (11:05 -0400)
* Include correct error code in WebSocket.ThrowOnInvalidState

* Address PR feedback

* Fix UWP leg

Commit migrated from https://github.com/dotnet/corefx/commit/ef145001b10660be940aeb9318e0ff7e306f65bc

src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs
src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.WinRT.cs
src/libraries/System.Net.WebSockets.Client/tests/AbortTest.cs
src/libraries/System.Net.WebSockets.Client/tests/CancelTest.cs
src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketTestBase.cs
src/libraries/System.Net.WebSockets.Client/tests/ConnectTest.cs
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocket.cs
src/libraries/System.Net.WebSockets/tests/WebSocketTests.cs

index bba1228..6f673ea 100644 (file)
@@ -174,7 +174,7 @@ namespace System.Net.WebSockets
 
                 if (response.StatusCode != HttpStatusCode.SwitchingProtocols)
                 {
-                    throw new WebSocketException(SR.Format(SR.net_WebSockets_Connect101Expected, (int) response.StatusCode));
+                    throw new WebSocketException(WebSocketError.NotAWebSocket, SR.Format(SR.net_WebSockets_Connect101Expected, (int) response.StatusCode));
                 }
 
                 // The Connection, Upgrade, and SecWebSocketAccept headers are required and with specific values.
@@ -227,7 +227,7 @@ namespace System.Net.WebSockets
                 {
                     throw;
                 }
-                throw new WebSocketException(SR.net_webstatus_ConnectFailure, exc);
+                throw new WebSocketException(WebSocketError.Faulted, SR.net_webstatus_ConnectFailure, exc);
             }
             finally
             {
@@ -280,10 +280,10 @@ namespace System.Net.WebSockets
             string[] array = (string[])values;
             if (array.Length != 1 || !string.Equals(array[0], expectedValue, StringComparison.OrdinalIgnoreCase))
             {
-                throw new WebSocketException(SR.Format(SR.net_WebSockets_InvalidResponseHeader, name, string.Join(", ", array)));
+                throw new WebSocketException(WebSocketError.HeaderError, SR.Format(SR.net_WebSockets_InvalidResponseHeader, name, string.Join(", ", array)));
             }
         }
 
-        private static void ThrowConnectFailure() => throw new WebSocketException(SR.net_webstatus_ConnectFailure);
+        private static void ThrowConnectFailure() => throw new WebSocketException(WebSocketError.Faulted, SR.net_webstatus_ConnectFailure);
     }
 }
index 38a9de0..7a711a8 100644 (file)
@@ -52,7 +52,7 @@ namespace System.Net.WebSockets
             {
                 WebErrorStatus status = RTWebSocketError.GetStatus(ex.HResult);
                 var inner = new Exception(status.ToString(), ex);
-                WebSocketException wex = new WebSocketException(SR.net_webstatus_ConnectFailure, inner);
+                WebSocketException wex = new WebSocketException(WebSocketError.Faulted, SR.net_webstatus_ConnectFailure, inner);
                 if (NetEventSource.IsEnabled) NetEventSource.Error(_webSocket, wex);
                 throw wex;
             }
index e68c61e..c5bf040 100644 (file)
@@ -33,7 +33,10 @@ namespace System.Net.WebSockets.Client.Tests
 
                 Assert.Equal(ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"), ex.Message);
 
-                Assert.Equal(WebSocketError.Success, ex.WebSocketErrorCode);
+                if (PlatformDetection.IsNetCore) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
+                {
+                    Assert.Equal(WebSocketError.Faulted, ex.WebSocketErrorCode);
+                }
                 Assert.Equal(WebSocketState.Closed, cws.State);
             }
         }
index 9bc3015..1e764d4 100644 (file)
@@ -30,7 +30,10 @@ namespace System.Net.WebSockets.Client.Tests
                 Assert.Equal(
                     ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"),
                     ex.Message);
-                Assert.Equal(WebSocketError.Success, ex.WebSocketErrorCode);
+                if (PlatformDetection.IsNetCore) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
+                {
+                    Assert.Equal(WebSocketError.Faulted, ex.WebSocketErrorCode);
+                }
                 Assert.Equal(WebSocketState.Closed, cws.State);
             }
         }
index c61dded..f0f2556 100644 (file)
@@ -47,7 +47,7 @@ namespace System.Net.WebSockets.Client.Tests
                     server = new Uri(string.Format("ws://{0}", Guid.NewGuid().ToString()));
                     exceptionMessage = ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure");
 
-                    yield return new object[] { server, exceptionMessage };
+                    yield return new object[] { server, exceptionMessage, WebSocketError.Faulted };
                 }
 
                 // Known server but not a real websocket endpoint.
@@ -56,7 +56,7 @@ namespace System.Net.WebSockets.Client.Tests
                     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, exceptionMessage };
+                    yield return new object[] { ub.Uri, exceptionMessage, WebSocketError.NotAWebSocket };
                 }
             }
         }
index dc9553e..a185735 100644 (file)
@@ -18,7 +18,7 @@ namespace System.Net.WebSockets.Client.Tests
 
         [OuterLoop] // TODO: Issue #11345
         [ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(UnavailableWebSocketServers))]
-        public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMessage(Uri server, string exceptionMessage)
+        public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMessage(Uri server, string exceptionMessage, WebSocketError errorCode)
         {
             using (var cws = new ClientWebSocket())
             {
@@ -26,7 +26,10 @@ namespace System.Net.WebSockets.Client.Tests
                 WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() =>
                     cws.ConnectAsync(server, cts.Token));
 
-                Assert.Equal(WebSocketError.Success, ex.WebSocketErrorCode);
+                if (PlatformDetection.IsNetCore && !PlatformDetection.IsUap) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
+                {
+                    Assert.Equal(errorCode, ex.WebSocketErrorCode);
+                }
                 Assert.Equal(WebSocketState.Closed, cws.State);
 
                 // .NET Framework and UAP implmentations have different exception message from .NET Core.
@@ -198,7 +201,10 @@ namespace System.Net.WebSockets.Client.Tests
                 WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() =>
                     cws.ConnectAsync(ub.Uri, cts.Token));
 
-                Assert.Equal(WebSocketError.Success, ex.WebSocketErrorCode);
+                if (PlatformDetection.IsNetCore) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
+                {
+                    Assert.Equal(WebSocketError.Faulted, ex.WebSocketErrorCode);
+                }
                 Assert.Equal(WebSocketState.Closed, cws.State);
                 Assert.Equal(ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"), ex.Message);
             }
index 1a2ae13..13c67c1 100644 (file)
@@ -100,7 +100,7 @@ namespace System.Net.WebSockets
                 validStatesText = string.Join(", ", validStates);
             }
 
-            throw new WebSocketException(SR.Format(SR.net_WebSockets_InvalidState, state, validStatesText));
+            throw new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState, state, validStatesText));
         }
 
         protected static bool IsStateTerminal(WebSocketState state) => 
index 0aad9a6..8279829 100644 (file)
@@ -129,7 +129,11 @@ namespace System.Net.WebSockets.Tests
         [InlineData(WebSocketState.Open, new WebSocketState[] { WebSocketState.Aborted, WebSocketState.CloseSent })]
         public static void ThrowOnInvalidState_ThrowsIfNotInValidList(WebSocketState state, WebSocketState[] validStates)
         {
-            Assert.Throws<WebSocketException>(() => ExposeProtectedWebSocket.ThrowOnInvalidState(state, validStates));
+            WebSocketException wse = Assert.Throws<WebSocketException>(() => ExposeProtectedWebSocket.ThrowOnInvalidState(state, validStates));
+            if (PlatformDetection.IsNetCore) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
+            {
+                Assert.Equal(WebSocketError.InvalidState, wse.WebSocketErrorCode);
+            }
         }
 
         [Theory]