Fix ProtoBuf mapping.json (#1236)

* Fix ProtoBuf Mappings

* [Fact(Skip = "#1233")]

* fix?

* PortUtils
This commit is contained in:
Stef Heyenrath
2025-01-26 08:37:17 +01:00
committed by GitHub
parent 442d8a715c
commit ed07da7d18
25 changed files with 932 additions and 220 deletions

View File

@@ -215,14 +215,16 @@ public interface IWireMockServer : IDisposable
/// This can be used if you have 1 or more <see cref="MappingModel"/> defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// </summary>
/// <param name="mappings">The MappingModels</param>
/// <returns><see cref="IWireMockServer"/></returns>
IWireMockServer WithMapping(params MappingModel[] mappings);
/// <summary>
/// Register the mappings (via json string).
///
/// This can be used if you the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// This can be used if you've the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// </summary>
/// <param name="mappings">The mapping(s) as json string.</param>
/// <returns><see cref="IWireMockServer"/></returns>
IWireMockServer WithMapping(string mappings);
/// <summary>
@@ -238,5 +240,5 @@ public interface IWireMockServer : IDisposable
/// </summary>
/// <param name="converterType">The <see cref="MappingConverterType"/></param>
/// <returns>C# code</returns>
public string MappingsToCSharpCode(MappingConverterType converterType);
string MappingsToCSharpCode(MappingConverterType converterType);
}

View File

@@ -130,7 +130,7 @@ public interface IWireMockAdminApi
Task<StatusModel> ReloadStaticMappingsAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Get a mapping based on the guid
/// Get a mapping based on the guid.
/// </summary>
/// <param name="guid">The Guid</param>
/// <returns>MappingModel</returns>
@@ -138,6 +138,15 @@ public interface IWireMockAdminApi
[Get("mappings/{guid}")]
Task<MappingModel> GetMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
/// <summary>
/// Get a mapping based on the guid.
/// </summary>
/// <param name="guid">The Guid</param>
/// <returns>MappingModel</returns>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("mappings/{guid}")]
Task<MappingModel> GetMappingAsync([Path] string guid, CancellationToken cancellationToken = default);
/// <summary>
/// Get the C# code from a mapping based on the guid
/// </summary>

View File

@@ -66,6 +66,12 @@ internal static class BodyDataMatchScoreCalculator
return stringMatcher.IsMatch(requestMessage.BodyAsString);
}
// In case the matcher is a IProtoBufMatcher, use the BodyAsBytes to match on.
if (matcher is IProtoBufMatcher protoBufMatcher)
{
return protoBufMatcher.IsMatchAsync(requestMessage.BodyAsBytes).GetAwaiter().GetResult();
}
return default;
}
}

View File

@@ -2,7 +2,6 @@
#if PROTOBUF
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ProtoBufJsonConverter;
@@ -28,7 +27,7 @@ public class ProtoBufMatcher : IProtoBufMatcher
/// <summary>
/// The Func to define the proto definition as id or texts.
/// </summary>
public Func<IdOrTexts> ProtoDefinition { get; }
public Func<IdOrTexts> ProtoDefinition { get; internal set; }
/// <summary>
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".

View File

@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using JsonConverter.Abstractions;
using WireMock.Matchers;
using WireMock.Util;

View File

