mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-28 20:01:46 +01:00
Fix ProtoBuf mapping.json (#1236)
* Fix ProtoBuf Mappings * [Fact(Skip = "#1233")] * fix? * PortUtils
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user