Skip to content

Commit 0d5fb07

Browse files
committed
Fix ProtoBuf Mappings
1 parent 9c94324 commit 0d5fb07

14 files changed

+713
-138
lines changed

src/WireMock.Net.RestClient/IWireMockAdminApi.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,23 @@ public interface IWireMockAdminApi
130130
Task<StatusModel> ReloadStaticMappingsAsync(CancellationToken cancellationToken = default);
131131

132132
/// <summary>
133-
/// Get a mapping based on the guid
133+
/// Get a mapping based on the guid.
134134
/// </summary>
135135
/// <param name="guid">The Guid</param>
136136
/// <returns>MappingModel</returns>
137137
/// <param name="cancellationToken">The optional cancellationToken.</param>
138138
[Get("mappings/{guid}")]
139139
Task<MappingModel> GetMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
140140

141+
/// <summary>
142+
/// Get a mapping based on the guid.
143+
/// </summary>
144+
/// <param name="guid">The Guid</param>
145+
/// <returns>MappingModel</returns>
146+
/// <param name="cancellationToken">The optional cancellationToken.</param>
147+
[Get("mappings/{guid}")]
148+
Task<MappingModel> GetMappingAsync([Path] string guid, CancellationToken cancellationToken = default);
149+
141150
/// <summary>
142151
/// Get the C# code from a mapping based on the guid
143152
/// </summary>

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> he
4949
public IResponseBuilder WithTrailingHeader(string name, params string[] values)
5050
{
5151
#if !TRAILINGHEADERS
52-
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
52+
throw new System.NotSupportedException("The WithTrailingHeader method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
5353
#else
5454

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

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

8383
Guard.NotNull(headers);

src/WireMock.Net/Serialization/MappingConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public MappingModel ToMappingModel(IMapping mapping)
379379
}
380380

381381
var bodyMatchers =
382-
protoBufMatcher?.Matcher != null ? new[] { protoBufMatcher.Matcher } : null ??
382+
protoBufMatcher?.Matcher != null ? [protoBufMatcher.Matcher] : null ??
383383
multiPartMatcher?.Matchers ??
384384
graphQLMatcher?.Matchers ??
385385
bodyMatcher?.Matchers;

src/WireMock.Net/Serialization/MatcherMapper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public MatcherMapper(WireMockServerSettings settings)
217217
{
218218
model.Pattern = texts[0];
219219
}
220-
else
220+
else if (texts.Count > 1)
221221
{
222222
model.Patterns = texts.Cast<object>().ToArray();
223223
}

src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs

+44-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ private Guid ConvertMappingAndRegisterAsRespondProvider(MappingModel mappingMode
116116
respondProvider.WithProbability(mappingModel.Probability.Value);
117117
}
118118

119+
if (mappingModel.ProtoDefinition != null)
120+
{
121+
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinition);
122+
}
123+
else if (mappingModel.ProtoDefinitions != null)
124+
{
125+
respondProvider.WithProtoDefinition(mappingModel.ProtoDefinitions);
126+
}
127+
119128
var responseBuilder = InitResponseBuilder(mappingModel.Response);
120129
respondProvider.RespondWith(responseBuilder);
121130

@@ -317,6 +326,22 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
317326
}
318327
}
319328

329+
if (responseModel.TrailingHeaders != null)
330+
{
331+
foreach (var entry in responseModel.TrailingHeaders)
332+
{
333+
if (entry.Value is string value)
334+
{
335+
responseBuilder.WithTrailingHeader(entry.Key, value);
336+
}
337+
else
338+
{
339+
var headers = JsonUtils.ParseJTokenToObject<string[]>(entry.Value);
340+
responseBuilder.WithTrailingHeader(entry.Key, headers);
341+
}
342+
}
343+
}
344+
320345
if (responseModel.BodyAsBytes != null)
321346
{
322347
responseBuilder = responseBuilder.WithBody(responseModel.BodyAsBytes, responseModel.BodyDestination, ToEncoding(responseModel.BodyEncoding));
@@ -327,7 +352,25 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
327352
}
328353
else if (responseModel.BodyAsJson != null)
329354
{
330-
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
355+
if (responseModel.ProtoBufMessageType != null)
356+
{
357+
if (responseModel.ProtoDefinition != null)
358+
{
359+
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinition, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
360+
}
361+
else if (responseModel.ProtoDefinitions != null)
362+
{
363+
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoDefinitions, responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
364+
}
365+
else
366+
{
367+
responseBuilder = responseBuilder.WithBodyAsProtoBuf(responseModel.ProtoBufMessageType, responseModel.BodyAsJson);
368+
}
369+
}
370+
else
371+
{
372+
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
373+
}
331374
}
332375
else if (responseModel.BodyAsFile != null)
333376
{

test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.GetMappingsAsync.cs test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.GetMappings.cs

+27-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,32 @@ message HelloReply {
3737
public async Task IWireMockAdminApi_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
3838
{
3939
// Arrange
40-
using var server = WireMockServer.StartWithAdminInterface();
40+
using var server = Given_WithBodyAsProtoBuf_AddedToServer();
41+
42+
// Act
43+
var api = RestClient.For<IWireMockAdminApi>(server.Url);
44+
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);
45+
46+
await Verifier.Verify(getMappingsResult, VerifySettings);
47+
}
48+
49+
[Fact]
50+
public async Task HttpClient_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
51+
{
52+
// Arrange
53+
using var server = Given_WithBodyAsProtoBuf_AddedToServer();
54+
55+
// Act
56+
var client = server.CreateClient();
57+
var getMappingsResult = await client.GetStringAsync("/__admin/mappings").ConfigureAwait(false);
58+
59+
await Verifier.VerifyJson(getMappingsResult, VerifySettings);
60+
}
61+
62+
public WireMockServer Given_WithBodyAsProtoBuf_AddedToServer()
63+
{
64+
// Arrange
65+
var server = WireMockServer.StartWithAdminInterface();
4166

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

@@ -122,13 +147,7 @@ public async Task IWireMockAdminApi_GetMappingsAsync_WithBodyAsProtoBuf_ShouldRe
122147
.WithTransformer()
123148
);
124149

125-
// Act
126-
var api = RestClient.For<IWireMockAdminApi>(server.Url);
127-
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);
128-
129-
await Verifier.Verify(getMappingsResult, VerifySettings);
130-
131-
server.Stop();
150+
return server;
132151
}
133152
}
134153
#endif

0 commit comments

Comments
 (0)