WebSocket Integration Tests - Summary
Overview
I've successfully created comprehensive integration tests for the WebSockets implementation in WireMock.Net. These tests are based on Examples 1 and 2 from WireMock.Net.WebSocketExamples and use ClientWebSocket to perform real WebSocket connections.
Test File Created
- Location:
test\WireMock.Net.Tests\WebSockets\WebSocketIntegrationTests.cs - Test Count: 13 integration tests
- Test Framework: xUnit with FluentAssertions
Test Coverage
Example 1: Echo Server Tests (5 tests)
-
Example1_EchoServer_Should_Echo_Text_Messages
- Tests basic echo functionality with a single text message
- Verifies message type and content
-
Example1_EchoServer_Should_Echo_Multiple_Messages
- Tests echo functionality with multiple sequential messages
- Ensures each message is echoed back correctly
-
Example1_EchoServer_Should_Echo_Binary_Messages
- Tests echo functionality with binary data
- Verifies binary message type and byte array content
-
Example1_EchoServer_Should_Handle_Empty_Messages
- Tests edge case of empty messages
- Ensures the server handles empty content gracefully
Example 2: Custom Message Handler Tests (8 tests)
-
Example2_CustomHandler_Should_Handle_Help_Command
- Tests
/helpcommand - Verifies the help text contains expected commands
- Tests
-
Example2_CustomHandler_Should_Handle_Time_Command
- Tests
/timecommand - Verifies server time response format
- Tests
-
Example2_CustomHandler_Should_Handle_Echo_Command
- Tests
/echo <text>command - Verifies text is echoed without the command prefix
- Tests
-
Example2_CustomHandler_Should_Handle_Upper_Command
- Tests
/upper <text>command - Verifies text is converted to uppercase
- Tests
-
Example2_CustomHandler_Should_Handle_Reverse_Command
- Tests
/reverse <text>command - Verifies text is reversed correctly
- Tests
-
Example2_CustomHandler_Should_Handle_Quit_Command
- Tests
/quitcommand - Verifies goodbye message and proper WebSocket closure
- Tests
-
Example2_CustomHandler_Should_Handle_Unknown_Command
- Tests invalid commands
- Verifies error message is sent to client
-
Example2_CustomHandler_Should_Handle_Multiple_Commands_In_Sequence
- Integration test running multiple commands in sequence
- Tests all commands together to verify state consistency
Key Features
Real WebSocket Testing
- Uses
ClientWebSocketfor authentic WebSocket connections - Tests actual network communication, not mocked responses
- Verifies WebSocket protocol compliance
Best Practices
- Each test is isolated with its own server instance
- Uses random ports (Port = 0) to avoid conflicts
- Proper cleanup with
IDisposablepattern - Uses FluentAssertions for readable test assertions
Coverage
- Text and binary message types
- Multiple message sequences
- Command parsing and handling
- Error handling for invalid commands
- Proper connection closure
Running the Tests
Run all WebSocket integration tests:
dotnet test --filter "FullyQualifiedName~WebSocketIntegrationTests"
Run only Example 1 tests:
dotnet test --filter "FullyQualifiedName~Example1"
Run only Example 2 tests:
dotnet test --filter "FullyQualifiedName~Example2"
Dependencies
The tests rely on:
System.Net.WebSockets.ClientWebSocketWireMock.Server.WireMockServerFluentAssertionsxUnit
All dependencies are already included in the test project.
Notes
- Tests use Port = 0 to automatically assign available ports
- Each test properly disposes of the server after completion
- Tests are independent and can run in parallel
- Binary message testing ensures support for non-text protocols