Skip to content

ManagedWebSocket: ThrowIfInvalidMessageType: ArgumentException should report actual invalid input, not assume all invalids are 'Close' #129066

@mattflaschen

Description

@mattflaschen

Description

In ManagedWebSocket, ThrowIfInvalidMessageType enforces that only Text and Binary are passed.

The issue is that the ArgumentException (resource string) reports that any invalid message type was "Close".

It should report the actual invalid value that was passed, which could be Close or any other WebSocketMessageType. This could be an arbitrary invalid value, e.g. 999.

It is true that the only 3 valid types are currently Text, Binary, and Close.

I don't believe it should be hard-coded to Close, because:

  1. It is possible that WebSocket will add new message types in the future. This is a tricky place to have to update.
  2. It is possible to pass an invalid value (e.g. 999) by parsing or casting. I did this intentionally to test my code on invalid inputs, but it is conceivable it could happen accidentally.

Reproduction Steps

await _webSocket.SendAsync(
    buffer: buffer,
    messageType: (WebSocketMessageType) 999,
    endOfMessage: true,
    cancellationToken: cancellationToken
);

Expected behavior

System.ArgumentException: The message type '999' is not allowed for the 'SendAsync' operation. Valid message types are: 'Binary, Text'. To close the WebSocket, use the 'CloseOutputAsync' operation instead.  (Parameter 'messageType')

Actual behavior

System.ArgumentException: The message type 'Close' is not allowed for the 'SendAsync' operation. Valid message types are: 'Binary, Text'. To close the WebSocket, use the 'CloseOutputAsync' operation instead.  (Parameter 'messageType')

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.NetuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions