mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-06-08 14:52:51 +02:00
a292f28dda
* 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
94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using Stef.Validation;
|
|
using WireMock.Models;
|
|
using WireMock.Settings;
|
|
using WireMock.Transformers;
|
|
using WireMock.Types;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Http;
|
|
|
|
internal class WebhookSender(WireMockServerSettings settings)
|
|
{
|
|
private const string ClientIp = "::1";
|
|
private static readonly ThreadLocal<Random> Random = new(() => new Random(DateTime.UtcNow.Millisecond));
|
|
|
|
private readonly WireMockServerSettings _settings = Guard.NotNull(settings);
|
|
|
|
public async Task<HttpResponseMessage> SendAsync(
|
|
HttpClient client,
|
|
IMapping mapping,
|
|
IWebhookRequest webhookRequest,
|
|
IRequestMessage originalRequestMessage,
|
|
IResponseMessage originalResponseMessage
|
|
)
|
|
{
|
|
Guard.NotNull(client);
|
|
Guard.NotNull(mapping);
|
|
Guard.NotNull(webhookRequest);
|
|
Guard.NotNull(originalRequestMessage);
|
|
Guard.NotNull(originalResponseMessage);
|
|
|
|
IBodyData? bodyData;
|
|
IDictionary<string, WireMockList<string>>? headers;
|
|
string requestUrl;
|
|
if (webhookRequest.UseTransformer == true)
|
|
{
|
|
var transformer = TransformerFactory.Create(webhookRequest.TransformerType, _settings);
|
|
bodyData = transformer.TransformBody(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.BodyData, webhookRequest.TransformerReplaceNodeOptions);
|
|
headers = transformer.TransformHeaders(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.Headers);
|
|
requestUrl = transformer.TransformString(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.Url);
|
|
|
|
mapping.Settings.WebhookSettings?.PostTransform(mapping, requestUrl, bodyData, headers);
|
|
}
|
|
else
|
|
{
|
|
bodyData = webhookRequest.BodyData;
|
|
headers = webhookRequest.Headers;
|
|
requestUrl = webhookRequest.Url;
|
|
}
|
|
|
|
// Create RequestMessage
|
|
var requestMessage = new RequestMessage(
|
|
new UrlDetails(requestUrl),
|
|
webhookRequest.Method,
|
|
ClientIp,
|
|
bodyData,
|
|
headers?.ToDictionary(x => x.Key, x => x.Value.ToArray())
|
|
)
|
|
{
|
|
DateTime = DateTime.UtcNow
|
|
};
|
|
|
|
// Create HttpRequestMessage
|
|
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, requestUrl);
|
|
|
|
// Delay (if required)
|
|
if (TryGetDelay(webhookRequest, out var delay))
|
|
{
|
|
await Task.Delay(delay.Value).ConfigureAwait(false);
|
|
}
|
|
|
|
// Call the URL
|
|
return await client.SendAsync(httpRequestMessage).ConfigureAwait(false);
|
|
}
|
|
|
|
private static bool TryGetDelay(IWebhookRequest webhookRequest, [NotNullWhen(true)] out int? delay)
|
|
{
|
|
delay = webhookRequest.Delay;
|
|
var minimumDelay = webhookRequest.MinimumRandomDelay;
|
|
var maximumDelay = webhookRequest.MaximumRandomDelay;
|
|
|
|
if (minimumDelay is not null && maximumDelay is not null && maximumDelay >= minimumDelay)
|
|
{
|
|
delay = Random.Value!.Next(minimumDelay.Value, maximumDelay.Value);
|
|
return true;
|
|
}
|
|
|
|
return delay is not null;
|
|
}
|
|
} |