Compare commits

...

8 Commits

Author SHA1 Message Date
Stef Heyenrath
f6c5225fe0 1.14.0 2025-10-06 17:00:19 +02:00
Stef Heyenrath
b9019a2f61 Update ProxyUrlReplaceSettingsModel with TransformTemplate property (#1362)
* Update ProxyUrlReplaceSettingsModel with TransformTemplate property + parse settings correctly

* oldValue nullable

* <Version>1.14.0-preview-01</Version>
2025-10-06 09:16:25 +02:00
Stef Heyenrath
b82dad2563 Add Tls13 (#1363)
* Add Tls13

* fix
2025-10-05 15:51:47 +02:00
Stef Heyenrath
45d4e7077d 1.13.0 2025-09-28 12:44:14 +02:00
Stef Heyenrath
19e95325fa ProxyUrlTransformer (#1361)
* ProxyUrlTransformer

* tests

* Update src/WireMock.Net.Shared/Settings/ProxyUrlReplaceSettings.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-09-28 12:40:33 +02:00
Stef Heyenrath
371bfdc160 TypeLoader: implement Try methods (#1358)
* TypeLoader: implement Try methods

* fix
2025-08-31 08:48:29 +02:00
Stef Heyenrath
5c5e104f2c 1.12.0 2025-08-30 11:49:20 +02:00
Stef Heyenrath
068fdf33e3 Upgrade Testcontainers to 4.7.0 (#1357)
* Upgrade Testcontainers to 4.7.0

* .
2025-08-30 11:46:52 +02:00
32 changed files with 530 additions and 178 deletions

View File

@@ -1,3 +1,17 @@
# 1.14.0 (06 October 2025)
- [#1362](https://github.com/wiremock/WireMock.Net/pull/1362) - Update ProxyUrlReplaceSettingsModel with TransformTemplate property [feature] contributed by [StefH](https://github.com/StefH)
- [#1363](https://github.com/wiremock/WireMock.Net/pull/1363) - Add Tls13 [bug] contributed by [StefH](https://github.com/StefH)
- [#1342](https://github.com/wiremock/WireMock.Net/issues/1342) - Facing SSL certificate validation error when using .NET 5 and above framework for some http services during recording [bug]
- [#1360](https://github.com/wiremock/WireMock.Net/issues/1360) - ProxyURL path configuration [feature]
# 1.13.0 (28 September 2025)
- [#1358](https://github.com/wiremock/WireMock.Net/pull/1358) - TypeLoader: implement Try methods [feature] contributed by [StefH](https://github.com/StefH)
- [#1361](https://github.com/wiremock/WireMock.Net/pull/1361) - ProxyUrlTransformer [feature] contributed by [StefH](https://github.com/StefH)
# 1.12.0 (30 August 2025)
- [#1357](https://github.com/wiremock/WireMock.Net/pull/1357) - Upgrade Testcontainers to 4.7.0 [feature] contributed by [StefH](https://github.com/StefH)
- [#1356](https://github.com/wiremock/WireMock.Net/issues/1356) - WireMock.Net 1.11.2 is not compatible with TestContainers 4.7.0 [bug]
# 1.11.2 (27 August 2025)
- [#1352](https://github.com/wiremock/WireMock.Net/pull/1352) - Add additional try-catch to TypeLoader logic [feature] contributed by [StefH](https://github.com/StefH)
- [#1354](https://github.com/wiremock/WireMock.Net/pull/1354) - Add System.Net.Http again to solve CVE warning [bug] contributed by [StefH](https://github.com/StefH)

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<Version>1.11.2</Version>
<Version>1.14.0</Version>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

View File

@@ -1,6 +1,6 @@
rem https://github.com/StefH/GitHubReleaseNotes
SET version=1.11.2
SET version=1.14.0
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --version %version% --token %GH_TOKEN%

View File

@@ -1,9 +1,7 @@
# 1.11.2 (27 August 2025)
- #1352 Add additional try-catch to TypeLoader logic [feature]
- #1354 Add System.Net.Http again to solve CVE warning [bug]
- #1355 Revert JetBrains.Annotations and add System.Text.RegularExpressions t&#8230; [bug]
- #1071 Split WireMock into multiple nuget packages depending on features [feature]
- #1351 Version 1.11.0 seems to have a dependency to non-existing version of JetBrans.Annotations [bug]
- #1353 1.11.0: NU1903: Warning As Error: Package 'System.Net.Http' 4.3.0 has a known high severity vulnerability, https://github.com/advisories/GHSA-7jgj-8wvc-jh57 [bug]
# 1.14.0 (06 October 2025)
- #1362 Update ProxyUrlReplaceSettingsModel with TransformTemplate property [feature]
- #1363 Add Tls13 [bug]
- #1342 Facing SSL certificate validation error when using .NET 5 and above framework for some http services during recording [bug]
- #1360 ProxyURL path configuration [feature]
The full release notes can be found here: https://github.com/wiremock/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -1,5 +1,7 @@
// Copyright © WireMock.Net
using WireMock.Types;
namespace WireMock.Admin.Settings;
/// <summary>
@@ -11,15 +13,25 @@ public class ProxyUrlReplaceSettingsModel
/// <summary>
/// The old path value to be replaced by the new path value
/// </summary>
public string OldValue { get; set; } = null!;
public string? OldValue { get; set; }
/// <summary>
/// The new path value to replace the old value with
/// </summary>
public string NewValue { get; set; } = null!;
public string? NewValue { get; set; }
/// <summary>
/// Defines if the case should be ignore when replacing.
/// Defines if the case should be ignored when replacing.
/// </summary>
public bool IgnoreCase { get; set; }
/// <summary>
/// Holds the transformation template.
/// </summary>
public string? TransformTemplate { get; set; }
/// <summary>
/// The transformer type.
/// </summary>
public TransformerType TransformerType { get; set; } = TransformerType.Handlebars;
}

View File

@@ -1,7 +1,6 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
@@ -33,16 +32,25 @@ internal class MimeKitUtils : IMimeKitUtils
StartsWithMultiPart(contentTypeHeader)
)
{
var bytes = requestMessage.BodyData?.DetectedBodyType switch
byte[] bytes;
switch (requestMessage.BodyData?.DetectedBodyType)
{
// If the body is bytes, use the BodyAsBytes to match on.
BodyType.Bytes => requestMessage.BodyData.BodyAsBytes!,
case BodyType.Bytes:
bytes = requestMessage.BodyData.BodyAsBytes!;
break;
// If the body is a String or MultiPart, use the BodyAsString to match on.
BodyType.String or BodyType.MultiPart => Encoding.UTF8.GetBytes(requestMessage.BodyData.BodyAsString!),
case BodyType.String or BodyType.MultiPart:
bytes = Encoding.UTF8.GetBytes(requestMessage.BodyData.BodyAsString!);
break;
_ => throw new NotSupportedException()
};
// Else return false.
default:
mimeMessageData = null;
return false;
}
var fixedBytes = FixBytes(bytes, contentTypeHeader[0]);

View File

@@ -15,8 +15,13 @@ internal static class HttpClientBuilder
var handler = new HttpClientHandler
{
CheckCertificateRevocationList = false,
#if NET5_0_OR_GREATER
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
#else
SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
#endif
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
#elif NET46
@@ -62,7 +67,10 @@ internal static class HttpClientBuilder
}
}
#if !NETSTANDARD1_3
#if NET5_0_OR_GREATER
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = (message, cert, chain, errors) => true;
#elif !NETSTANDARD1_3
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = (message, cert, chain, errors) => true;
#endif

View File

@@ -11,24 +11,17 @@ using Stef.Validation;
using WireMock.Models;
using WireMock.Settings;
using WireMock.Transformers;
using WireMock.Transformers.Handlebars;
using WireMock.Transformers.Scriban;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.Http;
internal class WebhookSender
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;
public WebhookSender(WireMockServerSettings settings)
{
_settings = Guard.NotNull(settings);
}
private readonly WireMockServerSettings _settings = Guard.NotNull(settings);
public async Task<HttpResponseMessage> SendAsync(
HttpClient client,
@@ -49,24 +42,7 @@ internal class WebhookSender
string requestUrl;
if (webhookRequest.UseTransformer == true)
{
ITransformer transformer;
switch (webhookRequest.TransformerType)
{
case TransformerType.Handlebars:
var factoryHandlebars = new HandlebarsContextFactory(_settings);
transformer = new Transformer(_settings, factoryHandlebars);
break;
case TransformerType.Scriban:
case TransformerType.ScribanDotLiquid:
var factoryDotLiquid = new ScribanContextFactory(_settings.FileSystemHandler, webhookRequest.TransformerType);
transformer = new Transformer(_settings, factoryDotLiquid);
break;
default:
throw new NotImplementedException($"TransformerType '{webhookRequest.TransformerType}' is not supported.");
}
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);

View File

@@ -12,7 +12,7 @@ namespace WireMock.Matchers.Request;
/// </summary>
public class RequestMessageMultiPartMatcher : IRequestMatcher
{
private static readonly IMimeKitUtils MimeKitUtils = TypeLoader.LoadStaticInstance<IMimeKitUtils>();
private readonly IMimeKitUtils _mimeKitUtils = LoadMimeKitUtils();
/// <summary>
/// The matchers.
@@ -62,7 +62,7 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher
return requestMatchResult.AddScore(GetType(), score, null);
}
if (!MimeKitUtils.TryGetMimeMessage(requestMessage, out var message))
if (!_mimeKitUtils.TryGetMimeMessage(requestMessage, out var message))
{
return requestMatchResult.AddScore(GetType(), score, null);
}
@@ -96,4 +96,14 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher
return requestMatchResult.AddScore(GetType(), score, exception);
}
private static IMimeKitUtils LoadMimeKitUtils()
{
if (TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out var mimeKitUtils))
{
return mimeKitUtils;
}
throw new InvalidOperationException("MimeKit is required for RequestMessageMultiPartMatcher. Please install the WireMock.Net.MimePart package.");
}
}

View File

@@ -178,9 +178,12 @@ namespace WireMock.Owin.Mappers
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
case BodyType.ProtoBuf:
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
var protoBufUtils = TypeLoader.LoadStaticInstance<IProtoBufUtils>();
return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
if (TypeLoader.TryLoadStaticInstance<IProtoBufUtils>(out var protoBufUtils))
{
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
}
break;
case BodyType.Bytes:
return bodyData.BodyAsBytes;

View File

@@ -13,16 +13,10 @@ using WireMock.Util;
namespace WireMock.Proxy;
internal class ProxyHelper
internal class ProxyHelper(WireMockServerSettings settings)
{
private readonly WireMockServerSettings _settings;
private readonly ProxyMappingConverter _proxyMappingConverter;
public ProxyHelper(WireMockServerSettings settings)
{
_settings = Guard.NotNull(settings);
_proxyMappingConverter = new ProxyMappingConverter(settings, new GuidUtils(), new DateTimeUtils());
}
private readonly WireMockServerSettings _settings = Guard.NotNull(settings);
private readonly ProxyMappingConverter _proxyMappingConverter = new(settings, new GuidUtils(), new DateTimeUtils());
public async Task<(IResponseMessage Message, IMapping? Mapping)> SendAsync(
IMapping? mapping,
@@ -39,18 +33,7 @@ internal class ProxyHelper
var requiredUri = new Uri(url);
// Create HttpRequestMessage
var replaceSettings = proxyAndRecordSettings.ReplaceSettings;
string proxyUrl;
if (replaceSettings is not null)
{
var stringComparison = replaceSettings.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
proxyUrl = url.Replace(replaceSettings.OldValue, replaceSettings.NewValue, stringComparison);
}
else
{
proxyUrl = url;
}
var proxyUrl = proxyAndRecordSettings.ReplaceSettings != null ? ProxyUrlTransformer.Transform(_settings, proxyAndRecordSettings.ReplaceSettings, url) : url;
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, proxyUrl);
// Call the URL

View File

@@ -0,0 +1,21 @@
// Copyright © WireMock.Net
using System;
using WireMock.Settings;
using WireMock.Transformers;
namespace WireMock.Proxy;
internal static class ProxyUrlTransformer
{
internal static string Transform(WireMockServerSettings settings, ProxyUrlReplaceSettings replaceSettings, string url)
{
if (!replaceSettings.UseTransformer)
{
return url.Replace(replaceSettings.OldValue, replaceSettings.NewValue, replaceSettings.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
var transformer = TransformerFactory.Create(replaceSettings.TransformerType, settings);
return transformer.Transform(replaceSettings.TransformTemplate, url);
}
}

View File

@@ -183,16 +183,9 @@ public class RequestMessage : IRequestMessage
#endif
#if MIMEKIT
try
if (TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out var mimeKitUtils) && mimeKitUtils.TryGetMimeMessage(this, out var mimeMessage))
{
if (TypeLoader.LoadStaticInstance<IMimeKitUtils>().TryGetMimeMessage(this, out var mimeMessage))
{
BodyAsMimeMessage = mimeMessage;
}
}
catch
{
// Ignore exception from MimeMessage.Load
BodyAsMimeMessage = mimeMessage;
}
#endif
}

View File

@@ -272,25 +272,8 @@ public partial class Response : IResponseBuilder
}
}
ITransformer responseMessageTransformer;
switch (TransformerType)
{
case TransformerType.Handlebars:
var factoryHandlebars = new HandlebarsContextFactory(settings);
responseMessageTransformer = new Transformer(settings, factoryHandlebars);
break;
case TransformerType.Scriban:
case TransformerType.ScribanDotLiquid:
var factoryDotLiquid = new ScribanContextFactory(settings.FileSystemHandler, TransformerType);
responseMessageTransformer = new Transformer(settings, factoryDotLiquid);
break;
default:
throw new NotSupportedException($"TransformerType '{TransformerType}' is not supported.");
}
return (responseMessageTransformer.Transform(mapping, requestMessage, responseMessage, UseTransformerForBodyAsFile, TransformerReplaceNodeOptions), null);
var transformer = TransformerFactory.Create(TransformerType, settings);
return (transformer.Transform(mapping, requestMessage, responseMessage, UseTransformerForBodyAsFile, TransformerReplaceNodeOptions), null);
}
if (!UseTransformer && ResponseMessage.BodyData?.BodyAsFileIsCached == true && responseMessage.BodyData?.BodyAsFile is not null)

View File

@@ -55,7 +55,12 @@ internal class MatcherMapper
case "CSharpCodeMatcher":
if (_settings.AllowCSharpCodeMatcher == true)
{
return TypeLoader.LoadNewInstance<ICSharpCodeMatcher>(matchBehaviour, matchOperator, stringPatterns);
if (TypeLoader.TryLoadNewInstance<ICSharpCodeMatcher>(out var csharpCodeMatcher, matchBehaviour, matchOperator, stringPatterns))
{
return csharpCodeMatcher;
}
throw new InvalidOperationException("The 'CSharpCodeMatcher' cannot be loaded. Please install the WireMock.Net.Matchers.CSharpCode package.");
}
throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because WireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'.");
@@ -75,7 +80,12 @@ internal class MatcherMapper
case "GraphQLMatcher":
var patternAsString = stringPatterns[0].GetPattern();
var schema = new AnyOf<string, StringPattern, ISchemaData>(patternAsString);
return TypeLoader.LoadNewInstance<IGraphQLMatcher>(schema, matcherModel.CustomScalars, matchBehaviour, matchOperator);
if (TypeLoader.TryLoadNewInstance<IGraphQLMatcher>(out var graphQLMatcher, schema, matcherModel.CustomScalars, matchBehaviour, matchOperator))
{
return graphQLMatcher;
}
throw new InvalidOperationException("The 'GraphQLMatcher' cannot be loaded. Please install the WireMock.Net.GraphQL package.");
case "MimePartMatcher":
return CreateMimePartMatcher(matchBehaviour, matcherModel);
@@ -282,18 +292,34 @@ internal class MatcherMapper
var contentTransferEncodingMatcher = Map(matcher.ContentTransferEncodingMatcher) as IStringMatcher;
var contentMatcher = Map(matcher.ContentMatcher);
return TypeLoader.LoadNewInstance<IMimePartMatcher>(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
if (TypeLoader.TryLoadNewInstance<IMimePartMatcher>(
out var mimePartMatcher,
matchBehaviour,
contentTypeMatcher,
contentDispositionMatcher,
contentTransferEncodingMatcher,
contentMatcher))
{
return mimePartMatcher;
}
throw new InvalidOperationException("The 'MimePartMatcher' cannot be loaded. Please install the WireMock.Net.MimePart package.");
}
private IProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, IReadOnlyList<string> protoDefinitions, MatcherModel matcher)
{
var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher;
return TypeLoader.LoadNewInstance<IProtoBufMatcher>(
if (TypeLoader.TryLoadNewInstance<IProtoBufMatcher>(
out var protobufMatcher,
() => ProtoDefinitionUtils.GetIdOrTexts(_settings, protoDefinitions.ToArray()),
matcher.ProtoBufMessageType!,
matchBehaviour ?? MatchBehaviour.AcceptOnMatch,
objectMatcher
);
objectMatcher))
{
return protobufMatcher;
}
throw new InvalidOperationException("The 'ProtoBufMatcher' cannot be loaded. Please install the WireMock.Net.ProtoBuf package.");
}
}

View File

@@ -366,10 +366,8 @@ public partial class WireMockServer
}
else if (responseModel.BodyAsJson != null)
{
if (responseModel.ProtoBufMessageType != null)
if (responseModel.ProtoBufMessageType != null && TypeLoader.TryLoadStaticInstance<IProtoBufUtils>(out var protoBufUtils))
{
var protoBufUtils = TypeLoader.LoadStaticInstance<IProtoBufUtils>();
if (responseModel.ProtoDefinition != null)
{
responseBuilder = protoBufUtils.UpdateResponseBuilder(responseBuilder, responseModel.ProtoBufMessageType, responseModel.BodyAsJson, responseModel.ProtoDefinition);

View File

@@ -202,14 +202,27 @@ public static class WireMockServerSettingsParser
private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleSettingsParser parser)
{
var proxyUrlReplaceOldValue = parser.GetStringValue("ProxyUrlReplaceOldValue");
var proxyUrlReplaceNewValue = parser.GetStringValue("ProxyUrlReplaceNewValue");
const string prefix = "ProxyUrlReplace";
var proxyUrlReplaceOldValue = parser.GetStringValue($"{prefix}OldValue");
var proxyUrlReplaceNewValue = parser.GetStringValue($"{prefix}NewValue");
if (!string.IsNullOrEmpty(proxyUrlReplaceOldValue) && proxyUrlReplaceNewValue != null)
{
settings.ReplaceSettings = new ProxyUrlReplaceSettings
{
OldValue = proxyUrlReplaceOldValue!,
NewValue = proxyUrlReplaceNewValue
OldValue = proxyUrlReplaceOldValue,
NewValue = proxyUrlReplaceNewValue,
IgnoreCase = parser.GetBoolValue($"{prefix}IgnoreCase")
};
return;
}
var transformTemplate = parser.GetStringValue($"{prefix}TransformTemplate");
if (!string.IsNullOrEmpty(transformTemplate))
{
settings.ReplaceSettings = new ProxyUrlReplaceSettings
{
TransformTemplate = transformTemplate,
TransformerType = parser.GetEnumValue($"{prefix}TransformerType", TransformerType.Handlebars)
};
}
}

View File

@@ -6,7 +6,7 @@ using WireMock.Util;
namespace WireMock.Transformers;
interface ITransformer
internal interface ITransformer
{
ResponseMessage Transform(IMapping mapping, IRequestMessage requestMessage, IResponseMessage original, bool useTransformerForBodyAsFile, ReplaceNodeOptions options);
@@ -15,4 +15,6 @@ interface ITransformer
IDictionary<string, WireMockList<string>> TransformHeaders(IMapping mapping, IRequestMessage originalRequestMessage, IResponseMessage originalResponseMessage, IDictionary<string, WireMockList<string>>? headers);
string TransformString(IMapping mapping, IRequestMessage originalRequestMessage, IResponseMessage originalResponseMessage, string? value);
string Transform(string template, object? model);
}

View File

@@ -1,9 +1,8 @@
// Copyright © WireMock.Net
namespace WireMock.Transformers
namespace WireMock.Transformers;
internal interface ITransformerContextFactory
{
interface ITransformerContextFactory
{
ITransformerContext Create();
}
ITransformerContext Create();
}

View File

@@ -75,6 +75,11 @@ internal class Transformer : ITransformer
return transformerContext.ParseAndRender(value, model);
}
public string Transform(string template, object? model)
{
return model is null ? string.Empty : _factory.Create().ParseAndRender(template, model);
}
public ResponseMessage Transform(IMapping mapping, IRequestMessage requestMessage, IResponseMessage original, bool useTransformerForBodyAsFile, ReplaceNodeOptions options)
{
var responseMessage = new ResponseMessage();

View File

@@ -0,0 +1,30 @@
// Copyright © WireMock.Net
using System;
using WireMock.Settings;
using WireMock.Transformers.Handlebars;
using WireMock.Transformers.Scriban;
using WireMock.Types;
namespace WireMock.Transformers;
internal static class TransformerFactory
{
internal static ITransformer Create(TransformerType transformerType, WireMockServerSettings settings)
{
switch (transformerType)
{
case TransformerType.Handlebars:
var factoryHandlebars = new HandlebarsContextFactory(settings);
return new Transformer(settings, factoryHandlebars);
case TransformerType.Scriban:
case TransformerType.ScribanDotLiquid:
var factoryDotLiquid = new ScribanContextFactory(settings.FileSystemHandler, transformerType);
return new Transformer(settings, factoryDotLiquid);
default:
throw new NotSupportedException($"{nameof(TransformerType)} '{transformerType}' is not supported.");
}
}
}

View File

@@ -328,7 +328,7 @@ public interface IWireMockAdminApi
/// <param name="protoDefinition">The ProtoDefinition as text.</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Post("protodefinitions/{id}")]
Task<StatusModel> AddProtoDefinitionAsync([Path] string id, [Body] string body, CancellationToken cancellationToken = default);
Task<StatusModel> AddProtoDefinitionAsync([Path] string id, [Body] string protoDefinition, CancellationToken cancellationToken = default);
/// <summary>
/// Check if a file exists

View File

@@ -100,7 +100,10 @@ public class RequestMessageGraphQLMatcher : IRequestMatcher
IDictionary<string, Type>? customScalars
)
{
var graphQLMatcher = TypeLoader.LoadNewInstance<IGraphQLMatcher>(schema, customScalars, matchBehaviour, MatchOperator.Or);
return [graphQLMatcher];
if (TypeLoader.TryLoadNewInstance<IGraphQLMatcher>(out var graphQLMatcher, schema, customScalars, matchBehaviour, MatchOperator.Or))
{
return [graphQLMatcher];
}
return [];
}
}

View File

@@ -25,7 +25,10 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher
/// <param name="matcher">The optional matcher to use to match the ProtoBuf as (json) object.</param>
public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func<IdOrTexts> protoDefinition, string messageType, IObjectMatcher? matcher = null)
{
Matcher = TypeLoader.LoadNewInstance<IProtoBufMatcher>(protoDefinition, messageType, matchBehaviour, matcher);
if (TypeLoader.TryLoadNewInstance<IProtoBufMatcher>(out var protoBufMatcher, protoDefinition, messageType, matchBehaviour, matcher))
{
Matcher = protoBufMatcher;
}
}
/// <inheritdoc />

View File

@@ -1,5 +1,8 @@
// Copyright © WireMock.Net
using System.Diagnostics.CodeAnalysis;
using WireMock.Types;
namespace WireMock.Settings;
/// <summary>
@@ -8,17 +11,35 @@ namespace WireMock.Settings;
public class ProxyUrlReplaceSettings
{
/// <summary>
/// The old path value to be replaced by the new path value
/// The old path value to be replaced by the new path value.
/// </summary>
public string OldValue { get; set; } = null!;
public string? OldValue { get; set; }
/// <summary>
/// The new path value to replace the old value with
/// The new path value to replace the old value with.
/// </summary>
public string NewValue { get; set; } = null!;
public string? NewValue { get; set; }
/// <summary>
/// Defines if the case should be ignored when replacing.
/// </summary>
public bool IgnoreCase { get; set; }
/// <summary>
/// Holds the transformation template used when <see cref="UseTransformer"/> is true.
/// </summary>
public string? TransformTemplate { get; set; }
/// <summary>
/// Use Transformer.
/// </summary>
[MemberNotNullWhen(true, nameof(TransformTemplate))]
[MemberNotNullWhen(false, nameof(OldValue))]
[MemberNotNullWhen(false, nameof(NewValue))]
public bool UseTransformer => !string.IsNullOrEmpty(TransformTemplate);
/// <summary>
/// The transformer type, in case <see cref="UseTransformer"/> is set to <c>true</c>.
/// </summary>
public TransformerType TransformerType { get; set; } = TransformerType.Handlebars;
}

View File

@@ -14,68 +14,130 @@ internal static class TypeLoader
{
private static readonly ConcurrentDictionary<string, Type> Assemblies = new();
private static readonly ConcurrentDictionary<Type, object> Instances = new();
private static readonly ConcurrentBag<(string FullName, Type Type)> InstancesWhichCannotBeFoundByFullName = [];
private static readonly ConcurrentBag<(string FullName, Type Type)> StaticInstancesWhichCannotBeFoundByFullName = [];
private static readonly ConcurrentBag<Type> InstancesWhichCannotBeFound = [];
private static readonly ConcurrentBag<Type> StaticInstancesWhichCannotBeFound = [];
public static TInterface LoadNewInstance<TInterface>(params object?[] args) where TInterface : class
public static bool TryLoadNewInstance<TInterface>([NotNullWhen(true)] out TInterface? instance, params object?[] args) where TInterface : class
{
var pluginType = GetPluginType<TInterface>();
var type = typeof(TInterface);
if (InstancesWhichCannotBeFound.Contains(type))
{
instance = null;
return false;
}
return (TInterface)Activator.CreateInstance(pluginType, args)!;
if (TryGetPluginType<TInterface>(out var pluginType))
{
instance = (TInterface)Activator.CreateInstance(pluginType, args)!;
return true;
}
InstancesWhichCannotBeFound.Add(type);
instance = null;
return false;
}
public static TInterface LoadStaticInstance<TInterface>(params object?[] args) where TInterface : class
public static bool TryLoadStaticInstance<TInterface>([NotNullWhen(true)] out TInterface? staticInstance, params object?[] args) where TInterface : class
{
var pluginType = GetPluginType<TInterface>();
var type = typeof(TInterface);
if (StaticInstancesWhichCannotBeFound.Contains(type))
{
staticInstance = null;
return false;
}
return (TInterface)Instances.GetOrAdd(pluginType, key => Activator.CreateInstance(key, args)!);
if (TryGetPluginType<TInterface>(out var pluginType))
{
staticInstance = (TInterface)Instances.GetOrAdd(pluginType, key => Activator.CreateInstance(key, args)!);
return true;
}
StaticInstancesWhichCannotBeFound.Add(type);
staticInstance = null;
return false;
}
public static TInterface LoadNewInstanceByFullName<TInterface>(string implementationTypeFullName, params object?[] args) where TInterface : class
public static bool TryLoadNewInstanceByFullName<TInterface>([NotNullWhen(true)] out TInterface? instance, string implementationTypeFullName, params object?[] args) where TInterface : class
{
Guard.NotNullOrEmpty(implementationTypeFullName);
var pluginType = GetPluginTypeByFullName<TInterface>(implementationTypeFullName);
var type = typeof(TInterface);
if (InstancesWhichCannotBeFoundByFullName.Contains((implementationTypeFullName, type)))
{
instance = null;
return false;
}
return (TInterface)Activator.CreateInstance(pluginType, args)!;
if (TryGetPluginTypeByFullName<TInterface>(implementationTypeFullName, out var pluginType))
{
instance = (TInterface)Activator.CreateInstance(pluginType, args)!;
return true;
}
InstancesWhichCannotBeFoundByFullName.Add((implementationTypeFullName, type));
instance = null;
return false;
}
public static TInterface LoadStaticInstanceByFullName<TInterface>(string implementationTypeFullName, params object?[] args) where TInterface : class
public static bool TryLoadStaticInstanceByFullName<TInterface>([NotNullWhen(true)] out TInterface? staticInstance, string implementationTypeFullName, params object?[] args) where TInterface : class
{
Guard.NotNullOrEmpty(implementationTypeFullName);
var pluginType = GetPluginTypeByFullName<TInterface>(implementationTypeFullName);
var type = typeof(TInterface);
if (StaticInstancesWhichCannotBeFoundByFullName.Contains((implementationTypeFullName, type)))
{
staticInstance = null;
return false;
}
return (TInterface)Instances.GetOrAdd(pluginType, key => Activator.CreateInstance(key, args)!);
if (TryGetPluginTypeByFullName<TInterface>(implementationTypeFullName, out var pluginType))
{
staticInstance = (TInterface)Instances.GetOrAdd(pluginType, key => Activator.CreateInstance(key, args)!);
return true;
}
StaticInstancesWhichCannotBeFoundByFullName.Add((implementationTypeFullName, type));
staticInstance = null;
return false;
}
private static Type GetPluginType<TInterface>() where TInterface : class
private static bool TryGetPluginType<TInterface>([NotNullWhen(true)] out Type? foundType) where TInterface : class
{
var key = typeof(TInterface).FullName!;
return Assemblies.GetOrAdd(key, _ =>
if (Assemblies.TryGetValue(key, out foundType))
{
if (TryFindTypeInDlls<TInterface>(null, out var foundType))
{
return foundType;
}
return true;
}
throw new DllNotFoundException($"No dll found which implements interface '{key}'.");
});
if (TryFindTypeInDlls<TInterface>(null, out foundType))
{
Assemblies.TryAdd(key, foundType);
return true;
}
return false;
}
private static Type GetPluginTypeByFullName<TInterface>(string implementationTypeFullName) where TInterface : class
private static bool TryGetPluginTypeByFullName<TInterface>(string implementationTypeFullName, [NotNullWhen(true)] out Type? foundType) where TInterface : class
{
var @interface = typeof(TInterface).FullName;
var key = $"{@interface}_{implementationTypeFullName}";
return Assemblies.GetOrAdd(key, _ =>
if (Assemblies.TryGetValue(key, out foundType))
{
if (TryFindTypeInDlls<TInterface>(implementationTypeFullName, out var foundType))
{
return foundType;
}
return true;
}
throw new DllNotFoundException($"No dll found which implements Interface '{@interface}' and has FullName '{implementationTypeFullName}'.");
});
if (TryFindTypeInDlls<TInterface>(implementationTypeFullName, out foundType))
{
Assemblies.TryAdd(key, foundType);
return true;
}
return false;
}
private static bool TryFindTypeInDlls<TInterface>(string? implementationTypeFullName, [NotNullWhen(true)] out Type? pluginType) where TInterface : class

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A fluent testcontainer builder for the Docker version of WireMock.Net</Description>
@@ -39,7 +39,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stef.Validation" Version="0.1.1" />
<PackageReference Include="Testcontainers" Version="4.5.0" />
<PackageReference Include="Testcontainers" Version="4.7.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,29 @@
{
Guid: 90356dba-b36c-469a-a17e-669cd84f1f06,
UpdatedAt: DateTime_1,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /1,
IgnoreCase: false
}
]
},
Body: {
Matcher: {
Name: RegexMatcher,
Pattern: hello,
IgnoreCase: true
}
}
},
Response: {
ProxyUrl: https://my-proxy.com,
ProxyUrlReplaceSettings: {
IgnoreCase: false,
TransformTemplate: x{{this}}y
}
}
}

