mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 00:03:48 +01:00
Add WebSockets (#1423)
* Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc
This commit is contained in:
@@ -4,11 +4,10 @@ using System.Net.Http.Json;
|
||||
using AwesomeAssertions;
|
||||
using Projects;
|
||||
using WireMock.Net.Aspire.Tests.Facts;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace WireMock.Net.Aspire.Tests;
|
||||
|
||||
public class IntegrationTests(ITestOutputHelper output)
|
||||
public class IntegrationTests
|
||||
{
|
||||
private record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);
|
||||
|
||||
|
||||
16963
test/WireMock.Net.Aspire.Tests/coverage.opencover.xml
Normal file
16963
test/WireMock.Net.Aspire.Tests/coverage.opencover.xml
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Authors>Gennadii Saltyshchak</Authors>
|
||||
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<DebugType>full</DebugType>
|
||||
@@ -28,10 +28,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.Extensions.Routing\WireMock.Net.Extensions.Routing.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.Extensions.Routing\WireMock.Net.Extensions.Routing.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
51302
test/WireMock.Net.Middleware.Tests/coverage.opencover.xml
Normal file
51302
test/WireMock.Net.Middleware.Tests/coverage.opencover.xml
Normal file
File diff suppressed because it is too large
Load Diff
@@ -10,15 +10,15 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AwesomeAssertions" Version="9.1.0" />
|
||||
<PackageReference Include="AwesomeAssertions" Version="9.3.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="WireMock.Net" Version="1.23.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
|
||||
<PackageReference Include="WireMock.Net" Version="1.25.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
@@ -29,6 +29,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="JetBrains.Annotations" Version="2025.2.4" />
|
||||
<PackageReference Update="SonarAnalyzer.CSharp" Version="10.11.0.117924" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
429
test/WireMock.Net.Tests/Matchers/FuncMatcherTests.cs
Normal file
429
test/WireMock.Net.Tests/Matchers/FuncMatcherTests.cs
Normal file
@@ -0,0 +1,429 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class FuncMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_Should_Return_Perfect_When_Function_Returns_True()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("test");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_Should_Return_Mismatch_When_Function_Returns_False()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("other");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_Should_Handle_Null_String()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == null;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act - passing null as object, not as string
|
||||
var result = matcher.IsMatch((object?)null);
|
||||
|
||||
// Assert - null object doesn't match, returns mismatch
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_With_ByteArray_Input_Should_Return_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_With_Null_Object_Should_Return_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch((object?)null);
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_Should_Handle_Exception()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => throw new InvalidOperationException("Test exception");
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("test");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
result.Exception.Should().NotBeNull();
|
||||
result.Exception.Should().BeOfType<InvalidOperationException>();
|
||||
result.Exception!.Message.Should().Be("Test exception");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_Should_Return_Perfect_When_Function_Returns_True()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length == 3;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_Should_Return_Mismatch_When_Function_Returns_False()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length == 3;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3, 4, 5 });
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_Should_Handle_Null_ByteArray()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b == null;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act - passing null as object, not as byte[]
|
||||
var result = matcher.IsMatch((object?)null);
|
||||
|
||||
// Assert - null object doesn't match, returns mismatch
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_With_String_Input_Should_Return_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("test");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_Should_Handle_Exception()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => throw new InvalidOperationException("Bytes exception");
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
result.Exception.Should().NotBeNull();
|
||||
result.Exception.Should().BeOfType<InvalidOperationException>();
|
||||
result.Exception!.Message.Should().Be("Bytes exception");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_With_Contains_Logic_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s?.Contains("foo") == true;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch("foo");
|
||||
var result2 = matcher.IsMatch("foobar");
|
||||
var result3 = matcher.IsMatch("bar");
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeTrue();
|
||||
result2.IsPerfect().Should().BeTrue();
|
||||
result3.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_With_Length_Logic_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length > 2;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch(new byte[] { 1 });
|
||||
var result2 = matcher.IsMatch(new byte[] { 1, 2 });
|
||||
var result3 = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeFalse();
|
||||
result2.IsPerfect().Should().BeFalse();
|
||||
result3.IsPerfect().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_Name_Should_Return_FuncMatcher()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act & Assert
|
||||
matcher.Name.Should().Be("FuncMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_MatchBehaviour_Should_Return_AcceptOnMatch_By_Default()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act & Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_MatchBehaviour_Should_Return_Custom_Value_For_String()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act & Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_MatchBehaviour_Should_Return_Custom_Value_For_Bytes()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null;
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act & Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_GetCSharpCodeArguments_For_String_Should_Return_Correct_Code()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var code = matcher.GetCSharpCodeArguments();
|
||||
|
||||
// Assert
|
||||
code.Should().Be("new FuncMatcher(/* Func<string?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_GetCSharpCodeArguments_For_Bytes_Should_Return_Correct_Code()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var code = matcher.GetCSharpCodeArguments();
|
||||
|
||||
// Assert
|
||||
code.Should().Be("new FuncMatcher(/* Func<byte[]?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_With_RejectOnMatch_For_String_Should_Invert_Result_When_True()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("test");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_With_RejectOnMatch_For_String_Should_Invert_Result_When_False()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("other");
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_With_RejectOnMatch_For_Bytes_Should_Invert_Result_When_True()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_With_RejectOnMatch_For_Bytes_Should_Invert_Result_When_False()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(new byte[0]);
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_IsMatch_With_Integer_Input_Should_Return_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(42);
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_IsMatch_With_Integer_Input_Should_Return_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(42);
|
||||
|
||||
// Assert
|
||||
result.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_With_Empty_String_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => string.IsNullOrEmpty(s);
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch("");
|
||||
var result2 = matcher.IsMatch("test");
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeTrue();
|
||||
result2.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_With_Empty_Array_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length == 0;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch(new byte[0]);
|
||||
var result2 = matcher.IsMatch(new byte[] { 1 });
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeTrue();
|
||||
result2.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_String_With_Complex_Logic_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s != null && s.Length > 3 && s.StartsWith("t") && s.EndsWith("t");
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch("test");
|
||||
var result2 = matcher.IsMatch("tart");
|
||||
var result3 = matcher.IsMatch("tes");
|
||||
var result4 = matcher.IsMatch("best");
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeTrue();
|
||||
result2.IsPerfect().Should().BeTrue();
|
||||
result3.IsPerfect().Should().BeFalse();
|
||||
result4.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FuncMatcher_For_Bytes_With_Specific_Byte_Check_Should_Work()
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0 && b[0] == 0xFF;
|
||||
var matcher = new FuncMatcher(func);
|
||||
|
||||
// Act
|
||||
var result1 = matcher.IsMatch(new byte[] { 0xFF, 0x00 });
|
||||
var result2 = matcher.IsMatch(new byte[] { 0x00, 0xFF });
|
||||
|
||||
// Assert
|
||||
result1.IsPerfect().Should().BeTrue();
|
||||
result2.IsPerfect().Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,36 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Owin.Mappers;
|
||||
using Xunit;
|
||||
#if NET452
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
#else
|
||||
using IContext = Microsoft.AspNetCore.Http.HttpContext;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Net.Tests.Owin
|
||||
namespace WireMock.Net.Tests.Owin;
|
||||
|
||||
public class GlobalExceptionMiddlewareTests
|
||||
{
|
||||
public class GlobalExceptionMiddlewareTests
|
||||
private readonly Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
private readonly Mock<IOwinResponseMapper> _responseMapperMock;
|
||||
|
||||
private readonly GlobalExceptionMiddleware _sut;
|
||||
|
||||
public GlobalExceptionMiddlewareTests()
|
||||
{
|
||||
private readonly Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
private readonly Mock<IOwinResponseMapper> _responseMapperMock;
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
|
||||
private readonly GlobalExceptionMiddleware _sut;
|
||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||
_responseMapperMock.SetupAllProperties();
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage?>(), It.IsAny<HttpResponse>())).Returns(Task.FromResult(true));
|
||||
|
||||
public GlobalExceptionMiddlewareTests()
|
||||
{
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
_sut = new GlobalExceptionMiddleware(null, _optionsMock.Object, _responseMapperMock.Object);
|
||||
}
|
||||
|
||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||
_responseMapperMock.SetupAllProperties();
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage?>(), It.IsAny<IResponse>())).Returns(Task.FromResult(true));
|
||||
|
||||
_sut = new GlobalExceptionMiddleware(null, _optionsMock.Object, _responseMapperMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GlobalExceptionMiddleware_Invoke_NullAsNext_DoesNotInvokeNextAndDoesNotThrow()
|
||||
{
|
||||
// Act
|
||||
Check.ThatCode(() => _sut.Invoke(null)).DoesNotThrow();
|
||||
}
|
||||
[Fact]
|
||||
public void GlobalExceptionMiddleware_Invoke_NullAsNext_DoesNotInvokeNextAndDoesNotThrow()
|
||||
{
|
||||
// Act
|
||||
Check.ThatCode(() => _sut.Invoke(null)).DoesNotThrow();
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,14 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using WireMock.Models;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Owin.Mappers;
|
||||
using WireMock.Util;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using System.Collections.Generic;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Requests;
|
||||
using WireMock.Settings;
|
||||
@@ -21,21 +17,12 @@ using WireMock.Handlers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.RequestBuilders;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using WireMock.Owin.ActivityTracing;
|
||||
using System.Diagnostics;
|
||||
#endif
|
||||
#if NET452
|
||||
using Microsoft.Owin;
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using IRequest = Microsoft.Owin.IOwinRequest;
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
#else
|
||||
using IContext = Microsoft.AspNetCore.Http.HttpContext;
|
||||
using IRequest = Microsoft.AspNetCore.Http.HttpRequest;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Net.Tests.Owin;
|
||||
|
||||
@@ -51,7 +38,7 @@ public class WireMockMiddlewareTests
|
||||
private readonly Mock<IMappingMatcher> _matcherMock;
|
||||
private readonly Mock<IMapping> _mappingMock;
|
||||
private readonly Mock<IRequestMatchResult> _requestMatchResultMock;
|
||||
private readonly Mock<IContext> _contextMock;
|
||||
private readonly Mock<HttpContext> _contextMock;
|
||||
|
||||
private readonly WireMockMiddleware _sut;
|
||||
|
||||
@@ -72,17 +59,18 @@ public class WireMockMiddlewareTests
|
||||
_requestMapperMock = new Mock<IOwinRequestMapper>();
|
||||
_requestMapperMock.SetupAllProperties();
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||
_responseMapperMock.SetupAllProperties();
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage?>(), It.IsAny<IResponse>())).Returns(Task.FromResult(true));
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage?>(), It.IsAny<HttpResponse>())).Returns(Task.FromResult(true));
|
||||
|
||||
_matcherMock = new Mock<IMappingMatcher>();
|
||||
_matcherMock.SetupAllProperties();
|
||||
// _matcherMock.Setup(m => m.FindBestMatch(It.IsAny<RequestMessage>())).Returns((new MappingMatcherResult(), new MappingMatcherResult()));
|
||||
|
||||
_contextMock = new Mock<IContext>();
|
||||
_contextMock = new Mock<HttpContext>();
|
||||
_contextMock.SetupGet(c => c.Items).Returns(new Dictionary<object, object?>());
|
||||
|
||||
_mappingMock = new Mock<IMapping>();
|
||||
|
||||
@@ -110,7 +98,7 @@ public class WireMockMiddlewareTests
|
||||
_optionsMock.Verify(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => (int)r.StatusCode! == 404 && ((StatusModel)r.BodyData!.BodyAsJson!).Status == "No matching mapping found";
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<HttpResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -128,7 +116,7 @@ public class WireMockMiddlewareTests
|
||||
_optionsMock.Verify(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => (int)r.StatusCode! == 404 && ((StatusModel)r.BodyData!.BodyAsJson!).Status == "No matching mapping found";
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<HttpResponse>()), Times.Once);
|
||||
|
||||
// Verify
|
||||
fileSystemHandlerMock.Verify(f => f.WriteUnmatchedRequest("98fae52e-76df-47d9-876f-2ee32e931d9b.LogEntry.json", It.IsAny<string>()));
|
||||
@@ -140,7 +128,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthenticationMatcher).Returns(new ExactMatcher());
|
||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||
@@ -155,7 +143,7 @@ public class WireMockMiddlewareTests
|
||||
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => (int?)r.StatusCode == 401;
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<HttpResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -163,7 +151,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]> { { "h", new[] { "x" } } });
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthenticationMatcher).Returns(new ExactMatcher());
|
||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||
@@ -178,7 +166,7 @@ public class WireMockMiddlewareTests
|
||||
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => (int?)r.StatusCode == 401;
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<HttpResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -196,7 +184,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthenticationMatcher).Returns(new ExactMatcher());
|
||||
|
||||
@@ -245,7 +233,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthenticationMatcher).Returns(new ExactMatcher());
|
||||
|
||||
@@ -300,7 +288,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Arrange
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/__admin/health"), "GET", "::1");
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.ActivityTracingOptions).Returns(new ActivityTracingOptions
|
||||
{
|
||||
@@ -329,7 +317,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Arrange
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/api/orders"), "GET", "::1");
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.ActivityTracingOptions).Returns(new ActivityTracingOptions
|
||||
{
|
||||
@@ -358,7 +346,7 @@ public class WireMockMiddlewareTests
|
||||
{
|
||||
// Arrange
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/api/orders"), "GET", "::1");
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<HttpContext>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.ActivityTracingOptions).Returns((ActivityTracingOptions?)null);
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Buffers;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
|
||||
namespace WireMock.Net.Tests.WebSockets;
|
||||
|
||||
internal static class ClientWebSocketExtensions
|
||||
{
|
||||
internal static Task SendAsync(this ClientWebSocket client, string text, bool endOfMessage = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
|
||||
}
|
||||
|
||||
internal static async Task<string> ReceiveAsTextAsync(this ClientWebSocket client, int bufferSize = 1024, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var receiveBuffer = ArrayPool<byte>.Shared.Lease(1024);
|
||||
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
|
||||
|
||||
if (result.MessageType != WebSocketMessageType.Text)
|
||||
{
|
||||
throw new InvalidOperationException($"Expected a text message but received a {result.MessageType} message.");
|
||||
}
|
||||
|
||||
if (!result.EndOfMessage)
|
||||
{
|
||||
throw new InvalidOperationException("Received message is too large for the buffer. Consider increasing the buffer size.");
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(receiveBuffer, 0, result.Count);
|
||||
}
|
||||
|
||||
internal static async Task<byte[]> ReceiveAsBytesAsync(this ClientWebSocket client, int bufferSize = 1024, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var receiveBuffer = ArrayPool<byte>.Shared.Lease(1024);
|
||||
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
|
||||
|
||||
if (result.MessageType != WebSocketMessageType.Binary)
|
||||
{
|
||||
throw new InvalidOperationException($"Expected a binary message but received a {result.MessageType} message.");
|
||||
}
|
||||
|
||||
if (!result.EndOfMessage)
|
||||
{
|
||||
throw new InvalidOperationException("Received message is too large for the buffer. Consider increasing the buffer size.");
|
||||
}
|
||||
|
||||
var receivedData = new byte[result.Count];
|
||||
Array.Copy(receiveBuffer, receivedData, result.Count);
|
||||
return receivedData;
|
||||
}
|
||||
}
|
||||
248
test/WireMock.Net.Tests/WebSockets/README.md
Normal file
248
test/WireMock.Net.Tests/WebSockets/README.md
Normal file
@@ -0,0 +1,248 @@
|
||||
# WebSocket Integration Tests - Summary
|
||||
|
||||
## Overview
|
||||
Comprehensive integration tests for the WebSockets implementation in WireMock.Net. These tests are based on Examples 1, 2, and 3 from `WireMock.Net.WebSocketExamples` and use `ClientWebSocket` to perform real WebSocket connections.
|
||||
|
||||
## Test File
|
||||
- **Location**: `test\WireMock.Net.Tests\WebSockets\WebSocketIntegrationTests.cs`
|
||||
- **Test Count**: 21 integration tests
|
||||
- **Test Framework**: xUnit with FluentAssertions
|
||||
|
||||
## Test Coverage Summary
|
||||
|
||||
| Category | Tests | Description |
|
||||
|----------|-------|-------------|
|
||||
| **Example 1: Echo Server** | 4 | Basic echo functionality with text/binary messages |
|
||||
| **Example 2: Custom Handlers** | 8 | Command processing and custom message handlers |
|
||||
| **Example 3: JSON (SendJsonAsync)** | 3 | JSON serialization and complex object handling |
|
||||
| **Broadcast** | 6 | Multi-client broadcasting functionality |
|
||||
| **Total** | **21** | |
|
||||
|
||||
## Detailed Test Descriptions
|
||||
|
||||
### Example 1: Echo Server Tests (4 tests)
|
||||
Tests the basic WebSocket echo functionality where messages are echoed back to the sender.
|
||||
|
||||
1. **Example1_EchoServer_Should_Echo_Text_Messages**
|
||||
- ✅ Single text message echo
|
||||
- ✅ Verifies message type and content
|
||||
|
||||
2. **Example1_EchoServer_Should_Echo_Multiple_Messages**
|
||||
- ✅ Multiple sequential messages
|
||||
- ✅ Each message echoed correctly
|
||||
|
||||
3. **Example1_EchoServer_Should_Echo_Binary_Messages**
|
||||
- ✅ Binary data echo
|
||||
- ✅ Byte array verification
|
||||
|
||||
4. **Example1_EchoServer_Should_Handle_Empty_Messages**
|
||||
- ✅ Edge case: empty messages
|
||||
- ✅ Graceful handling
|
||||
|
||||
### Example 2: Custom Message Handler Tests (8 tests)
|
||||
Tests custom message processing with various commands.
|
||||
|
||||
1. **Example2_CustomHandler_Should_Handle_Help_Command**
|
||||
- ✅ `/help` → Returns list of available commands
|
||||
|
||||
2. **Example2_CustomHandler_Should_Handle_Time_Command**
|
||||
- ✅ `/time` → Returns current server time
|
||||
|
||||
3. **Example2_CustomHandler_Should_Handle_Echo_Command**
|
||||
- ✅ `/echo <text>` → Echoes the text
|
||||
|
||||
4. **Example2_CustomHandler_Should_Handle_Upper_Command**
|
||||
- ✅ `/upper <text>` → Converts to uppercase
|
||||
|
||||
5. **Example2_CustomHandler_Should_Handle_Reverse_Command**
|
||||
- ✅ `/reverse <text>` → Reverses the text
|
||||
|
||||
6. **Example2_CustomHandler_Should_Handle_Quit_Command**
|
||||
- ✅ `/quit` → Sends goodbye and closes connection
|
||||
|
||||
7. **Example2_CustomHandler_Should_Handle_Unknown_Command**
|
||||
- ✅ Invalid commands → Error message
|
||||
|
||||
8. **Example2_CustomHandler_Should_Handle_Multiple_Commands_In_Sequence**
|
||||
- ✅ All commands in sequence
|
||||
- ✅ State consistency verification
|
||||
|
||||
### Example 3: SendJsonAsync Tests (3 tests)
|
||||
Tests JSON serialization and the `SendJsonAsync` functionality.
|
||||
|
||||
1. **Example3_JsonEndpoint_Should_Send_Json_Response**
|
||||
- ✅ Basic JSON response
|
||||
- ✅ Structure: `{ timestamp, message, length, type }`
|
||||
- ✅ Proper serialization
|
||||
|
||||
2. **Example3_JsonEndpoint_Should_Handle_Multiple_Json_Messages**
|
||||
- ✅ Sequential JSON messages
|
||||
- ✅ Each properly serialized
|
||||
|
||||
3. **Example3_JsonEndpoint_Should_Serialize_Complex_Objects**
|
||||
- ✅ Nested objects
|
||||
- ✅ Arrays within objects
|
||||
- ✅ Complex structures
|
||||
|
||||
### Broadcast Tests (6 tests)
|
||||
Tests the broadcast functionality with multiple simultaneous clients.
|
||||
|
||||
1. **Broadcast_Should_Send_Message_To_All_Connected_Clients**
|
||||
- ✅ 3 connected clients
|
||||
- ✅ All receive same broadcast
|
||||
- ✅ Timestamp in messages
|
||||
|
||||
2. **Broadcast_Should_Only_Send_To_Open_Connections**
|
||||
- ✅ Closed connections skipped
|
||||
- ✅ Only active clients receive
|
||||
|
||||
3. **BroadcastJson_Should_Send_Json_To_All_Clients**
|
||||
- ✅ JSON broadcasting
|
||||
- ✅ Multiple clients receive
|
||||
- ✅ Sender identification
|
||||
|
||||
4. **Broadcast_Should_Handle_Multiple_Sequential_Messages**
|
||||
- ✅ Sequential broadcasts
|
||||
- ✅ Message ordering
|
||||
- ✅ All clients receive all messages
|
||||
|
||||
5. **Broadcast_Should_Work_With_Many_Clients**
|
||||
- ✅ 5 simultaneous clients
|
||||
- ✅ Scalability test
|
||||
- ✅ Parallel message reception
|
||||
|
||||
6. **Broadcast Integration**
|
||||
- ✅ Complete flow testing
|
||||
|
||||
## Key Testing Features
|
||||
|
||||
### 🔌 Real WebSocket Connections
|
||||
- Uses `System.Net.WebSockets.ClientWebSocket`
|
||||
- Actual network communication
|
||||
- Protocol compliance verification
|
||||
|
||||
### 📤 SendJsonAsync Coverage
|
||||
```csharp
|
||||
await ctx.SendJsonAsync(new {
|
||||
timestamp = DateTime.UtcNow,
|
||||
message = msg.Text,
|
||||
data = complexObject
|
||||
});
|
||||
```
|
||||
- Simple objects
|
||||
- Complex nested structures
|
||||
- Arrays and collections
|
||||
|
||||
### 📡 Broadcast Coverage
|
||||
```csharp
|
||||
await ctx.BroadcastTextAsync("Message to all");
|
||||
await ctx.BroadcastJsonAsync(jsonObject);
|
||||
```
|
||||
- Multiple simultaneous clients
|
||||
- Text and JSON broadcasts
|
||||
- Connection state handling
|
||||
- Scalability testing
|
||||
|
||||
### ✨ Best Practices
|
||||
- ✅ Test isolation (each test has own server)
|
||||
- ✅ Random ports (Port = 0)
|
||||
- ✅ Proper cleanup (`IDisposable`)
|
||||
- ✅ FluentAssertions for readability
|
||||
- ✅ Async/await throughout
|
||||
- ✅ No test interdependencies
|
||||
|
||||
## Running the Tests
|
||||
|
||||
### All WebSocket Tests
|
||||
```bash
|
||||
dotnet test --filter "FullyQualifiedName~WebSocketIntegrationTests"
|
||||
```
|
||||
|
||||
### By Example
|
||||
```bash
|
||||
# Example 1: Echo
|
||||
dotnet test --filter "FullyQualifiedName~Example1"
|
||||
|
||||
# Example 2: Custom Handlers
|
||||
dotnet test --filter "FullyQualifiedName~Example2"
|
||||
|
||||
# Example 3: JSON
|
||||
dotnet test --filter "FullyQualifiedName~Example3"
|
||||
```
|
||||
|
||||
### By Feature
|
||||
```bash
|
||||
# Broadcast tests
|
||||
dotnet test --filter "FullyQualifiedName~Broadcast"
|
||||
|
||||
# JSON tests
|
||||
dotnet test --filter "FullyQualifiedName~Json"
|
||||
```
|
||||
|
||||
### Run Specific Test
|
||||
```bash
|
||||
dotnet test --filter "FullyQualifiedName~Example1_EchoServer_Should_Echo_Text_Messages"
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
| Package | Purpose |
|
||||
|---------|---------|
|
||||
| `System.Net.WebSockets.ClientWebSocket` | Real WebSocket client |
|
||||
| `WireMock.Server` | WireMock server instance |
|
||||
| `FluentAssertions` | Readable assertions |
|
||||
| `xUnit` | Test framework |
|
||||
| `Newtonsoft.Json` | JSON parsing in assertions |
|
||||
|
||||
All dependencies are included in `WireMock.Net.Tests.csproj`.
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### JSON Testing Pattern
|
||||
```csharp
|
||||
// Send text message
|
||||
await client.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
|
||||
// Receive JSON response
|
||||
var result = await client.ReceiveAsync(buffer, CancellationToken.None);
|
||||
var json = JObject.Parse(received);
|
||||
|
||||
// Assert structure
|
||||
json["message"].ToString().Should().Be(expectedMessage);
|
||||
json["timestamp"].Should().NotBeNull();
|
||||
```
|
||||
|
||||
### Broadcast Testing Pattern
|
||||
```csharp
|
||||
// Connect multiple clients
|
||||
var clients = new[] { new ClientWebSocket(), new ClientWebSocket() };
|
||||
foreach (var c in clients)
|
||||
await c.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
// Send from one client
|
||||
await clients[0].SendAsync(message, ...);
|
||||
|
||||
// All clients receive
|
||||
foreach (var c in clients) {
|
||||
var result = await c.ReceiveAsync(buffer, ...);
|
||||
// Assert all received the same message
|
||||
}
|
||||
```
|
||||
|
||||
## Test Timing Notes
|
||||
- Connection registration delays: 100-200ms
|
||||
- Ensures all clients are registered before broadcasting
|
||||
- Prevents race conditions in multi-client tests
|
||||
- Production code does not require delays
|
||||
|
||||
## Coverage Metrics
|
||||
- ✅ Text messages
|
||||
- ✅ Binary messages
|
||||
- ✅ Empty messages
|
||||
- ✅ JSON serialization (simple & complex)
|
||||
- ✅ Multiple sequential messages
|
||||
- ✅ Multiple simultaneous clients
|
||||
- ✅ Connection state transitions
|
||||
- ✅ Broadcast to all clients
|
||||
- ✅ Closed connection handling
|
||||
- ✅ Error scenarios
|
||||
601
test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs
Normal file
601
test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs
Normal file
@@ -0,0 +1,601 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Net.WebSockets;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Net.Xunit;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace WireMock.Net.Tests.WebSockets;
|
||||
|
||||
public class WebSocketIntegrationTests(ITestOutputHelper output)
|
||||
{
|
||||
[Fact]
|
||||
public async Task EchoServer_Should_Echo_Text_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/echo")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.WithEcho()
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/echo");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
var testMessage = "Hello, WebSocket!";
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
// Assert
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
received.Should().Be(testMessage);
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithText_Should_Send_Configured_Text()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
var responseMessage = "This is a predefined response";
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/message")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.SendMessage(m => m.WithText(responseMessage))
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/message");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
var testMessage = "Any message from client";
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
// Assert
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
received.Should().Be(responseMessage);
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithText_Should_Send_Same_Text_For_Multiple_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
var responseMessage = "Fixed response";
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/message")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.SendMessage(m => m.WithText(responseMessage))
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/message");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var testMessages = new[] { "First", "Second", "Third" };
|
||||
|
||||
// Act & Assert
|
||||
foreach (var testMessage in testMessages)
|
||||
{
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
received.Should().Be(responseMessage, $"should always return the fixed response regardless of input message '{testMessage}'");
|
||||
}
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithBinary_Should_Send_Configured_Bytes()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
var responseBytes = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF };
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/binary")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.SendMessage(m => m.WithBinary(responseBytes))
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/binary");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
var testMessage = "Any message from client";
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
// Assert
|
||||
var receivedData = await client.ReceiveAsBytesAsync();
|
||||
receivedData.Should().BeEquivalentTo(responseBytes);
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithBinary_Should_Send_Same_Bytes_For_Multiple_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
var responseBytes = new byte[] { 0x01, 0x02, 0x03 };
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/binary")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.SendMessage(m => m.WithBinary(responseBytes))
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/binary");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var testMessages = new[] { "First", "Second", "Third" };
|
||||
|
||||
// Act & Assert
|
||||
foreach (var testMessage in testMessages)
|
||||
{
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
var receivedData = await client.ReceiveAsBytesAsync();
|
||||
receivedData.Should().BeEquivalentTo(responseBytes, $"should always return the fixed bytes regardless of input message '{testMessage}'");
|
||||
}
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task EchoServer_Should_Echo_Multiple_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/echo")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws.WithEcho())
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/echo");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var testMessages = new[] { "Hello", "World", "WebSocket", "Test" };
|
||||
|
||||
// Act & Assert
|
||||
foreach (var testMessage in testMessages)
|
||||
{
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
|
||||
received.Should().Be(testMessage, $"message '{testMessage}' should be echoed back");
|
||||
}
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EchoServer_Should_Echo_Binary_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/echo")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws.WithEcho())
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/echo");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var testData = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
// Act
|
||||
await client.SendAsync(new ArraySegment<byte>(testData), WebSocketMessageType.Binary, true, CancellationToken.None);
|
||||
|
||||
var receivedData = await client.ReceiveAsBytesAsync();
|
||||
|
||||
// Assert
|
||||
receivedData.Should().BeEquivalentTo(testData);
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EchoServer_Should_Handle_Empty_Messages()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/echo")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws.WithEcho())
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/echo");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
// Act
|
||||
await client.SendAsync(string.Empty);
|
||||
|
||||
var receiveBuffer = new byte[1024];
|
||||
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.Count.Should().Be(0);
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CustomHandler_Should_Handle_Help_Command()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/chat")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.WithMessageHandler(async (message, context) =>
|
||||
{
|
||||
if (message.MessageType == WebSocketMessageType.Text)
|
||||
{
|
||||
var text = message.Text ?? string.Empty;
|
||||
|
||||
if (text.StartsWith("/help"))
|
||||
{
|
||||
await context.SendAsync("Available commands: /help, /time, /echo <text>, /upper <text>, /reverse <text>");
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/chat");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
// Act
|
||||
await client.SendAsync("/help");
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
|
||||
// Assert
|
||||
received.Should().Contain("Available commands");
|
||||
received.Should().Contain("/help");
|
||||
received.Should().Contain("/time");
|
||||
received.Should().Contain("/echo");
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CustomHandler_Should_Handle_Multiple_Commands_In_Sequence()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/chat")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.WithMessageHandler(async (message, context) =>
|
||||
{
|
||||
if (message.MessageType == WebSocketMessageType.Text)
|
||||
{
|
||||
var text = message.Text ?? string.Empty;
|
||||
|
||||
if (text.StartsWith("/help"))
|
||||
{
|
||||
await context.SendAsync("Available commands: /help, /time, /echo <text>, /upper <text>, /reverse <text>");
|
||||
}
|
||||
else if (text.StartsWith("/time"))
|
||||
{
|
||||
await context.SendAsync($"Server time: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");
|
||||
}
|
||||
else if (text.StartsWith("/echo "))
|
||||
{
|
||||
await context.SendAsync(text.Substring(6));
|
||||
}
|
||||
else if (text.StartsWith("/upper "))
|
||||
{
|
||||
await context.SendAsync(text.Substring(7).ToUpper());
|
||||
}
|
||||
else if (text.StartsWith("/reverse "))
|
||||
{
|
||||
var toReverse = text.Substring(9);
|
||||
var reversed = new string(toReverse.Reverse().ToArray());
|
||||
await context.SendAsync(reversed);
|
||||
}
|
||||
else if (text.StartsWith("/close"))
|
||||
{
|
||||
await context.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing connection");
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/chat");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var commands = new (string, Action<string>)[]
|
||||
{
|
||||
("/help", response => response.Should().Contain("Available commands")),
|
||||
("/time", response => response.Should().Contain("Server time")),
|
||||
("/echo Test", response => response.Should().Be("Test")),
|
||||
("/upper test", response => response.Should().Be("TEST")),
|
||||
("/reverse hello", response => response.Should().Be("olleh"))
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
foreach (var (command, assertion) in commands)
|
||||
{
|
||||
await client.SendAsync(command);
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
|
||||
assertion(received);
|
||||
}
|
||||
|
||||
await client.SendAsync("/close");
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenMessage_Should_Handle_Multiple_Conditions_Fluently()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/conditional")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.WithCloseTimeout(TimeSpan.FromSeconds(3))
|
||||
.WhenMessage("/help").SendMessage(m => m.WithText("Available commands"))
|
||||
.WhenMessage("/time").SendMessage(m => m.WithText($"Server time: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC"))
|
||||
.WhenMessage("/echo *").SendMessage(m => m.WithText("echo response"))
|
||||
.WhenMessage(new ExactMatcher("/exact")).SendMessage(m => m.WithText("is exact"))
|
||||
.WhenMessage(new FuncMatcher(s => s == "/func")).SendMessage(m => m.WithText("is func"))
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/conditional");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
var testCases = new (string message, string expectedContains)[]
|
||||
{
|
||||
("/help", "Available commands"),
|
||||
("/time", "Server time"),
|
||||
("/echo test", "echo response"),
|
||||
("/exact", "is exact"),
|
||||
("/func", "is func")
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
foreach (var (message, expectedContains) in testCases)
|
||||
{
|
||||
await client.SendAsync(message);
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
|
||||
received.Should().Contain(expectedContains, $"message '{message}' should return response containing '{expectedContains}'");
|
||||
}
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenMessage_Should_Close_Connection_When_AndClose_Is_Used()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/close")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.WhenMessage("/close").SendMessage(m => m.WithText("Closing connection").AndClose())
|
||||
)
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/close");
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
|
||||
// Act
|
||||
await client.SendAsync("/close");
|
||||
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
|
||||
// Assert
|
||||
received.Should().Contain("Closing connection");
|
||||
|
||||
// Try to receive again - this will complete the close handshake
|
||||
// and update the client state to Closed
|
||||
try
|
||||
{
|
||||
var receiveBuffer = new byte[1024];
|
||||
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
|
||||
|
||||
// If we get here, the message type should be Close
|
||||
result.MessageType.Should().Be(WebSocketMessageType.Close);
|
||||
}
|
||||
catch (WebSocketException)
|
||||
{
|
||||
// Connection was closed, which is expected
|
||||
}
|
||||
|
||||
// Verify the connection is CloseReceived
|
||||
client.State.Should().Be(WebSocketState.CloseReceived);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithTransformer_Should_Transform_Message_Using_Handlebars()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/transform")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(ws => ws
|
||||
.SendMessage(m => m.WithText("{{request.Path}} {{[String.Lowercase] message.Text}}"))
|
||||
)
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/transform");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
var testMessage = "HellO";
|
||||
await client.SendAsync(testMessage);
|
||||
|
||||
// Assert
|
||||
var received = await client.ReceiveAsTextAsync();
|
||||
received.Should().Be("/ws/transform hello");
|
||||
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", CancellationToken.None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user