RichardSzalay.MockHttp.WebSockets 0.1.0-pre1

This is a prerelease version of RichardSzalay.MockHttp.WebSockets.
dotnet add package RichardSzalay.MockHttp.WebSockets --version 0.1.0-pre1
                    
NuGet\Install-Package RichardSzalay.MockHttp.WebSockets -Version 0.1.0-pre1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RichardSzalay.MockHttp.WebSockets" Version="0.1.0-pre1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RichardSzalay.MockHttp.WebSockets" Version="0.1.0-pre1" />
                    
Directory.Packages.props
<PackageReference Include="RichardSzalay.MockHttp.WebSockets" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RichardSzalay.MockHttp.WebSockets --version 0.1.0-pre1
                    
#r "nuget: RichardSzalay.MockHttp.WebSockets, 0.1.0-pre1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=RichardSzalay.MockHttp.WebSockets&version=0.1.0-pre1&prerelease
                    
Install RichardSzalay.MockHttp.WebSockets as a Cake Addin
#tool nuget:?package=RichardSzalay.MockHttp.WebSockets&version=0.1.0-pre1&prerelease
                    
Install RichardSzalay.MockHttp.WebSockets as a Cake Tool

Write blazing fast tests for ClientWebSocket with no abstractions. Real WebSockets, no network.

This repository is in early development phase. There are no official releases yet.

Raw WebSocket Usage

var cancellationToken = CancellationToken.None;

using var mockServer = MockWebSocketServer.ForEndpoint(async (ws, ct) =>
{
    // This is the 'server'. When it exits, the Websocket will shutdown.
    
    Memory<byte> buffer = new(new byte[4]);

    while (!ws.CloseStatus.HasValue)
    {
        var result = await ws.ReceiveAsync(buffer, ct);

        if (result.MessageType == WebSocketMessageType.Close)
        {
            continue;
        }

        var messageValue = int.Parse(buffer.Span.Slice(0, result.Count));
        
        var sendBytes = Encoding.UTF8.GetBytes((messageValue + 1).ToString(), buffer.Span);

        await ws.SendAsync(buffer.Slice(0, sendBytes), WebSocketMessageType.Binary, false, ct);
    }

    if (ws.State == WebSocketState.CloseReceived)
    {
        await ws.CloseAsync(ws.CloseStatus.Value, ws.CloseStatusDescription, ct);
    }
});

// This needs to be passed into ClientWebSocket.ConnectAsync
var messageInvoker = mockServer.ToMessageInvoker();

// Everything below is the 'client', so probably in your application code not your test

var client = new ClientWebSocket();
await client.ConnectAsync(new Uri("ws://localhost"), messageInvoker, cancellationToken);

int messageValue = 1;
Memory<byte> buffer = new(new byte[4]);

for (var i = 0; i < 10; i++)
{
    var bufferLength = Encoding.UTF8.GetBytes(messageValue.ToString(), buffer.Span);

    await client.SendAsync(buffer.Slice(0, bufferLength), WebSocketMessageType.Text, false,
        cancellationToken);

    var receiveResult = await client.ReceiveAsync(buffer, cancellationToken);

    messageValue = int.Parse(buffer.Span.Slice(0, receiveResult.Count));
}

await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Done", cancellationToken);

Client Mock Support

This library primarily focuses on mocking remote WebSocket servers in order to test ClientWebSocket code. The reverse is already possible using Microsoft.AspNetCore.Mvc.Testing, but can be supplemented by MockHttp.WebSocket's serialization helpers since they simply wrap WebSocket:

// Standard Microsoft.AspNetCore.Mvc.Testing stuff, no custom injection required
using var host = new WebApplicationFactory<Program>();
var webSocketClient = host.Server.CreateWebSocketClient();
var webSocket = await webSocketClient.ConnectAsync(new Uri(streamUrl!), CancellationToken.None);

var mockJsonClient = new JsonWebSocket(webSocket);

// Have at...
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0-pre1 93 21 days ago

0.1.0-pre1
 - Initial release for testing and feedback
 - NOTE: Public APIs are subject to change until 1.0.0