mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-29 22:02:16 +02:00
Add ProtoDefinition to WireMockContainer (#1250)
* AddProtoDefinitionAsync * ... * Body * " * . * . * . * [Fact(Skip = "new docker is needed")] * x
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.7.1</VersionPrefix>
|
<VersionPrefix>1.7.2-preview-01</VersionPrefix>
|
||||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
|
|||||||
@@ -311,6 +311,15 @@ public interface IWireMockAdminApi
|
|||||||
[Delete("files/{filename}")]
|
[Delete("files/{filename}")]
|
||||||
Task<StatusModel> DeleteFileAsync([Path] string filename, CancellationToken cancellationToken = default);
|
Task<StatusModel> DeleteFileAsync([Path] string filename, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a Grpc ProtoDefinition at server-level.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Unique identifier for the ProtoDefinition.</param>
|
||||||
|
/// <param name="protoDefinition">The ProtoDefinition as text.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
|
[Post("protodefinitions/{id}")]
|
||||||
|
Task<StatusModel> AddProtoDefinitionAsync([Path] string id, [Body] string body, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if a file exists
|
/// Check if a file exists
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using Docker.DotNet.Models;
|
|||||||
using DotNet.Testcontainers.Builders;
|
using DotNet.Testcontainers.Builders;
|
||||||
using DotNet.Testcontainers.Configurations;
|
using DotNet.Testcontainers.Configurations;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using Stef.Validation;
|
||||||
|
|
||||||
namespace WireMock.Net.Testcontainers;
|
namespace WireMock.Net.Testcontainers;
|
||||||
|
|
||||||
@@ -28,6 +29,8 @@ public sealed class WireMockConfiguration : ContainerConfiguration
|
|||||||
|
|
||||||
public List<string> AdditionalUrls { get; private set; } = [];
|
public List<string> AdditionalUrls { get; private set; } = [];
|
||||||
|
|
||||||
|
public Dictionary<string, string[]> ProtoDefinitions { get; set; } = new();
|
||||||
|
|
||||||
public WireMockConfiguration(string? username = null, string? password = null)
|
public WireMockConfiguration(string? username = null, string? password = null)
|
||||||
{
|
{
|
||||||
Username = username;
|
Username = username;
|
||||||
@@ -74,7 +77,8 @@ public sealed class WireMockConfiguration : ContainerConfiguration
|
|||||||
StaticMappingsPath = BuildConfiguration.Combine(oldValue.StaticMappingsPath, newValue.StaticMappingsPath);
|
StaticMappingsPath = BuildConfiguration.Combine(oldValue.StaticMappingsPath, newValue.StaticMappingsPath);
|
||||||
WatchStaticMappings = BuildConfiguration.Combine(oldValue.WatchStaticMappings, newValue.WatchStaticMappings);
|
WatchStaticMappings = BuildConfiguration.Combine(oldValue.WatchStaticMappings, newValue.WatchStaticMappings);
|
||||||
WatchStaticMappingsInSubdirectories = BuildConfiguration.Combine(oldValue.WatchStaticMappingsInSubdirectories, newValue.WatchStaticMappingsInSubdirectories);
|
WatchStaticMappingsInSubdirectories = BuildConfiguration.Combine(oldValue.WatchStaticMappingsInSubdirectories, newValue.WatchStaticMappingsInSubdirectories);
|
||||||
AdditionalUrls = BuildConfiguration.Combine(oldValue.AdditionalUrls.AsEnumerable(), newValue.AdditionalUrls.AsEnumerable()).ToList();
|
AdditionalUrls = Combine(oldValue.AdditionalUrls, newValue.AdditionalUrls);
|
||||||
|
ProtoDefinitions = Combine(oldValue.ProtoDefinitions, newValue.ProtoDefinitions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -107,7 +111,35 @@ public sealed class WireMockConfiguration : ContainerConfiguration
|
|||||||
/// <returns><see cref="WireMockConfiguration"/></returns>
|
/// <returns><see cref="WireMockConfiguration"/></returns>
|
||||||
public WireMockConfiguration WithAdditionalUrl(string url)
|
public WireMockConfiguration WithAdditionalUrl(string url)
|
||||||
{
|
{
|
||||||
AdditionalUrls.Add(url);
|
AdditionalUrls.Add(Guard.NotNullOrWhiteSpace(url));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a Grpc ProtoDefinition at server-level.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Unique identifier for the ProtoDefinition.</param>
|
||||||
|
/// <param name="protoDefinition">The ProtoDefinition as text.</param>
|
||||||
|
/// <returns><see cref="WireMockConfiguration"/></returns>
|
||||||
|
public WireMockConfiguration AddProtoDefinition(string id, params string[] protoDefinition)
|
||||||
|
{
|
||||||
|
Guard.NotNullOrWhiteSpace(id);
|
||||||
|
Guard.NotNullOrEmpty(protoDefinition);
|
||||||
|
|
||||||
|
ProtoDefinitions[id] = protoDefinition;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<T> Combine<T>(List<T> oldValue, List<T> newValue)
|
||||||
|
{
|
||||||
|
return oldValue.Concat(newValue).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<TKey, TValue> Combine<TKey, TValue>(Dictionary<TKey, TValue> oldValue, Dictionary<TKey, TValue> newValue)
|
||||||
|
{
|
||||||
|
return newValue
|
||||||
|
.Concat(oldValue.Where(item => !newValue.Keys.Contains(item.Key)))
|
||||||
|
.ToDictionary(item => item.Key, item => item.Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ using DotNet.Testcontainers.Containers;
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using RestEase;
|
using RestEase;
|
||||||
|
using Stef.Validation;
|
||||||
using WireMock.Client;
|
using WireMock.Client;
|
||||||
using WireMock.Client.Extensions;
|
using WireMock.Client.Extensions;
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
@@ -40,9 +41,9 @@ public sealed class WireMockContainer : DockerContainer
|
|||||||
/// <param name="configuration">The container configuration.</param>
|
/// <param name="configuration">The container configuration.</param>
|
||||||
public WireMockContainer(WireMockConfiguration configuration) : base(configuration)
|
public WireMockContainer(WireMockConfiguration configuration) : base(configuration)
|
||||||
{
|
{
|
||||||
_configuration = Stef.Validation.Guard.NotNull(configuration);
|
_configuration = Guard.NotNull(configuration);
|
||||||
|
|
||||||
Started += WireMockContainer_Started;
|
Started += async (sender, eventArgs) => await WireMockContainerStartedAsync(sender, eventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -175,8 +176,6 @@ public sealed class WireMockContainer : DockerContainer
|
|||||||
_enhancedFileSystemWatcher = null;
|
_enhancedFileSystemWatcher = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Started -= WireMockContainer_Started;
|
|
||||||
|
|
||||||
return base.DisposeAsyncCore();
|
return base.DisposeAsyncCore();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,10 +194,17 @@ public sealed class WireMockContainer : DockerContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WireMockContainer_Started(object sender, EventArgs e)
|
private async Task WireMockContainerStartedAsync(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
_adminApi = CreateWireMockAdminClient();
|
_adminApi = CreateWireMockAdminClient();
|
||||||
|
|
||||||
|
RegisterEnhancedFileSystemWatcher();
|
||||||
|
|
||||||
|
await CallAdditionalActionsAfterStartedAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterEnhancedFileSystemWatcher()
|
||||||
|
{
|
||||||
if (!_configuration.WatchStaticMappings || string.IsNullOrEmpty(_configuration.StaticMappingsPath))
|
if (!_configuration.WatchStaticMappings || string.IsNullOrEmpty(_configuration.StaticMappingsPath))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -214,6 +220,25 @@ public sealed class WireMockContainer : DockerContainer
|
|||||||
_enhancedFileSystemWatcher.EnableRaisingEvents = true;
|
_enhancedFileSystemWatcher.EnableRaisingEvents = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CallAdditionalActionsAfterStartedAsync()
|
||||||
|
{
|
||||||
|
foreach (var kvp in _configuration.ProtoDefinitions)
|
||||||
|
{
|
||||||
|
Logger.LogInformation("Adding ProtoDefinition {Id}", kvp.Key);
|
||||||
|
foreach (var protoDefinition in kvp.Value)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _adminApi!.AddProtoDefinitionAsync(kvp.Key, protoDefinition);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogWarning(ex, "Error adding ProtoDefinition '{Id}'.", kvp.Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async void FileCreatedChangedOrDeleted(object sender, FileSystemEventArgs args)
|
private async void FileCreatedChangedOrDeleted(object sender, FileSystemEventArgs args)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -164,6 +164,23 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
|
|||||||
return WithPortBinding(port, true);
|
return WithPortBinding(port, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a Grpc ProtoDefinition at server-level.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Unique identifier for the ProtoDefinition.</param>
|
||||||
|
/// <param name="protoDefinition">The ProtoDefinition as text.</param>
|
||||||
|
/// <returns><see cref="WireMockContainerBuilder"/></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public WireMockContainerBuilder AddProtoDefinition(string id, params string[] protoDefinition)
|
||||||
|
{
|
||||||
|
Guard.NotNullOrWhiteSpace(id);
|
||||||
|
Guard.NotNullOrEmpty(protoDefinition);
|
||||||
|
|
||||||
|
DockerResourceConfiguration.AddProtoDefinition(id, protoDefinition);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private WireMockContainerBuilder WithCommand(string param, bool value)
|
private WireMockContainerBuilder WithCommand(string param, bool value)
|
||||||
{
|
{
|
||||||
return !value ? this : WithCommand($"{param} true");
|
return !value ? this : WithCommand($"{param} true");
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ public partial class WireMockServer
|
|||||||
public RegexMatcher ScenariosNameMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+$");
|
public RegexMatcher ScenariosNameMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+$");
|
||||||
public RegexMatcher ScenariosNameWithResetMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+\\/reset$");
|
public RegexMatcher ScenariosNameWithResetMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+\\/reset$");
|
||||||
public RegexMatcher FilesFilenamePathMatcher => new($"^{_prefixEscaped}\\/files\\/.+$");
|
public RegexMatcher FilesFilenamePathMatcher => new($"^{_prefixEscaped}\\/files\\/.+$");
|
||||||
|
public RegexMatcher ProtoDefinitionsIdPathMatcher => new($"^{_prefixEscaped}\\/protodefinitions\\/.+$");
|
||||||
}
|
}
|
||||||
|
|
||||||
#region InitAdmin
|
#region InitAdmin
|
||||||
@@ -147,6 +148,9 @@ public partial class WireMockServer
|
|||||||
// __admin/openapi
|
// __admin/openapi
|
||||||
Given(Request.Create().WithPath($"{_adminPaths.OpenApi}/convert").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(OpenApiConvertToMappings));
|
Given(Request.Create().WithPath($"{_adminPaths.OpenApi}/convert").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(OpenApiConvertToMappings));
|
||||||
Given(Request.Create().WithPath($"{_adminPaths.OpenApi}/save").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(OpenApiSaveToMappings));
|
Given(Request.Create().WithPath($"{_adminPaths.OpenApi}/save").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(OpenApiSaveToMappings));
|
||||||
|
|
||||||
|
// __admin/protodefinitions/{id}
|
||||||
|
Given(Request.Create().WithPath(_adminPaths.ProtoDefinitionsIdPathMatcher).UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(ProtoDefinitionAdd));
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -369,7 +373,7 @@ public partial class WireMockServer
|
|||||||
{
|
{
|
||||||
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
|
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
|
||||||
{
|
{
|
||||||
var code = _mappingBuilder.ToCSharpCode(guid, GetMappingConverterType(requestMessage));
|
var code = _mappingBuilder.ToCSharpCode(guid, GetEnumFromQuery(requestMessage, MappingConverterType.Server));
|
||||||
if (code is null)
|
if (code is null)
|
||||||
{
|
{
|
||||||
_settings.Logger.Warn("HttpStatusCode set to 404 : Mapping not found");
|
_settings.Logger.Warn("HttpStatusCode set to 404 : Mapping not found");
|
||||||
@@ -383,15 +387,16 @@ public partial class WireMockServer
|
|||||||
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, "GUID is missing");
|
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, "GUID is missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MappingConverterType GetMappingConverterType(IRequestMessage requestMessage)
|
private static TEnum GetEnumFromQuery<TEnum>(IRequestMessage requestMessage, TEnum defaultValue)
|
||||||
|
where TEnum : struct
|
||||||
{
|
{
|
||||||
if (requestMessage.QueryIgnoreCase?.TryGetValue(nameof(MappingConverterType), out var values) == true &&
|
if (requestMessage.QueryIgnoreCase?.TryGetValue(typeof(TEnum).Name, out var values) == true &&
|
||||||
Enum.TryParse(values.FirstOrDefault(), true, out MappingConverterType parsed))
|
Enum.TryParse<TEnum>(values.FirstOrDefault(), true, out var parsed))
|
||||||
{
|
{
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MappingConverterType.Server;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IMapping? FindMappingByGuid(IRequestMessage requestMessage)
|
private IMapping? FindMappingByGuid(IRequestMessage requestMessage)
|
||||||
@@ -465,7 +470,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
private IResponseMessage MappingsCodeGet(IRequestMessage requestMessage)
|
private IResponseMessage MappingsCodeGet(IRequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
var converterType = GetMappingConverterType(requestMessage);
|
var converterType = GetEnumFromQuery(requestMessage, MappingConverterType.Server);
|
||||||
|
|
||||||
var code = _mappingBuilder.ToCSharpCode(converterType);
|
var code = _mappingBuilder.ToCSharpCode(converterType);
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,22 @@ public partial class WireMockServer
|
|||||||
{
|
{
|
||||||
private static readonly Encoding[] FileBodyIsString = [Encoding.UTF8, Encoding.ASCII];
|
private static readonly Encoding[] FileBodyIsString = [Encoding.UTF8, Encoding.ASCII];
|
||||||
|
|
||||||
|
#region ProtoDefinitions/{id}
|
||||||
|
private IResponseMessage ProtoDefinitionAdd(IRequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
if (requestMessage.Body is null)
|
||||||
|
{
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, "Body is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
var id = requestMessage.Path.Split('/').Last();
|
||||||
|
|
||||||
|
AddProtoDefinition(id, requestMessage.Body);
|
||||||
|
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.OK, "ProtoDefinition added");
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Files/{filename}
|
#region Files/{filename}
|
||||||
private IResponseMessage FilePost(IRequestMessage requestMessage)
|
private IResponseMessage FilePost(IRequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -602,7 +602,14 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
|
|
||||||
_settings.ProtoDefinitions ??= new Dictionary<string, string[]>();
|
_settings.ProtoDefinitions ??= new Dictionary<string, string[]>();
|
||||||
|
|
||||||
_settings.ProtoDefinitions[id] = protoDefinition;
|
if (_settings.ProtoDefinitions.TryGetValue(id, out var existingProtoDefinitions))
|
||||||
|
{
|
||||||
|
_settings.ProtoDefinitions[id] = existingProtoDefinitions.Union(protoDefinition).ToArray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_settings.ProtoDefinitions[id] = protoDefinition;
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
#if NET6_0_OR_GREATER
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -129,9 +128,9 @@ public partial class TestcontainersTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockContainer_Build_Grpc_ProtoDefinitionFromJson_UsingGrpcGeneratedClient()
|
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);
|
var reply = await When_GrpcClient_Calls_SayHelloAsync(wireMockContainer);
|
||||||
|
|
||||||
@@ -140,7 +139,21 @@ public partial class TestcontainersTests
|
|||||||
await wireMockContainer.StopAsync();
|
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()
|
var wireMockContainer = new WireMockContainerBuilder()
|
||||||
.WithAutoRemove(true)
|
.WithAutoRemove(true)
|
||||||
@@ -153,9 +166,23 @@ public partial class TestcontainersTests
|
|||||||
return wireMockContainer;
|
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();
|
using var httpClient = wireMockContainer.CreateClient();
|
||||||
|
|
||||||
@@ -178,7 +205,7 @@ public partial class TestcontainersTests
|
|||||||
reply.Message.Should().Be("hello stef POST");
|
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));
|
return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings", filename));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,6 +162,9 @@
|
|||||||
<None Update="__admin\mappings\*.xml">
|
<None Update="__admin\mappings\*.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="__admin\mappings\*.proto">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="__admin\mappings\subdirectory\*.xml">
|
<None Update="__admin\mappings\subdirectory\*.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ namespace WireMock.Net.Tests;
|
|||||||
|
|
||||||
public class WireMockServerAdminTests
|
public class WireMockServerAdminTests
|
||||||
{
|
{
|
||||||
private const int NumStaticMappings = 10;
|
|
||||||
|
|
||||||
private static string GetCurrentFolder()
|
private static string GetCurrentFolder()
|
||||||
{
|
{
|
||||||
return Directory.GetCurrentDirectory();
|
return Directory.GetCurrentDirectory();
|
||||||
@@ -40,8 +38,8 @@ public class WireMockServerAdminTests
|
|||||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
||||||
server.ReadStaticMappings(folder);
|
server.ReadStaticMappings(folder);
|
||||||
|
|
||||||
Check.That(server.Mappings).HasSize(NumStaticMappings);
|
Check.That(server.Mappings).HasSize(Constants.NumStaticMappings);
|
||||||
Check.That(server.MappingModels).HasSize(NumStaticMappings);
|
Check.That(server.MappingModels).HasSize(Constants.NumStaticMappings);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
server.ResetMappings();
|
server.ResetMappings();
|
||||||
@@ -220,7 +218,7 @@ public class WireMockServerAdminTests
|
|||||||
server.ReadStaticMappings(folder);
|
server.ReadStaticMappings(folder);
|
||||||
|
|
||||||
var mappings = server.Mappings.ToArray();
|
var mappings = server.Mappings.ToArray();
|
||||||
Check.That(mappings).HasSize(NumStaticMappings);
|
Check.That(mappings).HasSize(Constants.NumStaticMappings);
|
||||||
|
|
||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ public class WireMockServerProxyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().HaveCount(37);
|
server.Mappings.Should().HaveCount(Constants.NumAdminMappings + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -75,8 +75,6 @@ public class WireMockServerSettingsTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void WireMockServer_WireMockServerSettings_PriorityFromAllAdminMappingsIsLow_When_StartAdminInterface_IsTrue()
|
public void WireMockServer_WireMockServerSettings_PriorityFromAllAdminMappingsIsLow_When_StartAdminInterface_IsTrue()
|
||||||
{
|
{
|
||||||
const int count = 35;
|
|
||||||
|
|
||||||
// Assign and Act
|
// Assign and Act
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
@@ -85,15 +83,13 @@ public class WireMockServerSettingsTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().NotBeNull();
|
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();
|
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_IsMinus2000000_When_StartAdminInterface_IsTrue()
|
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_IsMinus2000000_When_StartAdminInterface_IsTrue()
|
||||||
{
|
{
|
||||||
const int count = 36;
|
|
||||||
|
|
||||||
// Assign and Act
|
// Assign and Act
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
@@ -106,9 +102,9 @@ public class WireMockServerSettingsTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Mappings.Should().NotBeNull();
|
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);
|
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": {
|
"Response": {
|
||||||
"BodyAsJson": {
|
"BodyAsJson": {
|
||||||
"message": "hello {{request.BodyAsJson.name}}"
|
"message": "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||||
},
|
},
|
||||||
"UseTransformer": true,
|
"UseTransformer": true,
|
||||||
"TransformerType": "Handlebars",
|
"TransformerType": "Handlebars",
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
},
|
},
|
||||||
"Response": {
|
"Response": {
|
||||||
"BodyAsJson": {
|
"BodyAsJson": {
|
||||||
"message": "hello {{request.BodyAsJson.name}}"
|
"message": "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||||
},
|
},
|
||||||
"UseTransformer": true,
|
"UseTransformer": true,
|
||||||
"TransformerType": "Handlebars",
|
"TransformerType": "Handlebars",
|
||||||
|
|||||||
Reference in New Issue
Block a user