Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Fix ClientWebSocket error message when endpoint is not a real websocket #29159

Merged
merged 3 commits into from
Apr 18, 2018

Conversation

caesar-chen
Copy link
Contributor

Close: #20360

@caesar-chen caesar-chen added this to the 2.2.0 milestone Apr 17, 2018
@caesar-chen caesar-chen self-assigned this Apr 17, 2018
@caesar-chen
Copy link
Contributor Author

@dotnet-bot test Outerloop Linux x64 Debug Build please
@dotnet-bot test Outerloop OSX x64 Debug Build please
@dotnet-bot test Outerloop Windows x64 Debug Build please

@dotnet-bot test Outerloop UWP CoreCLR x64 Release Build

@@ -29,7 +28,7 @@ public class ConnectTest : ClientWebSocketTestBase

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

@davidsh davidsh Apr 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible this test will fail in UAP since string resources from our CoreFx libraries are stripped out.

Instead of this Assert.Equal, there is a test helper "Assert" class that should be used here when wanting to test exception messages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See patterns like this in the code: ClientWebSocketUnitTest.cs:

            AssertExtensions.Throws<ObjectDisposedException>(
                () => cws.ReceiveAsync(segment, ct).GetAwaiter().GetResult(),
                expectedException.Message);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I vaguely remember that we need to use AssertExtensions for UAP run, because we don't want to validate the message, as it could be translated in future. I think this is the case.

Will fix.

Assert.Equal(ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"), ex.Message);

// The .Net Native toolchain optimizes away exception messages.
if (!PlatformDetection.IsNetNative)
Copy link
Contributor Author

@caesar-chen caesar-chen Apr 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to PlatformDetection instead of AssertExtension, because we also want to validate the ex.WebSocketErrorCode above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should still be able to use AssertExtension.Throws and save the exception it gives you back. And then check the error code. Using AssertExtension can remove you needing to check for "IsNetNative".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertExtension doesn't contain method overload to examine WebSocketException (or Exception in general) error message field and return the Exception. Which means, if we use other overload to save the Exception, we still need to use IsNetNative check in our test code to verify the ErrorMessage.

  1. It's possible to add a method overload inside AssertExtension class dedicated for WebSocketException, but the overhead is that we need to add System.Net.WebSockets reference inside the CoreFx.private.TestUtilities, and I'm not sure if that's desirable (Currently that project has no dependency on System.Net).
  2. It's not feasible to add a method overload for Exception in general that, do both check error message and return the exception. Because all possible distinct function signatures have been taken. Or we will need to add public API (new methods) for that class.

My preference is to use the current if check in our test code, if the desire for checking WebSocketException is high, we could consider add it to the common AssertExtension.

@davidsh Do you have any thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for looking into this. Let's just keep it the way you're doing it in this PR.

@caesar-chen
Copy link
Contributor Author

@dotnet-bot test Outerloop Linux x64 Debug Build please
@dotnet-bot test Outerloop OSX x64 Debug Build please
@dotnet-bot test Outerloop Windows x64 Debug Build please

@dotnet-bot test Outerloop UWP CoreCLR x64 Release Build

@davidsh
Copy link
Contributor

davidsh commented Apr 17, 2018

@dotnet-bot Test Outerloop UWP NETNative x86 Release Build

@dotnet dotnet deleted a comment from dotnet-bot Apr 17, 2018
@dotnet dotnet deleted a comment from dotnet-bot Apr 17, 2018
@@ -29,7 +28,12 @@ public class ConnectTest : ClientWebSocketTestBase

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

".Net" -> ".NET"

@caesar-chen
Copy link
Contributor Author

@dotnet-bot test Outerloop Linux x64 Debug Build please
@dotnet-bot test Outerloop OSX x64 Debug Build please
@dotnet-bot test Outerloop Windows x64 Debug Build please

@dotnet-bot test Outerloop UWP CoreCLR x64 Release Build
@dotnet-bot Test Outerloop UWP NETNative x86 Release Build

@caesar-chen caesar-chen merged commit 0c15e75 into dotnet:master Apr 18, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…et (dotnet/corefx#29159)

* fix clientwebsocket error message

* address feedback

* fix comment


Commit migrated from dotnet/corefx@0c15e75
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants