Skip to content

Commit

Permalink
Fix ProtoBuf Mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jan 9, 2025
1 parent 9c94324 commit 0d5fb07
Show file tree
Hide file tree
Showing 14 changed files with 713 additions and 138 deletions.
11 changes: 10 additions & 1 deletion src/WireMock.Net.RestClient/IWireMockAdminApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,23 @@ public interface IWireMockAdminApi
Task<StatusModel> ReloadStaticMappingsAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Get a mapping based on the guid
/// Get a mapping based on the guid.
/// </summary>
/// <param name="guid">The Guid</param>
/// <returns>MappingModel</returns>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("mappings/{guid}")]
Task<MappingModel> GetMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);

/// <summary>
/// Get a mapping based on the guid.
/// </summary>
/// <param name="guid">The Guid</param>
/// <returns>MappingModel</returns>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("mappings/{guid}")]
Task<MappingModel> GetMappingAsync([Path] string guid, CancellationToken cancellationToken = default);

/// <summary>
/// Get the C# code from a mapping based on the guid
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> he
public IResponseBuilder WithTrailingHeader(string name, params string[] values)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeader method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");

Check warning on line 52 in src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs#L52

Added line #L52 was not covered by tests
#else

Guard.NotNull(name);
Expand All @@ -63,7 +63,7 @@ public IResponseBuilder WithTrailingHeader(string name, params string[] values)
public IResponseBuilder WithTrailingHeaders(IDictionary<string, string> headers)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeaders method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");

Check warning on line 66 in src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs#L66

Added line #L66 was not covered by tests
#else

Guard.NotNull(headers);
Expand All @@ -77,7 +77,7 @@ public IResponseBuilder WithTrailingHeaders(IDictionary<string, string> headers)
public IResponseBuilder WithTrailingHeaders(IDictionary<string, string[]> headers)
{
#if !TRAILINGHEADERS
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new System.NotSupportedException("The WithTrailingHeaders method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");

Check warning on line 80 in src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/ResponseBuilders/Response.WithHeaders.cs#L80

Added line #L80 was not covered by tests
#else

Guard.NotNull(headers);
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public MappingModel ToMappingModel(IMapping mapping)
}

var bodyMatchers =
protoBufMatcher?.Matcher != null ? new[] { protoBufMatcher.Matcher } : null ??
protoBufMatcher?.Matcher != null ? [protoBufMatcher.Matcher] : null ??
multiPartMatcher?.Matchers ??
graphQLMatcher?.Matchers ??
bodyMatcher?.Matchers;
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Serialization/MatcherMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public MatcherMapper(WireMockServerSettings settings)
{
model.Pattern = texts[0];
}
else
else if (texts.Count > 1)
{
model.Patterns = texts.Cast<object>().ToArray();
}
Expand Down
45 changes: 44 additions & 1 deletion src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ private Guid ConvertMappingAndRegisterAsRespondProvider(MappingModel mappingMode
respondProvider.WithProbability(mappingModel.Probability.Value);
}

if (mappingModel.ProtoDefinition != null)
{
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinition);
}
else if (mappingModel.ProtoDefinitions != null)
{
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinitions);
}

var responseBuilder = InitResponseBuilder(mappingModel.Response);
respondProvider.RespondWith(responseBuilder);

Expand Down Expand Up @@ -317,6 +326,22 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
}
}

if (responseModel.TrailingHeaders != null)
{
foreach (var entry in responseModel.TrailingHeaders)
{
if (entry.Value is string value)
{
responseBuilder.WithTrailingHeader(entry.Key, value);
}
else
{
var headers = JsonUtils.ParseJTokenToObject<string[]>(entry.Value);
responseBuilder.WithTrailingHeader(entry.Key, headers);
}
}
}

if (responseModel.BodyAsBytes != null)
{
responseBuilder = responseBuilder.WithBody(responseModel.BodyAsBytes, responseModel.BodyDestination, ToEncoding(responseModel.BodyEncoding));
Expand All @@ -327,7 +352,25 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
}
else if (responseModel.BodyAsJson != null)
{
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
if (responseModel.ProtoBufMessageType != null)
{
if (responseModel.ProtoDefinition != null)
{
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinition, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
else if (responseModel.ProtoDefinitions != null)
{
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinitions, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
else
{
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
}
}
else
{
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
}
}
else if (responseModel.BodyAsFile != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,32 @@ message HelloReply {
public async Task IWireMockAdminApi_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
{
// Arrange
using var server = WireMockServer.StartWithAdminInterface();
using var server = Given_WithBodyAsProtoBuf_AddedToServer();

// Act
var api = RestClient.For<IWireMockAdminApi>(server.Url);
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);

await Verifier.Verify(getMappingsResult, VerifySettings);
}

[Fact]
public async Task HttpClient_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
{
// Arrange
using var server = Given_WithBodyAsProtoBuf_AddedToServer();

// Act
var client = server.CreateClient();
var getMappingsResult = await client.GetStringAsync("/__admin/mappings").ConfigureAwait(false);

await Verifier.VerifyJson(getMappingsResult, VerifySettings);
}

public WireMockServer Given_WithBodyAsProtoBuf_AddedToServer()
{
// Arrange
var server = WireMockServer.StartWithAdminInterface();

var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" });

Expand Down Expand Up @@ -122,13 +147,7 @@ public async Task IWireMockAdminApi_GetMappingsAsync_WithBodyAsProtoBuf_ShouldRe
.WithTransformer()
);

// Act
var api = RestClient.For<IWireMockAdminApi>(server.Url);
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);

await Verifier.Verify(getMappingsResult, VerifySettings);

server.Stop();
return server;
}
}
#endif
Loading

0 comments on commit 0d5fb07

Please sign in to comment.