From 8436a840d1fe73c935bf9048bb3f800b2a1fb3fd Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 29 Dec 2023 10:05:54 +0100 Subject: [PATCH] . --- .../Server/IRespondWithAProvider.cs | 7 ++++ .../Server/RespondWithAProvider.cs | 33 ++++++++++--------- .../Grpc/WireMockServerTests.Grpc.cs | 2 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index ea3bec77a..ff6884d77 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -184,4 +184,11 @@ IRespondWithAProvider WithWebhook( /// The probability when this request should be matched. Value is between 0 and 1. /// The . IRespondWithAProvider WithProbability(double probability); + + /// + /// Add a Grpc ProtoDefinition which is used for the request and the response. + /// + /// The proto definition as a string. + /// The . + IRespondWithAProvider WithProtoDefinition(string protoDefinition); } \ No newline at end of file diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index 7b7740fa8..7dd71c338 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -43,6 +43,8 @@ internal class RespondWithAProvider : IRespondWithAProvider public object? Data { get; private set; } + public string? ProtoDefinition { get; private set; } + /// /// Initializes a new instance of the class. /// @@ -76,7 +78,8 @@ public RespondWithAProvider( /// The provider. public void RespondWith(IResponseProvider provider) { - var mapping = new Mapping( + var mapping = new Mapping + ( Guid, _dateTimeUtils.UtcNow, _title, @@ -94,13 +97,13 @@ public void RespondWith(IResponseProvider provider) _useWebhookFireAndForget, TimeSettings, Data, - _probability); + _probability + ); _registrationCallback(mapping, _saveToFile); } /// - [PublicAPI] public IRespondWithAProvider WithData(object data) { Data = data; @@ -117,7 +120,6 @@ public IRespondWithAProvider WithGuid(string guid) public IRespondWithAProvider WithGuid(Guid guid) { Guid = guid; - return this; } @@ -133,7 +135,6 @@ public IRespondWithAProvider WithTitle(string title) public IRespondWithAProvider WithDescription(string description) { _description = description; - return this; } @@ -141,7 +142,6 @@ public IRespondWithAProvider WithDescription(string description) public IRespondWithAProvider WithPath(string path) { _path = path; - return this; } @@ -149,15 +149,13 @@ public IRespondWithAProvider WithPath(string path) public IRespondWithAProvider AtPriority(int priority) { _priority = priority; - return this; } /// public IRespondWithAProvider InScenario(string scenario) { - _scenario = scenario; - + _scenario = Guard.NotNullOrWhiteSpace(scenario); return this; } @@ -209,9 +207,7 @@ public IRespondWithAProvider WillSetStateTo(int state, int? times = 1) /// public IRespondWithAProvider WithTimeSettings(ITimeSettings timeSettings) { - Guard.NotNull(timeSettings, nameof(timeSettings)); - - TimeSettings = timeSettings; + TimeSettings = Guard.NotNull(timeSettings); return this; } @@ -219,10 +215,9 @@ public IRespondWithAProvider WithTimeSettings(ITimeSettings timeSettings) /// public IRespondWithAProvider WithWebhook(params IWebhook[] webhooks) { - Guard.HasNoNulls(webhooks, nameof(webhooks)); + Guard.HasNoNulls(webhooks); Webhooks = webhooks; - return this; } @@ -283,14 +278,19 @@ public IRespondWithAProvider WithWebhook( public IRespondWithAProvider WithWebhookFireAndForget(bool useWebhooksFireAndForget) { _useWebhookFireAndForget = useWebhooksFireAndForget; - return this; } public IRespondWithAProvider WithProbability(double probability) { _probability = Guard.Condition(probability, p => p is >= 0 and <= 1.0); + return this; + } + /// + public IRespondWithAProvider WithProtoDefinition(string protoDefinition) + { + ProtoDefinition = Guard.NotNullOrWhiteSpace(protoDefinition); return this; } @@ -299,7 +299,8 @@ private static IWebhook InitWebhook( string method, IDictionary>? headers, bool useTransformer, - TransformerType transformerType) + TransformerType transformerType + ) { return new Webhook { diff --git a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs index f70dc7a43..95dcd3dc1 100644 --- a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs +++ b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs @@ -14,7 +14,7 @@ namespace WireMock.Net.Tests; public partial class WireMockServerTests { [Fact] - public async Task WireMockServer_WithBodyAsProtoBuf_UsingGrpcGeneratedClient() + public async Task WireMockServer_WithBodyAsProtoBuf_InlineProtoDefinition_UsingGrpcGeneratedClient() { // Arrange using var server = WireMockServer.Start(useHttp2: true);