mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 19:02:24 +01:00
Fix reading JsonMatcher-mapping with object as pattern (#505)
* Fix reading mapping with object pattern for JsonMatcher * . * .
This commit is contained in:
@@ -36,7 +36,7 @@ namespace WireMock.Serialization
|
|||||||
string matcherName = parts[0];
|
string matcherName = parts[0];
|
||||||
string matcherType = parts.Length > 1 ? parts[1] : null;
|
string matcherType = parts.Length > 1 ? parts[1] : null;
|
||||||
|
|
||||||
string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.OfType<string>().ToArray() : new[] { matcher.Pattern as string };
|
string[] stringPatterns = (matcher.Patterns != null ? matcher.Patterns : new[] { matcher.Pattern }).OfType<string>().ToArray();
|
||||||
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
||||||
bool ignoreCase = matcher.IgnoreCase == true;
|
bool ignoreCase = matcher.IgnoreCase == true;
|
||||||
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
|
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
|
||||||
@@ -64,7 +64,8 @@ namespace WireMock.Serialization
|
|||||||
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
|
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
|
||||||
|
|
||||||
case "JsonMatcher":
|
case "JsonMatcher":
|
||||||
return new JsonMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
|
object value = matcher.Pattern ?? matcher.Patterns;
|
||||||
|
return new JsonMatcher(matchBehaviour, value, ignoreCase, throwExceptionWhenMatcherFails);
|
||||||
|
|
||||||
case "JsonPathMatcher":
|
case "JsonPathMatcher":
|
||||||
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
|
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Moq;
|
using System;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Moq;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using System;
|
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Serialization;
|
using WireMock.Serialization;
|
||||||
@@ -26,7 +27,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var model = _sut.Map((IMatcher)null);
|
var model = _sut.Map((IMatcher)null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(model).IsNull();
|
model.Should().BeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -36,7 +37,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var model = _sut.Map((IMatcher[])null);
|
var model = _sut.Map((IMatcher[])null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(model).IsNull();
|
model.Should().BeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -50,7 +51,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var models = _sut.Map(new[] { matcherMock1.Object, matcherMock2.Object });
|
var models = _sut.Map(new[] { matcherMock1.Object, matcherMock2.Object });
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(models).HasSize(2);
|
models.Should().HaveCount(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -65,10 +66,10 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var model = _sut.Map(matcherMock.Object);
|
var model = _sut.Map(matcherMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(model.IgnoreCase).IsNull();
|
model.IgnoreCase.Should().BeNull();
|
||||||
Check.That(model.Name).Equals("test");
|
model.Name.Should().Be("test");
|
||||||
Check.That(model.Pattern).IsNull();
|
model.Pattern.Should().BeNull();
|
||||||
Check.That(model.Patterns).ContainsExactly("p1", "p2");
|
model.Patterns.Should().Contain("p1", "p2");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -82,7 +83,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var model = _sut.Map(matcherMock.Object);
|
var model = _sut.Map(matcherMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(model.IgnoreCase).Equals(true);
|
model.IgnoreCase.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -92,7 +93,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var result = _sut.Map((MatcherModel)null);
|
var result = _sut.Map((MatcherModel)null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(result).IsNull();
|
result.Should().BeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -119,8 +120,8 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var matcher = (LinqMatcher)_sut.Map(model);
|
var matcher = (LinqMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("p");
|
matcher.GetPatterns().Should().Contain("p");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -137,8 +138,88 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
var matcher = (LinqMatcher)_sut.Map(model);
|
var matcher = (LinqMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("p1", "p2");
|
matcher.GetPatterns().Should().Contain("p1", "p2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_String()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
|
||||||
|
var model = new MatcherModel
|
||||||
|
{
|
||||||
|
Name = "JsonMatcher",
|
||||||
|
Pattern = pattern
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var matcher = (JsonMatcher)_sut.Map(model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
|
matcher.Value.Should().BeEquivalentTo(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_As_String()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }";
|
||||||
|
var pattern2 = "{ \"X\": \"x\" }";
|
||||||
|
var patterns = new[] { pattern1, pattern2 };
|
||||||
|
var model = new MatcherModel
|
||||||
|
{
|
||||||
|
Name = "JsonMatcher",
|
||||||
|
Pattern = patterns
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var matcher = (JsonMatcher)_sut.Map(model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
|
matcher.Value.Should().BeEquivalentTo(patterns);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_Object()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var pattern = new { AccountIds = new[] { 1, 2, 3 } };
|
||||||
|
var model = new MatcherModel
|
||||||
|
{
|
||||||
|
Name = "JsonMatcher",
|
||||||
|
Pattern = pattern
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var matcher = (JsonMatcher)_sut.Map(model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
|
matcher.Value.Should().BeEquivalentTo(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_As_Object()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
|
||||||
|
object pattern2 = new { X = "x" };
|
||||||
|
var patterns = new[] { pattern1, pattern2 };
|
||||||
|
var model = new MatcherModel
|
||||||
|
{
|
||||||
|
Name = "JsonMatcher",
|
||||||
|
Patterns = patterns
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var matcher = (JsonMatcher)_sut.Map(model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||||
|
matcher.Value.Should().BeEquivalentTo(patterns);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user