mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 02:41:53 +01:00
Version 2.x (#1359)
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * 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 * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
This commit is contained in:
@@ -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(bufferSize);
|
||||
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(bufferSize);
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user