@@ -5,8 +5,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Stef.Validation;
using WireMock.Matchers;
using WireMock.Matchers.Request;
namespace WireMock.RequestBuilders;
@@ -71,6 +73,19 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder
return _requestMatchers.OfType<T>().FirstOrDefault(func);
}
internal bool TryGetProtoBufMatcher([NotNullWhen(true)] out IProtoBufMatcher? protoBufMatcher)
{
protoBufMatcher = GetRequestMessageMatcher<RequestMessageProtoBufMatcher>()?.Matcher;
if (protoBufMatcher != null)
{
return true;
}
var bodyMatcher = GetRequestMessageMatcher<RequestMessageBodyMatcher>();
protoBufMatcher = bodyMatcher?.Matchers?.OfType<IProtoBufMatcher>().FirstOrDefault();
return protoBufMatcher != null;
}
private IRequestBuilder Add<T>(T requestMatcher) where T : IRequestMatcher
{
foreach (var existing in _requestMatchers.OfType<T>().ToArray())

View File

@@ -49,7 +49,7 @@ public partial class Response
public IResponseBuilder WithTrailingHeader(string name, params string[] values)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeader method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else
Guard.NotNull(name);
@@ -63,7 +63,7 @@ public partial class Response
public IResponseBuilder WithTrailingHeaders(IDictionary<string, string> headers)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeaders method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else
Guard.NotNull(headers);
@@ -77,7 +77,7 @@ public partial class Response
public IResponseBuilder WithTrailingHeaders(IDictionary<string, string[]> headers)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeaders method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else
Guard.NotNull(headers);

View File

@@ -8,7 +8,6 @@ using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.Proxy;
using WireMock.RequestBuilders;
using WireMock.Settings;
@@ -264,16 +263,15 @@ public partial class Response : IResponseBuilder
if (UseTransformer)
{
// Check if the body matcher is a RequestMessageProtoBufMatcher and try to decode the byte-array to a BodyAsJson.
if (mapping.RequestMatcher is Request requestMatcher && requestMessage is RequestMessage request)
// If the body matcher is a RequestMessageProtoBufMatcher or BodyMatcher with a ProtoBufMatcher then try to decode the byte-array to a BodyAsJson.
if (mapping.RequestMatcher is Request request && requestMessage is RequestMessage requestMessageImplementation)
{
var protoBufMatcher = requestMatcher.GetRequestMessageMatcher<RequestMessageProtoBufMatcher>()?.Matcher;
if (protoBufMatcher != null)
if (request.TryGetProtoBufMatcher(out var protoBufMatcher))
{
var decoded = await protoBufMatcher.DecodeAsync(request.BodyData?.BodyAsBytes).ConfigureAwait(false);
var decoded = await protoBufMatcher.DecodeAsync(requestMessage.BodyData?.BodyAsBytes).ConfigureAwait(false);
if (decoded != null)
{
request.BodyAsJson = JsonUtils.ConvertValueToJToken(decoded);
requestMessageImplementation.BodyAsJson = JsonUtils.ConvertValueToJToken(decoded);
}
}
}

View File

@@ -379,7 +379,7 @@ internal class MappingConverter(MatcherMapper mapper)
}
var bodyMatchers =
protoBufMatcher?.Matcher != null ? new[] { protoBufMatcher.Matcher } : null ??
protoBufMatcher?.Matcher != null ? [protoBufMatcher.Matcher] : null ??
multiPartMatcher?.Matchers ??
graphQLMatcher?.Matchers ??
bodyMatcher?.Matchers;

View File

@@ -220,7 +220,7 @@ internal class MatcherMapper
{
model.Pattern = texts[0];
}
else
else if (texts.Count > 1)
{
model.Patterns = texts.Cast<object>().ToArray();
}
@@ -296,27 +296,9 @@ internal class MatcherMapper
{
var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher;
IdOrTexts protoDefinitionAsIdOrTexts;
if (protoDefinitions.Count == 1)
{
var idOrText = protoDefinitions[0];
if (_settings.ProtoDefinitions?.TryGetValue(idOrText, out var protoDefinitionFromSettings) == true)
{
protoDefinitionAsIdOrTexts = new(idOrText, protoDefinitionFromSettings);
}
else
{
protoDefinitionAsIdOrTexts = new(null, protoDefinitions);
}
}
else
{
protoDefinitionAsIdOrTexts = new(null, protoDefinitions);
}
return new ProtoBufMatcher(
() => protoDefinitionAsIdOrTexts,
matcher!.ProtoBufMessageType!,
() => ProtoDefinitionHelper.GetIdOrTexts(_settings, protoDefinitions.ToArray()),
matcher.ProtoBufMessageType!,
matchBehaviour ?? MatchBehaviour.AcceptOnMatch,
objectMatcher
);

View File

@@ -123,14 +123,14 @@ public interface IRespondWithAProvider
void ThenRespondWithStatusCode(HttpStatusCode code);
/// <summary>
/// Sets the the scenario.
/// Sets the scenario.
/// </summary>
/// <param name="scenario">The scenario.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider InScenario(string scenario);
/// <summary>
/// Sets the the scenario with an integer value.
/// Sets the scenario with an integer value.
/// </summary>
/// <param name="scenario">The scenario.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
@@ -220,7 +220,7 @@ public interface IRespondWithAProvider
/// <summary>
/// Data Object which can be used when WithTransformer is used.
/// e.g. lookup an path in this object using
/// e.g. lookup a path in this object using
/// <param name="data">The data dictionary object.</param>
/// <example>
/// lookup data "1"

View File

@@ -17,7 +17,7 @@ using WireMock.Util;
namespace WireMock.Server;
/// <summary>
/// The respond with a provider.
/// The RespondWithAProvider.
/// </summary>
internal class RespondWithAProvider : IRespondWithAProvider
{
@@ -37,7 +37,6 @@ internal class RespondWithAProvider : IRespondWithAProvider
private int _timesInSameState = 1;
private bool? _useWebhookFireAndForget;
private double? _probability;
private IdOrTexts? _protoDefinition;
private GraphQLSchemaDetails? _graphQLSchemaDetails;
public Guid Guid { get; private set; }
@@ -48,6 +47,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
public object? Data { get; private set; }
public IdOrTexts? ProtoDefinition { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
/// </summary>
@@ -104,9 +105,9 @@ internal class RespondWithAProvider : IRespondWithAProvider
mapping.WithProbability(_probability.Value);
}
if (_protoDefinition != null)
if (ProtoDefinition != null)
{
mapping.WithProtoDefinition(_protoDefinition.Value);
mapping.WithProtoDefinition(ProtoDefinition.Value);
}
_registrationCallback(mapping, _saveToFile);
@@ -296,7 +297,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
Guard.NotNull(url);
Guard.NotNull(method);
Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) };
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];
if (body != null)
{
@@ -323,7 +324,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
Guard.NotNull(url);
Guard.NotNull(method);
Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) };
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];
if (body != null)
{
@@ -355,23 +356,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
{
Guard.NotNull(protoDefinitionOrId);
if (protoDefinitionOrId.Length == 1)
{
var idOrText = protoDefinitionOrId[0];
if (_settings.ProtoDefinitions?.TryGetValue(idOrText, out var protoDefinitions) == true)
{
_protoDefinition = new(idOrText, protoDefinitions);
}
else
{
_protoDefinition = new(null, protoDefinitionOrId);
}
}
else
{
_protoDefinition = new(null, protoDefinitionOrId);
}
ProtoDefinition = ProtoDefinitionHelper.GetIdOrTexts(_settings, protoDefinitionOrId);
return this;
}

View File

@@ -42,9 +42,9 @@ public partial class WireMockServer
Guard.NotNull(mappingModel.Request);
Guard.NotNull(mappingModel.Response);
var requestBuilder = InitRequestBuilder(mappingModel.Request);
var request = (Request)InitRequestBuilder(mappingModel.Request, mappingModel);
var respondProvider = Given(requestBuilder, mappingModel.SaveToFile == true);
var respondProvider = Given(request, mappingModel.SaveToFile == true);
if (guid != null)
{
@@ -116,13 +116,23 @@ public partial class WireMockServer
respondProvider.WithProbability(mappingModel.Probability.Value);
}
// ProtoDefinition is defined at Mapping level
if (mappingModel.ProtoDefinition != null)
{
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinition);
}
else if (mappingModel.ProtoDefinitions != null)
{
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinitions);
}
var responseBuilder = InitResponseBuilder(mappingModel.Response);
respondProvider.RespondWith(responseBuilder);
return respondProvider.Guid;
}
private IRequestBuilder InitRequestBuilder(RequestModel requestModel)
private IRequestBuilder InitRequestBuilder(RequestModel requestModel, MappingModel? mappingModel = null)
{
var requestBuilder = Request.Create();
@@ -216,7 +226,7 @@ public partial class WireMockServer
if (requestModel.Params != null)
{
foreach (var paramModel in requestModel.Params.Where(p => p is { Matchers: { } }))
foreach (var paramModel in requestModel.Params.Where(p => p is { Matchers: not null }))
{
var ignoreCase = paramModel.IgnoreCase == true;
requestBuilder = requestBuilder.WithParam(paramModel.Name, ignoreCase, paramModel.Matchers!.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
@@ -225,7 +235,15 @@ public partial class WireMockServer
if (requestModel.Body?.Matcher != null)
{
requestBuilder = requestBuilder.WithBody(_matcherMapper.Map(requestModel.Body.Matcher)!);
var bodyMatcher = _matcherMapper.Map(requestModel.Body.Matcher)!;
#if PROTOBUF
// If the BodyMatcher is a ProtoBufMatcher, and if ProtoDefinition is defined on Mapping-level, set the ProtoDefinition from that Mapping.
if (bodyMatcher is ProtoBufMatcher protoBufMatcher && mappingModel?.ProtoDefinition != null)
{
protoBufMatcher.ProtoDefinition = () => ProtoDefinitionHelper.GetIdOrTexts(_settings, mappingModel.ProtoDefinition);
}
#endif
requestBuilder = requestBuilder.WithBody(bodyMatcher);
}
else if (requestModel.Body?.Matchers != null)
{
@@ -308,7 +326,7 @@ public partial class WireMockServer
}
else if (responseModel.HeadersRaw != null)
{
foreach (string headerLine in responseModel.HeadersRaw.Split(["\n", "\r\n"], StringSplitOptions.RemoveEmptyEntries))
foreach (var headerLine in responseModel.HeadersRaw.Split(["\n", "\r\n"], StringSplitOptions.RemoveEmptyEntries))
{
int indexColon = headerLine.IndexOf(":", StringComparison.Ordinal);
string key = headerLine.Substring(0, indexColon).TrimStart(' ', '\t');
@@ -317,6 +335,22 @@ public partial class WireMockServer
}
}
if (responseModel.TrailingHeaders != null)
{
foreach (var entry in responseModel.TrailingHeaders)
{
if (entry.Value is string value)
{
responseBuilder.WithTrailingHeader(entry.Key, value);
}
else
{
var headers = JsonUtils.ParseJTokenToObject<string[]>(entry.Value);
responseBuilder.WithTrailingHeader(entry.Key, headers);
}
}
}
if (responseModel.BodyAsBytes != null)
{
responseBuilder = responseBuilder.WithBody(responseModel.BodyAsBytes, responseModel.BodyDestination, ToEncoding(responseModel.BodyEncoding));
@@ -327,7 +361,26 @@ public partial class WireMockServer
}
else if (responseModel.BodyAsJson != null)
{
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
if (responseModel.ProtoBufMessageType != null)
{
if (responseModel.ProtoDefinition != null)
{
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinition, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
else if (responseModel.ProtoDefinitions != null)
{
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinitions, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
else
{
// ProtoDefinition(s) is/are defined at Mapping/Server level
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
}
else
{
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
}
}
else if (responseModel.BodyAsFile != null)
{

View File

@@ -1,7 +1,9 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
@@ -17,25 +19,96 @@ internal static class PortUtils
private static readonly Regex UrlDetailsRegex = new(@"^((?<proto>\w+)://)(?<host>[^/]+?):(?<port>\d+)\/?$", RegexOptions.Compiled, WireMockConstants.DefaultRegexTimeout);
/// <summary>
/// Finds a free TCP port.
/// Finds a random, free port to be listened on.
/// </summary>
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
/// <returns>A random, free port to be listened on.</returns>
/// <remarks>https://github.com/SeleniumHQ/selenium/blob/trunk/dotnet/src/webdriver/Internal/PortUtilities.cs</remarks>
public static int FindFreeTcpPort()
{
TcpListener? tcpListener = null;
// Locate a free port on the local machine by binding a socket to an IPEndPoint using IPAddress.Any and port 0.
// The socket will select a free port.
var portSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
tcpListener = new TcpListener(IPAddress.Loopback, 0);
tcpListener.Start();
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
var socketEndPoint = new IPEndPoint(IPAddress.Any, 0);
portSocket.Bind(socketEndPoint);
socketEndPoint = (IPEndPoint)portSocket.LocalEndPoint!;
return socketEndPoint.Port;
}
finally
{
tcpListener?.Stop();
#if !NETSTANDARD1_3
portSocket.Close();
#endif
portSocket.Dispose();
}
}
/// <summary>
/// Finds a specified number of random, free ports to be listened on.
/// </summary>
/// <param name="count">The number of free ports to find.</param>
/// <returns>A list of random, free ports to be listened on.</returns>
public static IReadOnlyList<int> FindFreeTcpPorts(int count)
{
var sockets = Enumerable
.Range(0, count)
.Select(_ => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
.ToArray();
var freePorts = new List<int>();
try
{
foreach (var socket in sockets)
{
var socketEndPoint = new IPEndPoint(IPAddress.Any, 0);
socket.Bind(socketEndPoint);
socketEndPoint = (IPEndPoint)socket.LocalEndPoint!;
freePorts.Add(socketEndPoint.Port);
}
return freePorts;
}
finally
{
foreach (var socket in sockets)
{
#if !NETSTANDARD1_3
socket.Close();
#endif
socket.Dispose();
}
}
}
///// <summary>
///// Finds free TCP ports.
///// </summary>
//public static IReadOnlyList<int> FindFreeTcpPorts(int numPorts)
//{
// var freePorts = new List<int>();
// TcpListener? tcpListener = null;
// try
// {
// for (var i = 0; i < numPorts; i++)
// {
// tcpListener = new TcpListener(IPAddress.Loopback, 0);
// tcpListener.Start();
// freePorts.Add(((IPEndPoint)tcpListener.LocalEndpoint).Port);
// }
// }
// finally
// {
// tcpListener?.Stop();
// }
// return freePorts;
//}
/// <summary>
/// Extract the isHttps, isHttp2, protocol, host and port from a URL.
/// </summary>

View File

@@ -0,0 +1,27 @@
// Copyright © WireMock.Net
using WireMock.Models;
using WireMock.Settings;
namespace WireMock.Util;
internal static class ProtoDefinitionHelper
{
internal static IdOrTexts GetIdOrTexts(WireMockServerSettings settings, params string[] protoDefinitionOrId)
{
switch (protoDefinitionOrId.Length)
{
case 1:
var idOrText = protoDefinitionOrId[0];
if (settings.ProtoDefinitions?.TryGetValue(idOrText, out var protoDefinitions) == true)
{
return new(idOrText, protoDefinitions);
}
return new(null, protoDefinitionOrId);
default:
return new(null, protoDefinitionOrId);
}
}
}