From bb456fbf9a35eda9010144e8951091f610ed3fea Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 22 Dec 2024 17:28:50 +0100 Subject: [PATCH 1/2] Add functionality to call a PostTransform method after the Webhook request has been transformed --- src/WireMock.Net/Http/WebhookSender.cs | 12 +++++++----- src/WireMock.Net/Settings/WebhookSettings.cs | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/WireMock.Net/Http/WebhookSender.cs b/src/WireMock.Net/Http/WebhookSender.cs index 95947e45..03087955 100644 --- a/src/WireMock.Net/Http/WebhookSender.cs +++ b/src/WireMock.Net/Http/WebhookSender.cs @@ -46,7 +46,7 @@ IResponseMessage originalResponseMessage IBodyData? bodyData; IDictionary>? headers; - string webhookRequestUrl; + string requestUrl; if (webhookRequest.UseTransformer == true) { ITransformer transformer; @@ -69,18 +69,20 @@ IResponseMessage originalResponseMessage bodyData = transformer.TransformBody(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.BodyData, webhookRequest.TransformerReplaceNodeOptions); headers = transformer.TransformHeaders(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.Headers); - webhookRequestUrl = transformer.TransformString(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.Url); + requestUrl = transformer.TransformString(mapping, originalRequestMessage, originalResponseMessage, webhookRequest.Url); } else { bodyData = webhookRequest.BodyData; headers = webhookRequest.Headers; - webhookRequestUrl = webhookRequest.Url; + requestUrl = webhookRequest.Url; } + mapping.Settings.WebhookSettings?.PostTransform(mapping, requestUrl, bodyData, headers); + // Create RequestMessage var requestMessage = new RequestMessage( - new UrlDetails(webhookRequestUrl), + new UrlDetails(requestUrl), webhookRequest.Method, ClientIp, bodyData, @@ -91,7 +93,7 @@ IResponseMessage originalResponseMessage }; // Create HttpRequestMessage - var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, webhookRequestUrl); + var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, requestUrl); // Delay (if required) if (TryGetDelay(webhookRequest, out var delay)) diff --git a/src/WireMock.Net/Settings/WebhookSettings.cs b/src/WireMock.Net/Settings/WebhookSettings.cs index 79960033..3c02eda2 100644 --- a/src/WireMock.Net/Settings/WebhookSettings.cs +++ b/src/WireMock.Net/Settings/WebhookSettings.cs @@ -1,5 +1,9 @@ // Copyright © WireMock.Net +using System.Collections.Generic; +using WireMock.Types; +using WireMock.Util; + namespace WireMock.Settings; /// @@ -7,4 +11,14 @@ namespace WireMock.Settings; /// public class WebhookSettings : HttpClientSettings { + /// + /// Executes an action after the transformation of the request body. + /// + /// The mapping used for the request. + /// The request Url. + /// The body data of the request. [Optional] + /// The headers of the request. [Optional] + public virtual void PostTransform(IMapping mapping, string requestUrl, IBodyData? bodyData = null, IDictionary>? headers = null) + { + } } \ No newline at end of file From d2d4f5a1d54630ee9edad4ecc7f6d74f2bfc803f Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 22 Dec 2024 17:30:53 +0100 Subject: [PATCH 2/2] UseTransformer == true --- src/WireMock.Net/Http/WebhookSender.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WireMock.Net/Http/WebhookSender.cs b/src/WireMock.Net/Http/WebhookSender.cs index 03087955..606dadd0 100644 --- a/src/WireMock.Net/Http/WebhookSender.cs +++ b/src/WireMock.Net/Http/WebhookSender.cs @@ -70,6 +70,8 @@ IResponseMessage originalResponseMessage 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 { @@ -78,8 +80,6 @@ IResponseMessage originalResponseMessage requestUrl = webhookRequest.Url; } - mapping.Settings.WebhookSettings?.PostTransform(mapping, requestUrl, bodyData, headers); - // Create RequestMessage var requestMessage = new RequestMessage( new UrlDetails(requestUrl),