This commit is contained in:
Stef Heyenrath
2026-02-22 10:57:17 +01:00
parent 1c69bb51cc
commit 4ed71d578a
4 changed files with 29 additions and 5 deletions

View File

@@ -7,16 +7,29 @@ namespace WireMock.WebSockets;
internal class WebSocketMessageBuilder : IWebSocketMessageBuilder
{
/// <inheritdoc />
public string? MessageText { get; private set; }
/// <inheritdoc />
public byte[]? MessageBytes { get; private set; }
/// <inheritdoc />
public TimeSpan? Delay { get; private set; }
/// <inheritdoc />
public WebSocketMessageType Type { get; private set; }
/// <inheritdoc />
public bool ShouldClose { get; private set; }
/// <inheritdoc />
public IWebSocketMessageBuilder WithEcho()
{
Type = WebSocketMessageType.Close;
return this;
}
/// <inheritdoc />
public IWebSocketMessageBuilder WithText(string text)
{
MessageText = Guard.NotNull(text);
@@ -24,6 +37,7 @@ internal class WebSocketMessageBuilder : IWebSocketMessageBuilder
return this;
}
/// <inheritdoc />
public IWebSocketMessageBuilder WithBinary(byte[] bytes)
{
MessageBytes = Guard.NotNull(bytes);
@@ -31,23 +45,27 @@ internal class WebSocketMessageBuilder : IWebSocketMessageBuilder
return this;
}
/// <inheritdoc />
public IWebSocketMessageBuilder WithDelay(TimeSpan delay)
{
Delay = delay;
return this;
}
/// <inheritdoc />
public IWebSocketMessageBuilder WithDelay(int delayInMilliseconds)
{
Guard.Condition(delayInMilliseconds, d => d >= 0);
return WithDelay(TimeSpan.FromMilliseconds(delayInMilliseconds));
}
/// <inheritdoc />
public IWebSocketMessageBuilder Close()
{
ShouldClose = true;
return this;
}
/// <inheritdoc />
public IWebSocketMessageBuilder AndClose() => Close();
}

View File

@@ -1,10 +1,8 @@
// Copyright © WireMock.Net
using System;
using JetBrains.Annotations;
using WireMock.Matchers;
using WireMock.Settings;
using WireMock.Types;
namespace WireMock.WebSockets;

View File

@@ -9,6 +9,12 @@ namespace WireMock.WebSockets;
/// </summary>
public interface IWebSocketMessageBuilder
{
/// <summary>
/// Echo all received messages back to client
/// </summary>
[PublicAPI]
IWebSocketMessageBuilder WithEcho();
/// <summary>
/// Send a specific text message
/// </summary>

View File

@@ -676,7 +676,7 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
public async Task WithWebSocketProxy_Should_Proxy_Messages_To_Target_Server()
{
// Arrange - Start target echo server
using var targetServer = WireMockServer.Start(new WireMockServerSettings
var targetServer = WireMockServer.Start(new WireMockServerSettings
{
Logger = new TestOutputHelperWireMockLogger(output),
Urls = ["ws://localhost:0"]
@@ -698,14 +698,13 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
Urls = ["ws://localhost:0"]
});
var targetUrl = $"{targetServer.Url}/ws/target".Replace("http://", "ws://");
proxyServer
.Given(Request.Create()
.WithPath("/ws/proxy")
.WithWebSocketUpgrade()
)
.RespondWith(Response.Create()
.WithWebSocketProxy(targetUrl)
.WithWebSocketProxy(targetServer.Url!)
);
using var client = new ClientWebSocket();
@@ -723,6 +722,9 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
received.Should().Be(testMessage, "message should be proxied to target echo server and echoed back");
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", _ct);
targetServer.Stop();
targetServer.Dispose();
}
[Fact]