mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 02:41:53 +01:00
WebHook : UseFireAndForget + Delay (#803)
* UseFireAndForget * ... * delay * async * updated code accorsing to proposal * Change nuget to package reference for WireMock.Net.Console.Net472.Classic, move the new FireAndForget into the main mapping, out of individual webhook mappings making it all or nothing, update tests, change Middleware to await or not the firing of all webhooks. Update models as needed. (#804) Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com> * small update * Tweak middleware and fix bug in example (#806) Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com> * .ConfigureAwait(false) Co-authored-by: mattisking <mattisking@gmail.com> Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com>
This commit is contained in:
@@ -74,4 +74,9 @@ public class MappingModel
|
||||
/// The Webhooks.
|
||||
/// </summary>
|
||||
public WebhookModel[]? Webhooks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fire and forget for webhooks.
|
||||
/// </summary>
|
||||
public bool? UseWebhooksFireAndForget { get; set; }
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
namespace WireMock.Admin.Mappings
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// The Webhook
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class WebhookModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The Webhook
|
||||
/// The Webhook Request.
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class WebhookModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The Webhook Request.
|
||||
/// </summary>
|
||||
public WebhookRequestModel Request { get; set; }
|
||||
}
|
||||
public WebhookRequestModel Request { get; set; } = null!;
|
||||
}
|
||||
@@ -11,12 +11,12 @@ public class WebhookRequestModel
|
||||
/// <summary>
|
||||
/// Gets or sets the Url.
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
public string Url { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// The method
|
||||
/// </summary>
|
||||
public string Method { get; set; }
|
||||
public string Method { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the headers.
|
||||
@@ -47,4 +47,19 @@ public class WebhookRequestModel
|
||||
/// The ReplaceNodeOptions to use when transforming a JSON node.
|
||||
/// </summary>
|
||||
public string? TransformerReplaceNodeOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? Delay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the minimum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MinimumRandomDelay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MaximumRandomDelay { get; set; }
|
||||
}
|
||||
@@ -2,46 +2,60 @@ using System.Collections.Generic;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Models
|
||||
namespace WireMock.Models;
|
||||
|
||||
/// <summary>
|
||||
/// IWebhookRequest
|
||||
/// </summary>
|
||||
public interface IWebhookRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// IWebhookRequest
|
||||
/// The Webhook Url.
|
||||
/// </summary>
|
||||
public interface IWebhookRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// The Webhook Url.
|
||||
/// </summary>
|
||||
string Url { get; set; }
|
||||
string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The method to use.
|
||||
/// </summary>
|
||||
string Method { get; set; }
|
||||
/// <summary>
|
||||
/// The method to use.
|
||||
/// </summary>
|
||||
string Method { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Headers to send.
|
||||
/// </summary>
|
||||
IDictionary<string, WireMockList<string>>? Headers { get; }
|
||||
/// <summary>
|
||||
/// The Headers to send.
|
||||
/// </summary>
|
||||
IDictionary<string, WireMockList<string>>? Headers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The body to send.
|
||||
/// </summary>
|
||||
IBodyData? BodyData { get; set; }
|
||||
/// <summary>
|
||||
/// The body to send.
|
||||
/// </summary>
|
||||
IBodyData? BodyData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Use Transformer.
|
||||
/// </summary>
|
||||
bool? UseTransformer { get; set; }
|
||||
/// <summary>
|
||||
/// Use Transformer.
|
||||
/// </summary>
|
||||
bool? UseTransformer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The transformer type.
|
||||
/// </summary>
|
||||
TransformerType TransformerType { get; set; }
|
||||
/// <summary>
|
||||
/// The transformer type.
|
||||
/// </summary>
|
||||
TransformerType TransformerType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ReplaceNodeOptions to use when transforming a JSON node.
|
||||
/// </summary>
|
||||
ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// The ReplaceNodeOptions to use when transforming a JSON node.
|
||||
/// </summary>
|
||||
ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delay in milliseconds.
|
||||
/// </summary>
|
||||
int? Delay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the minimum random delay in milliseconds.
|
||||
/// </summary>
|
||||
int? MinimumRandomDelay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum random delay in milliseconds.
|
||||
/// </summary>
|
||||
int? MaximumRandomDelay { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Stef.Validation;
|
||||
using WireMock.Models;
|
||||
@@ -17,6 +19,7 @@ namespace WireMock.Http;
|
||||
internal class WebhookSender
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
private static readonly ThreadLocal<Random> Random = new(() => new Random(DateTime.UtcNow.Millisecond));
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
|
||||
@@ -25,20 +28,26 @@ internal class WebhookSender
|
||||
_settings = Guard.NotNull(settings);
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> SendAsync(HttpClient client, IMapping mapping, IWebhookRequest request, IRequestMessage originalRequestMessage, IResponseMessage originalResponseMessage)
|
||||
public async Task<HttpResponseMessage> SendAsync(
|
||||
HttpClient client,
|
||||
IMapping mapping,
|
||||
IWebhookRequest webhookRequest,
|
||||
IRequestMessage originalRequestMessage,
|
||||
IResponseMessage originalResponseMessage
|
||||
)
|
||||
{
|
||||
Guard.NotNull(client);
|
||||
Guard.NotNull(mapping);
|
||||
Guard.NotNull(request);
|
||||
Guard.NotNull(webhookRequest);
|
||||
Guard.NotNull(originalRequestMessage);
|
||||
Guard.NotNull(originalResponseMessage);
|
||||
|
||||
IBodyData? bodyData;
|
||||
IDictionary<string, WireMockList<string>>? headers;
|
||||
if (request.UseTransformer == true)
|
||||
if (webhookRequest.UseTransformer == true)
|
||||
{
|
||||
ITransformer responseMessageTransformer;
|
||||
switch (request.TransformerType)
|
||||
switch (webhookRequest.TransformerType)
|
||||
{
|
||||
case TransformerType.Handlebars:
|
||||
var factoryHandlebars = new HandlebarsContextFactory(_settings.FileSystemHandler, _settings.HandlebarsRegistrationCallback);
|
||||
@@ -47,26 +56,26 @@ internal class WebhookSender
|
||||
|
||||
case TransformerType.Scriban:
|
||||
case TransformerType.ScribanDotLiquid:
|
||||
var factoryDotLiquid = new ScribanContextFactory(_settings.FileSystemHandler, request.TransformerType);
|
||||
var factoryDotLiquid = new ScribanContextFactory(_settings.FileSystemHandler, webhookRequest.TransformerType);
|
||||
responseMessageTransformer = new Transformer(factoryDotLiquid);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException($"TransformerType '{request.TransformerType}' is not supported.");
|
||||
throw new NotImplementedException($"TransformerType '{webhookRequest.TransformerType}' is not supported.");
|
||||
}
|
||||
|
||||
(bodyData, headers) = responseMessageTransformer.Transform(mapping, originalRequestMessage, originalResponseMessage, request.BodyData, request.Headers, request.TransformerReplaceNodeOptions);
|
||||
(bodyData, headers) = responseMessageTransformer.Transform(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.BodyData, webhookRequest.Headers, webhookRequest.TransformerReplaceNodeOptions);
|
||||
}
|
||||
else
|
||||
{
|
||||
bodyData = request.BodyData;
|
||||
headers = request.Headers;
|
||||
bodyData = webhookRequest.BodyData;
|
||||
headers = webhookRequest.Headers;
|
||||
}
|
||||
|
||||
// Create RequestMessage
|
||||
var requestMessage = new RequestMessage(
|
||||
new UrlDetails(request.Url),
|
||||
request.Method,
|
||||
new UrlDetails(webhookRequest.Url),
|
||||
webhookRequest.Method,
|
||||
ClientIp,
|
||||
bodyData,
|
||||
headers?.ToDictionary(x => x.Key, x => x.Value.ToArray())
|
||||
@@ -76,9 +85,30 @@ internal class WebhookSender
|
||||
};
|
||||
|
||||
// Create HttpRequestMessage
|
||||
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, request.Url);
|
||||
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, webhookRequest.Url);
|
||||
|
||||
// Delay (if required)
|
||||
if (TryGetDelay(webhookRequest, out var delay))
|
||||
{
|
||||
await Task.Delay(delay.Value).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// Call the URL
|
||||
return client.SendAsync(httpRequestMessage);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,11 @@ public interface IMapping
|
||||
/// </summary>
|
||||
IWebhook[]? Webhooks { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Use Fire and Forget for the defined webhook(s). [Optional]
|
||||
/// </summary>
|
||||
public bool? UseWebhooksFireAndForget { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ProvideResponseAsync
|
||||
/// </summary>
|
||||
|
||||
@@ -63,6 +63,9 @@ public class Mapping : IMapping
|
||||
/// <inheritdoc />
|
||||
public IWebhook[]? Webhooks { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool? UseWebhooksFireAndForget { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ITimeSettings? TimeSettings { get; }
|
||||
|
||||
@@ -82,6 +85,7 @@ public class Mapping : IMapping
|
||||
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
|
||||
/// <param name="stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]</param>
|
||||
/// <param name="webhooks">The Webhooks. [Optional]</param>
|
||||
/// <param name="useWebhooksFireAndForget">Use Fire and Forget for the defined webhook(s). [Optional]</param>
|
||||
/// <param name="timeSettings">The TimeSettings. [Optional]</param>
|
||||
public Mapping(
|
||||
Guid guid,
|
||||
@@ -97,6 +101,7 @@ public class Mapping : IMapping
|
||||
string? nextState,
|
||||
int? stateTimes,
|
||||
IWebhook[]? webhooks,
|
||||
bool? useWebhooksFireAndForget,
|
||||
ITimeSettings? timeSettings)
|
||||
{
|
||||
Guid = guid;
|
||||
@@ -112,6 +117,7 @@ public class Mapping : IMapping
|
||||
NextState = nextState;
|
||||
StateTimes = stateTimes;
|
||||
Webhooks = webhooks;
|
||||
UseWebhooksFireAndForget = useWebhooksFireAndForget;
|
||||
TimeSettings = timeSettings;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
||||
/// <summary>
|
||||
/// The key
|
||||
/// </summary>
|
||||
public string? Key { get; }
|
||||
public string Key { get; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Defines if the key should be matched using case-ignore.
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
namespace WireMock.Models
|
||||
namespace WireMock.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Webhook
|
||||
/// </summary>
|
||||
public class Webhook : IWebhook
|
||||
{
|
||||
/// <summary>
|
||||
/// Webhook
|
||||
/// </summary>
|
||||
public class Webhook : IWebhook
|
||||
{
|
||||
/// <inheritdoc cref="IWebhook.Request"/>
|
||||
public IWebhookRequest Request { get; set; }
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public IWebhookRequest Request { get; set; } = null!;
|
||||
}
|
||||
@@ -2,32 +2,40 @@ using System.Collections.Generic;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Models
|
||||
namespace WireMock.Models;
|
||||
|
||||
/// <summary>
|
||||
/// WebhookRequest
|
||||
/// </summary>
|
||||
public class WebhookRequest : IWebhookRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// WebhookRequest
|
||||
/// </summary>
|
||||
public class WebhookRequest : IWebhookRequest
|
||||
{
|
||||
/// <inheritdoc cref="IWebhookRequest.Url"/>
|
||||
public string Url { get; set; }
|
||||
/// <inheritdoc />
|
||||
public string Url { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.Method"/>
|
||||
public string Method { get; set; }
|
||||
/// <inheritdoc />
|
||||
public string Method { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.Headers"/>
|
||||
public IDictionary<string, WireMockList<string>>? Headers { get; set; }
|
||||
/// <inheritdoc />
|
||||
public IDictionary<string, WireMockList<string>>? Headers { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.BodyData"/>
|
||||
public IBodyData? BodyData { get; set; }
|
||||
/// <inheritdoc />
|
||||
public IBodyData? BodyData { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.UseTransformer"/>
|
||||
public bool? UseTransformer { get; set; }
|
||||
/// <inheritdoc />
|
||||
public bool? UseTransformer { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.TransformerType"/>
|
||||
public TransformerType TransformerType { get; set; }
|
||||
/// <inheritdoc />
|
||||
public TransformerType TransformerType { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWebhookRequest.TransformerReplaceNodeOptions"/>
|
||||
public ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public ReplaceNodeOptions TransformerReplaceNodeOptions { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int? Delay { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int? MinimumRandomDelay { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int? MaximumRandomDelay { get; set; }
|
||||
}
|
||||
@@ -11,8 +11,8 @@ using WireMock.Serialization;
|
||||
using WireMock.Types;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using System.Collections.Generic;
|
||||
#if !USE_ASPNETCORE
|
||||
using Microsoft.Owin;
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using OwinMiddleware = Microsoft.Owin.OwinMiddleware;
|
||||
using Next = Microsoft.Owin.OwinMiddleware;
|
||||
@@ -161,6 +161,7 @@ namespace WireMock.Owin
|
||||
_options.Logger.Error($"Providing a Response for Mapping '{result.Match?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
|
||||
response = ResponseMessageBuilder.Create(ex.Message, 500);
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
var log = new LogEntry
|
||||
@@ -201,20 +202,46 @@ namespace WireMock.Owin
|
||||
|
||||
private async Task SendToWebhooksAsync(IMapping mapping, IRequestMessage request, IResponseMessage response)
|
||||
{
|
||||
var tasks = new List<Func<Task>>();
|
||||
for (int index = 0; index < mapping.Webhooks?.Length; index++)
|
||||
{
|
||||
var httpClientForWebhook = HttpClientBuilder.Build(mapping.Settings.WebhookSettings ?? new WebhookSettings());
|
||||
var webhookSender = new WebhookSender(mapping.Settings);
|
||||
var webhookRequest = mapping.Webhooks[index].Request;
|
||||
var webHookIndex = index;
|
||||
|
||||
tasks.Add(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await webhookSender.SendAsync(httpClientForWebhook, mapping, webhookRequest, request, response).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_options.Logger.Error($"Sending message to Webhook [{webHookIndex}] from Mapping '{mapping.Guid}' failed. Exception: {ex}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (mapping.UseWebhooksFireAndForget == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await webhookSender.SendAsync(httpClientForWebhook, mapping, mapping.Webhooks[index].Request, request, response).ConfigureAwait(false);
|
||||
// Do not wait
|
||||
await Task.Run(() =>
|
||||
{
|
||||
Task.WhenAll(tasks.Select(async task => await task.Invoke())).ConfigureAwait(false);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
_options.Logger.Error($"Sending message to Webhook [{index}] from Mapping '{mapping.Guid}' failed. Exception: {ex}");
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.WhenAll(tasks.Select(async task => await task.Invoke())).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateScenarioState(IMapping mapping)
|
||||
|
||||
@@ -119,6 +119,7 @@ internal class ProxyHelper
|
||||
nextState: null,
|
||||
stateTimes: null,
|
||||
webhooks: null,
|
||||
useWebhooksFireAndForget: null,
|
||||
timeSettings: null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ internal class MappingConverter
|
||||
TimeSettings = TimeSettingsMapper.Map(mapping.TimeSettings),
|
||||
Title = mapping.Title,
|
||||
Description = mapping.Description,
|
||||
UseWebhooksFireAndForget = mapping.UseWebhooksFireAndForget,
|
||||
Priority = mapping.Priority != 0 ? mapping.Priority : null,
|
||||
Scenario = mapping.Scenario,
|
||||
WhenStateIs = mapping.ExecutionConditionState,
|
||||
@@ -168,7 +169,7 @@ internal class MappingConverter
|
||||
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
|
||||
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
|
||||
|
||||
if (response.ResponseMessage.Headers != null && response.ResponseMessage.Headers.Count > 0)
|
||||
if (response.ResponseMessage.Headers is { Count: > 0 })
|
||||
{
|
||||
mappingModel.Response.Headers = MapHeaders(response.ResponseMessage.Headers);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ internal static class WebhookMapper
|
||||
{
|
||||
Url = model.Request.Url,
|
||||
Method = model.Request.Method,
|
||||
Delay = model.Request.Delay,
|
||||
MinimumRandomDelay = model.Request.MinimumRandomDelay,
|
||||
MaximumRandomDelay = model.Request.MaximumRandomDelay,
|
||||
Headers = model.Request.Headers?.ToDictionary(x => x.Key, x => new WireMockList<string>(x.Value)) ?? new Dictionary<string, WireMockList<string>>()
|
||||
}
|
||||
};
|
||||
@@ -27,6 +30,7 @@ internal static class WebhookMapper
|
||||
if (model.Request.UseTransformer == true)
|
||||
{
|
||||
webhook.Request.UseTransformer = true;
|
||||
|
||||
if (!Enum.TryParse<TransformerType>(model.Request.TransformerType, out var transformerType))
|
||||
{
|
||||
transformerType = TransformerType.Handlebars;
|
||||
@@ -37,7 +41,6 @@ internal static class WebhookMapper
|
||||
{
|
||||
option = ReplaceNodeOptions.None;
|
||||
}
|
||||
|
||||
webhook.Request.TransformerReplaceNodeOptions = option;
|
||||
}
|
||||
|
||||
@@ -72,7 +75,7 @@ internal static class WebhookMapper
|
||||
public static WebhookModel Map(IWebhook webhook)
|
||||
{
|
||||
Guard.NotNull(webhook);
|
||||
|
||||
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
@@ -82,7 +85,10 @@ internal static class WebhookMapper
|
||||
Headers = webhook.Request.Headers?.ToDictionary(x => x.Key, x => x.Value.ToString()),
|
||||
UseTransformer = webhook.Request.UseTransformer,
|
||||
TransformerType = webhook.Request.UseTransformer == true ? webhook.Request.TransformerType.ToString() : null,
|
||||
TransformerReplaceNodeOptions = webhook.Request.TransformerReplaceNodeOptions.ToString()
|
||||
TransformerReplaceNodeOptions = webhook.Request.TransformerReplaceNodeOptions.ToString(),
|
||||
Delay = webhook.Request.Delay,
|
||||
MinimumRandomDelay = webhook.Request.MinimumRandomDelay,
|
||||
MaximumRandomDelay = webhook.Request.MaximumRandomDelay,
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -123,6 +123,13 @@ namespace WireMock.Server
|
||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||
IRespondWithAProvider WithWebhook(params IWebhook[] webhooks);
|
||||
|
||||
/// <summary>
|
||||
/// Support FireAndForget for any configured Webhooks
|
||||
/// </summary>
|
||||
/// <param name="UseWebhooksFireAndForget"></param>
|
||||
/// <returns></returns>
|
||||
IRespondWithAProvider WithWebhookFireAndForget(bool UseWebhooksFireAndForget);
|
||||
|
||||
/// <summary>
|
||||
/// Add a Webhook to call after the response has been generated.
|
||||
/// </summary>
|
||||
|
||||
@@ -30,6 +30,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly bool _saveToFile;
|
||||
|
||||
private bool _useWebhookFireAndForget = false;
|
||||
|
||||
public Guid Guid { get; private set; } = Guid.NewGuid();
|
||||
|
||||
public IWebhook[]? Webhooks { get; private set; }
|
||||
@@ -57,7 +59,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
/// <param name="provider">The provider.</param>
|
||||
public void RespondWith(IResponseProvider provider)
|
||||
{
|
||||
_registrationCallback(new Mapping(Guid, _title, _description, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, TimeSettings), _saveToFile);
|
||||
_registrationCallback(new Mapping(Guid, _title, _description, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, _useWebhookFireAndForget, TimeSettings), _saveToFile);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -233,6 +235,13 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
return this;
|
||||
}
|
||||
|
||||
public IRespondWithAProvider WithWebhookFireAndForget(bool useWebhooksFireAndForget)
|
||||
{
|
||||
_useWebhookFireAndForget = useWebhooksFireAndForget;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private static IWebhook InitWebhook(
|
||||
string url,
|
||||
string method,
|
||||
|
||||
Reference in New Issue
Block a user