From f9a05714f70d4011cfd4f3c0bf985ce49b14839b Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 24 Aug 2025 09:30:48 +0200 Subject: [PATCH 1/3] Create WireMock.Net.ProtoBuf project --- Directory.Build.props | 7 +- WireMock.Net Solution.sln | 7 ++ .../WireMock.Net.Abstractions.csproj | 22 ---- .../Owin/Mappers/OwinResponseMapper.cs | 5 +- .../RequestBuilders/Request.cs | 5 +- .../Serialization/MappingConverter.cs | 10 +- .../Serialization/MatcherMapper.cs | 11 +- .../Server/WireMockServer.ConvertMapping.cs | 6 +- .../WireMock.Net.Minimal.csproj | 14 +-- .../Matchers/ProtoBufMatcher.cs | 16 +-- .../Models/ProtoDefinitionData.cs | 0 .../Properties/AssemblyInfo.cs | 8 ++ .../IRequestBuilderExtensions.cs | 108 ++++++++++++++++++ .../Util/ProtoBufUtils.cs | 8 +- .../Util/ProtoDefinitionHelper.cs | 6 +- .../Util/WireMockProtoFileResolver.cs | 4 +- .../WireMock.Net.ProtoBuf.csproj | 37 ++++++ .../Extensions/StringExtensions.cs | 0 .../IMapping.cs | 0 .../Matchers/IProtoBufMatcher.cs | 17 +++ .../Request/RequestMessageProtoBufMatcher.cs | 7 +- .../Models/GraphQLSchemaDetails.cs | 0 .../Properties/AssemblyInfo.cs | 1 + .../RequestBuilders/IBodyRequestBuilder.cs | 2 +- .../IProtoBufRequestBuilder.cs | 67 ----------- .../RequestBuilders/IRequestBuilder.cs | 13 ++- .../ResponseProviders/IResponseProvider.cs | 2 +- .../Settings/HandlebarsSettings.cs | 0 .../Settings/HttpClientSettings.cs | 0 .../Settings/ProxyAndRecordSettings.cs | 0 .../Settings/ProxySaveMappingSetting.cs | 0 .../Settings/ProxySaveMappingSettings.cs | 0 .../Settings/ProxyUrlReplaceSettings.cs | 0 .../Settings/WebProxySettings.cs | 0 .../Settings/WebhookSettings.cs | 0 .../Settings/WireMockCertificateSettings.cs | 0 .../Settings/WireMockServerSettings.cs | 1 + .../Util/FilePathUtils.cs | 0 .../Util/IProtoBufUtils.cs | 16 +++ .../Util/SingletonFactory.cs | 0 .../WireMock.Net.Shared.csproj | 30 +++++ 41 files changed, 269 insertions(+), 161 deletions(-) rename src/{WireMock.Net.Minimal => WireMock.Net.ProtoBuf}/Matchers/ProtoBufMatcher.cs (91%) rename src/{WireMock.Net.Minimal => WireMock.Net.ProtoBuf}/Models/ProtoDefinitionData.cs (100%) create mode 100644 src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs create mode 100644 src/WireMock.Net.ProtoBuf/RequestBuilders/IRequestBuilderExtensions.cs rename src/{WireMock.Net.Minimal => WireMock.Net.ProtoBuf}/Util/ProtoBufUtils.cs (88%) rename src/{WireMock.Net.Minimal => WireMock.Net.ProtoBuf}/Util/ProtoDefinitionHelper.cs (98%) rename src/{WireMock.Net.Minimal => WireMock.Net.ProtoBuf}/Util/WireMockProtoFileResolver.cs (99%) create mode 100644 src/WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Extensions/StringExtensions.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/IMapping.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Matchers/Request/RequestMessageProtoBufMatcher.cs (85%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Models/GraphQLSchemaDetails.cs (100%) delete mode 100644 src/WireMock.Net.Shared/RequestBuilders/IProtoBufRequestBuilder.cs rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseProviders/IResponseProvider.cs (88%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/HandlebarsSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/HttpClientSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/ProxyAndRecordSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/ProxySaveMappingSetting.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/ProxySaveMappingSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/ProxyUrlReplaceSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/WebProxySettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/WebhookSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/WireMockCertificateSettings.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Settings/WireMockServerSettings.cs (99%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Util/FilePathUtils.cs (100%) create mode 100644 src/WireMock.Net.Shared/Util/IProtoBufUtils.cs rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/Util/SingletonFactory.cs (100%) diff --git a/Directory.Build.props b/Directory.Build.props index 668f81190..8d639b989 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -50,15 +50,12 @@ - - - - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 2fd76f427..89ca8789c 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -142,6 +142,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Extensions.Rou EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Extensions.Routing", "src\WireMock.Net.Extensions.Routing\WireMock.Net.Extensions.Routing.csproj", "{1E874C8F-08A2-493B-8421-619F9A6E9E77}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.ProtoBuf", "src\WireMock.Net.ProtoBuf\WireMock.Net.ProtoBuf.csproj", "{B47413AA-55D3-49A7-896A-17ADBFF72407}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -344,6 +346,10 @@ Global {1E874C8F-08A2-493B-8421-619F9A6E9E77}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E874C8F-08A2-493B-8421-619F9A6E9E77}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E874C8F-08A2-493B-8421-619F9A6E9E77}.Release|Any CPU.Build.0 = Release|Any CPU + {B47413AA-55D3-49A7-896A-17ADBFF72407}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B47413AA-55D3-49A7-896A-17ADBFF72407}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B47413AA-55D3-49A7-896A-17ADBFF72407}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B47413AA-55D3-49A7-896A-17ADBFF72407}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -400,6 +406,7 @@ Global {B6269AAC-170A-4346-8B9A-444DED3D9A45} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} {3FCBCA9C-9DB0-4A96-B47E-30470764CC9C} = {0BB8B634-407A-4610-A91F-11586990767A} {1E874C8F-08A2-493B-8421-619F9A6E9E77} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} + {B47413AA-55D3-49A7-896A-17ADBFF72407} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458} diff --git a/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj b/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj index 49e7fae75..0af7ecb78 100644 --- a/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj +++ b/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj @@ -38,9 +38,6 @@ - - - all @@ -50,34 +47,15 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs index 41a8fc09e..6f966ea66 100644 --- a/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs @@ -177,11 +177,10 @@ private bool IsFault(IResponseMessage responseMessage) var jsonBody = JsonConvert.SerializeObject(bodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore }); return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); -#if PROTOBUF case BodyType.ProtoBuf: var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts; - return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false); -#endif + var protoBufUtils = TypeLoader.LoadNewInstance(); + return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false); case BodyType.Bytes: return bodyData.BodyAsBytes; diff --git a/src/WireMock.Net.Minimal/RequestBuilders/Request.cs b/src/WireMock.Net.Minimal/RequestBuilders/Request.cs index 2b3f21ae4..4071cf959 100644 --- a/src/WireMock.Net.Minimal/RequestBuilders/Request.cs +++ b/src/WireMock.Net.Minimal/RequestBuilders/Request.cs @@ -20,9 +20,7 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder { private readonly IList _requestMatchers; - /// - /// The link back to the Mapping. - /// + /// public IMapping Mapping { get; set; } = null!; /// @@ -73,6 +71,7 @@ public IList GetRequestMessageMatchers() where T : IRequestMatcher return _requestMatchers.OfType().FirstOrDefault(func); } + /// public IRequestBuilder Add(T requestMatcher) where T : IRequestMatcher { foreach (var existing in _requestMatchers.OfType().ToArray()) diff --git a/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs b/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs index f5c31a546..5819d7cce 100644 --- a/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs +++ b/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs @@ -126,12 +126,10 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings } } -#if PROTOBUF if (requestMessageProtoBufMatcher?.Matcher != null) { sb.AppendLine(" // .WithBodyAsProtoBuf() is not yet supported"); } -#endif if (requestMessageBodyMatcher?.Matchers != null) { @@ -188,7 +186,7 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings sb.AppendLine($" .WithStatusCode({(int)httpStatusCode})"); } - if (response.ResponseMessage.Headers is { }) + if (response.ResponseMessage.Headers != null) { foreach (var header in response.ResponseMessage.Headers) { @@ -196,7 +194,7 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings } } - if (response.ResponseMessage.TrailingHeaders is { }) + if (response.ResponseMessage.TrailingHeaders != null) { foreach (var header in response.ResponseMessage.TrailingHeaders) { @@ -400,13 +398,11 @@ public MappingModel ToMappingModel(IMapping mapping) { 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().Any() == true && mappingModel.ProtoDefinition != null) + if (bodyMatchers?.OfType().Any() == true && mappingModel.ProtoDefinition != null) { matcherModel.Pattern = null; } -#endif } mappingModel.Request.Body = new BodyModel(); diff --git a/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs b/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs index 73d4c7230..2491a66f2 100644 --- a/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs @@ -80,10 +80,9 @@ public MatcherMapper(WireMockServerSettings settings) case "MimePartMatcher": return CreateMimePartMatcher(matchBehaviour, matcherModel); -#if PROTOBUF - case nameof(ProtoBufMatcher): + case "ProtoBufMatcher": return CreateProtoBufMatcher(matchBehaviour, stringPatterns.GetPatterns(), matcherModel); -#endif + case nameof(RegexMatcher): return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, useRegexExtended, matchOperator); @@ -210,7 +209,7 @@ public MatcherMapper(WireMockServerSettings settings) break; #if PROTOBUF - case ProtoBufMatcher protoBufMatcher: + case IProtoBufMatcher protoBufMatcher: protoBufMatcher.ProtoDefinition().Value(id => model.Pattern = id, texts => { if (texts.Count == 1) @@ -286,8 +285,7 @@ private IMimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, Ma return TypeLoader.LoadNewInstance(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher); } -#if PROTOBUF - private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, IReadOnlyList protoDefinitions, MatcherModel matcher) + private IProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, IReadOnlyList protoDefinitions, MatcherModel matcher) { var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher; @@ -298,5 +296,4 @@ private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, IR objectMatcher ); } -#endif } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs index 50d8c4e69..f1169c502 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs @@ -241,13 +241,13 @@ private IRequestBuilder InitRequestBuilder(RequestModel requestModel, MappingMod if (requestModel.Body?.Matcher != null) { 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) + if (bodyMatcher is IProtoBufMatcher protoBufMatcher && mappingModel?.ProtoDefinition != null) { protoBufMatcher.ProtoDefinition = () => ProtoDefinitionHelper.GetIdOrTexts(_settings, mappingModel.ProtoDefinition); } -#endif + requestBuilder = requestBuilder.WithBody(bodyMatcher); } else if (requestModel.Body?.Matchers != null) diff --git a/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj b/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj index f3fb103a7..107dc10bb 100644 --- a/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj +++ b/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj @@ -125,9 +125,9 @@ - + - - - - - - - diff --git a/src/WireMock.Net.Minimal/Matchers/ProtoBufMatcher.cs b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs similarity index 91% rename from src/WireMock.Net.Minimal/Matchers/ProtoBufMatcher.cs rename to src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs index 12e0c61b1..1c67f59e8 100644 --- a/src/WireMock.Net.Minimal/Matchers/ProtoBufMatcher.cs +++ b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -#if PROTOBUF using System; using System.Threading; using System.Threading.Tasks; @@ -24,19 +23,13 @@ public class ProtoBufMatcher : IProtoBufMatcher /// public MatchBehaviour MatchBehaviour { get; } - /// - /// The Func to define the proto definition as id or texts. - /// + /// public Func ProtoDefinition { get; internal set; } - /// - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// + /// public string MessageType { get; } - /// - /// The Matcher to use (optional). - /// + /// public IObjectMatcher? Matcher { get; } private static readonly Converter ProtoBufToJsonConverter = SingletonFactory.GetInstance(); @@ -122,5 +115,4 @@ public string GetCSharpCodeArguments() return null; } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Models/ProtoDefinitionData.cs b/src/WireMock.Net.ProtoBuf/Models/ProtoDefinitionData.cs similarity index 100% rename from src/WireMock.Net.Minimal/Models/ProtoDefinitionData.cs rename to src/WireMock.Net.ProtoBuf/Models/ProtoDefinitionData.cs diff --git a/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs b/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..cf30594ab --- /dev/null +++ b/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs @@ -0,0 +1,8 @@ +// Copyright © WireMock.Net + +using System.Runtime.CompilerServices; + +// [assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] + +// Needed for Moq in the UnitTest project +// [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/RequestBuilders/IRequestBuilderExtensions.cs b/src/WireMock.Net.ProtoBuf/RequestBuilders/IRequestBuilderExtensions.cs new file mode 100644 index 000000000..9771428f5 --- /dev/null +++ b/src/WireMock.Net.ProtoBuf/RequestBuilders/IRequestBuilderExtensions.cs @@ -0,0 +1,108 @@ +// Copyright © WireMock.Net + +using System; +using System.Collections.Generic; +using WireMock.Matchers; +using WireMock.Matchers.Request; +using WireMock.Models; + +namespace WireMock.RequestBuilders; + +/// +/// IRequestBuilderExtensions extensions for ProtoBuf. +/// +// ReSharper disable once InconsistentNaming +public static class IRequestBuilderExtensions +{ + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The proto definition as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, matchBehaviour); + } + + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The proto definition as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The matcher to use to match the ProtoBuf as (json) object. + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, matcher, matchBehaviour); + } + + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The proto definitions as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, IReadOnlyList protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType)); + } + + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The proto definitions as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The matcher to use to match the ProtoBuf as (json) object. + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, IReadOnlyList protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType, matcher)); + } + + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(requestBuilder), messageType)); + } + + /// + /// WithBodyAsProtoBuf + /// + /// The . + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The matcher to use to match the ProtoBuf as (json) object. + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(requestBuilder), messageType, matcher)); + } + + private static Func ProtoDefinitionFunc(IRequestBuilder requestBuilder) + { + return () => + { + if (requestBuilder.Mapping.ProtoDefinition == null) + { + throw new InvalidOperationException($"No ProtoDefinition defined on mapping '{requestBuilder.Mapping.Guid}'. Please use the WireMockServerSettings to define ProtoDefinitions."); + } + + return requestBuilder.Mapping.ProtoDefinition.Value; + }; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Util/ProtoBufUtils.cs b/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs similarity index 88% rename from src/WireMock.Net.Minimal/Util/ProtoBufUtils.cs rename to src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs index a76d2171e..66295a78d 100644 --- a/src/WireMock.Net.Minimal/Util/ProtoBufUtils.cs +++ b/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -#if PROTOBUF using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -10,9 +9,9 @@ namespace WireMock.Util; -internal static class ProtoBufUtils +internal class ProtoBufUtils : IProtoBufUtils { - internal static async Task GetProtoBufMessageWithHeaderAsync( + public async Task GetProtoBufMessageWithHeaderAsync( IReadOnlyList? protoDefinitions, string? messageType, object? value, @@ -33,5 +32,4 @@ internal static async Task GetProtoBufMessageWithHeaderAsync( .GetInstance() .ConvertAsync(request, cancellationToken).ConfigureAwait(false); } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Util/ProtoDefinitionHelper.cs b/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs similarity index 98% rename from src/WireMock.Net.Minimal/Util/ProtoDefinitionHelper.cs rename to src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs index d616abc24..e12d73acb 100644 --- a/src/WireMock.Net.Minimal/Util/ProtoDefinitionHelper.cs +++ b/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -#if PROTOBUF using System.Collections.Generic; using System.IO; using System.Threading; @@ -42,7 +41,7 @@ public static async Task FromDirectory(string directory, Ca // Build comment and get content from file. var comment = $"// {protoRelativePath}"; -#if NETSTANDARD2_0 +#if NETSTANDARD2_0 || NET462 var content = File.ReadAllText(filePath); #else var content = await File.ReadAllTextAsync(filePath, cancellationToken); @@ -97,5 +96,4 @@ internal static IdOrTexts GetIdOrTexts(WireMockServerSettings settings, params s return new(null, protoDefinitionOrId); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Util/WireMockProtoFileResolver.cs b/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs similarity index 99% rename from src/WireMock.Net.Minimal/Util/WireMockProtoFileResolver.cs rename to src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs index e81893f2c..b28a06899 100644 --- a/src/WireMock.Net.Minimal/Util/WireMockProtoFileResolver.cs +++ b/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -#if PROTOBUF using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -71,5 +70,4 @@ private static bool TryGetValidPath(string path, [NotNullWhen(true)] out string? validPath = null; return false; } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj b/src/WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj new file mode 100644 index 000000000..090808f15 --- /dev/null +++ b/src/WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj @@ -0,0 +1,37 @@ + + + + 1.10.1-preview-00 + ProtoBuf and gRPC support for WireMock.Net + WireMock.Net.ProtoBuf + Stef Heyenrath + netstandard2.1;net462;net6.0;net8.0 + true + wiremock;matchers;matcher;protobuf;grpc + WireMock + {B47413AA-55D3-49A7-896A-17ADBFF72407} + true + true + true + true + ../WireMock.Net/WireMock.Net.ruleset + true + ../WireMock.Net/WireMock.Net.snk + + true + MIT + + + + true + + + + + + + + + + + \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Extensions/StringExtensions.cs b/src/WireMock.Net.Shared/Extensions/StringExtensions.cs similarity index 100% rename from src/WireMock.Net.Minimal/Extensions/StringExtensions.cs rename to src/WireMock.Net.Shared/Extensions/StringExtensions.cs diff --git a/src/WireMock.Net.Minimal/IMapping.cs b/src/WireMock.Net.Shared/IMapping.cs similarity index 100% rename from src/WireMock.Net.Minimal/IMapping.cs rename to src/WireMock.Net.Shared/IMapping.cs diff --git a/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs b/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs index 9cd183ae0..d6f40f26e 100644 --- a/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs @@ -1,5 +1,8 @@ // Copyright © WireMock.Net +using System; +using WireMock.Models; + namespace WireMock.Matchers; /// @@ -7,4 +10,18 @@ namespace WireMock.Matchers; /// public interface IProtoBufMatcher : IDecodeBytesMatcher, IBytesMatcher { + /// + /// The Func to define the proto definition as id or texts. + /// + Func ProtoDefinition { get; } + + /// + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// + string MessageType { get; } + + /// + /// The Matcher to use (optional). + /// + IObjectMatcher? Matcher { get; } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageProtoBufMatcher.cs b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs similarity index 85% rename from src/WireMock.Net.Minimal/Matchers/Request/RequestMessageProtoBufMatcher.cs rename to src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs index 0f5f62ff7..372bd906b 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageProtoBufMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs @@ -2,6 +2,7 @@ using System; using WireMock.Models; +using WireMock.Util; namespace WireMock.Matchers.Request; @@ -24,11 +25,7 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher /// The optional matcher to use to match the ProtoBuf as (json) object. public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func protoDefinition, string messageType, IObjectMatcher? matcher = null) { -#if PROTOBUF - Matcher = new ProtoBufMatcher(protoDefinition, messageType, matchBehaviour, matcher); -#else - throw new System.NotSupportedException("The ProtoBufMatcher can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); -#endif + Matcher = TypeLoader.LoadNewInstance(protoDefinition, messageType, matchBehaviour, matcher); } /// diff --git a/src/WireMock.Net.Minimal/Models/GraphQLSchemaDetails.cs b/src/WireMock.Net.Shared/Models/GraphQLSchemaDetails.cs similarity index 100% rename from src/WireMock.Net.Minimal/Models/GraphQLSchemaDetails.cs rename to src/WireMock.Net.Shared/Models/GraphQLSchemaDetails.cs diff --git a/src/WireMock.Net.Shared/Properties/AssemblyInfo.cs b/src/WireMock.Net.Shared/Properties/AssemblyInfo.cs index 60438b3d3..2b4ab3ada 100644 --- a/src/WireMock.Net.Shared/Properties/AssemblyInfo.cs +++ b/src/WireMock.Net.Shared/Properties/AssemblyInfo.cs @@ -5,6 +5,7 @@ [assembly: InternalsVisibleTo("WireMock.Net.Minimal, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] [assembly: InternalsVisibleTo("WireMock.Net.MimePart, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] [assembly: InternalsVisibleTo("WireMock.Net.GraphQL, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] +[assembly: InternalsVisibleTo("WireMock.Net.ProtoBuf, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] [assembly: InternalsVisibleTo("WireMock.Net.Matchers.CSharpCode, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] // [assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] [assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] diff --git a/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs index f720988f8..f22c1b336 100644 --- a/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs +++ b/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs @@ -10,7 +10,7 @@ namespace WireMock.RequestBuilders; /// /// The BodyRequestBuilder interface. /// -public interface IBodyRequestBuilder : IProtoBufRequestBuilder +public interface IBodyRequestBuilder : IMultiPartRequestBuilder { /// /// WithBody: IMatcher diff --git a/src/WireMock.Net.Shared/RequestBuilders/IProtoBufRequestBuilder.cs b/src/WireMock.Net.Shared/RequestBuilders/IProtoBufRequestBuilder.cs deleted file mode 100644 index c3908be7a..000000000 --- a/src/WireMock.Net.Shared/RequestBuilders/IProtoBufRequestBuilder.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright © WireMock.Net - -using System.Collections.Generic; -using WireMock.Matchers; - -namespace WireMock.RequestBuilders; - -/// -/// The ProtoBufRequestBuilder interface. -/// -public interface IProtoBufRequestBuilder : IMultiPartRequestBuilder -{ - /// - /// WithBodyAsProtoBuf - /// - /// The proto definition as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBodyAsProtoBuf - /// - /// The proto definition as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The matcher to use to match the ProtoBuf as (json) object. - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBodyAsProtoBuf - /// - /// The proto definitions as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBodyAsProtoBuf - /// - /// The proto definitions as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The matcher to use to match the ProtoBuf as (json) object. - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBodyAsProtoBuf - /// - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBodyAsProtoBuf - /// - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The matcher to use to match the ProtoBuf as (json) object. - /// The match behaviour. (default = "AcceptOnMatch") - /// The . - IRequestBuilder WithBodyAsProtoBuf(string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); -} \ No newline at end of file diff --git a/src/WireMock.Net.Shared/RequestBuilders/IRequestBuilder.cs b/src/WireMock.Net.Shared/RequestBuilders/IRequestBuilder.cs index 2ebe40f33..9f217d86c 100644 --- a/src/WireMock.Net.Shared/RequestBuilders/IRequestBuilder.cs +++ b/src/WireMock.Net.Shared/RequestBuilders/IRequestBuilder.cs @@ -9,5 +9,16 @@ namespace WireMock.RequestBuilders; /// public interface IRequestBuilder : IClientIPRequestBuilder { - public IRequestBuilder Add(T requestMatcher) where T : IRequestMatcher; + /// + /// Adds a request matcher to the builder. + /// + /// The type of the request matcher. + /// The request matcher to add. + /// The current instance. + IRequestBuilder Add(T requestMatcher) where T : IRequestMatcher; + + /// + /// The link back to the Mapping. + /// + IMapping Mapping { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseProviders/IResponseProvider.cs b/src/WireMock.Net.Shared/ResponseProviders/IResponseProvider.cs similarity index 88% rename from src/WireMock.Net.Minimal/ResponseProviders/IResponseProvider.cs rename to src/WireMock.Net.Shared/ResponseProviders/IResponseProvider.cs index b82b2730c..81fa5ca63 100644 --- a/src/WireMock.Net.Minimal/ResponseProviders/IResponseProvider.cs +++ b/src/WireMock.Net.Shared/ResponseProviders/IResponseProvider.cs @@ -18,6 +18,6 @@ public interface IResponseProvider /// The used mapping. /// The request. /// The WireMockServerSettings. - /// The including a new (optional) . + /// The including a new (optional) . Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings); } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Settings/HandlebarsSettings.cs b/src/WireMock.Net.Shared/Settings/HandlebarsSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/HandlebarsSettings.cs rename to src/WireMock.Net.Shared/Settings/HandlebarsSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/HttpClientSettings.cs b/src/WireMock.Net.Shared/Settings/HttpClientSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/HttpClientSettings.cs rename to src/WireMock.Net.Shared/Settings/HttpClientSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/ProxyAndRecordSettings.cs b/src/WireMock.Net.Shared/Settings/ProxyAndRecordSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/ProxyAndRecordSettings.cs rename to src/WireMock.Net.Shared/Settings/ProxyAndRecordSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/ProxySaveMappingSetting.cs b/src/WireMock.Net.Shared/Settings/ProxySaveMappingSetting.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/ProxySaveMappingSetting.cs rename to src/WireMock.Net.Shared/Settings/ProxySaveMappingSetting.cs diff --git a/src/WireMock.Net.Minimal/Settings/ProxySaveMappingSettings.cs b/src/WireMock.Net.Shared/Settings/ProxySaveMappingSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/ProxySaveMappingSettings.cs rename to src/WireMock.Net.Shared/Settings/ProxySaveMappingSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/ProxyUrlReplaceSettings.cs b/src/WireMock.Net.Shared/Settings/ProxyUrlReplaceSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/ProxyUrlReplaceSettings.cs rename to src/WireMock.Net.Shared/Settings/ProxyUrlReplaceSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/WebProxySettings.cs b/src/WireMock.Net.Shared/Settings/WebProxySettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/WebProxySettings.cs rename to src/WireMock.Net.Shared/Settings/WebProxySettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/WebhookSettings.cs b/src/WireMock.Net.Shared/Settings/WebhookSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/WebhookSettings.cs rename to src/WireMock.Net.Shared/Settings/WebhookSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/WireMockCertificateSettings.cs b/src/WireMock.Net.Shared/Settings/WireMockCertificateSettings.cs similarity index 100% rename from src/WireMock.Net.Minimal/Settings/WireMockCertificateSettings.cs rename to src/WireMock.Net.Shared/Settings/WireMockCertificateSettings.cs diff --git a/src/WireMock.Net.Minimal/Settings/WireMockServerSettings.cs b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs similarity index 99% rename from src/WireMock.Net.Minimal/Settings/WireMockServerSettings.cs rename to src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs index 8fb01e222..86f99f382 100644 --- a/src/WireMock.Net.Minimal/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs @@ -14,6 +14,7 @@ using WireMock.Types; using System.Globalization; using WireMock.Models; + #if USE_ASPNETCORE using Microsoft.Extensions.DependencyInjection; #endif diff --git a/src/WireMock.Net.Minimal/Util/FilePathUtils.cs b/src/WireMock.Net.Shared/Util/FilePathUtils.cs similarity index 100% rename from src/WireMock.Net.Minimal/Util/FilePathUtils.cs rename to src/WireMock.Net.Shared/Util/FilePathUtils.cs diff --git a/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs b/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs new file mode 100644 index 000000000..d0a77c6e2 --- /dev/null +++ b/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs @@ -0,0 +1,16 @@ +// Copyright © WireMock.Net + +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using JsonConverter.Abstractions; + +namespace WireMock.Util; + +/// +/// Defines the interface for ProtoBufUtils. +/// +public interface IProtoBufUtils +{ + Task GetProtoBufMessageWithHeaderAsync(IReadOnlyList? protoDefinitions, string? messageType, object? value, IJsonConverter? jsonConverter = null, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Util/SingletonFactory.cs b/src/WireMock.Net.Shared/Util/SingletonFactory.cs similarity index 100% rename from src/WireMock.Net.Minimal/Util/SingletonFactory.cs rename to src/WireMock.Net.Shared/Util/SingletonFactory.cs diff --git a/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj b/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj index 2e438c5eb..49249b104 100644 --- a/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj +++ b/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj @@ -29,6 +29,24 @@ true + + $(DefineConstants);NETSTANDARD;USE_ASPNETCORE + + + + $(DefineConstants);USE_ASPNETCORE + + + + $(DefineConstants);USE_ASPNETCORE;NET46 + + + + + + + + all @@ -37,6 +55,18 @@ + + + + + + + + + + + + From f4fa4008448c5f4365bf6f777831d420b7958b60 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 24 Aug 2025 20:18:15 +0200 Subject: [PATCH 2/3] ok --- README.md | 3 +- .../IResponseMessage.cs | 14 +-- .../Delegates/WireMockHttpRequestHandler.cs | 2 +- .../WireMockRouter.cs | 2 +- .../Owin/Mappers/OwinResponseMapper.cs | 2 +- .../Request.WithBodyAsProtoBuf.cs | 61 ---------- .../ResponseBuilders/IResponseBuilder.cs | 10 -- .../ResponseBuilders/Response.WithBody.cs | 70 ------------ .../ResponseBuilders/Response.WithCallback.cs | 12 +- .../ResponseBuilders/Response.cs | 12 +- .../Serialization/MatcherMapper.cs | 4 +- .../Server/RespondWithAProvider.cs | 7 +- .../Server/WireMockServer.ConvertMapping.cs | 11 +- .../Matchers/ProtoBufMatcher.cs | 2 +- .../Properties/AssemblyInfo.cs | 2 +- .../IResponseBuilderExtensions.cs | 106 ++++++++++++++++++ .../Util/ProtoBufUtils.cs | 12 ++ ...Helper.cs => ProtoDefinitionDataHelper.cs} | 21 +--- .../Util/WireMockProtoFileResolver.cs | 4 +- .../Matchers/IProtoBufMatcher.cs | 2 +- .../ResponseBuilders/BodyDestinationFormat.cs | 0 .../ResponseBuilders/IBodyResponseBuilder.cs | 52 +-------- .../ICallbackResponseBuilder.cs | 4 +- .../ResponseBuilders/IDelayResponseBuilder.cs | 0 .../ResponseBuilders/IFaultRequestBuilder.cs | 0 .../IHeadersResponseBuilder.cs | 0 .../ResponseBuilders/IProxyResponseBuilder.cs | 0 .../ResponseBuilders/IResponseBuilder.cs | 19 ++++ .../IStatusCodeResponseBuilder.cs | 0 .../ITransformResponseBuilder.cs | 0 .../Util/IProtoBufUtils.cs | 3 + .../Util/ProtoDefinitionUtils.cs | 30 +++++ .../WireMock.Net.Shared.csproj | 6 - src/WireMock.Net/WireMock.Net.csproj | 1 + .../Grpc/ProtoBufUtilsTests.cs | 2 + .../Grpc/ProtoDefinitionHelperTests.cs | 6 +- 36 files changed, 218 insertions(+), 264 deletions(-) delete mode 100644 src/WireMock.Net.Minimal/RequestBuilders/Request.WithBodyAsProtoBuf.cs delete mode 100644 src/WireMock.Net.Minimal/ResponseBuilders/IResponseBuilder.cs create mode 100644 src/WireMock.Net.ProtoBuf/ResponseBuilders/IResponseBuilderExtensions.cs rename src/WireMock.Net.ProtoBuf/Util/{ProtoDefinitionHelper.cs => ProtoDefinitionDataHelper.cs} (82%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/BodyDestinationFormat.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IBodyResponseBuilder.cs (69%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/ICallbackResponseBuilder.cs (76%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IDelayResponseBuilder.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IFaultRequestBuilder.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IHeadersResponseBuilder.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IProxyResponseBuilder.cs (100%) create mode 100644 src/WireMock.Net.Shared/ResponseBuilders/IResponseBuilder.cs rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/IStatusCodeResponseBuilder.cs (100%) rename src/{WireMock.Net.Minimal => WireMock.Net.Shared}/ResponseBuilders/ITransformResponseBuilder.cs (100%) create mode 100644 src/WireMock.Net.Shared/Util/ProtoDefinitionUtils.cs diff --git a/README.md b/README.md index f1ec01a84..66b150703 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,14 @@ For more info, see also this WIKI page: [What is WireMock.Net](https://github.co |   **WireMock.Net.OpenApiParser** | [![NuGet Badge WireMock.Net.OpenApiParser](https://img.shields.io/nuget/v/WireMock.Net.OpenApiParser)](https://www.nuget.org/packages/WireMock.Net.OpenApiParser) | [![MyGet Badge WireMock.Net.OpenApiParser](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Net.OpenApiParser?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.OpenApiParser) |   **WireMock.Net.MimePart** | [![NuGet Badge WireMock.Net.MimePart](https://img.shields.io/nuget/v/WireMock.Net.MimePart)](https://www.nuget.org/packages/WireMock.Net.MimePart) | [![MyGet Badge WireMock.Net.MimePart](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Net.MimePart?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.MimePart) |   **WireMock.Net.GraphQL** | [![NuGet Badge WireMock.Net.GraphQL](https://img.shields.io/nuget/v/WireMock.Net.GraphQL)](https://www.nuget.org/packages/WireMock.Net.GraphQL) | [![MyGet Badge WireMock.Net.GraphQL](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Net.GraphQL?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.GraphQL) +|   **WireMock.Net.ProtoBuf** | [![NuGet Badge WireMock.Net.ProtoBuf](https://img.shields.io/nuget/v/WireMock.Net.ProtoBuf)](https://www.nuget.org/packages/WireMock.Net.ProtoBuf) | [![MyGet Badge WireMock.Net.ProtoBuf](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Net.ProtoBuf?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.ProtoBuf) | | | | |   **WireMock.Net.RestClient** | [![NuGet Badge WireMock.Net.RestClient](https://img.shields.io/nuget/v/WireMock.Net.RestClient)](https://www.nuget.org/packages/WireMock.Net.RestClient) | [![MyGet Badge WireMock.Net.RestClient](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Net.RestClient?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.RestClient) |   **WireMock.Org.RestClient** | [![NuGet Badge WireMock.Org.RestClient](https://img.shields.io/nuget/v/WireMock.Org.RestClient)](https://www.nuget.org/packages/WireMock.Org.RestClient) | [![MyGet Badge WireMock.Org.RestClient](https://img.shields.io/myget/wiremock-net/vpre/WireMock.Org.RestClient?includePreReleases=true&label=MyGet)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Org.RestClient)
-🔺 **WireMock.Net.Minimal** does not include *WireMock.Net.MimePart* and *WireMock.Net.GraphQL*. +🔺 **WireMock.Net.Minimal** does not include *WireMock.Net.MimePart*, *WireMock.Net.GraphQL* and *WireMock.Net.ProtoBuf*. --- diff --git a/src/WireMock.Net.Abstractions/IResponseMessage.cs b/src/WireMock.Net.Abstractions/IResponseMessage.cs index 92cd22192..7951e2ed1 100644 --- a/src/WireMock.Net.Abstractions/IResponseMessage.cs +++ b/src/WireMock.Net.Abstractions/IResponseMessage.cs @@ -15,12 +15,12 @@ public interface IResponseMessage /// /// The Body. /// - IBodyData? BodyData { get; } + IBodyData? BodyData { get; set; } /// /// Gets the body destination (Null, SameAsSource, String or Bytes). /// - string? BodyDestination { get; } + string? BodyDestination { get; set; } /// /// Gets or sets the body. @@ -30,27 +30,27 @@ public interface IResponseMessage /// /// Gets the Fault percentage. /// - double? FaultPercentage { get; } + double? FaultPercentage { get; set; } /// /// The FaultType. /// - FaultType FaultType { get; } + FaultType FaultType { get; set; } /// /// Gets the headers. /// - IDictionary>? Headers { get; } + IDictionary>? Headers { get; set; } /// /// Gets the trailing headers. /// - IDictionary>? TrailingHeaders { get; } + IDictionary>? TrailingHeaders { get; set; } /// /// Gets or sets the status code. /// - object? StatusCode { get; } + object? StatusCode { get; set; } /// /// Adds the header. diff --git a/src/WireMock.Net.Extensions.Routing/Delegates/WireMockHttpRequestHandler.cs b/src/WireMock.Net.Extensions.Routing/Delegates/WireMockHttpRequestHandler.cs index d094aac88..83164bc4f 100644 --- a/src/WireMock.Net.Extensions.Routing/Delegates/WireMockHttpRequestHandler.cs +++ b/src/WireMock.Net.Extensions.Routing/Delegates/WireMockHttpRequestHandler.cs @@ -7,4 +7,4 @@ namespace WireMock.Net.Extensions.Routing.Delegates; /// /// The incoming request message. /// A task that resolves to a . -public delegate Task WireMockHttpRequestHandler(IRequestMessage requestMessage); +public delegate Task WireMockHttpRequestHandler(IRequestMessage requestMessage); diff --git a/src/WireMock.Net.Extensions.Routing/WireMockRouter.cs b/src/WireMock.Net.Extensions.Routing/WireMockRouter.cs index 8661e3145..297049eba 100644 --- a/src/WireMock.Net.Extensions.Routing/WireMockRouter.cs +++ b/src/WireMock.Net.Extensions.Routing/WireMockRouter.cs @@ -105,7 +105,7 @@ private static WireMockHttpRequestHandler CreateHttpRequestHandler( Func requestHandler) => request => CreateResponseMessageAsync(requestHandler(request)); - private static async Task CreateResponseMessageAsync(object? response) + private static async Task CreateResponseMessageAsync(object? response) { var awaitedResponse = response is Task task ? await task.ToGenericTaskAsync() diff --git a/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs index 6f966ea66..1bb412d4b 100644 --- a/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net.Minimal/Owin/Mappers/OwinResponseMapper.cs @@ -179,7 +179,7 @@ private bool IsFault(IResponseMessage responseMessage) case BodyType.ProtoBuf: var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts; - var protoBufUtils = TypeLoader.LoadNewInstance(); + var protoBufUtils = TypeLoader.LoadStaticInstance(); return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false); case BodyType.Bytes: diff --git a/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBodyAsProtoBuf.cs b/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBodyAsProtoBuf.cs deleted file mode 100644 index 395e472e7..000000000 --- a/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBodyAsProtoBuf.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using System.Collections.Generic; -using WireMock.Matchers; -using WireMock.Matchers.Request; -using WireMock.Models; - -namespace WireMock.RequestBuilders; - -public partial class Request -{ - /// - public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return WithBodyAsProtoBuf([protoDefinition], messageType, matchBehaviour); - } - - /// - public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return WithBodyAsProtoBuf([protoDefinition], messageType, matcher, matchBehaviour); - } - - /// - public IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType)); - } - - /// - public IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType, matcher)); - } - - /// - public IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(), messageType)); - } - - /// - public IRequestBuilder WithBodyAsProtoBuf(string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - return Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(), messageType, matcher)); - } - - private Func ProtoDefinitionFunc() - { - return () => - { - if (Mapping.ProtoDefinition == null) - { - throw new InvalidOperationException($"No ProtoDefinition defined on mapping '{Mapping.Guid}'. Please use the WireMockServerSettings to define ProtoDefinitions."); - } - - return Mapping.ProtoDefinition.Value; - }; - } -} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IResponseBuilder.cs b/src/WireMock.Net.Minimal/ResponseBuilders/IResponseBuilder.cs deleted file mode 100644 index 2492cff21..000000000 --- a/src/WireMock.Net.Minimal/ResponseBuilders/IResponseBuilder.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright © WireMock.Net - -namespace WireMock.ResponseBuilders; - -/// -/// The ResponseBuilder interface. -/// -public interface IResponseBuilder : IProxyResponseBuilder -{ -} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithBody.cs b/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithBody.cs index 9c0aa7edb..f9a7d8ab8 100644 --- a/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithBody.cs +++ b/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithBody.cs @@ -1,12 +1,10 @@ // Copyright © WireMock.Net using System; -using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using JsonConverter.Abstractions; using Stef.Validation; -using WireMock.Exceptions; using WireMock.Models; using WireMock.Types; using WireMock.Util; @@ -235,72 +233,4 @@ public IResponseBuilder WithBody(object body, Encoding? encoding, IJsonConverter return this; } - - /// - public IResponseBuilder WithBodyAsProtoBuf( - string protoDefinition, - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ) - { - return WithBodyAsProtoBuf([protoDefinition], messageType, value, jsonConverter, options); - } - - /// - public IResponseBuilder WithBodyAsProtoBuf( - IReadOnlyList protoDefinitions, - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ) - { - Guard.NotNullOrEmpty(protoDefinitions); - Guard.NotNullOrWhiteSpace(messageType); - Guard.NotNull(value); - -#if !PROTOBUF - throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); -#else - ResponseMessage.BodyDestination = null; - ResponseMessage.BodyData = new BodyData - { - DetectedBodyType = BodyType.ProtoBuf, - BodyAsJson = value, - ProtoDefinition = () => new IdOrTexts(null, protoDefinitions), - ProtoBufMessageType = messageType - }; - - return this; -#endif - } - - /// - public IResponseBuilder WithBodyAsProtoBuf( - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ) - { - Guard.NotNullOrWhiteSpace(messageType); - Guard.NotNull(value); - -#if !PROTOBUF - throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); -#else - ResponseMessage.BodyDestination = null; - ResponseMessage.BodyData = new BodyData - { - DetectedBodyType = BodyType.ProtoBuf, - BodyAsJson = value, - ProtoDefinition = () => Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."), - ProtoBufMessageType = messageType - }; - - return this; -#endif - } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithCallback.cs b/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithCallback.cs index cdfca99d1..2782d9bf8 100644 --- a/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithCallback.cs +++ b/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithCallback.cs @@ -13,13 +13,13 @@ public partial class Response /// A delegate to execute to generate the response. /// [MemberNotNullWhen(true, nameof(WithCallbackUsed))] - public Func? Callback { get; private set; } + public Func? Callback { get; private set; } /// /// A delegate to execute to generate the response async. /// [MemberNotNullWhen(true, nameof(WithCallbackUsed))] - public Func>? CallbackAsync { get; private set; } + public Func>? CallbackAsync { get; private set; } /// /// Defines if the method WithCallback(...) is used. @@ -27,7 +27,7 @@ public partial class Response public bool WithCallbackUsed { get; private set; } /// - public IResponseBuilder WithCallback(Func callbackHandler) + public IResponseBuilder WithCallback(Func callbackHandler) { Guard.NotNull(callbackHandler); @@ -35,14 +35,14 @@ public IResponseBuilder WithCallback(Func call } /// - public IResponseBuilder WithCallback(Func> callbackHandler) + public IResponseBuilder WithCallback(Func> callbackHandler) { Guard.NotNull(callbackHandler); return WithCallbackInternal(true, callbackHandler); } - private IResponseBuilder WithCallbackInternal(bool withCallbackUsed, Func callbackHandler) + private IResponseBuilder WithCallbackInternal(bool withCallbackUsed, Func callbackHandler) { Guard.NotNull(callbackHandler); @@ -52,7 +52,7 @@ private IResponseBuilder WithCallbackInternal(bool withCallbackUsed, Func> callbackHandler) + private IResponseBuilder WithCallbackInternal(bool withCallbackUsed, Func> callbackHandler) { Guard.NotNull(callbackHandler); diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/Response.cs b/src/WireMock.Net.Minimal/ResponseBuilders/Response.cs index 08713fa27..bcf984917 100644 --- a/src/WireMock.Net.Minimal/ResponseBuilders/Response.cs +++ b/src/WireMock.Net.Minimal/ResponseBuilders/Response.cs @@ -28,9 +28,7 @@ public partial class Response : IResponseBuilder private TimeSpan? _delay; - /// - /// The link back to the mapping. - /// + /// public IMapping Mapping { get; set; } = null!; /// @@ -81,10 +79,8 @@ public TimeSpan? Delay /// public ReplaceNodeOptions TransformerReplaceNodeOptions { get; private set; } - /// - /// Gets the response message. - /// - public ResponseMessage ResponseMessage { get; } + /// + public IResponseMessage ResponseMessage { get; } /// /// Creates this instance. @@ -226,7 +222,7 @@ string RemoveFirstOccurrence(string source, string find) ).ConfigureAwait(false); } - ResponseMessage responseMessage; + IResponseMessage responseMessage; if (!WithCallbackUsed) { responseMessage = ResponseMessage; diff --git a/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs b/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs index 2491a66f2..429ac6795 100644 --- a/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net.Minimal/Serialization/MatcherMapper.cs @@ -289,8 +289,8 @@ private IProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, I { var objectMatcher = Map(matcher.ContentMatcher) as IObjectMatcher; - return new ProtoBufMatcher( - () => ProtoDefinitionHelper.GetIdOrTexts(_settings, protoDefinitions.ToArray()), + return TypeLoader.LoadNewInstance( + () => ProtoDefinitionUtils.GetIdOrTexts(_settings, protoDefinitions.ToArray()), matcher.ProtoBufMessageType!, matchBehaviour ?? MatchBehaviour.AcceptOnMatch, objectMatcher diff --git a/src/WireMock.Net.Minimal/Server/RespondWithAProvider.cs b/src/WireMock.Net.Minimal/Server/RespondWithAProvider.cs index 2dc4e07c3..510252b83 100644 --- a/src/WireMock.Net.Minimal/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net.Minimal/Server/RespondWithAProvider.cs @@ -356,12 +356,9 @@ public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinition { Guard.NotNull(protoDefinitionOrId); -#if PROTOBUF - ProtoDefinition = ProtoDefinitionHelper.GetIdOrTexts(_settings, protoDefinitionOrId); + ProtoDefinition = ProtoDefinitionUtils.GetIdOrTexts(_settings, protoDefinitionOrId); + return this; -#else - throw new NotSupportedException("The WithProtoDefinition method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); -#endif } /// diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs index f1169c502..d345b0577 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs @@ -245,7 +245,7 @@ private IRequestBuilder InitRequestBuilder(RequestModel requestModel, MappingMod // If the BodyMatcher is a ProtoBufMatcher, and if ProtoDefinition is defined on Mapping-level, set the ProtoDefinition from that Mapping. if (bodyMatcher is IProtoBufMatcher protoBufMatcher && mappingModel?.ProtoDefinition != null) { - protoBufMatcher.ProtoDefinition = () => ProtoDefinitionHelper.GetIdOrTexts(_settings, mappingModel.ProtoDefinition); + protoBufMatcher.ProtoDefinition = () => ProtoDefinitionUtils.GetIdOrTexts(_settings, mappingModel.ProtoDefinition); } requestBuilder = requestBuilder.WithBody(bodyMatcher); @@ -368,18 +368,19 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel) { if (responseModel.ProtoBufMessageType != null) { + var protoBufUtils = TypeLoader.LoadStaticInstance(); + if (responseModel.ProtoDefinition != null) { - responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinition, responseModel.ProtoBufMessageType, responseModel.BodyAsJson); + responseBuilder = protoBufUtils.UpdateResponseBuilder(responseBuilder, responseModel.ProtoBufMessageType, responseModel.BodyAsJson, responseModel.ProtoDefinition); } else if (responseModel.ProtoDefinitions != null) { - responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinitions, responseModel.ProtoBufMessageType, responseModel.BodyAsJson); + responseBuilder = protoBufUtils.UpdateResponseBuilder(responseBuilder, responseModel.ProtoBufMessageType, responseModel.BodyAsJson, responseModel.ProtoDefinitions); } else { - // ProtoDefinition(s) is/are defined at Mapping/Server level - responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoBufMessageType, responseModel.BodyAsJson); + responseBuilder = protoBufUtils.UpdateResponseBuilder(responseBuilder, responseModel.ProtoBufMessageType, responseModel.BodyAsJson); } } else diff --git a/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs index 1c67f59e8..bbba63f3f 100644 --- a/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs +++ b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs @@ -24,7 +24,7 @@ public class ProtoBufMatcher : IProtoBufMatcher public MatchBehaviour MatchBehaviour { get; } /// - public Func ProtoDefinition { get; internal set; } + public Func ProtoDefinition { get; set; } /// public string MessageType { get; } diff --git a/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs b/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs index cf30594ab..6bfd54d8c 100644 --- a/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs +++ b/src/WireMock.Net.ProtoBuf/Properties/AssemblyInfo.cs @@ -2,7 +2,7 @@ using System.Runtime.CompilerServices; -// [assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] +[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] // Needed for Moq in the UnitTest project // [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/ResponseBuilders/IResponseBuilderExtensions.cs b/src/WireMock.Net.ProtoBuf/ResponseBuilders/IResponseBuilderExtensions.cs new file mode 100644 index 000000000..6bb68a442 --- /dev/null +++ b/src/WireMock.Net.ProtoBuf/ResponseBuilders/IResponseBuilderExtensions.cs @@ -0,0 +1,106 @@ +// Copyright © WireMock.Net + +using System.Collections.Generic; +using JsonConverter.Abstractions; +using Stef.Validation; +using WireMock.Exceptions; +using WireMock.Models; +using WireMock.Types; +using WireMock.Util; + +namespace WireMock.ResponseBuilders; + +/// +/// Extensions for to implement WithBodyAsProtoBuf. +/// +public static class IResponseBuilderExtensions +{ + /// + /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. + /// + /// The response builder. + /// The proto definition as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The object to convert to protobuf byte[]. + /// The [optional]. Default value is NewtonsoftJsonConverter. + /// The [optional]. + /// A . + public static IResponseBuilder WithBodyAsProtoBuf( + this IResponseBuilder responseBuilder, + string protoDefinition, + string messageType, + object value, + IJsonConverter? jsonConverter = null, + JsonConverterOptions? options = null + ) + { + return responseBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, value, jsonConverter, options); + } + + /// + /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on proto definitions, message type and the value. + /// + /// The response builder. + /// The proto definition as text. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The object to convert to protobuf byte[]. + /// The [optional]. Default value is NewtonsoftJsonConverter. + /// The [optional]. + /// A . + public static IResponseBuilder WithBodyAsProtoBuf( + this IResponseBuilder responseBuilder, + IReadOnlyList protoDefinitions, + string messageType, + object value, + IJsonConverter? jsonConverter = null, + JsonConverterOptions? options = null + ) + { + Guard.NotNullOrEmpty(protoDefinitions); + Guard.NotNullOrWhiteSpace(messageType); + Guard.NotNull(value); + + responseBuilder.ResponseMessage.BodyDestination = null; + responseBuilder.ResponseMessage.BodyData = new BodyData + { + DetectedBodyType = BodyType.ProtoBuf, + BodyAsJson = value, + ProtoDefinition = () => new IdOrTexts(null, protoDefinitions), + ProtoBufMessageType = messageType + }; + + return responseBuilder; + } + + /// + /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. + /// + /// The response builder. + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The object to convert to protobuf byte[]. + /// The [optional]. Default value is NewtonsoftJsonConverter. + /// The [optional]. + /// A . + public static IResponseBuilder WithBodyAsProtoBuf( + this IResponseBuilder responseBuilder, + string messageType, + object value, + IJsonConverter? jsonConverter = null, + JsonConverterOptions? options = null + ) + { + Guard.NotNullOrWhiteSpace(messageType); + Guard.NotNull(value); + + responseBuilder.ResponseMessage.BodyDestination = null; + responseBuilder.ResponseMessage.BodyData = new BodyData + { + DetectedBodyType = BodyType.ProtoBuf, + BodyAsJson = value, + ProtoDefinition = () => responseBuilder.Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."), + ProtoBufMessageType = messageType + }; + + return responseBuilder; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs b/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs index 66295a78d..591ae5b4e 100644 --- a/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs +++ b/src/WireMock.Net.ProtoBuf/Util/ProtoBufUtils.cs @@ -6,6 +6,7 @@ using JsonConverter.Abstractions; using ProtoBufJsonConverter; using ProtoBufJsonConverter.Models; +using WireMock.ResponseBuilders; namespace WireMock.Util; @@ -32,4 +33,15 @@ public async Task GetProtoBufMessageWithHeaderAsync( .GetInstance() .ConvertAsync(request, cancellationToken).ConfigureAwait(false); } + + public IResponseBuilder UpdateResponseBuilder(IResponseBuilder responseBuilder, string protoBufMessageType, object bodyAsJson, params string[] protoDefinitions) + { + if (protoDefinitions.Length > 0) + { + return responseBuilder.WithBodyAsProtoBuf(protoDefinitions, protoBufMessageType, bodyAsJson); + } + + // ProtoDefinition(s) is/are defined at Mapping/Server level + return responseBuilder.WithBodyAsProtoBuf(protoBufMessageType, bodyAsJson); + } } \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs b/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionDataHelper.cs similarity index 82% rename from src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs rename to src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionDataHelper.cs index e12d73acb..1ba3c06f8 100644 --- a/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionHelper.cs +++ b/src/WireMock.Net.ProtoBuf/Util/ProtoDefinitionDataHelper.cs @@ -8,14 +8,13 @@ using ProtoBufJsonConverter.Models; using Stef.Validation; using WireMock.Models; -using WireMock.Settings; namespace WireMock.Util; /// /// Some helper methods for Proto Definitions. /// -public static class ProtoDefinitionHelper +internal static class ProtoDefinitionDataHelper { /// /// Builds a dictionary of ProtoDefinitions from a directory. @@ -78,22 +77,4 @@ public static async Task FromDirectory(string directory, Ca return new ProtoDefinitionData(fileNameMappedToProtoDefinition); } - - internal static IdOrTexts GetIdOrTexts(WireMockServerSettings settings, params string[] protoDefinitionOrId) - { - switch (protoDefinitionOrId.Length) - { - case 1: - var idOrText = protoDefinitionOrId[0]; - if (settings.ProtoDefinitions?.TryGetValue(idOrText, out var protoDefinitions) == true) - { - return new(idOrText, protoDefinitions); - } - - return new(null, protoDefinitionOrId); - - default: - return new(null, protoDefinitionOrId); - } - } } \ No newline at end of file diff --git a/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs b/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs index b28a06899..9366c542d 100644 --- a/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs +++ b/src/WireMock.Net.ProtoBuf/Util/WireMockProtoFileResolver.cs @@ -29,10 +29,10 @@ public WireMockProtoFileResolver(IReadOnlyCollection protoDefinitions) foreach (var extraProtoDefinition in protoDefinitions) { - var firstNonEmptyLine = extraProtoDefinition.Split(['\r', '\n']).FirstOrDefault(l => !string.IsNullOrEmpty(l)); + var firstNonEmptyLine = extraProtoDefinition.Split('\r', '\n').FirstOrDefault(l => !string.IsNullOrEmpty(l)); if (firstNonEmptyLine != null) { - if (TryGetValidPath(firstNonEmptyLine.TrimStart(['/', ' ']), out var validPath)) + if (TryGetValidPath(firstNonEmptyLine.TrimStart('/', ' '), out var validPath)) { _files.Add(validPath, extraProtoDefinition); } diff --git a/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs b/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs index d6f40f26e..8c90c8d03 100644 --- a/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/IProtoBufMatcher.cs @@ -13,7 +13,7 @@ public interface IProtoBufMatcher : IDecodeBytesMatcher, IBytesMatcher /// /// The Func to define the proto definition as id or texts. /// - Func ProtoDefinition { get; } + Func ProtoDefinition { get; set; } /// /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/BodyDestinationFormat.cs b/src/WireMock.Net.Shared/ResponseBuilders/BodyDestinationFormat.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/BodyDestinationFormat.cs rename to src/WireMock.Net.Shared/ResponseBuilders/BodyDestinationFormat.cs diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IBodyResponseBuilder.cs similarity index 69% rename from src/WireMock.Net.Minimal/ResponseBuilders/IBodyResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IBodyResponseBuilder.cs index 758ea69a5..a3ca1d0bf 100644 --- a/src/WireMock.Net.Minimal/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock.Net.Shared/ResponseBuilders/IBodyResponseBuilder.cs @@ -1,7 +1,6 @@ // Copyright © WireMock.Net using System; -using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using JsonConverter.Abstractions; @@ -84,7 +83,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder IResponseBuilder WithBodyAsJson(Func bodyFactory, Encoding? encoding = null); /// - /// WithBodyAsJson : Create a ... response based on a async callback function. + /// WithBodyAsJson : Create a ... response based on an async callback function. /// /// The async delegate to build the body. /// The body encoding. @@ -117,53 +116,4 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder /// The [optional]. /// A . IResponseBuilder WithBody(object body, Encoding? encoding, IJsonConverter jsonConverter, JsonConverterOptions? options = null); - - /// - /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. - /// - /// The proto definition as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The object to convert to protobuf byte[]. - /// The [optional]. Default value is NewtonsoftJsonConverter. - /// The [optional]. - /// A . - IResponseBuilder WithBodyAsProtoBuf( - string protoDefinition, - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ); - - /// - /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on proto definitions, message type and the value. - /// - /// The proto definition as text. - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The object to convert to protobuf byte[]. - /// The [optional]. Default value is NewtonsoftJsonConverter. - /// The [optional]. - /// A . - IResponseBuilder WithBodyAsProtoBuf( - IReadOnlyList protoDefinitions, - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ); - - /// - /// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. - /// - /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". - /// The object to convert to protobuf byte[]. - /// The [optional]. Default value is NewtonsoftJsonConverter. - /// The [optional]. - /// A . - IResponseBuilder WithBodyAsProtoBuf( - string messageType, - object value, - IJsonConverter? jsonConverter = null, - JsonConverterOptions? options = null - ); } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/ICallbackResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/ICallbackResponseBuilder.cs similarity index 76% rename from src/WireMock.Net.Minimal/ResponseBuilders/ICallbackResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/ICallbackResponseBuilder.cs index 1eaf1148a..5a9275422 100644 --- a/src/WireMock.Net.Minimal/ResponseBuilders/ICallbackResponseBuilder.cs +++ b/src/WireMock.Net.Shared/ResponseBuilders/ICallbackResponseBuilder.cs @@ -17,12 +17,12 @@ public interface ICallbackResponseBuilder : IResponseProvider /// /// The . [PublicAPI] - IResponseBuilder WithCallback(Func callbackHandler); + IResponseBuilder WithCallback(Func callbackHandler); /// /// The async callback builder /// /// The . [PublicAPI] - IResponseBuilder WithCallback(Func> callbackHandler); + IResponseBuilder WithCallback(Func> callbackHandler); } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IDelayResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IDelayResponseBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/IDelayResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IDelayResponseBuilder.cs diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IFaultRequestBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IFaultRequestBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/IFaultRequestBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IFaultRequestBuilder.cs diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IHeadersResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IHeadersResponseBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/IHeadersResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IHeadersResponseBuilder.cs diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IProxyResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IProxyResponseBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/IProxyResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IProxyResponseBuilder.cs diff --git a/src/WireMock.Net.Shared/ResponseBuilders/IResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IResponseBuilder.cs new file mode 100644 index 000000000..73c35229f --- /dev/null +++ b/src/WireMock.Net.Shared/ResponseBuilders/IResponseBuilder.cs @@ -0,0 +1,19 @@ +// Copyright © WireMock.Net + +namespace WireMock.ResponseBuilders; + +/// +/// The ResponseBuilder interface. +/// +public interface IResponseBuilder : IProxyResponseBuilder +{ + /// + /// The link back to the mapping. + /// + IMapping Mapping { get; } + + /// + /// Gets the response message. + /// + IResponseMessage ResponseMessage { get; } +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/IStatusCodeResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/IStatusCodeResponseBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/IStatusCodeResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/IStatusCodeResponseBuilder.cs diff --git a/src/WireMock.Net.Minimal/ResponseBuilders/ITransformResponseBuilder.cs b/src/WireMock.Net.Shared/ResponseBuilders/ITransformResponseBuilder.cs similarity index 100% rename from src/WireMock.Net.Minimal/ResponseBuilders/ITransformResponseBuilder.cs rename to src/WireMock.Net.Shared/ResponseBuilders/ITransformResponseBuilder.cs diff --git a/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs b/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs index d0a77c6e2..7170d6ddf 100644 --- a/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs +++ b/src/WireMock.Net.Shared/Util/IProtoBufUtils.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using JsonConverter.Abstractions; +using WireMock.ResponseBuilders; namespace WireMock.Util; @@ -13,4 +14,6 @@ namespace WireMock.Util; public interface IProtoBufUtils { Task GetProtoBufMessageWithHeaderAsync(IReadOnlyList? protoDefinitions, string? messageType, object? value, IJsonConverter? jsonConverter = null, CancellationToken cancellationToken = default); + + IResponseBuilder UpdateResponseBuilder(IResponseBuilder responseBuilder, string protoBufMessageType, object bodyAsJson, params string[] protoDefinitions); } \ No newline at end of file diff --git a/src/WireMock.Net.Shared/Util/ProtoDefinitionUtils.cs b/src/WireMock.Net.Shared/Util/ProtoDefinitionUtils.cs new file mode 100644 index 000000000..a492a3643 --- /dev/null +++ b/src/WireMock.Net.Shared/Util/ProtoDefinitionUtils.cs @@ -0,0 +1,30 @@ +// Copyright © WireMock.Net + +using WireMock.Models; +using WireMock.Settings; + +namespace WireMock.Util; + +/// +/// Some helper methods for Proto Definitions. +/// +public static class ProtoDefinitionUtils +{ + internal static IdOrTexts GetIdOrTexts(WireMockServerSettings settings, params string[] protoDefinitionOrId) + { + switch (protoDefinitionOrId.Length) + { + case 1: + var idOrText = protoDefinitionOrId[0]; + if (settings.ProtoDefinitions?.TryGetValue(idOrText, out var protoDefinitions) == true) + { + return new(idOrText, protoDefinitions); + } + + return new(null, protoDefinitionOrId); + + default: + return new(null, protoDefinitionOrId); + } + } +} \ No newline at end of file diff --git a/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj b/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj index 49249b104..2b01dd010 100644 --- a/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj +++ b/src/WireMock.Net.Shared/WireMock.Net.Shared.csproj @@ -41,12 +41,6 @@ $(DefineConstants);USE_ASPNETCORE;NET46 - - - - - - all diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 90b06baa4..f07348a06 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -35,5 +35,6 @@ + \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs b/test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs index c8ff777c3..aa1df07d2 100644 --- a/test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs +++ b/test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs @@ -12,6 +12,8 @@ namespace WireMock.Net.Tests.Grpc; public class ProtoBufUtilsTests { + private static readonly IProtoBufUtils ProtoBufUtils = new ProtoBufUtils(); + [Fact] public async Task GetProtoBufMessageWithHeader_MultipleProtoFiles() { diff --git a/test/WireMock.Net.Tests/Grpc/ProtoDefinitionHelperTests.cs b/test/WireMock.Net.Tests/Grpc/ProtoDefinitionHelperTests.cs index 7a501a25d..c9c4c29d5 100644 --- a/test/WireMock.Net.Tests/Grpc/ProtoDefinitionHelperTests.cs +++ b/test/WireMock.Net.Tests/Grpc/ProtoDefinitionHelperTests.cs @@ -12,6 +12,8 @@ namespace WireMock.Net.Tests.Grpc; public class ProtoDefinitionHelperTests { + private static readonly IProtoBufUtils ProtoBufUtils = new ProtoBufUtils(); + [Fact] public async Task FromDirectory_Greet_ShouldReturnModifiedProtoFiles() { @@ -21,7 +23,7 @@ public async Task FromDirectory_Greet_ShouldReturnModifiedProtoFiles() var expectedComment = $"// {expectedFilename}"; // Act - var protoDefinitionData = await ProtoDefinitionHelper.FromDirectory(directory); + var protoDefinitionData = await ProtoDefinitionDataHelper.FromDirectory(directory); var protoDefinitions = protoDefinitionData.ToList("greet"); // Assert @@ -50,7 +52,7 @@ public async Task FromDirectory_OpenTelemetry_ShouldReturnModifiedProtoFiles() var directory = Path.Combine(Directory.GetCurrentDirectory(), "Grpc", "ot"); // Act - var protoDefinitionData = await ProtoDefinitionHelper.FromDirectory(directory); + var protoDefinitionData = await ProtoDefinitionDataHelper.FromDirectory(directory); var protoDefinitions = protoDefinitionData.ToList("trace_service"); // Assert From ac480cc1c56aab9e1c649f3d23dcc3b4803377c0 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 25 Aug 2025 20:21:41 +0200 Subject: [PATCH 3/3] Update Directory.Build.props Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 8d639b989..5166753d2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -55,7 +55,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive