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

View File

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

View File

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

View File

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