View File

@@ -450,6 +450,55 @@ public partial class WireMockAdminApiTests
server.Stop();
}
[Fact]
public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate()
{
// Arrange
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f06");
var server = WireMockServer.StartWithAdminInterface();
var api = RestClient.For<IWireMockAdminApi>(server.Url);
// Act
var model = new MappingModel
{
Guid = guid,
Request = new RequestModel
{
Path = "/1",
Body = new BodyModel
{
Matcher = new MatcherModel
{
Name = "RegexMatcher",
Pattern = "hello",
IgnoreCase = true
}
}
},
Response = new ResponseModel
{
ProxyUrl = "https://my-proxy.com",
ProxyUrlReplaceSettings = new ProxyUrlReplaceSettingsModel
{
TransformTemplate = "x{{this}}y"
}
}
};
var postMappingResult = await api.PostMappingAsync(model).ConfigureAwait(false);
// Assert
postMappingResult.Should().NotBeNull();
var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid);
mapping.Should().NotBeNull();
var getMappingResult = await api.GetMappingAsync(guid).ConfigureAwait(false);
await Verifier.Verify(getMappingResult, VerifySettings).DontScrubGuids();
server.Stop();
}
[Fact]
public async Task IWireMockAdminApi_GetRequestsAsync_Json()
{

View File

@@ -0,0 +1,98 @@
using System.Globalization;
using Moq;
using WireMock.Handlers;
using WireMock.Proxy;
using WireMock.Settings;
using WireMock.Types;
using Xunit;
namespace WireMock.Net.Tests.Proxy;
public class ProxyUrlTransformerTests
{
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock = new();
[Fact]
public void Transform_WithUseTransformerFalse_PerformsSimpleReplace_CaseSensitive()
{
// Arrange
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
Culture = CultureInfo.InvariantCulture
};
var replaceSettings = new ProxyUrlReplaceSettings
{
TransformTemplate = null,
OldValue = "/old",
NewValue = "/new",
IgnoreCase = false
};
var url = "http://example.com/old/path";
var expected = "http://example.com/new/path";
// Act
var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void Transform_WithUseTransformerFalse_PerformsSimpleReplace_IgnoreCase()
{
// Arrange
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
Culture = CultureInfo.InvariantCulture
};
var replaceSettings = new ProxyUrlReplaceSettings
{
TransformTemplate = null, // UseTransformer == false
OldValue = "/OLD",
NewValue = "/new",
IgnoreCase = true
};
var url = "http://example.com/old/path"; // lowercase 'old' but OldValue is uppercase
var expected = "http://example.com/new/path";
// Act
var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void Transform_WithUseTransformerTrue_UsesTransformer_ToTransformUrl()
{
// Arrange
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
Culture = CultureInfo.InvariantCulture
};
// Handlebars is the default TransformerType; the TransformTemplate uses the model directly.
var replaceSettings = new ProxyUrlReplaceSettings
{
TransformTemplate = "{{this}}-transformed",
// TransformerType defaults to Handlebars but set explicitly for clarity.
TransformerType = TransformerType.Handlebars
};
var url = "http://example.com/path";
var expected = "http://example.com/path-transformed";
// Act
var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url);
// Assert
Assert.Equal(expected, actual);
}
}

