mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-29 03:37:03 +02:00
Create WireMock.Net.MimePart project (#1300)
* Create WireMock.Net.MimePart project * . * REFACTOR * ILRepack * -- * ... * x * x * . * fix * public class MimePartMatcher * shared * min * . * <!--<DelaySign>true</DelaySign>--> * Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
176
src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs
Normal file
176
src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Requests;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Owin;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class LogEntryMapper
|
||||
{
|
||||
private readonly IWireMockMiddlewareOptions _options;
|
||||
|
||||
public LogEntryMapper(IWireMockMiddlewareOptions options)
|
||||
{
|
||||
_options = Guard.NotNull(options);
|
||||
}
|
||||
|
||||
public LogEntryModel Map(ILogEntry logEntry)
|
||||
{
|
||||
var logRequestModel = new LogRequestModel
|
||||
{
|
||||
DateTime = logEntry.RequestMessage.DateTime,
|
||||
ClientIP = logEntry.RequestMessage.ClientIP,
|
||||
Path = logEntry.RequestMessage.Path,
|
||||
AbsolutePath = logEntry.RequestMessage.AbsolutePath,
|
||||
Url = logEntry.RequestMessage.Url,
|
||||
AbsoluteUrl = logEntry.RequestMessage.AbsoluteUrl,
|
||||
ProxyUrl = logEntry.RequestMessage.ProxyUrl,
|
||||
Query = logEntry.RequestMessage.Query,
|
||||
Method = logEntry.RequestMessage.Method,
|
||||
HttpVersion = logEntry.RequestMessage.HttpVersion,
|
||||
Headers = logEntry.RequestMessage.Headers,
|
||||
Cookies = logEntry.RequestMessage.Cookies
|
||||
};
|
||||
|
||||
if (logEntry.RequestMessage.BodyData != null)
|
||||
{
|
||||
logRequestModel.DetectedBodyType = logEntry.RequestMessage.BodyData.DetectedBodyType?.ToString();
|
||||
logRequestModel.DetectedBodyTypeFromContentType = logEntry.RequestMessage.BodyData.DetectedBodyTypeFromContentType?.ToString();
|
||||
|
||||
switch (logEntry.RequestMessage.BodyData.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
logRequestModel.Body = logEntry.RequestMessage.BodyData.BodyAsString;
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
logRequestModel.Body = logEntry.RequestMessage.BodyData.BodyAsString; // In case of Json, do also save the Body as string (backwards compatible)
|
||||
logRequestModel.BodyAsJson = logEntry.RequestMessage.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
case BodyType.Bytes:
|
||||
logRequestModel.BodyAsBytes = logEntry.RequestMessage.BodyData.BodyAsBytes;
|
||||
break;
|
||||
}
|
||||
|
||||
logRequestModel.BodyEncoding = logEntry.RequestMessage.BodyData.Encoding != null
|
||||
? new EncodingModel
|
||||
{
|
||||
EncodingName = logEntry.RequestMessage.BodyData.Encoding.EncodingName,
|
||||
CodePage = logEntry.RequestMessage.BodyData.Encoding.CodePage,
|
||||
WebName = logEntry.RequestMessage.BodyData.Encoding.WebName
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
var logResponseModel = new LogResponseModel
|
||||
{
|
||||
StatusCode = logEntry.ResponseMessage.StatusCode,
|
||||
Headers = logEntry.ResponseMessage.Headers
|
||||
};
|
||||
|
||||
if (logEntry.ResponseMessage.FaultType != FaultType.NONE)
|
||||
{
|
||||
logResponseModel.FaultType = logEntry.ResponseMessage.FaultType.ToString();
|
||||
logResponseModel.FaultPercentage = logEntry.ResponseMessage.FaultPercentage;
|
||||
}
|
||||
|
||||
if (logEntry.ResponseMessage.BodyData != null)
|
||||
{
|
||||
logResponseModel.BodyOriginal = logEntry.ResponseMessage.BodyOriginal;
|
||||
logResponseModel.BodyDestination = logEntry.ResponseMessage.BodyDestination;
|
||||
|
||||
logResponseModel.DetectedBodyType = logEntry.ResponseMessage.BodyData.DetectedBodyType;
|
||||
logResponseModel.DetectedBodyTypeFromContentType = logEntry.ResponseMessage.BodyData.DetectedBodyTypeFromContentType;
|
||||
|
||||
MapBody(logEntry, logResponseModel);
|
||||
|
||||
logResponseModel.BodyEncoding = logEntry.ResponseMessage.BodyData.Encoding != null
|
||||
? new EncodingModel
|
||||
{
|
||||
EncodingName = logEntry.ResponseMessage.BodyData.Encoding.EncodingName,
|
||||
CodePage = logEntry.ResponseMessage.BodyData.Encoding.CodePage,
|
||||
WebName = logEntry.ResponseMessage.BodyData.Encoding.WebName
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
return new LogEntryModel
|
||||
{
|
||||
Guid = logEntry.Guid,
|
||||
Request = logRequestModel,
|
||||
Response = logResponseModel,
|
||||
|
||||
MappingGuid = logEntry.MappingGuid,
|
||||
MappingTitle = logEntry.MappingTitle,
|
||||
RequestMatchResult = Map(logEntry.RequestMatchResult),
|
||||
|
||||
PartialMappingGuid = logEntry.PartialMappingGuid,
|
||||
PartialMappingTitle = logEntry.PartialMappingTitle,
|
||||
PartialRequestMatchResult = Map(logEntry.PartialMatchResult)
|
||||
};
|
||||
}
|
||||
|
||||
private void MapBody(ILogEntry logEntry, LogResponseModel logResponseModel)
|
||||
{
|
||||
switch (logEntry.ResponseMessage.BodyData!.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
if (!string.IsNullOrEmpty(logEntry.ResponseMessage.BodyData.IsFuncUsed) && _options.DoNotSaveDynamicResponseInLogEntry == true)
|
||||
{
|
||||
logResponseModel.Body = logEntry.ResponseMessage.BodyData.IsFuncUsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
logResponseModel.Body = logEntry.ResponseMessage.BodyData.BodyAsString;
|
||||
}
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
logResponseModel.BodyAsJson = logEntry.ResponseMessage.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
case BodyType.Bytes:
|
||||
logResponseModel.BodyAsBytes = logEntry.ResponseMessage.BodyData.BodyAsBytes;
|
||||
break;
|
||||
|
||||
case BodyType.File:
|
||||
logResponseModel.BodyAsFile = logEntry.ResponseMessage.BodyData.BodyAsFile;
|
||||
logResponseModel.BodyAsFileIsCached = logEntry.ResponseMessage.BodyData.BodyAsFileIsCached;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static LogRequestMatchModel? Map(IRequestMatchResult? matchResult)
|
||||
{
|
||||
if (matchResult == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new LogRequestMatchModel
|
||||
{
|
||||
IsPerfectMatch = matchResult.IsPerfectMatch,
|
||||
TotalScore = matchResult.TotalScore,
|
||||
TotalNumber = matchResult.TotalNumber,
|
||||
AverageTotalScore = matchResult.AverageTotalScore,
|
||||
MatchDetails = matchResult.MatchDetails.Select(md => new
|
||||
{
|
||||
Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty),
|
||||
md.Score
|
||||
} as object).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
627
src/WireMock.Net.Minimal/Serialization/MappingConverter.cs
Normal file
627
src/WireMock.Net.Minimal/Serialization/MappingConverter.cs
Normal file
@@ -0,0 +1,627 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
using static WireMock.Util.CSharpFormatter;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class MappingConverter(MatcherMapper mapper)
|
||||
{
|
||||
private static readonly string AcceptOnMatch = MatchBehaviour.AcceptOnMatch.GetFullyQualifiedEnumValue();
|
||||
|
||||
private readonly MatcherMapper _mapper = Guard.NotNull(mapper);
|
||||
|
||||
public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings = null)
|
||||
{
|
||||
settings ??= new MappingConverterSettings();
|
||||
|
||||
var request = (Request)mapping.RequestMatcher;
|
||||
var response = (Response)mapping.Provider;
|
||||
|
||||
var clientIPMatcher = request.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
|
||||
var pathMatcher = request.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
||||
var urlMatcher = request.GetRequestMessageMatcher<RequestMessageUrlMatcher>();
|
||||
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
||||
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
||||
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||
var requestMessageBodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||
var requestMessageHttpVersionMatcher = request.GetRequestMessageMatcher<RequestMessageHttpVersionMatcher>();
|
||||
var requestMessageGraphQLMatcher = request.GetRequestMessageMatcher<RequestMessageGraphQLMatcher>();
|
||||
var requestMessageMultiPartMatcher = request.GetRequestMessageMatcher<RequestMessageMultiPartMatcher>();
|
||||
var requestMessageProtoBufMatcher = request.GetRequestMessageMatcher<RequestMessageProtoBufMatcher>();
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
if (settings.ConverterType == MappingConverterType.Server)
|
||||
{
|
||||
if (settings.AddStart)
|
||||
{
|
||||
sb.AppendLine("var server = WireMockServer.Start();");
|
||||
}
|
||||
sb.AppendLine("server");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (settings.AddStart)
|
||||
{
|
||||
sb.AppendLine("var builder = new MappingBuilder();");
|
||||
}
|
||||
sb.AppendLine("builder");
|
||||
}
|
||||
|
||||
// Request
|
||||
sb.AppendLine(" .Given(Request.Create()");
|
||||
sb.AppendLine($" .UsingMethod({To1Or2Or3Arguments(methodMatcher?.MatchBehaviour, methodMatcher?.MatchOperator, methodMatcher?.Methods, HttpRequestMethod.GET)})");
|
||||
|
||||
if (pathMatcher?.Matchers != null)
|
||||
{
|
||||
sb.AppendLine($" .WithPath({To1Or2Arguments(pathMatcher.MatchOperator, pathMatcher.Matchers)})");
|
||||
}
|
||||
else if (urlMatcher?.Matchers != null)
|
||||
{
|
||||
sb.AppendLine($" .WithUrl({To1Or2Arguments(urlMatcher.MatchOperator, urlMatcher.Matchers)})");
|
||||
}
|
||||
|
||||
foreach (var paramsMatcher in paramsMatchers.Where(pm => pm.Matchers != null))
|
||||
{
|
||||
sb.AppendLine($" .WithParam({To2Or3Arguments(paramsMatcher.Key, paramsMatcher.MatchBehaviour, paramsMatcher.Matchers!)})");
|
||||
}
|
||||
|
||||
if (clientIPMatcher?.Matchers != null)
|
||||
{
|
||||
sb.AppendLine($" .WithClientIP({ToValueArguments(GetStringArray(clientIPMatcher.Matchers))})");
|
||||
}
|
||||
|
||||
foreach (var headerMatcher in headerMatchers.Where(h => h.Matchers != null))
|
||||
{
|
||||
var headerBuilder = new StringBuilder($"\"{headerMatcher.Name}\", {ToValueArguments(GetStringArray(headerMatcher.Matchers!))}, true");
|
||||
if (headerMatcher.MatchOperator != MatchOperator.Or)
|
||||
{
|
||||
headerBuilder.Append($"{AcceptOnMatch}, {headerMatcher.MatchOperator.GetFullyQualifiedEnumValue()}");
|
||||
}
|
||||
sb.AppendLine($" .WithHeader({headerBuilder})");
|
||||
}
|
||||
|
||||
foreach (var cookieMatcher in cookieMatchers.Where(c => c.Matchers != null))
|
||||
{
|
||||
sb.AppendLine($" .WithCookie(\"{cookieMatcher.Name}\", {ToValueArguments(GetStringArray(cookieMatcher.Matchers!))}, true)");
|
||||
}
|
||||
|
||||
if (requestMessageHttpVersionMatcher?.HttpVersion != null)
|
||||
{
|
||||
sb.AppendLine($" .WithHttpVersion({requestMessageHttpVersionMatcher.HttpVersion})");
|
||||
}
|
||||
|
||||
#if GRAPHQL
|
||||
if (requestMessageGraphQLMatcher?.Matchers != null)
|
||||
{
|
||||
if (requestMessageGraphQLMatcher.Matchers.OfType<GraphQLMatcher>().FirstOrDefault() is { } graphQLMatcher && graphQLMatcher.GetPatterns().Any())
|
||||
{
|
||||
sb.AppendLine($" .WithGraphQLSchema({GetString(graphQLMatcher)})");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (requestMessageMultiPartMatcher?.Matchers != null)
|
||||
{
|
||||
if (requestMessageMultiPartMatcher.Matchers.OfType<IMimePartMatcher>().Any())
|
||||
{
|
||||
sb.AppendLine(" // .WithMultiPart() is not yet supported");
|
||||
}
|
||||
}
|
||||
|
||||
#if PROTOBUF
|
||||
if (requestMessageProtoBufMatcher?.Matcher != null)
|
||||
{
|
||||
sb.AppendLine(" // .WithBodyAsProtoBuf() is not yet supported");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (requestMessageBodyMatcher?.Matchers != null)
|
||||
{
|
||||
var firstMatcher = requestMessageBodyMatcher.Matchers.FirstOrDefault();
|
||||
|
||||
switch (firstMatcher)
|
||||
{
|
||||
case IStringMatcher stringMatcher when stringMatcher.GetPatterns().Length > 0:
|
||||
sb.AppendLine($" .WithBody({GetString(stringMatcher)})");
|
||||
break;
|
||||
|
||||
case JsonMatcher jsonMatcher:
|
||||
{
|
||||
var matcherType = jsonMatcher.GetType().Name;
|
||||
sb.AppendLine($" .WithBody(new {matcherType}(");
|
||||
sb.AppendLine($" value: {ConvertToAnonymousObjectDefinition(jsonMatcher.Value, 3)},");
|
||||
sb.AppendLine($" ignoreCase: {ToCSharpBooleanLiteral(jsonMatcher.IgnoreCase)},");
|
||||
sb.AppendLine($" regex: {ToCSharpBooleanLiteral(jsonMatcher.Regex)}");
|
||||
sb.AppendLine(@" ))");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sb.AppendLine(@" )");
|
||||
|
||||
// Guid
|
||||
sb.AppendLine($" .WithGuid(\"{mapping.Guid}\")");
|
||||
|
||||
if (mapping.Probability != null)
|
||||
{
|
||||
sb.AppendLine($" .WithProbability({mapping.Probability.Value.ToString(CultureInfoUtils.CultureInfoEnUS)})");
|
||||
}
|
||||
|
||||
// Response
|
||||
sb.AppendLine(" .RespondWith(Response.Create()");
|
||||
|
||||
if (response.ResponseMessage.StatusCode is int or string)
|
||||
{
|
||||
sb.AppendLine($" .WithStatusCode({JsonConvert.SerializeObject(response.ResponseMessage.StatusCode)})");
|
||||
}
|
||||
else if (response.ResponseMessage.StatusCode is HttpStatusCode httpStatusCode)
|
||||
{
|
||||
sb.AppendLine($" .WithStatusCode({(int)httpStatusCode})");
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.Headers is { })
|
||||
{
|
||||
foreach (var header in response.ResponseMessage.Headers)
|
||||
{
|
||||
sb.AppendLine($" .WithHeader(\"{header.Key}\", {ToValueArguments(header.Value.ToArray())})");
|
||||
}
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.TrailingHeaders is { })
|
||||
{
|
||||
foreach (var header in response.ResponseMessage.TrailingHeaders)
|
||||
{
|
||||
sb.AppendLine($" .WithTrailingHeader(\"{header.Key}\", {ToValueArguments(header.Value.ToArray())})");
|
||||
}
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.BodyData is { } bodyData)
|
||||
{
|
||||
switch (response.ResponseMessage.BodyData.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
sb.AppendLine($" .WithBody({ToCSharpStringLiteral(bodyData.BodyAsString)})");
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
if (bodyData.BodyAsJson is string bodyStringValue)
|
||||
{
|
||||
sb.AppendLine($" .WithBody({ToCSharpStringLiteral(bodyStringValue)})");
|
||||
}
|
||||
else if (bodyData.BodyAsJson is { } jsonBody)
|
||||
{
|
||||
var anonymousObjectDefinition = ConvertToAnonymousObjectDefinition(jsonBody);
|
||||
sb.AppendLine($" .WithBodyAsJson({anonymousObjectDefinition})");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (response.Delay is { })
|
||||
{
|
||||
sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})");
|
||||
}
|
||||
else if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 })
|
||||
{
|
||||
sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})");
|
||||
}
|
||||
|
||||
if (response.UseTransformer)
|
||||
{
|
||||
var transformerArgs = response.TransformerType != TransformerType.Handlebars ? response.TransformerType.GetFullyQualifiedEnumValue() : string.Empty;
|
||||
sb.AppendLine($" .WithTransformer({transformerArgs})");
|
||||
}
|
||||
sb.AppendLine(@" );");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public MappingModel ToMappingModel(IMapping mapping)
|
||||
{
|
||||
var request = (Request)mapping.RequestMatcher;
|
||||
var response = (Response)mapping.Provider;
|
||||
|
||||
var clientIPMatcher = request.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
|
||||
var pathMatcher = request.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
||||
var urlMatcher = request.GetRequestMessageMatcher<RequestMessageUrlMatcher>();
|
||||
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
||||
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
||||
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||
var graphQLMatcher = request.GetRequestMessageMatcher<RequestMessageGraphQLMatcher>();
|
||||
var multiPartMatcher = request.GetRequestMessageMatcher<RequestMessageMultiPartMatcher>();
|
||||
var protoBufMatcher = request.GetRequestMessageMatcher<RequestMessageProtoBufMatcher>();
|
||||
var httpVersionMatcher = request.GetRequestMessageMatcher<RequestMessageHttpVersionMatcher>();
|
||||
|
||||
var mappingModel = new MappingModel
|
||||
{
|
||||
Guid = mapping.Guid,
|
||||
UpdatedAt = mapping.UpdatedAt,
|
||||
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,
|
||||
SetStateTo = mapping.NextState,
|
||||
Data = mapping.Data,
|
||||
Probability = mapping.Probability,
|
||||
Request = new RequestModel
|
||||
{
|
||||
Headers = headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
|
||||
{
|
||||
Name = hm.Name,
|
||||
IgnoreCase = hm.IgnoreCase ? true : null,
|
||||
RejectOnMatch = hm.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null,
|
||||
Matchers = _mapper.Map(hm.Matchers),
|
||||
}).ToList() : null,
|
||||
|
||||
Cookies = cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
|
||||
{
|
||||
Name = cm.Name,
|
||||
IgnoreCase = cm.IgnoreCase ? true : null,
|
||||
RejectOnMatch = cm.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null,
|
||||
Matchers = _mapper.Map(cm.Matchers)
|
||||
}).ToList() : null,
|
||||
|
||||
Params = paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
|
||||
{
|
||||
Name = pm.Key,
|
||||
IgnoreCase = pm.IgnoreCase ? true : null,
|
||||
RejectOnMatch = pm.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null,
|
||||
Matchers = _mapper.Map(pm.Matchers)
|
||||
}).ToList() : null
|
||||
},
|
||||
Response = new ResponseModel()
|
||||
};
|
||||
|
||||
mapping.ProtoDefinition?.Value(
|
||||
id => mappingModel.ProtoDefinition = id,
|
||||
texts =>
|
||||
{
|
||||
if (texts.Count == 1)
|
||||
{
|
||||
mappingModel.ProtoDefinition = texts[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
mappingModel.ProtoDefinitions = texts.ToArray();
|
||||
}
|
||||
});
|
||||
|
||||
if (methodMatcher != null)
|
||||
{
|
||||
mappingModel.Request.Methods = methodMatcher.Methods;
|
||||
mappingModel.Request.MethodsRejectOnMatch = methodMatcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null;
|
||||
mappingModel.Request.MethodsMatchOperator = methodMatcher.Methods.Length > 1 ? methodMatcher.MatchOperator.ToString() : null;
|
||||
}
|
||||
|
||||
if (httpVersionMatcher?.HttpVersion != null)
|
||||
{
|
||||
mappingModel.Request.HttpVersion = httpVersionMatcher.HttpVersion;
|
||||
}
|
||||
|
||||
if (clientIPMatcher?.Matchers != null)
|
||||
{
|
||||
var clientIPMatchers = _mapper.Map(clientIPMatcher.Matchers);
|
||||
mappingModel.Request.Path = new ClientIPModel
|
||||
{
|
||||
Matchers = clientIPMatchers,
|
||||
MatchOperator = clientIPMatchers?.Length > 1 ? clientIPMatcher.MatchOperator.ToString() : null
|
||||
};
|
||||
}
|
||||
|
||||
if (pathMatcher?.Matchers != null)
|
||||
{
|
||||
var pathMatchers = _mapper.Map(pathMatcher.Matchers);
|
||||
mappingModel.Request.Path = new PathModel
|
||||
{
|
||||
Matchers = pathMatchers,
|
||||
MatchOperator = pathMatchers?.Length > 1 ? pathMatcher.MatchOperator.ToString() : null
|
||||
};
|
||||
}
|
||||
else if (urlMatcher?.Matchers != null)
|
||||
{
|
||||
var urlMatchers = _mapper.Map(urlMatcher.Matchers);
|
||||
mappingModel.Request.Url = new UrlModel
|
||||
{
|
||||
Matchers = urlMatchers,
|
||||
MatchOperator = urlMatchers?.Length > 1 ? urlMatcher.MatchOperator.ToString() : null
|
||||
};
|
||||
}
|
||||
|
||||
if (response.MinimumDelayMilliseconds >= 0 || response.MaximumDelayMilliseconds > 0)
|
||||
{
|
||||
mappingModel.Response.MinimumRandomDelay = response.MinimumDelayMilliseconds;
|
||||
mappingModel.Response.MaximumRandomDelay = response.MaximumDelayMilliseconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
mappingModel.Response.Delay = (int?)(response.Delay == Timeout.InfiniteTimeSpan ? TimeSpan.MaxValue.TotalMilliseconds : response.Delay?.TotalMilliseconds);
|
||||
}
|
||||
|
||||
var nonNullableWebHooks = mapping.Webhooks?.ToArray() ?? [];
|
||||
if (nonNullableWebHooks.Length == 1)
|
||||
{
|
||||
mappingModel.Webhook = WebhookMapper.Map(nonNullableWebHooks[0]);
|
||||
}
|
||||
else if (mapping.Webhooks?.Length > 1)
|
||||
{
|
||||
mappingModel.Webhooks = mapping.Webhooks.Select(WebhookMapper.Map).ToArray();
|
||||
}
|
||||
|
||||
var bodyMatchers =
|
||||
protoBufMatcher?.Matcher != null ? [protoBufMatcher.Matcher] : null ??
|
||||
multiPartMatcher?.Matchers ??
|
||||
graphQLMatcher?.Matchers ??
|
||||
bodyMatcher?.Matchers;
|
||||
|
||||
var matchOperator =
|
||||
multiPartMatcher?.MatchOperator ??
|
||||
graphQLMatcher?.MatchOperator ??
|
||||
bodyMatcher?.MatchOperator ??
|
||||
MatchOperator.Or;
|
||||
|
||||
if (bodyMatchers != null)
|
||||
{
|
||||
void AfterMap(MatcherModel matcherModel)
|
||||
{
|
||||
#if PROTOBUF
|
||||
// In case the ProtoDefinition is defined at the Mapping level, clear the Pattern at the Matcher level
|
||||
if (bodyMatchers?.OfType<ProtoBufMatcher>().Any() == true && mappingModel.ProtoDefinition != null)
|
||||
{
|
||||
matcherModel.Pattern = null;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
mappingModel.Request.Body = new BodyModel();
|
||||
|
||||
if (bodyMatchers.Length == 1)
|
||||
{
|
||||
mappingModel.Request.Body.Matcher = _mapper.Map(bodyMatchers[0], AfterMap);
|
||||
}
|
||||
else if (bodyMatchers.Length > 1)
|
||||
{
|
||||
mappingModel.Request.Body.Matchers = _mapper.Map(bodyMatchers, AfterMap);
|
||||
mappingModel.Request.Body.MatchOperator = matchOperator.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
if (response.ProxyAndRecordSettings != null)
|
||||
{
|
||||
mappingModel.Response.StatusCode = null;
|
||||
mappingModel.Response.Headers = null;
|
||||
mappingModel.Response.BodyDestination = null;
|
||||
mappingModel.Response.BodyAsJson = null;
|
||||
mappingModel.Response.BodyAsJsonIndented = null;
|
||||
mappingModel.Response.Body = null;
|
||||
mappingModel.Response.BodyAsBytes = null;
|
||||
mappingModel.Response.BodyAsFile = null;
|
||||
mappingModel.Response.BodyAsFileIsCached = null;
|
||||
mappingModel.Response.UseTransformer = null;
|
||||
mappingModel.Response.TransformerType = null;
|
||||
mappingModel.Response.UseTransformerForBodyAsFile = null;
|
||||
mappingModel.Response.TransformerReplaceNodeOptions = null;
|
||||
mappingModel.Response.BodyEncoding = null;
|
||||
mappingModel.Response.Fault = null;
|
||||
|
||||
mappingModel.Response.WebProxy = TinyMapperUtils.Instance.Map(response.ProxyAndRecordSettings.WebProxySettings);
|
||||
mappingModel.Response.ProxyUrl = response.ProxyAndRecordSettings.Url;
|
||||
mappingModel.Response.ProxyUrlReplaceSettings = TinyMapperUtils.Instance.Map(response.ProxyAndRecordSettings.ReplaceSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
mappingModel.Response.WebProxy = null;
|
||||
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
|
||||
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
|
||||
|
||||
if (response.ResponseMessage.Headers is { Count: > 0 })
|
||||
{
|
||||
mappingModel.Response.Headers = MapHeaders(response.ResponseMessage.Headers);
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.TrailingHeaders is { Count: > 0 })
|
||||
{
|
||||
mappingModel.Response.TrailingHeaders = MapHeaders(response.ResponseMessage.TrailingHeaders);
|
||||
}
|
||||
|
||||
if (response.UseTransformer)
|
||||
{
|
||||
mappingModel.Response.UseTransformer = response.UseTransformer;
|
||||
mappingModel.Response.TransformerType = response.TransformerType.ToString();
|
||||
mappingModel.Response.TransformerReplaceNodeOptions = response.TransformerReplaceNodeOptions.ToString();
|
||||
}
|
||||
|
||||
if (response.UseTransformerForBodyAsFile)
|
||||
{
|
||||
mappingModel.Response.UseTransformerForBodyAsFile = response.UseTransformerForBodyAsFile;
|
||||
}
|
||||
|
||||
MapResponse(response, mappingModel);
|
||||
|
||||
if (response.ResponseMessage.FaultType != FaultType.NONE)
|
||||
{
|
||||
mappingModel.Response.Fault = new FaultModel
|
||||
{
|
||||
Type = response.ResponseMessage.FaultType.ToString(),
|
||||
Percentage = response.ResponseMessage.FaultPercentage
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return mappingModel;
|
||||
}
|
||||
|
||||
private static void MapResponse(Response response, MappingModel mappingModel)
|
||||
{
|
||||
if (response.ResponseMessage.BodyData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (response.ResponseMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString;
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
|
||||
if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true)
|
||||
{
|
||||
mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented;
|
||||
}
|
||||
break;
|
||||
|
||||
case BodyType.ProtoBuf:
|
||||
// If the ProtoDefinition(s) is/are not defined at the MappingModel, get the ProtoDefinition(s) from the ResponseMessage.
|
||||
if (mappingModel.ProtoDefinition == null && mappingModel.ProtoDefinitions == null)
|
||||
{
|
||||
response.ResponseMessage.BodyData.ProtoDefinition?.Invoke().Value(
|
||||
id => mappingModel.Response.ProtoDefinition = id,
|
||||
texts =>
|
||||
{
|
||||
if (texts.Count == 1)
|
||||
{
|
||||
mappingModel.Response.ProtoDefinition = texts[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
mappingModel.Response.ProtoDefinitions = texts.ToArray();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
mappingModel.Response.ProtoBufMessageType = response.ResponseMessage.BodyData.ProtoBufMessageType;
|
||||
mappingModel.Response.BodyAsBytes = null;
|
||||
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
case BodyType.Bytes:
|
||||
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes;
|
||||
break;
|
||||
|
||||
case BodyType.File:
|
||||
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyData.BodyAsFile;
|
||||
mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached;
|
||||
break;
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
|
||||
{
|
||||
mappingModel.Response.BodyEncoding = new EncodingModel
|
||||
{
|
||||
EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName,
|
||||
CodePage = response.ResponseMessage.BodyData.Encoding.CodePage,
|
||||
WebName = response.ResponseMessage.BodyData.Encoding.WebName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetString(IStringMatcher stringMatcher)
|
||||
{
|
||||
return stringMatcher.GetPatterns().Select(p => ToCSharpStringLiteral(p.GetPattern())).First();
|
||||
}
|
||||
|
||||
private static string[] GetStringArray(IReadOnlyList<IStringMatcher> stringMatchers)
|
||||
{
|
||||
return stringMatchers.SelectMany(m => m.GetPatterns()).Select(p => p.GetPattern()).ToArray();
|
||||
}
|
||||
|
||||
private static string To2Or3Arguments(string key, MatchBehaviour? matchBehaviour, IReadOnlyList<IStringMatcher> matchers)
|
||||
{
|
||||
var sb = new StringBuilder($"\"{key}\", ");
|
||||
|
||||
if (matchBehaviour.HasValue && matchBehaviour != MatchBehaviour.AcceptOnMatch)
|
||||
{
|
||||
sb.AppendFormat("{0}, ", matchBehaviour.Value.GetFullyQualifiedEnumValue());
|
||||
}
|
||||
|
||||
sb.AppendFormat("{0}", MappingConverterUtils.ToCSharpCodeArguments(matchers));
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static string To1Or2Or3Arguments(MatchBehaviour? matchBehaviour, MatchOperator? matchOperator, string[]? values, string defaultValue)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
if (matchBehaviour.HasValue && matchBehaviour != MatchBehaviour.AcceptOnMatch)
|
||||
{
|
||||
sb.AppendFormat("{0}, ", matchBehaviour.Value.GetFullyQualifiedEnumValue());
|
||||
}
|
||||
|
||||
if (matchOperator.HasValue && matchOperator != MatchOperator.Or)
|
||||
{
|
||||
sb.AppendFormat("{0}, ", matchOperator.Value.GetFullyQualifiedEnumValue());
|
||||
}
|
||||
|
||||
return sb.Append(ToValueArguments(values, defaultValue)).ToString();
|
||||
}
|
||||
|
||||
private static string To1Or2Arguments(MatchOperator? matchOperator, IReadOnlyList<IStringMatcher> matchers)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
if (matchOperator.HasValue && matchOperator != MatchOperator.Or)
|
||||
{
|
||||
sb.AppendFormat("{0}, ", matchOperator.Value.GetFullyQualifiedEnumValue());
|
||||
}
|
||||
|
||||
sb.AppendFormat("{0}", MappingConverterUtils.ToCSharpCodeArguments(matchers));
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static string ToValueArguments(string[]? values, string defaultValue = "")
|
||||
{
|
||||
return values is { } ? string.Join(", ", values.Select(ToCSharpStringLiteral)) : ToCSharpStringLiteral(defaultValue);
|
||||
}
|
||||
|
||||
private static IDictionary<string, object> MapHeaders(IDictionary<string, WireMockList<string>> dictionary)
|
||||
{
|
||||
var newDictionary = new Dictionary<string, object>();
|
||||
foreach (var entry in dictionary)
|
||||
{
|
||||
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
|
||||
if (entry.Value.Count == 1)
|
||||
{
|
||||
newDictionary.Add(entry.Key, entry.Value.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
newDictionary.Add(entry.Key, entry.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return newDictionary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// MappingConverterSettings
|
||||
/// </summary>
|
||||
public class MappingConverterSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Use 'Server' or 'Builder'.
|
||||
///
|
||||
/// Default is Server
|
||||
/// </summary>
|
||||
public MappingConverterType ConverterType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Add "var server = WireMockServer.Start();"
|
||||
/// or
|
||||
/// Add "var builder = new MappingBuilder();"
|
||||
///
|
||||
/// Default it's false.
|
||||
/// </summary>
|
||||
public bool AddStart { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Creates sanitized file names for mappings
|
||||
/// </summary>
|
||||
public class MappingFileNameSanitizer
|
||||
{
|
||||
private const char ReplaceChar = '_';
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
|
||||
public MappingFileNameSanitizer(WireMockServerSettings settings)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates sanitized file names for mappings
|
||||
/// </summary>
|
||||
public string BuildSanitizedFileName(IMapping mapping)
|
||||
{
|
||||
string name;
|
||||
if (!string.IsNullOrEmpty(mapping.Title))
|
||||
{
|
||||
// remove 'Proxy Mapping for ' and an extra space character after the HTTP request method
|
||||
name = mapping.Title.Replace(ProxyAndRecordSettings.DefaultPrefixForSavedMappingFile, "").Replace(' '.ToString(), string.Empty);
|
||||
if (_settings.ProxyAndRecordSettings?.AppendGuidToSavedMappingFile == true)
|
||||
{
|
||||
name += $"{ReplaceChar}{mapping.Guid}";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
name = mapping.Guid.ToString();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_settings.ProxyAndRecordSettings?.PrefixForSavedMappingFile))
|
||||
{
|
||||
name = $"{_settings.ProxyAndRecordSettings.PrefixForSavedMappingFile}{ReplaceChar}{name}";
|
||||
}
|
||||
return $"{Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, ReplaceChar))}.json";
|
||||
}
|
||||
}
|
||||
61
src/WireMock.Net.Minimal/Serialization/MappingToFileSaver.cs
Normal file
61
src/WireMock.Net.Minimal/Serialization/MappingToFileSaver.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Stef.Validation;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class MappingToFileSaver
|
||||
{
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly MappingFileNameSanitizer _fileNameSanitizer;
|
||||
|
||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
_mappingConverter = Guard.NotNull(mappingConverter);
|
||||
_fileNameSanitizer = new MappingFileNameSanitizer(settings);
|
||||
}
|
||||
|
||||
public void SaveMappingsToFile(IMapping[] mappings, string? folder = null)
|
||||
{
|
||||
folder ??= _settings.FileSystemHandler.GetMappingFolder();
|
||||
|
||||
if (!_settings.FileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
_settings.FileSystemHandler.CreateFolder(folder);
|
||||
}
|
||||
|
||||
var models = mappings.Select(_mappingConverter.ToMappingModel).ToArray();
|
||||
|
||||
Save(models, folder);
|
||||
}
|
||||
|
||||
public void SaveMappingToFile(IMapping mapping, string? folder = null)
|
||||
{
|
||||
folder ??= _settings.FileSystemHandler.GetMappingFolder();
|
||||
|
||||
if (!_settings.FileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
_settings.FileSystemHandler.CreateFolder(folder);
|
||||
}
|
||||
|
||||
var model = _mappingConverter.ToMappingModel(mapping);
|
||||
|
||||
var filename = _fileNameSanitizer.BuildSanitizedFileName(mapping);
|
||||
var path = Path.Combine(folder, filename);
|
||||
|
||||
Save(model, path);
|
||||
}
|
||||
|
||||
private void Save(object value, string path)
|
||||
{
|
||||
_settings.Logger.Info("Saving Mapping file {0}", path);
|
||||
|
||||
_settings.FileSystemHandler.WriteMappingFile(path, JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsDefault));
|
||||
}
|
||||
}
|
||||
301
src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs
Normal file
301
src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs
Normal file
@@ -0,0 +1,301 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AnyOfTypes;
|
||||
using SimMetrics.Net;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Models;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class MatcherMapper
|
||||
{
|
||||
private readonly WireMockServerSettings _settings;
|
||||
|
||||
public MatcherMapper(WireMockServerSettings settings)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
}
|
||||
|
||||
public IMatcher[]? Map(IEnumerable<MatcherModel>? matchers)
|
||||
{
|
||||
return matchers?.Select(Map).OfType<IMatcher>().ToArray();
|
||||
}
|
||||
|
||||
public IMatcher? Map(MatcherModel? matcherModel)
|
||||
{
|
||||
if (matcherModel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] parts = matcherModel.Name.Split('.');
|
||||
string matcherName = parts[0];
|
||||
string? matcherType = parts.Length > 1 ? parts[1] : null;
|
||||
var stringPatterns = ParseStringPatterns(matcherModel);
|
||||
var matchBehaviour = matcherModel.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
||||
var matchOperator = StringUtils.ParseMatchOperator(matcherModel.MatchOperator);
|
||||
bool ignoreCase = matcherModel.IgnoreCase == true;
|
||||
bool useRegexExtended = _settings.UseRegexExtended == true;
|
||||
bool useRegex = matcherModel.Regex == true;
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
case nameof(NotNullOrEmptyMatcher):
|
||||
return new NotNullOrEmptyMatcher(matchBehaviour);
|
||||
|
||||
case "CSharpCodeMatcher":
|
||||
if (_settings.AllowCSharpCodeMatcher == true)
|
||||
{
|
||||
return TypeLoader.LoadNewInstance<ICSharpCodeMatcher>(matchBehaviour, matchOperator, stringPatterns);
|
||||
}
|
||||
|
||||
throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because WireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'.");
|
||||
|
||||
case "LinqMatcher":
|
||||
throw new NotSupportedException("It's not allowed to use the 'LinqMatcher' due to CVE.");
|
||||
|
||||
//case nameof(LinqMatcher):
|
||||
// return new LinqMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||
|
||||
case nameof(ExactMatcher):
|
||||
return new ExactMatcher(matchBehaviour, ignoreCase, matchOperator, stringPatterns);
|
||||
|
||||
case nameof(ExactObjectMatcher):
|
||||
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0]);
|
||||
#if GRAPHQL
|
||||
case nameof(GraphQLMatcher):
|
||||
return new GraphQLMatcher(stringPatterns[0].GetPattern(), matcherModel.CustomScalars, matchBehaviour, matchOperator);
|
||||
#endif
|
||||
|
||||
case "MimePartMatcher":
|
||||
return CreateMimePartMatcher(matchBehaviour, matcherModel);
|
||||
|
||||
#if PROTOBUF
|
||||
case nameof(ProtoBufMatcher):
|
||||
return CreateProtoBufMatcher(matchBehaviour, stringPatterns.GetPatterns(), matcherModel);
|
||||
#endif
|
||||
case nameof(RegexMatcher):
|
||||
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, useRegexExtended, matchOperator);
|
||||
|
||||
case nameof(JsonMatcher):
|
||||
var valueForJsonMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonMatcher(matchBehaviour, valueForJsonMatcher!, ignoreCase, useRegex);
|
||||
|
||||
case nameof(JsonPartialMatcher):
|
||||
var valueForJsonPartialMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher!, ignoreCase, useRegex);
|
||||
|
||||
case nameof(JsonPartialWildcardMatcher):
|
||||
var valueForJsonPartialWildcardMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, useRegex);
|
||||
|
||||
case nameof(JsonPathMatcher):
|
||||
return new JsonPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||
|
||||
case nameof(JmesPathMatcher):
|
||||
return new JmesPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||
|
||||
case nameof(XPathMatcher):
|
||||
return new XPathMatcher(matchBehaviour, matchOperator, matcherModel.XmlNamespaceMap, stringPatterns);
|
||||
|
||||
case nameof(WildcardMatcher):
|
||||
return new WildcardMatcher(matchBehaviour, stringPatterns, ignoreCase, matchOperator);
|
||||
|
||||
case nameof(ContentTypeMatcher):
|
||||
return new ContentTypeMatcher(matchBehaviour, stringPatterns, ignoreCase);
|
||||
|
||||
case nameof(FormUrlEncodedMatcher):
|
||||
return new FormUrlEncodedMatcher(matchBehaviour, stringPatterns, ignoreCase);
|
||||
|
||||
case nameof(SimMetricsMatcher):
|
||||
SimMetricType type = SimMetricType.Levenstein;
|
||||
if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
|
||||
{
|
||||
throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
|
||||
}
|
||||
|
||||
return new SimMetricsMatcher(matchBehaviour, stringPatterns, type);
|
||||
|
||||
default:
|
||||
if (_settings.CustomMatcherMappings != null && _settings.CustomMatcherMappings.ContainsKey(matcherName))
|
||||
{
|
||||
return _settings.CustomMatcherMappings[matcherName](matcherModel);
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public MatcherModel[]? Map(IEnumerable<IMatcher>? matchers, Action<MatcherModel>? afterMap = null)
|
||||
{
|
||||
return matchers?.Select(m => Map(m, afterMap)).OfType<MatcherModel>().ToArray();
|
||||
}
|
||||
|
||||
public MatcherModel? Map(IMatcher? matcher, Action<MatcherModel>? afterMap = null)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
bool? ignoreCase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : null;
|
||||
bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null;
|
||||
|
||||
var model = new MatcherModel
|
||||
{
|
||||
RejectOnMatch = rejectOnMatch,
|
||||
IgnoreCase = ignoreCase,
|
||||
Name = matcher.Name
|
||||
};
|
||||
|
||||
switch (matcher)
|
||||
{
|
||||
case JsonMatcher jsonMatcher:
|
||||
model.Regex = jsonMatcher.Regex;
|
||||
break;
|
||||
|
||||
case XPathMatcher xpathMatcher:
|
||||
model.XmlNamespaceMap = xpathMatcher.XmlNamespaceMap;
|
||||
break;
|
||||
#if GRAPHQL
|
||||
case GraphQLMatcher graphQLMatcher:
|
||||
model.CustomScalars = graphQLMatcher.CustomScalars;
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
switch (matcher)
|
||||
{
|
||||
// If the matcher is a IStringMatcher, get the operator & patterns.
|
||||
case IStringMatcher stringMatcher:
|
||||
var stringPatterns = stringMatcher.GetPatterns();
|
||||
if (stringPatterns.Length == 1)
|
||||
{
|
||||
if (stringPatterns[0].IsFirst)
|
||||
{
|
||||
model.Pattern = stringPatterns[0].First;
|
||||
}
|
||||
else
|
||||
{
|
||||
model.Pattern = stringPatterns[0].Second.Pattern;
|
||||
model.PatternAsFile = stringPatterns[0].Second.PatternAsFile;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.Patterns = stringPatterns.Select(p => p.GetPattern()).Cast<object>().ToArray();
|
||||
model.MatchOperator = stringMatcher.MatchOperator.ToString();
|
||||
}
|
||||
break;
|
||||
|
||||
// If the matcher is a IObjectMatcher, get the value (can be string or object or byte[]).
|
||||
case IObjectMatcher objectMatcher:
|
||||
model.Pattern = objectMatcher.Value;
|
||||
break;
|
||||
|
||||
case IMimePartMatcher mimePartMatcher:
|
||||
model.ContentDispositionMatcher = Map(mimePartMatcher.ContentDispositionMatcher);
|
||||
model.ContentMatcher = Map(mimePartMatcher.ContentMatcher);
|
||||
model.ContentTransferEncodingMatcher = Map(mimePartMatcher.ContentTransferEncodingMatcher);
|
||||
model.ContentTypeMatcher = Map(mimePartMatcher.ContentTypeMatcher);
|
||||
break;
|
||||
|
||||
#if PROTOBUF
|
||||
case ProtoBufMatcher protoBufMatcher:
|
||||
protoBufMatcher.ProtoDefinition().Value(id => model.Pattern = id, texts =>
|
||||
{
|
||||
if (texts.Count == 1)
|
||||
{
|
||||
model.Pattern = texts[0];
|
||||
}
|
||||
else if (texts.Count > 1)
|
||||
{
|
||||
model.Patterns = texts.Cast<object>().ToArray();
|
||||
}
|
||||
});
|
||||
|
||||
model.ProtoBufMessageType = protoBufMatcher.MessageType;
|
||||
model.ContentMatcher = Map(protoBufMatcher.Matcher);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
afterMap?.Invoke(model);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
private AnyOf<string, StringPattern>[] ParseStringPatterns(MatcherModel matcher)
|
||||
{
|
||||
if (matcher.Pattern is string patternAsString)
|
||||
{
|
||||
return [new AnyOf<string, StringPattern>(patternAsString)];
|
||||
}
|
||||
|
||||
if (matcher.Pattern is IEnumerable<string> patternAsStringArray)
|
||||
{
|
||||
return patternAsStringArray.ToAnyOfPatterns();
|
||||
}
|
||||
|
||||
if (matcher.Patterns?.OfType<string>() is { } patternsAsStringArray)
|
||||
{
|
||||
return patternsAsStringArray.ToAnyOfPatterns();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(matcher.PatternAsFile))
|
||||
{
|
||||
var patternAsFile = matcher.PatternAsFile!;
|
||||
var pattern = _settings.FileSystemHandler.ReadFileAsString(patternAsFile);
|
||||
return [new AnyOf<string, StringPattern>(new StringPattern { Pattern = pattern, PatternAsFile = patternAsFile })];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private static ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, AnyOf<string, StringPattern> stringPattern)
|
||||
{
|
||||
byte[] bytePattern;
|
||||
try
|
||||
{
|
||||
bytePattern = Convert.FromBase64String(stringPattern.GetPattern());
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new ArgumentException($"Matcher 'ExactObjectMatcher' has invalid pattern. The pattern value '{stringPattern}' is not a Base64String.", nameof(stringPattern));
|
||||
}
|
||||
|
||||
return new ExactObjectMatcher(matchBehaviour, bytePattern);
|
||||
}
|
||||
|
||||
private IMimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel matcher)
|
||||
{
|
||||
var contentTypeMatcher = Map(matcher?.ContentTypeMatcher) as IStringMatcher;
|
||||
var contentDispositionMatcher = Map(matcher?.ContentDispositionMatcher) as IStringMatcher;
|
||||
var contentTransferEncodingMatcher = Map(matcher?.ContentTransferEncodingMatcher) as IStringMatcher;
|
||||
var contentMatcher = Map(matcher?.ContentMatcher);
|
||||
|
||||
return TypeLoader.LoadNewInstance<IMimePartMatcher>(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
|
||||
}
|
||||
|
||||
#if PROTOBUF
|
||||
private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, IReadOnlyList<string> protoDefinitions, MatcherModel matcher)
|
||||
{
|
||||
var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher;
|
||||
|
||||
return new ProtoBufMatcher(
|
||||
() => ProtoDefinitionHelper.GetIdOrTexts(_settings, protoDefinitions.ToArray()),
|
||||
matcher.ProtoBufMessageType!,
|
||||
matchBehaviour ?? MatchBehaviour.AcceptOnMatch,
|
||||
objectMatcher
|
||||
);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
174
src/WireMock.Net.Minimal/Serialization/PactMapper.cs
Normal file
174
src/WireMock.Net.Minimal/Serialization/PactMapper.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Pact.Models.V2;
|
||||
using WireMock.Server;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal static class PactMapper
|
||||
{
|
||||
private const string DefaultMethod = "GET";
|
||||
private const int DefaultStatusCode = 200;
|
||||
private const string DefaultConsumer = "Default Consumer";
|
||||
private const string DefaultProvider = "Default Provider";
|
||||
|
||||
public static (string FileName, byte[] Bytes) ToPact(WireMockServer server, string? filename = null)
|
||||
{
|
||||
var consumer = server.Consumer ?? DefaultConsumer;
|
||||
var provider = server.Provider ?? DefaultProvider;
|
||||
|
||||
filename ??= $"{consumer} - {provider}.json";
|
||||
|
||||
var pact = new Pact.Models.V2.Pact
|
||||
{
|
||||
Consumer = new Pacticipant { Name = consumer },
|
||||
Provider = new Pacticipant { Name = provider }
|
||||
};
|
||||
|
||||
foreach (var mapping in server.MappingModels.OrderBy(m => m.Guid))
|
||||
{
|
||||
var path = mapping.Request.GetPathAsString();
|
||||
if (path == null)
|
||||
{
|
||||
// Path is null (probably a Func<>), skip this.
|
||||
continue;
|
||||
}
|
||||
|
||||
var interaction = new Interaction
|
||||
{
|
||||
Description = mapping.Description,
|
||||
ProviderState = mapping.Title,
|
||||
Request = MapRequest(mapping.Request, path),
|
||||
Response = MapResponse(mapping.Response)
|
||||
};
|
||||
|
||||
pact.Interactions.Add(interaction);
|
||||
}
|
||||
|
||||
return (filename, JsonUtils.SerializeAsPactFile(pact));
|
||||
}
|
||||
|
||||
private static PactRequest MapRequest(RequestModel request, string path)
|
||||
{
|
||||
return new PactRequest
|
||||
{
|
||||
Method = request.Methods?.FirstOrDefault() ?? DefaultMethod,
|
||||
Path = path,
|
||||
Query = MapQueryParameters(request.Params),
|
||||
Headers = MapRequestHeaders(request.Headers),
|
||||
Body = MapBody(request.Body)
|
||||
};
|
||||
}
|
||||
|
||||
private static PactResponse MapResponse(ResponseModel? response)
|
||||
{
|
||||
if (response == null)
|
||||
{
|
||||
return new PactResponse();
|
||||
}
|
||||
|
||||
return new PactResponse
|
||||
{
|
||||
Status = MapStatusCode(response.StatusCode),
|
||||
Headers = MapResponseHeaders(response.Headers),
|
||||
Body = MapBody(response)
|
||||
};
|
||||
}
|
||||
|
||||
private static object? MapBody(ResponseModel? response)
|
||||
{
|
||||
if (response?.BodyAsJson != null)
|
||||
{
|
||||
return response.BodyAsJson;
|
||||
}
|
||||
|
||||
if (response?.Body != null)
|
||||
{
|
||||
return TryDeserializeJsonStringAsObject(response.Body);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int MapStatusCode(object? statusCode)
|
||||
{
|
||||
if (statusCode is string statusCodeAsString)
|
||||
{
|
||||
return int.TryParse(statusCodeAsString, out var statusCodeAsInt) ? statusCodeAsInt : DefaultStatusCode;
|
||||
}
|
||||
|
||||
if (statusCode != null)
|
||||
{
|
||||
// Convert to Int32 because Newtonsoft deserializes an 'object' with a number value to a long.
|
||||
return Convert.ToInt32(statusCode);
|
||||
}
|
||||
|
||||
return DefaultStatusCode;
|
||||
}
|
||||
|
||||
private static string? MapQueryParameters(IList<ParamModel>? queryParameters)
|
||||
{
|
||||
if (queryParameters == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var values = queryParameters
|
||||
.Where(qp => qp.Matchers != null && qp.Matchers.Any() && qp.Matchers[0].Pattern is string)
|
||||
.Select(param => $"{Uri.EscapeDataString(param.Name)}={Uri.EscapeDataString((string)param.Matchers![0].Pattern!)}");
|
||||
|
||||
return string.Join("&", values);
|
||||
}
|
||||
|
||||
private static IDictionary<string, string>? MapRequestHeaders(IList<HeaderModel>? headers)
|
||||
{
|
||||
var validHeaders = headers?.Where(h => h.Matchers != null && h.Matchers.Any() && h.Matchers[0].Pattern is string);
|
||||
return validHeaders?.ToDictionary(x => x.Name, y => (string)y.Matchers![0].Pattern!);
|
||||
}
|
||||
|
||||
private static IDictionary<string, string>? MapResponseHeaders(IDictionary<string, object>? headers)
|
||||
{
|
||||
var validHeaders = headers?.Where(h => h.Value is string);
|
||||
return validHeaders?.ToDictionary(x => x.Key, y => (string)y.Value);
|
||||
}
|
||||
|
||||
private static object? MapBody(BodyModel? body)
|
||||
{
|
||||
return MapMatcherPattern(body?.Matcher ?? body?.Matchers?.FirstOrDefault());
|
||||
}
|
||||
|
||||
private static object? MapMatcherPattern(MatcherModel? matcher)
|
||||
{
|
||||
var pattern = matcher?.Pattern ?? matcher?.Patterns?.FirstOrDefault();
|
||||
if (pattern is string patternAsString)
|
||||
{
|
||||
return TryDeserializeJsonStringAsObject(patternAsString);
|
||||
}
|
||||
|
||||
return pattern;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// In case it's a string, try to deserialize into object, else just return the string
|
||||
/// </summary>
|
||||
private static object? TryDeserializeJsonStringAsObject(string? value)
|
||||
{
|
||||
return value != null ? JsonUtils.TryDeserializeObject<object?>(value) ?? value : null;
|
||||
}
|
||||
|
||||
//private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue)
|
||||
//{
|
||||
// if (matchers != null && matchers.Any() && matchers[0].Pattern is string patternAsString)
|
||||
// {
|
||||
// return patternAsString;
|
||||
// }
|
||||
|
||||
// return defaultValue;
|
||||
//}
|
||||
}
|
||||
207
src/WireMock.Net.Minimal/Serialization/ProxyMappingConverter.cs
Normal file
207
src/WireMock.Net.Minimal/Serialization/ProxyMappingConverter.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class ProxyMappingConverter
|
||||
{
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly IGuidUtils _guidUtils;
|
||||
private readonly IDateTimeUtils _dateTimeUtils;
|
||||
|
||||
public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUtils, IDateTimeUtils dateTimeUtils)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
_guidUtils = Guard.NotNull(guidUtils);
|
||||
_dateTimeUtils = Guard.NotNull(dateTimeUtils);
|
||||
}
|
||||
|
||||
public IMapping? ToMapping(IMapping? mapping, ProxyAndRecordSettings proxyAndRecordSettings, IRequestMessage requestMessage, ResponseMessage responseMessage)
|
||||
{
|
||||
var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers;
|
||||
var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? []) { "Cookie" };
|
||||
var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? [];
|
||||
var excludedParams = proxyAndRecordSettings.ExcludedParams ?? [];
|
||||
|
||||
var request = (Request?)mapping?.RequestMatcher;
|
||||
var clientIPMatcher = request?.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
|
||||
var pathMatcher = request?.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
||||
var headerMatchers = request?.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||
var cookieMatchers = request?.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
||||
var paramMatchers = request?.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
||||
var methodMatcher = request?.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||
var bodyMatcher = request?.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||
var httpVersionMatcher = request?.GetRequestMessageMatcher<RequestMessageHttpVersionMatcher>();
|
||||
|
||||
var newRequest = Request.Create();
|
||||
|
||||
// ClientIP
|
||||
if (useDefinedRequestMatchers && clientIPMatcher?.Matchers is not null)
|
||||
{
|
||||
newRequest.WithClientIP(clientIPMatcher.MatchOperator, clientIPMatcher.Matchers.ToArray());
|
||||
}
|
||||
|
||||
// Path
|
||||
if (useDefinedRequestMatchers && pathMatcher?.Matchers is not null)
|
||||
{
|
||||
newRequest.WithPath(pathMatcher.MatchOperator, pathMatcher.Matchers.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
newRequest.WithPath(requestMessage.Path);
|
||||
}
|
||||
|
||||
// Method
|
||||
if (useDefinedRequestMatchers && methodMatcher is not null)
|
||||
{
|
||||
newRequest.UsingMethod(methodMatcher.Methods);
|
||||
}
|
||||
else
|
||||
{
|
||||
newRequest.UsingMethod(requestMessage.Method);
|
||||
}
|
||||
|
||||
// HttpVersion
|
||||
if (useDefinedRequestMatchers && httpVersionMatcher?.HttpVersion is not null)
|
||||
{
|
||||
newRequest.WithHttpVersion(httpVersionMatcher.HttpVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
newRequest.WithHttpVersion(requestMessage.HttpVersion);
|
||||
}
|
||||
|
||||
// QueryParams
|
||||
if (useDefinedRequestMatchers && paramMatchers is not null)
|
||||
{
|
||||
foreach (var paramMatcher in paramMatchers)
|
||||
{
|
||||
if (!excludedParams.Contains(paramMatcher.Key, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithParam(paramMatcher.Key, paramMatcher.MatchBehaviour, paramMatcher.Matchers!.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
requestMessage.Query?.Loop((key, value) =>
|
||||
{
|
||||
if (!excludedParams.Contains(key, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithParam(key, false, value.ToArray());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Cookies
|
||||
if (useDefinedRequestMatchers && cookieMatchers is not null)
|
||||
{
|
||||
foreach (var cookieMatcher in cookieMatchers.Where(hm => hm.Matchers is not null))
|
||||
{
|
||||
if (!excludedCookies.Contains(cookieMatcher.Name, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithCookie(cookieMatcher.Name, cookieMatcher.Matchers!);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
requestMessage.Cookies?.Loop((key, value) =>
|
||||
{
|
||||
if (!excludedCookies.Contains(key, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithCookie(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Headers
|
||||
if (useDefinedRequestMatchers && headerMatchers is not null)
|
||||
{
|
||||
foreach (var headerMatcher in headerMatchers.Where(hm => hm.Matchers is not null))
|
||||
{
|
||||
if (!excludedHeaders.Contains(headerMatcher.Name, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithHeader(headerMatcher.Name, headerMatcher.Matchers!);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
requestMessage.Headers?.Loop((key, value) =>
|
||||
{
|
||||
if (!excludedHeaders.Contains(key, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
newRequest.WithHeader(key, value.ToArray());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Body
|
||||
if (useDefinedRequestMatchers && bodyMatcher?.Matchers is not null)
|
||||
{
|
||||
newRequest.WithBody(bodyMatcher.Matchers);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
case BodyType.Json:
|
||||
newRequest.WithBody(new JsonMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsJson!, true));
|
||||
break;
|
||||
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
newRequest.WithBody(new ExactMatcher(MatchBehaviour.AcceptOnMatch, true, MatchOperator.Or, requestMessage.BodyData.BodyAsString!));
|
||||
break;
|
||||
|
||||
case BodyType.Bytes:
|
||||
newRequest.WithBody(new ExactObjectMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsBytes!));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Title
|
||||
var title = useDefinedRequestMatchers && !string.IsNullOrEmpty(mapping?.Title) ?
|
||||
mapping!.Title :
|
||||
$"Proxy Mapping for {requestMessage.Method} {requestMessage.Path}";
|
||||
|
||||
// Description
|
||||
var description = useDefinedRequestMatchers && !string.IsNullOrEmpty(mapping?.Description) ?
|
||||
mapping!.Description :
|
||||
$"Proxy Mapping for {requestMessage.Method} {requestMessage.Path}";
|
||||
|
||||
return new Mapping
|
||||
(
|
||||
guid: _guidUtils.NewGuid(),
|
||||
updatedAt: _dateTimeUtils.UtcNow,
|
||||
title: title,
|
||||
description: description,
|
||||
path: null,
|
||||
settings: _settings,
|
||||
requestMatcher: newRequest,
|
||||
provider: Response.Create(responseMessage),
|
||||
priority: WireMockConstants.ProxyPriority, // This was 0
|
||||
scenario: null,
|
||||
executionConditionState: null,
|
||||
nextState: null,
|
||||
stateTimes: null,
|
||||
webhooks: null,
|
||||
useWebhooksFireAndForget: null,
|
||||
timeSettings: null,
|
||||
data: mapping?.Data
|
||||
);
|
||||
}
|
||||
}
|
||||
329
src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs
Normal file
329
src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs
Normal file
@@ -0,0 +1,329 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NJsonSchema;
|
||||
using NJsonSchema.Extensions;
|
||||
using NSwag;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Server;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal static class SwaggerMapper
|
||||
{
|
||||
private const string DefaultMethod = "GET";
|
||||
private const string Generator = "WireMock.Net";
|
||||
|
||||
private static readonly JsonSchema JsonSchemaString = new() { Type = JsonObjectType.String };
|
||||
|
||||
public static string ToSwagger(WireMockServer server)
|
||||
{
|
||||
var openApiDocument = new OpenApiDocument
|
||||
{
|
||||
Generator = Generator,
|
||||
Info = new OpenApiInfo
|
||||
{
|
||||
Title = $"{Generator} Mappings Swagger specification",
|
||||
Version = SystemUtils.Version
|
||||
},
|
||||
};
|
||||
|
||||
foreach (var url in server.Urls)
|
||||
{
|
||||
openApiDocument.Servers.Add(new OpenApiServer
|
||||
{
|
||||
Url = url
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var mapping in server.MappingModels)
|
||||
{
|
||||
var path = mapping.Request.GetPathAsString();
|
||||
if (path == null)
|
||||
{
|
||||
// Path is null (probably a Func<>), skip this.
|
||||
continue;
|
||||
}
|
||||
|
||||
var operation = new OpenApiOperation();
|
||||
foreach (var openApiParameter in MapRequestQueryParameters(mapping.Request.Params))
|
||||
{
|
||||
operation.Parameters.Add(openApiParameter);
|
||||
}
|
||||
|
||||
foreach (var openApiParameter in MapRequestHeaders(mapping.Request.Headers))
|
||||
{
|
||||
operation.Parameters.Add(openApiParameter);
|
||||
}
|
||||
|
||||
foreach (var openApiParameter in MapRequestCookies(mapping.Request.Cookies))
|
||||
{
|
||||
operation.Parameters.Add(openApiParameter);
|
||||
}
|
||||
|
||||
operation.RequestBody = MapRequestBody(mapping.Request);
|
||||
|
||||
var response = MapResponse(mapping.Response);
|
||||
if (response != null)
|
||||
{
|
||||
operation.Responses.Add(mapping.Response.GetStatusCodeAsString(), response);
|
||||
}
|
||||
|
||||
var method = mapping.Request.Methods?.FirstOrDefault() ?? DefaultMethod;
|
||||
if (!openApiDocument.Paths.ContainsKey(path))
|
||||
{
|
||||
var openApiPathItem = new OpenApiPathItem
|
||||
{
|
||||
{ method, operation }
|
||||
};
|
||||
|
||||
openApiDocument.Paths.Add(path, openApiPathItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
// The combination of path+method uniquely identify an operation. Duplicates are not allowed.
|
||||
if (!openApiDocument.Paths[path].ContainsKey(method))
|
||||
{
|
||||
openApiDocument.Paths[path].Add(method, operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return openApiDocument.ToJson(SchemaType.OpenApi3, Formatting.Indented);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<OpenApiParameter> MapRequestQueryParameters(IList<ParamModel>? queryParameters)
|
||||
{
|
||||
if (queryParameters == null)
|
||||
{
|
||||
return new List<OpenApiParameter>();
|
||||
}
|
||||
|
||||
return queryParameters
|
||||
.Where(x => x.Matchers != null && x.Matchers.Any())
|
||||
.Select(x => new
|
||||
{
|
||||
x.Name,
|
||||
Details = GetDetailsFromMatcher(x.Matchers![0])
|
||||
})
|
||||
.Select(x => new OpenApiParameter
|
||||
{
|
||||
Name = x.Name,
|
||||
Example = x.Details.Example,
|
||||
Description = x.Details.Description,
|
||||
Kind = OpenApiParameterKind.Query,
|
||||
Schema = x.Details.JsonSchemaRegex,
|
||||
IsRequired = !x.Details.Reject
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static IEnumerable<OpenApiParameter> MapRequestHeaders(IList<HeaderModel>? headers)
|
||||
{
|
||||
if (headers == null)
|
||||
{
|
||||
return new List<OpenApiHeader>();
|
||||
}
|
||||
|
||||
return headers
|
||||
.Where(x => x.Matchers != null && x.Matchers.Any())
|
||||
.Select(x => new
|
||||
{
|
||||
x.Name,
|
||||
Details = GetDetailsFromMatcher(x.Matchers![0])
|
||||
})
|
||||
.Select(x => new OpenApiHeader
|
||||
{
|
||||
Name = x.Name,
|
||||
Example = x.Details.Example,
|
||||
Description = x.Details.Description,
|
||||
Kind = OpenApiParameterKind.Header,
|
||||
Schema = x.Details.JsonSchemaRegex,
|
||||
IsRequired = !x.Details.Reject
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static IReadOnlyList<OpenApiParameter> MapRequestCookies(IList<CookieModel>? cookies)
|
||||
{
|
||||
if (cookies == null)
|
||||
{
|
||||
return new List<OpenApiParameter>();
|
||||
}
|
||||
|
||||
return cookies
|
||||
.Where(x => x.Matchers != null && x.Matchers.Any())
|
||||
.Select(x => new
|
||||
{
|
||||
x.Name,
|
||||
Details = GetDetailsFromMatcher(x.Matchers![0])
|
||||
})
|
||||
.Select(x => new OpenApiParameter
|
||||
{
|
||||
Name = x.Name,
|
||||
Example = x.Details.Example,
|
||||
Description = x.Details.Description,
|
||||
Kind = OpenApiParameterKind.Cookie,
|
||||
Schema = x.Details.JsonSchemaRegex,
|
||||
IsRequired = !x.Details.Reject
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static (JsonSchema JsonSchemaRegex, string? Example, string? Description, bool Reject) GetDetailsFromMatcher(MatcherModel matcher)
|
||||
{
|
||||
var pattern = GetPatternAsStringFromMatcher(matcher);
|
||||
var reject = matcher.RejectOnMatch == true;
|
||||
var description = $"{matcher.Name} with RejectOnMatch = '{reject}' and Pattern = '{pattern}'";
|
||||
|
||||
return matcher.Name is nameof(RegexMatcher) ?
|
||||
(new JsonSchema { Type = JsonObjectType.String, Format = "regex", Pattern = pattern }, pattern, description, reject) :
|
||||
(JsonSchemaString, pattern, description, reject);
|
||||
}
|
||||
|
||||
private static OpenApiRequestBody? MapRequestBody(RequestModel request)
|
||||
{
|
||||
var body = MapRequestBody(request.Body);
|
||||
if (body == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var openApiMediaType = new OpenApiMediaType
|
||||
{
|
||||
Schema = GetJsonSchema(body)
|
||||
};
|
||||
|
||||
var requestBodyPost = new OpenApiRequestBody();
|
||||
requestBodyPost.Content.Add(GetContentType(request), openApiMediaType);
|
||||
|
||||
return requestBodyPost;
|
||||
}
|
||||
|
||||
private static OpenApiResponse? MapResponse(ResponseModel response)
|
||||
{
|
||||
if (response.Body != null)
|
||||
{
|
||||
return new OpenApiResponse
|
||||
{
|
||||
Schema = new JsonSchemaProperty
|
||||
{
|
||||
Type = JsonObjectType.String,
|
||||
Example = response.Body
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (response.BodyAsBytes != null)
|
||||
{
|
||||
// https://stackoverflow.com/questions/62794949/how-to-define-byte-array-in-openapi-3-0
|
||||
return new OpenApiResponse
|
||||
{
|
||||
Schema = new JsonSchemaProperty
|
||||
{
|
||||
Type = JsonObjectType.Array,
|
||||
Items =
|
||||
{
|
||||
new JsonSchema
|
||||
{
|
||||
Type = JsonObjectType.String,
|
||||
Format = JsonFormatStrings.Byte
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (response.BodyAsJson == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new OpenApiResponse
|
||||
{
|
||||
Schema = GetJsonSchema(response.BodyAsJson)
|
||||
};
|
||||
}
|
||||
|
||||
private static JsonSchema GetJsonSchema(object instance)
|
||||
{
|
||||
switch (instance)
|
||||
{
|
||||
case string instanceAsString:
|
||||
try
|
||||
{
|
||||
var value = JsonConvert.DeserializeObject(instanceAsString);
|
||||
return GetJsonSchema(value!);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return JsonSchemaString;
|
||||
}
|
||||
|
||||
default:
|
||||
return instance.ToJsonSchema();
|
||||
}
|
||||
}
|
||||
|
||||
private static object? MapRequestBody(BodyModel? body)
|
||||
{
|
||||
if (body == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var matcher = GetMatcher(body.Matcher, body.Matchers);
|
||||
if (matcher is { Name: nameof(JsonMatcher) })
|
||||
{
|
||||
var pattern = GetPatternAsStringFromMatcher(matcher);
|
||||
if (JsonUtils.TryParseAsJObject(pattern, out var jObject))
|
||||
{
|
||||
return jObject;
|
||||
}
|
||||
|
||||
return pattern;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string GetContentType(RequestModel request)
|
||||
{
|
||||
var contentType = request.Headers?.FirstOrDefault(h => h.Name == "Content-Type");
|
||||
|
||||
return contentType == null ?
|
||||
WireMockConstants.ContentTypeJson :
|
||||
GetPatternAsStringFromMatchers(contentType.Matchers, WireMockConstants.ContentTypeJson);
|
||||
}
|
||||
|
||||
private static string GetPatternAsStringFromMatchers(IList<MatcherModel>? matchers, string defaultValue)
|
||||
{
|
||||
if (matchers == null || !matchers.Any())
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return GetPatternAsStringFromMatcher(matchers.First()) ?? defaultValue;
|
||||
}
|
||||
|
||||
private static string? GetPatternAsStringFromMatcher(MatcherModel matcher)
|
||||
{
|
||||
if (matcher.Pattern is string patternAsString)
|
||||
{
|
||||
return patternAsString;
|
||||
}
|
||||
|
||||
return matcher.Patterns?.FirstOrDefault() as string;
|
||||
}
|
||||
|
||||
private static MatcherModel? GetMatcher(MatcherModel? matcher, MatcherModel[]? matchers)
|
||||
{
|
||||
return matcher ?? matchers?.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
28
src/WireMock.Net.Minimal/Serialization/TimeSettingsMapper.cs
Normal file
28
src/WireMock.Net.Minimal/Serialization/TimeSettingsMapper.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using WireMock.Models;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal static class TimeSettingsMapper
|
||||
{
|
||||
public static TimeSettingsModel? Map(ITimeSettings? settings)
|
||||
{
|
||||
return settings != null ? new TimeSettingsModel
|
||||
{
|
||||
Start = settings.Start,
|
||||
End = settings.End,
|
||||
TTL = settings.TTL
|
||||
} : null;
|
||||
}
|
||||
|
||||
public static ITimeSettings? Map(TimeSettingsModel? settings)
|
||||
{
|
||||
return settings != null ? new TimeSettings
|
||||
{
|
||||
Start = settings.Start,
|
||||
End = settings.End,
|
||||
TTL = settings.TTL
|
||||
} : null;
|
||||
}
|
||||
}
|
||||
118
src/WireMock.Net.Minimal/Serialization/WebhookMapper.cs
Normal file
118
src/WireMock.Net.Minimal/Serialization/WebhookMapper.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Http;
|
||||
using WireMock.Models;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal static class WebhookMapper
|
||||
{
|
||||
public static IWebhook Map(WebhookModel model)
|
||||
{
|
||||
var webhook = new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
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>>()
|
||||
}
|
||||
};
|
||||
|
||||
if (model.Request.UseTransformer == true)
|
||||
{
|
||||
webhook.Request.UseTransformer = true;
|
||||
|
||||
if (!Enum.TryParse<TransformerType>(model.Request.TransformerType, out var transformerType))
|
||||
{
|
||||
transformerType = TransformerType.Handlebars;
|
||||
}
|
||||
webhook.Request.TransformerType = transformerType;
|
||||
|
||||
if (!Enum.TryParse<ReplaceNodeOptions>(model.Request.TransformerReplaceNodeOptions, out var option))
|
||||
{
|
||||
option = ReplaceNodeOptions.EvaluateAndTryToConvert;
|
||||
}
|
||||
webhook.Request.TransformerReplaceNodeOptions = option;
|
||||
}
|
||||
|
||||
IEnumerable<string>? contentTypeHeader = null;
|
||||
if (webhook.Request.Headers != null && webhook.Request.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
contentTypeHeader = webhook.Request.Headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
}
|
||||
|
||||
if (model.Request.Body != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = model.Request.Body,
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
else if (model.Request.BodyAsJson != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = model.Request.BodyAsJson,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
|
||||
return webhook;
|
||||
}
|
||||
|
||||
public static WebhookModel Map(IWebhook webhook)
|
||||
{
|
||||
Guard.NotNull(webhook);
|
||||
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = webhook.Request.Url,
|
||||
Method = webhook.Request.Method,
|
||||
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(),
|
||||
Delay = webhook.Request.Delay,
|
||||
MinimumRandomDelay = webhook.Request.MinimumRandomDelay,
|
||||
MaximumRandomDelay = webhook.Request.MaximumRandomDelay,
|
||||
}
|
||||
};
|
||||
|
||||
if (webhook.Request.BodyData != null)
|
||||
{
|
||||
switch (webhook.Request.BodyData.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
model.Request.Body = webhook.Request.BodyData.BodyAsString;
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
model.Request.BodyAsJson = webhook.Request.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
default:
|
||||
// Empty
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user