mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-13 05:45:54 +01:00
Add ProtoDefinition to WireMockContainer (#1250)
* AddProtoDefinitionAsync * ... * Body * " * . * . * . * [Fact(Skip = "new docker is needed")] * x
This commit is contained in:
10
test/WireMock.Net.Tests/Constants.cs
Normal file
10
test/WireMock.Net.Tests/Constants.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
internal static class Constants
|
||||
{
|
||||
internal const int NumStaticMappings = 10;
|
||||
|
||||
internal const int NumAdminMappings = 36;
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#if NET6_0_OR_GREATER
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -129,9 +128,9 @@ public partial class TestcontainersTests
|
||||
[Fact]
|
||||
public async Task WireMockContainer_Build_Grpc_ProtoDefinitionFromJson_UsingGrpcGeneratedClient()
|
||||
{
|
||||
var wireMockContainer = await Given_WireMockContainerIsStartedForHttpAndGrpc();
|
||||
var wireMockContainer = await Given_WireMockContainerIsStartedForHttpAndGrpcAsync();
|
||||
|
||||
await Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(wireMockContainer);
|
||||
await Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(wireMockContainer, "protobuf-mapping-1.json");
|
||||
|
||||
var reply = await When_GrpcClient_Calls_SayHelloAsync(wireMockContainer);
|
||||
|
||||
@@ -140,7 +139,21 @@ public partial class TestcontainersTests
|
||||
await wireMockContainer.StopAsync();
|
||||
}
|
||||
|
||||
private static async Task<WireMockContainer> Given_WireMockContainerIsStartedForHttpAndGrpc()
|
||||
[Fact]
|
||||
public async Task WireMockContainer_Build_Grpc_ProtoDefinitionAtServerLevel_UsingGrpcGeneratedClient()
|
||||
{
|
||||
var wireMockContainer = await Given_WireMockContainerWithProtoDefinitionAtServerLevelIsStartedForHttpAndGrpcAsync();
|
||||
|
||||
await Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(wireMockContainer, "protobuf-mapping-4.json");
|
||||
|
||||
var reply = await When_GrpcClient_Calls_SayHelloAsync(wireMockContainer);
|
||||
|
||||
Then_ReplyMessage_Should_BeCorrect(reply);
|
||||
|
||||
await wireMockContainer.StopAsync();
|
||||
}
|
||||
|
||||
private static async Task<WireMockContainer> Given_WireMockContainerIsStartedForHttpAndGrpcAsync()
|
||||
{
|
||||
var wireMockContainer = new WireMockContainerBuilder()
|
||||
.WithAutoRemove(true)
|
||||
@@ -153,9 +166,23 @@ public partial class TestcontainersTests
|
||||
return wireMockContainer;
|
||||
}
|
||||
|
||||
private static async Task Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(WireMockContainer wireMockContainer)
|
||||
private static async Task<WireMockContainer> Given_WireMockContainerWithProtoDefinitionAtServerLevelIsStartedForHttpAndGrpcAsync()
|
||||
{
|
||||
var mappingsJson = ReadMappingFile("protobuf-mapping-1.json");
|
||||
var wireMockContainer = new WireMockContainerBuilder()
|
||||
.WithAutoRemove(true)
|
||||
.WithCleanUp(true)
|
||||
.AddUrl("grpc://*:9090")
|
||||
.AddProtoDefinition("my-greeter", ReadFile("greet.proto"))
|
||||
.Build();
|
||||
|
||||
await wireMockContainer.StartAsync();
|
||||
|
||||
return wireMockContainer;
|
||||
}
|
||||
|
||||
private static async Task Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(WireMockContainer wireMockContainer, string filename)
|
||||
{
|
||||
var mappingsJson = ReadFile(filename);
|
||||
|
||||
using var httpClient = wireMockContainer.CreateClient();
|
||||
|
||||
@@ -178,7 +205,7 @@ public partial class TestcontainersTests
|
||||
reply.Message.Should().Be("hello stef POST");
|
||||
}
|
||||
|
||||
private static string ReadMappingFile(string filename)
|
||||
private static string ReadFile(string filename)
|
||||
{
|
||||
return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings", filename));
|
||||
}
|
||||
|
||||
@@ -162,6 +162,9 @@
|
||||
<None Update="__admin\mappings\*.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="__admin\mappings\*.proto">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="__admin\mappings\subdirectory\*.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
@@ -26,8 +26,6 @@ namespace WireMock.Net.Tests;
|
||||
|
||||
public class WireMockServerAdminTests
|
||||
{
|
||||
private const int NumStaticMappings = 10;
|
||||
|
||||
private static string GetCurrentFolder()
|
||||
{
|
||||
return Directory.GetCurrentDirectory();
|
||||
@@ -40,8 +38,8 @@ public class WireMockServerAdminTests
|
||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
||||
server.ReadStaticMappings(folder);
|
||||
|
||||
Check.That(server.Mappings).HasSize(NumStaticMappings);
|
||||
Check.That(server.MappingModels).HasSize(NumStaticMappings);
|
||||
Check.That(server.Mappings).HasSize(Constants.NumStaticMappings);
|
||||
Check.That(server.MappingModels).HasSize(Constants.NumStaticMappings);
|
||||
|
||||
// Act
|
||||
server.ResetMappings();
|
||||
@@ -220,7 +218,7 @@ public class WireMockServerAdminTests
|
||||
server.ReadStaticMappings(folder);
|
||||
|
||||
var mappings = server.Mappings.ToArray();
|
||||
Check.That(mappings).HasSize(NumStaticMappings);
|
||||
Check.That(mappings).HasSize(Constants.NumStaticMappings);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ public class WireMockServerProxyTests
|
||||
}
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().HaveCount(37);
|
||||
server.Mappings.Should().HaveCount(Constants.NumAdminMappings + 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -75,8 +75,6 @@ public class WireMockServerSettingsTests
|
||||
[Fact]
|
||||
public void WireMockServer_WireMockServerSettings_PriorityFromAllAdminMappingsIsLow_When_StartAdminInterface_IsTrue()
|
||||
{
|
||||
const int count = 35;
|
||||
|
||||
// Assign and Act
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
@@ -85,15 +83,13 @@ public class WireMockServerSettingsTests
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().NotBeNull();
|
||||
server.Mappings.Should().HaveCount(count);
|
||||
server.Mappings.Should().HaveCount(Constants.NumAdminMappings);
|
||||
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_IsMinus2000000_When_StartAdminInterface_IsTrue()
|
||||
{
|
||||
const int count = 36;
|
||||
|
||||
// Assign and Act
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
@@ -106,9 +102,9 @@ public class WireMockServerSettingsTests
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().NotBeNull();
|
||||
server.Mappings.Should().HaveCount(count);
|
||||
server.Mappings.Should().HaveCount(Constants.NumAdminMappings + 1);
|
||||
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(count - 1);
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(Constants.NumAdminMappings);
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
|
||||
}
|
||||
|
||||
|
||||
21
test/WireMock.Net.Tests/__admin/mappings/greet.proto
Normal file
21
test/WireMock.Net.Tests/__admin/mappings/greet.proto
Normal file
@@ -0,0 +1,21 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
enum PhoneType {
|
||||
none = 0;
|
||||
mobile = 1;
|
||||
home = 2;
|
||||
}
|
||||
PhoneType phoneType = 2;
|
||||
}
|
||||
@@ -31,7 +31,7 @@
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}}"
|
||||
"message": "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}}"
|
||||
"message": "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
|
||||
Reference in New Issue
Block a user