View File

@@ -25,6 +25,8 @@ public partial class TestcontainersTests(ITestOutputHelper testOutputHelper)
var adminPassword = $"password_{Guid.NewGuid()}";
var wireMockContainer = new WireMockContainerBuilder()
.WithAdminUserNameAndPassword(adminUsername, adminPassword)
.WithAutoRemove(true)
.WithCleanUp(true)
.Build();
await StartTestAsync(wireMockContainer);

View File

@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using System.IO;
using AnyOfTypes;
using FluentAssertions;
@@ -56,7 +55,7 @@ public class TypeLoaderTests
}
[Fact]
public void LoadNewInstance()
public void TryLoadNewInstance()
{
var current = Directory.GetCurrentDirectory();
try
@@ -65,10 +64,11 @@ public class TypeLoaderTests
// Act
AnyOf<string, StringPattern> pattern = "x";
var result = TypeLoader.LoadNewInstance<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, pattern);
var result = TypeLoader.TryLoadNewInstance<ICSharpCodeMatcher>(out var instance, MatchBehaviour.AcceptOnMatch, MatchOperator.Or, pattern);
// Assert
result.Should().NotBeNull();
result.Should().BeTrue();
instance.Should().BeOfType<CSharpCodeMatcher>();
}
finally
{
@@ -77,63 +77,66 @@ public class TypeLoaderTests
}
[Fact]
public void LoadNewInstanceByFullName()
public void TryLoadNewInstanceByFullName()
{
// Act
var result = TypeLoader.LoadNewInstanceByFullName<IDummyInterfaceWithImplementation>(typeof(DummyClass).FullName!);
var result = TypeLoader.TryLoadNewInstanceByFullName<IDummyInterfaceWithImplementation>(out var instance, typeof(DummyClass).FullName!);
// Assert
result.Should().BeOfType<DummyClass>();
result.Should().BeTrue();
instance.Should().BeOfType<DummyClass>();
}
[Fact]
public void LoadStaticInstance_ShouldOnlyCreateInstanceOnce()
public void TryLoadStaticInstance_ShouldOnlyCreateInstanceOnce()
{
// Arrange
var counter = new Counter();
// Act
var result = TypeLoader.LoadStaticInstance<IDummyInterfaceWithImplementationUsedForStaticTest>(counter);
TypeLoader.LoadStaticInstance<IDummyInterfaceWithImplementationUsedForStaticTest>(counter);
var result = TypeLoader.TryLoadStaticInstance<IDummyInterfaceWithImplementationUsedForStaticTest>(out var staticInstance, counter);
TypeLoader.TryLoadStaticInstance(out staticInstance, counter);
// Assert
result.Should().BeOfType<DummyClass1UsedForStaticTest>();
result.Should().BeTrue();
staticInstance.Should().BeOfType<DummyClass1UsedForStaticTest>();
counter.Value.Should().Be(1);
}
[Fact]
public void LoadStaticInstanceByFullName_ShouldOnlyCreateInstanceOnce()
public void TryLoadStaticInstanceByFullName_ShouldOnlyCreateInstanceOnce()
{
// Arrange
var counter = new Counter();
var fullName = typeof(DummyClass2UsedForStaticTest).FullName!;
// Act
var result = TypeLoader.LoadStaticInstanceByFullName<IDummyInterfaceWithImplementationUsedForStaticTest>(fullName, counter);
TypeLoader.LoadStaticInstanceByFullName<IDummyInterfaceWithImplementationUsedForStaticTest>(fullName, counter);
var result = TypeLoader.TryLoadStaticInstanceByFullName<IDummyInterfaceWithImplementationUsedForStaticTest>(out var staticInstance, fullName, counter);
TypeLoader.TryLoadStaticInstanceByFullName(out staticInstance, fullName, counter);
// Assert
result.Should().BeOfType<DummyClass2UsedForStaticTest>();
result.Should().BeTrue();
staticInstance.Should().BeOfType<DummyClass2UsedForStaticTest>();
counter.Value.Should().Be(1);
}
[Fact]
public void LoadNewInstance_ButNoImplementationFoundForInterface_ThrowsException()
public void TryLoadNewInstance_ButNoImplementationFoundForInterface_ReturnsFalse()
{
// Act
Action a = () => TypeLoader.LoadNewInstance<IDummyInterfaceNoImplementation>();
var result = TypeLoader.TryLoadNewInstance<IDummyInterfaceNoImplementation>(out _);
// Assert
a.Should().Throw<DllNotFoundException>().WithMessage("No dll found which implements Interface 'WireMock.Net.Tests.Util.TypeLoaderTests+IDummyInterfaceNoImplementation'.");
result.Should().BeFalse();
}
[Fact]
public void LoadNewInstanceByFullName_ButNoImplementationFoundForInterface_ThrowsException()
public void TryLoadNewInstanceByFullName_ButNoImplementationFoundForInterface_ReturnsFalse()
{
// Act
Action a = () => TypeLoader.LoadNewInstanceByFullName<IDummyInterfaceWithImplementation>("xyz");
var result = TypeLoader.TryLoadNewInstanceByFullName<IDummyInterfaceWithImplementation>(out _, "xyz");
// Assert
a.Should().Throw<DllNotFoundException>().WithMessage("No dll found which implements Interface 'WireMock.Net.Tests.Util.TypeLoaderTests+IDummyInterfaceWithImplementation' and has FullName 'xyz'.");
result.Should().BeFalse();
}
}