mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 16:01:39 +02:00
...
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
@@ -10,7 +9,7 @@ namespace WireMock.Owin.ActivityTracing;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides an ActivitySource for WireMock.Net distributed tracing.
|
/// Provides an ActivitySource for WireMock.Net distributed tracing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class WireMockActivitySource
|
internal static class WireMockActivitySource
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the ActivitySource used by WireMock.Net.
|
/// The name of the ActivitySource used by WireMock.Net.
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using System.Net;
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Stef.Validation;
|
|
||||||
using WireMock.Constants;
|
using WireMock.Constants;
|
||||||
using WireMock.Owin;
|
using WireMock.Owin;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
@@ -15,8 +14,6 @@ namespace WireMock.ResponseProviders;
|
|||||||
|
|
||||||
internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponseProvider
|
internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponseProvider
|
||||||
{
|
{
|
||||||
private readonly WebSocketBuilder _builder = Guard.NotNull(builder);
|
|
||||||
|
|
||||||
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(
|
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(
|
||||||
IMapping mapping,
|
IMapping mapping,
|
||||||
HttpContext context,
|
HttpContext context,
|
||||||
@@ -35,13 +32,13 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
|||||||
#if NET8_0_OR_GREATER
|
#if NET8_0_OR_GREATER
|
||||||
var acceptContext = new WebSocketAcceptContext
|
var acceptContext = new WebSocketAcceptContext
|
||||||
{
|
{
|
||||||
SubProtocol = _builder.AcceptProtocol,
|
SubProtocol = builder.AcceptProtocol,
|
||||||
KeepAliveInterval = _builder.KeepAliveIntervalSeconds ?? TimeSpan.FromSeconds(WebSocketConstants.DefaultKeepAliveIntervalSeconds)
|
KeepAliveInterval = builder.KeepAliveIntervalSeconds ?? TimeSpan.FromSeconds(WebSocketConstants.DefaultKeepAliveIntervalSeconds)
|
||||||
|
|
||||||
};
|
};
|
||||||
var webSocket = await context.WebSockets.AcceptWebSocketAsync(acceptContext).ConfigureAwait(false);
|
var webSocket = await context.WebSockets.AcceptWebSocketAsync(acceptContext).ConfigureAwait(false);
|
||||||
#else
|
#else
|
||||||
var webSocket = await context.WebSockets.AcceptWebSocketAsync(_builder.AcceptProtocol).ConfigureAwait(false);
|
var webSocket = await context.WebSockets.AcceptWebSocketAsync(builder.AcceptProtocol).ConfigureAwait(false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Get options from HttpContext.Items (set by WireMockMiddleware)
|
// Get options from HttpContext.Items (set by WireMockMiddleware)
|
||||||
@@ -52,7 +49,7 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get or create registry from options
|
// Get or create registry from options
|
||||||
var registry = _builder.IsBroadcast
|
var registry = builder.IsBroadcast
|
||||||
? options.WebSocketRegistries.GetOrAdd(mapping.Guid, _ => new WebSocketConnectionRegistry())
|
? options.WebSocketRegistries.GetOrAdd(mapping.Guid, _ => new WebSocketConnectionRegistry())
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
@@ -63,7 +60,7 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
|||||||
requestMessage,
|
requestMessage,
|
||||||
mapping,
|
mapping,
|
||||||
registry,
|
registry,
|
||||||
_builder
|
builder
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update scenario state following the same pattern as WireMockMiddleware
|
// Update scenario state following the same pattern as WireMockMiddleware
|
||||||
@@ -73,25 +70,22 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder) : IResponsePr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add to registry if broadcast is enabled
|
// Add to registry if broadcast is enabled
|
||||||
if (registry != null)
|
registry?.AddConnection(wsContext);
|
||||||
{
|
|
||||||
registry.AddConnection(wsContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Handle the WebSocket based on configuration
|
// Handle the WebSocket based on configuration
|
||||||
if (_builder.ProxySettings != null)
|
if (builder.ProxySettings != null)
|
||||||
{
|
{
|
||||||
await HandleProxyAsync(wsContext, _builder.ProxySettings).ConfigureAwait(false);
|
await HandleProxyAsync(wsContext, builder.ProxySettings).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else if (_builder.IsEcho)
|
else if (builder.IsEcho)
|
||||||
{
|
{
|
||||||
await HandleEchoAsync(wsContext).ConfigureAwait(false);
|
await HandleEchoAsync(wsContext).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else if (_builder.MessageHandler != null)
|
else if (builder.MessageHandler != null)
|
||||||
{
|
{
|
||||||
await HandleCustomAsync(wsContext, _builder.MessageHandler).ConfigureAwait(false);
|
await HandleCustomAsync(wsContext, builder.MessageHandler).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user