mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-09 18:33:33 +02:00
Add Grpc ProtoBuf support (request-response) (#1047)
* ProtoBuf
* .
* x
* ---
* x
* fx
* ...
* sc
* ...
* .
* groen
* x
* fix tests
* ok!?
* fix tests
* fix tests
* !
* x
* 6
* .
* x
* ivaluematcher
* transformer
* .
* sc
* .
* mapping
* x
* tra
* com
* ...
* .
* .
* .
* AddProtoDefinition
* .
* set
* grpahj
* .
* .
* IdOrText
* ...
* async
* async2
* .
* t
* nuget
* <PackageReference Include="ProtoBufJsonConverter" Version="0.2.0-preview-04" />
* http version
* tests
* .WithHttpVersion("2")
* <PackageReference Include="ProtoBufJsonConverter" Version="0.2.0" />
* HttpVersionParser
This commit is contained in:
@@ -25,29 +25,25 @@ internal class MatcherMapper
|
||||
|
||||
public IMatcher[]? Map(IEnumerable<MatcherModel>? matchers)
|
||||
{
|
||||
if (matchers == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return matchers.Select(Map).Where(m => m != null).ToArray()!;
|
||||
return matchers?.Select(Map).OfType<IMatcher>().ToArray();
|
||||
}
|
||||
|
||||
public IMatcher? Map(MatcherModel? matcher)
|
||||
public IMatcher? Map(MatcherModel? matcherModel)
|
||||
{
|
||||
if (matcher == null)
|
||||
if (matcherModel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] parts = matcher.Name.Split('.');
|
||||
string[] parts = matcherModel.Name.Split('.');
|
||||
string matcherName = parts[0];
|
||||
string? matcherType = parts.Length > 1 ? parts[1] : null;
|
||||
var stringPatterns = ParseStringPatterns(matcher);
|
||||
var matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
||||
var matchOperator = StringUtils.ParseMatchOperator(matcher.MatchOperator);
|
||||
bool ignoreCase = matcher.IgnoreCase == true;
|
||||
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 = matcher.Regex == true;
|
||||
bool useRegex = matcherModel.Regex == true;
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
@@ -72,26 +68,31 @@ internal class MatcherMapper
|
||||
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0]);
|
||||
#if GRAPHQL
|
||||
case nameof(GraphQLMatcher):
|
||||
return new GraphQLMatcher(stringPatterns[0].GetPattern(), matcher.CustomScalars, matchBehaviour, matchOperator);
|
||||
return new GraphQLMatcher(stringPatterns[0].GetPattern(), matcherModel.CustomScalars, matchBehaviour, matchOperator);
|
||||
#endif
|
||||
|
||||
#if MIMEKIT
|
||||
case nameof(MimePartMatcher):
|
||||
return CreateMimePartMatcher(matchBehaviour, matcher);
|
||||
return CreateMimePartMatcher(matchBehaviour, matcherModel);
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
case nameof(ProtoBufMatcher):
|
||||
return CreateProtoBufMatcher(matchBehaviour, stringPatterns[0].GetPattern(), matcherModel);
|
||||
#endif
|
||||
case nameof(RegexMatcher):
|
||||
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, useRegexExtended, matchOperator);
|
||||
|
||||
case nameof(JsonMatcher):
|
||||
var valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns;
|
||||
var valueForJsonMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonMatcher(matchBehaviour, valueForJsonMatcher!, ignoreCase);
|
||||
|
||||
case nameof(JsonPartialMatcher):
|
||||
var valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns;
|
||||
var valueForJsonPartialMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher!, ignoreCase, useRegex);
|
||||
|
||||
case nameof(JsonPartialWildcardMatcher):
|
||||
var valueForJsonPartialWildcardMatcher = matcher.Pattern ?? matcher.Patterns;
|
||||
var valueForJsonPartialWildcardMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, useRegex);
|
||||
|
||||
case nameof(JsonPathMatcher):
|
||||
@@ -101,7 +102,7 @@ internal class MatcherMapper
|
||||
return new JmesPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||
|
||||
case nameof(XPathMatcher):
|
||||
return new XPathMatcher(matchBehaviour, matchOperator, matcher.XmlNamespaceMap, stringPatterns);
|
||||
return new XPathMatcher(matchBehaviour, matchOperator, matcherModel.XmlNamespaceMap, stringPatterns);
|
||||
|
||||
case nameof(WildcardMatcher):
|
||||
return new WildcardMatcher(matchBehaviour, stringPatterns, ignoreCase, matchOperator);
|
||||
@@ -121,19 +122,19 @@ internal class MatcherMapper
|
||||
default:
|
||||
if (_settings.CustomMatcherMappings != null && _settings.CustomMatcherMappings.ContainsKey(matcherName))
|
||||
{
|
||||
return _settings.CustomMatcherMappings[matcherName](matcher);
|
||||
return _settings.CustomMatcherMappings[matcherName](matcherModel);
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public MatcherModel[]? Map(IEnumerable<IMatcher>? matchers)
|
||||
public MatcherModel[]? Map(IEnumerable<IMatcher>? matchers, Action<MatcherModel>? afterMap = null)
|
||||
{
|
||||
return matchers?.Where(m => m != null).Select(Map).ToArray();
|
||||
return matchers?.Select(m => Map(m, afterMap)).OfType<MatcherModel>().ToArray();
|
||||
}
|
||||
|
||||
public MatcherModel? Map(IMatcher? matcher)
|
||||
public MatcherModel? Map(IMatcher? matcher, Action<MatcherModel>? afterMap = null)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
@@ -194,14 +195,9 @@ internal class MatcherMapper
|
||||
}
|
||||
break;
|
||||
|
||||
// If the matcher is a IValueMatcher, get the value (can be string or object).
|
||||
case IValueMatcher valueMatcher:
|
||||
model.Pattern = valueMatcher.Value;
|
||||
break;
|
||||
|
||||
// If the matcher is a ExactObjectMatcher, get the ValueAsObject or ValueAsBytes.
|
||||
case ExactObjectMatcher exactObjectMatcher:
|
||||
model.Pattern = exactObjectMatcher.ValueAsObject ?? exactObjectMatcher.ValueAsBytes;
|
||||
// If the matcher is a IObjectMatcher, get the value (can be string or object or byte[]).
|
||||
case IObjectMatcher objectMatcher:
|
||||
model.Pattern = objectMatcher.Value;
|
||||
break;
|
||||
|
||||
#if MIMEKIT
|
||||
@@ -212,8 +208,18 @@ internal class MatcherMapper
|
||||
model.ContentTypeMatcher = Map(mimePartMatcher.ContentTypeMatcher);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
case ProtoBufMatcher protoBufMatcher:
|
||||
model.Pattern = protoBufMatcher.ProtoDefinition().Value;
|
||||
model.ProtoBufMessageType = protoBufMatcher.MessageType;
|
||||
model.ContentMatcher = Map(protoBufMatcher.Matcher);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
afterMap?.Invoke(model);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
@@ -260,7 +266,7 @@ internal class MatcherMapper
|
||||
}
|
||||
|
||||
#if MIMEKIT
|
||||
private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel? matcher)
|
||||
private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel matcher)
|
||||
{
|
||||
var contentTypeMatcher = Map(matcher?.ContentTypeMatcher) as IStringMatcher;
|
||||
var contentDispositionMatcher = Map(matcher?.ContentDispositionMatcher) as IStringMatcher;
|
||||
@@ -270,4 +276,28 @@ internal class MatcherMapper
|
||||
return new MimePartMatcher(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, string protoDefinitionOrId, MatcherModel matcher)
|
||||
{
|
||||
var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher;
|
||||
|
||||
IdOrText protoDefinition;
|
||||
if (_settings.ProtoDefinitions?.TryGetValue(protoDefinitionOrId, out var protoDefinitionFromSettings) == true)
|
||||
{
|
||||
protoDefinition = new(protoDefinitionOrId, protoDefinitionFromSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
protoDefinition = new(null, protoDefinitionOrId);
|
||||
}
|
||||
|
||||
return new ProtoBufMatcher(
|
||||
() => protoDefinition,
|
||||
matcher!.ProtoBufMessageType!,
|
||||
matchBehaviour ?? MatchBehaviour.AcceptOnMatch,
|
||||
objectMatcher
|
||||
);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user