Fix #1492: Serialize WithClientIP matcher to Request.ClientIP (#1493)

This commit is contained in:
Dmytro Nikitin
2026-08-06 19:28:40 +02:00
committed by GitHub
parent 97c8e4a08a
commit d39f7c771f
7 changed files with 105 additions and 4 deletions
@@ -342,7 +342,7 @@ internal class MappingConverter(MatcherMapper mapper)
if (clientIPMatcher?.Matchers != null)
{
var clientIPMatchers = _mapper.Map(clientIPMatcher.Matchers);
mappingModel.Request.Path = new ClientIPModel
mappingModel.Request.ClientIP = new ClientIPModel
{
Matchers = clientIPMatchers,
MatchOperator = clientIPMatchers?.Length > 1 ? clientIPMatcher.MatchOperator.ToString() : null
@@ -155,7 +155,8 @@ public partial class WireMockServer
var clientIPModel = _settings.DefaultJsonSerializer.ParseJsonToken<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel.Matchers != null)
{
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
var matchOperator = StringUtils.ParseMatchOperator(clientIPModel.MatchOperator);
requestBuilder = requestBuilder.WithClientIP(matchOperator, clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
}
}
}
@@ -0,0 +1,29 @@
{
Guid: Guid_1,
UpdatedAt: DateTime_1,
Title: ,
Description: ,
Priority: 42,
Request: {
ClientIP: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: 1.2.3.4,
IgnoreCase: false
}
]
},
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /foo,
IgnoreCase: false
}
]
}
},
Response: {},
UseWebhooksFireAndForget: false
}
@@ -5,7 +5,7 @@
Description: ,
Priority: 42,
Request: {
Path: {
ClientIP: {
Matchers: [
{
Name: WildcardMatcher,
@@ -5,7 +5,7 @@
Description: ,
Priority: 42,
Request: {
Path: {
ClientIP: {
Matchers: [
{
Name: WildcardMatcher,
@@ -0,0 +1,29 @@
[
{
Guid: Guid_1,
UpdatedAt: DateTime_1,
Request: {
ClientIP: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: 1.2.3.4,
IgnoreCase: false
}
]
},
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /foo,
IgnoreCase: false
}
]
}
},
Response: {
StatusCode: 200
}
}
]
@@ -6,6 +6,7 @@ using WireMock.Models;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
using WireMock.Server;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
@@ -376,6 +377,47 @@ message HelloReply {
return Verify(model);
}
[Fact]
public Task ToMappingModel_Request_WithClientIP_And_Path_MapsClientIPToClientIPModel_NotPath()
{
// Arrange: a request that gates on BOTH ClientIP and Path.
var request = Request.Create().WithPath("/foo").WithClientIP("1.2.3.4");
var response = Response.Create();
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null);
// Act
var model = _sut.ToMappingModel(mapping);
// Assert
model.Should().NotBeNull();
// Verify
return Verify(model);
}
[Fact]
public Task WithClientIP_And_Path_SurvivesMappingModelRoundTrip_AsClientIPMatcher()
{
// Arrange: a server with a mapping that gates on BOTH ClientIP and Path.
using var source = WireMockServer.Start();
source
.Given(Request.Create().WithPath("/foo").WithClientIP("1.2.3.4"))
.RespondWith(Response.Create().WithSuccess());
var models = source.MappingModels.ToArray();
models.Should().ContainSingle();
// Act
using var target = WireMockServer.Start();
target.WithMapping(models);
// Assert
var request = (Request)target.Mappings.Single(m => !m.IsAdminInterface).RequestMatcher;
// Verify
return Verify(target.MappingModels);
}
[Fact]
public Task ToMappingModel_Request_WithHeader_And_Cookie_ReturnsCorrectModel()
{