diff --git a/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs b/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs index 013244de..f296550e 100644 --- a/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs +++ b/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs @@ -215,14 +215,16 @@ public interface IWireMockServer : IDisposable /// This can be used if you have 1 or more defined and want to register these in WireMock.Net directly instead of using the fluent syntax. /// /// The MappingModels + /// IWireMockServer WithMapping(params MappingModel[] mappings); /// /// Register the mappings (via json string). /// - /// This can be used if you the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax. + /// This can be used if you've the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax. /// /// The mapping(s) as json string. + /// IWireMockServer WithMapping(string mappings); /// @@ -238,5 +240,5 @@ public interface IWireMockServer : IDisposable /// /// The /// C# code - public string MappingsToCSharpCode(MappingConverterType converterType); + string MappingsToCSharpCode(MappingConverterType converterType); } \ No newline at end of file diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index 0ea2332d..a45eb714 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -296,7 +296,7 @@ public IRespondWithAProvider WithWebhook( Guard.NotNull(url); Guard.NotNull(method); - Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) }; + Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)]; if (body != null) { @@ -323,7 +323,7 @@ public IRespondWithAProvider WithWebhook( Guard.NotNull(url); Guard.NotNull(method); - Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) }; + Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)]; if (body != null) { @@ -371,7 +371,7 @@ public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinition { _protoDefinition = new(null, protoDefinitionOrId); } - + return this; } diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.PostMappings.cs b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.PostMappings.cs index e02c5217..2dfad302 100644 --- a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.PostMappings.cs +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.PostMappings.cs @@ -2,37 +2,18 @@ #if !(NET452 || NET461 || NETCOREAPP3_1) using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; -using System.Net.Http.Json; using System.Text; using System.Threading.Tasks; using FluentAssertions; -using Moq; using NFluent; using RestEase; -using VerifyTests; -using VerifyXunit; using WireMock.Admin.Mappings; -using WireMock.Admin.Settings; using WireMock.Client; -using WireMock.Client.Extensions; using WireMock.Constants; -using WireMock.Handlers; -using WireMock.Logging; -using WireMock.Matchers; using WireMock.Models; -using WireMock.Net.Tests.VerifyExtensions; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; using WireMock.Server; -using WireMock.Settings; -using WireMock.Types; -using WireMock.Util; using Xunit; namespace WireMock.Net.Tests.AdminApi; @@ -64,14 +45,9 @@ public async Task HttpClient_PostMappingsAsync_ForProtoBufMapping(string mapping result.EnsureSuccessStatusCode(); // Assert - var mappings = server.Mappings - .Where(m => !m.IsAdminInterface) - .OrderBy(m => m.Title) - .ToArray(); - var mapping = await httpClient.GetStringAsync($"/__admin/mappings/{guid}"); - - RemoveLineContainingUpdatedAt(mapping).Should().Be(mappingsJson); + mapping = RemoveLineContainingUpdatedAt(mapping); + mapping.Should().Be(mappingsJson); } [Fact] diff --git a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs index 063a2c69..f5820bf8 100644 --- a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs +++ b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs @@ -2,17 +2,21 @@ #if PROTOBUF using System; +using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Text; using System.Threading.Tasks; using FluentAssertions; using Greet; using Grpc.Net.Client; +using WireMock.Constants; using WireMock.Matchers; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; +using WireMock.Settings; using Xunit; // ReSharper disable once CheckNamespace @@ -128,8 +132,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_JsonPartialWildcardMatcher() // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); - - server.Stop(); } [Theory] @@ -171,8 +173,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf(string data) var responseBytes = await response.Content.ReadAsByteArrayAsync(); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv"); - - server.Stop(); } [Fact] @@ -209,8 +209,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes() var responseBytes = await response.Content.ReadAsByteArrayAsync(); Convert.ToBase64String(responseBytes).Should().Be(""); - - server.Stop(); } [Fact] @@ -251,8 +249,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinition_WithWe var responseBytes = await response.Content.ReadAsByteArrayAsync(); Convert.ToBase64String(responseBytes).Should().Be(""); - - server.Stop(); } [Fact] @@ -294,8 +290,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_MultipleFiles() var responseBytes = await response.Content.ReadAsByteArrayAsync(); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv"); - - server.Stop(); } [Fact] @@ -333,8 +327,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_InlineProtoDefinition_UsingG // Assert reply.Message.Should().Be("hello stef POST"); - - server.Stop(); } [Fact] @@ -374,8 +366,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_MappingProtoDefinition_Using // Assert reply.Message.Should().Be("hello stef POST"); - - server.Stop(); } [Fact] @@ -410,16 +400,71 @@ public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinition_UsingG ); // Act - var channel = GrpcChannel.ForAddress(server.Url!); + var reply = await When_GrpcClient_Calls_SayHelloAsync(server.Url!); + + // Assert + Then_ReplyMessage_Should_BeCorrect(reply); + } + + [Fact(Skip = "#1233")] + public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinitionFromJson_UsingGrpcGeneratedClient() + { + var server = Given_When_ServerStartedUsingHttp2(); + Given_ProtoDefinition_IsAddedOnServerLevel(server); + await Given_When_ProtoBufMappingIsAddedViaAdminInterfaceAsync(server); + + var reply = await When_GrpcClient_Calls_SayHelloAsync(server.Url!); + + Then_ReplyMessage_Should_BeCorrect(reply); + } + + private static WireMockServer Given_When_ServerStartedUsingHttp2() + { + var settings = new WireMockServerSettings + { + UseHttp2 = false, + StartAdminInterface = true + }; + return WireMockServer.Start(settings); + } + + private static void Given_ProtoDefinition_IsAddedOnServerLevel(WireMockServer server) + { + server.AddProtoDefinition("my-greeter-351f0240-bba0-4bcb-93c6-1feba0fe0003", ReadProtoFile("greet.proto")); + } + + private static async Task Given_When_ProtoBufMappingIsAddedViaAdminInterfaceAsync(WireMockServer server) + { + var mappingsJson = ReadMappingFile("protobuf-mapping-3.json"); + + using var httpClient = server.CreateClient(); + + var result = await httpClient.PostAsync("/__admin/mappings", new StringContent(mappingsJson, Encoding.UTF8, WireMockConstants.ContentTypeJson)); + result.EnsureSuccessStatusCode(); + } + + private static async Task When_GrpcClient_Calls_SayHelloAsync(string address) + { + var channel = GrpcChannel.ForAddress(address); var client = new Greeter.GreeterClient(channel); - var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" }); + return await client.SayHelloAsync(new HelloRequest { Name = "stef" }); + } - // Assert + private static void Then_ReplyMessage_Should_BeCorrect(HelloReply reply) + { reply.Message.Should().Be("hello stef POST"); + } - server.Stop(); + private static string ReadMappingFile(string filename) + { + return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings", filename)); + } + + private static string ReadProtoFile(string filename) + { + return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Grpc", filename)); } } #endif \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 2468b7e4..3583956e 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -134,6 +134,7 @@ Client + PreserveNewest PreserveNewest diff --git a/test/WireMock.Net.Tests/__admin/mappings/protobuf-mapping-3.json b/test/WireMock.Net.Tests/__admin/mappings/protobuf-mapping-3.json index bc767b09..627f878f 100644 --- a/test/WireMock.Net.Tests/__admin/mappings/protobuf-mapping-3.json +++ b/test/WireMock.Net.Tests/__admin/mappings/protobuf-mapping-3.json @@ -44,5 +44,5 @@ }, "ProtoBufMessageType": "greet.HelloReply" }, - "ProtoDefinition": "my-greeter" + "ProtoDefinition": "my-greeter-351f0240-bba0-4bcb-93c6-1feba0fe0003" } \ No newline at end of file