mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-02-25 10:15:04 +01:00
more tsts
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
using AwesomeAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using WireMock.OpenTelemetry;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.OpenTelemetry;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using AwesomeAssertions;
|
||||
using Moq;
|
||||
using WireMock.Logging;
|
||||
@@ -9,11 +10,31 @@ using WireMock.Models;
|
||||
using WireMock.Owin.ActivityTracing;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
using WireMock.WebSockets;
|
||||
|
||||
namespace WireMock.Net.Tests.Owin.ActivityTracing;
|
||||
|
||||
public class WireMockActivitySourceTests
|
||||
public class WireMockActivitySourceTests : IDisposable
|
||||
{
|
||||
private readonly ActivityListener _activityListener;
|
||||
|
||||
public WireMockActivitySourceTests()
|
||||
{
|
||||
// Set up ActivityListener for tests
|
||||
_activityListener = new ActivityListener
|
||||
{
|
||||
ShouldListenTo = source => source.Name == WireMockActivitySource.SourceName,
|
||||
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData
|
||||
};
|
||||
|
||||
ActivitySource.AddActivityListener(_activityListener);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_activityListener?.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithRequest_ShouldSetRequestTagsAndBody_WhenEnabled()
|
||||
{
|
||||
@@ -176,16 +197,264 @@ public class WireMockActivitySourceTests
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
var exception = new InvalidOperationException("boom");
|
||||
var exception = new InvalidOperationException("Yes, Rico; Kaboom.");
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.RecordException(activity, exception);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.OtelStatusCode).Should().Be("ERROR");
|
||||
activity.GetTagItem("otel.status_description").Should().Be("boom");
|
||||
activity.GetTagItem("otel.status_description").Should().Be("Yes, Rico; Kaboom.");
|
||||
activity.GetTagItem("exception.type").Should().Be(typeof(InvalidOperationException).FullName);
|
||||
activity.GetTagItem("exception.message").Should().Be("boom");
|
||||
activity.GetTagItem("exception.message").Should().Be("Yes, Rico; Kaboom.");
|
||||
activity.GetTagItem("exception.stacktrace").Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartRequestActivity_ShouldCreateActivity_WithCorrectDisplayName()
|
||||
{
|
||||
// Arrange
|
||||
var requestMethod = "POST";
|
||||
var requestPath = "/api/users";
|
||||
|
||||
// Act
|
||||
using var activity = WireMockActivitySource.StartRequestActivity(requestMethod, requestPath);
|
||||
|
||||
// Assert
|
||||
activity.Should().NotBeNull();
|
||||
activity.DisplayName.Should().Be("WireMock POST /api/users");
|
||||
activity.Kind.Should().Be(ActivityKind.Server);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartWebSocketMessageActivity_ShouldCreateActivity_WithCorrectName()
|
||||
{
|
||||
// Arrange
|
||||
var mappingGuid = Guid.NewGuid();
|
||||
var direction = WebSocketMessageDirection.Receive;
|
||||
|
||||
// Act
|
||||
using var activity = WireMockActivitySource.StartWebSocketMessageActivity(direction, mappingGuid);
|
||||
|
||||
// Assert
|
||||
activity.Should().NotBeNull();
|
||||
activity.DisplayName.Should().Be("WireMock WebSocket receive");
|
||||
activity.Kind.Should().Be(ActivityKind.Server);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartWebSocketMessageActivity_ShouldSetMappingGuidTag()
|
||||
{
|
||||
// Arrange
|
||||
var mappingGuid = Guid.NewGuid();
|
||||
var direction = WebSocketMessageDirection.Send;
|
||||
|
||||
// Act
|
||||
using var activity = WireMockActivitySource.StartWebSocketMessageActivity(direction, mappingGuid);
|
||||
|
||||
// Assert
|
||||
activity.Should().NotBeNull();
|
||||
activity.GetTagItem(WireMockSemanticConventions.MappingGuid).Should().Be(mappingGuid.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartWebSocketMessageActivity_ShouldCreateActivityForSendDirection()
|
||||
{
|
||||
// Arrange
|
||||
var mappingGuid = Guid.NewGuid();
|
||||
var direction = WebSocketMessageDirection.Send;
|
||||
|
||||
// Act
|
||||
using var activity = WireMockActivitySource.StartWebSocketMessageActivity(direction, mappingGuid);
|
||||
|
||||
// Assert
|
||||
activity.Should().NotBeNull();
|
||||
activity.DisplayName.Should().Be("WireMock WebSocket send");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartWebSocketMessageActivity_ShouldCreateActivityWithListenerConfigured()
|
||||
{
|
||||
// Arrange
|
||||
var mappingGuid = Guid.NewGuid();
|
||||
var direction = WebSocketMessageDirection.Receive;
|
||||
|
||||
// Act - ActivityListener is configured in test constructor
|
||||
using var activity = WireMockActivitySource.StartWebSocketMessageActivity(direction, mappingGuid);
|
||||
|
||||
// Assert - activity should be created since listener is active
|
||||
activity.Should().NotBeNull();
|
||||
activity.DisplayName.Should().Be("WireMock WebSocket receive");
|
||||
activity.GetTagItem(WireMockSemanticConventions.MappingGuid).Should().Be(mappingGuid.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldSetMessageTypeTag()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
var messageType = WebSocketMessageType.Text;
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
messageType,
|
||||
messageSize: 100,
|
||||
endOfMessage: true
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageType).Should().Be("Text");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldSetMessageSizeTag()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Binary,
|
||||
messageSize: 256,
|
||||
endOfMessage: true
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageSize).Should().Be(256);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldSetEndOfMessageTag()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Text,
|
||||
messageSize: 50,
|
||||
endOfMessage: false
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketEndOfMessage).Should().Be(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldSetOkStatus()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Text,
|
||||
messageSize: 100,
|
||||
endOfMessage: true
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.OtelStatusCode).Should().Be("OK");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldRecordTextContent_WhenEnabled()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
var options = new ActivityTracingOptions { RecordRequestBody = true };
|
||||
var textContent = "Hello WebSocket";
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Text,
|
||||
messageSize: textContent.Length,
|
||||
endOfMessage: true,
|
||||
textContent: textContent,
|
||||
options: options
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageContent).Should().Be(textContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldNotRecordTextContent_WhenDisabled()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
var options = new ActivityTracingOptions { RecordRequestBody = false };
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Text,
|
||||
messageSize: 100,
|
||||
endOfMessage: true,
|
||||
textContent: "Hello WebSocket",
|
||||
options: options
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageContent).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldNotRecordBinaryContent()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
var options = new ActivityTracingOptions { RecordRequestBody = true };
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Binary,
|
||||
messageSize: 100,
|
||||
endOfMessage: true,
|
||||
textContent: "should not record",
|
||||
options: options
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageContent).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldHandleNullActivity()
|
||||
{
|
||||
// Arrange & Act - should not throw
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
null,
|
||||
WebSocketMessageType.Text,
|
||||
messageSize: 100,
|
||||
endOfMessage: true
|
||||
);
|
||||
|
||||
// Assert - no exception thrown
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnrichWithWebSocketMessage_ShouldHandleClosedMessageType()
|
||||
{
|
||||
// Arrange
|
||||
using var activity = new Activity("test").Start();
|
||||
|
||||
// Act
|
||||
WireMockActivitySource.EnrichWithWebSocketMessage(
|
||||
activity,
|
||||
WebSocketMessageType.Close,
|
||||
messageSize: 0,
|
||||
endOfMessage: true
|
||||
);
|
||||
|
||||
// Assert
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageType).Should().Be("Close");
|
||||
activity.GetTagItem(WireMockSemanticConventions.WebSocketMessageSize).Should().Be(0);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,36 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
|
||||
{
|
||||
private readonly CancellationToken _ct = testContext.Current.CancellationToken;
|
||||
|
||||
[Fact]
|
||||
public async Task WithNoSetupShouldJustWaitForClose_AndCLose_When_ClientCloses()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(output),
|
||||
Urls = ["ws://localhost:0"]
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/ws/x")
|
||||
.WithWebSocketUpgrade()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithWebSocket(_ => { })
|
||||
);
|
||||
|
||||
using var client = new ClientWebSocket();
|
||||
var uri = new Uri($"{server.Url}/ws/x");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, _ct);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
// Assert
|
||||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Test complete", _ct);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EchoServer_Should_Echo_Text_Messages()
|
||||
{
|
||||
@@ -40,7 +70,7 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
|
||||
var uri = new Uri($"{server.Url}/ws/echo");
|
||||
|
||||
// Act
|
||||
await client.ConnectAsync(uri, CancellationToken.None);
|
||||
await client.ConnectAsync(uri, _ct);
|
||||
client.State.Should().Be(WebSocketState.Open);
|
||||
|
||||
var testMessage = "Hello, WebSocket!";
|
||||
@@ -906,7 +936,6 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
|
||||
|
||||
var uri = new Uri($"{server.Url}/ws/broadcast");
|
||||
|
||||
// Act
|
||||
await client1.ConnectAsync(uri, _ct);
|
||||
await client2.ConnectAsync(uri, _ct);
|
||||
await client3.ConnectAsync(uri, _ct);
|
||||
|
||||
@@ -551,7 +551,9 @@ public class WireMockServerWebSocketIntegrationTests(ITestOutputHelper output, I
|
||||
var uri = new Uri($"{server.Url}/ws/test");
|
||||
|
||||
await client1.ConnectAsync(uri, _ct);
|
||||
await Task.Delay(100, _ct);
|
||||
await client2.ConnectAsync(uri, _ct);
|
||||
await Task.Delay(100, _ct);
|
||||
|
||||
var initialConnections = server.GetWebSocketConnections();
|
||||
initialConnections.Should().HaveCount(2);
|
||||
|
||||
Reference in New Issue
Block a user