mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 17:41:01 +01:00
Add RegEx support to JsonMatcher (#1091)
* json matcher regex * better test * regression
This commit is contained in:
@@ -40,7 +40,7 @@ public class JsonMatcherTests
|
||||
var matcher = new JsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
var name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("JsonMatcher");
|
||||
@@ -53,7 +53,7 @@ public class JsonMatcherTests
|
||||
var matcher = new JsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
object value = matcher.Value;
|
||||
var value = matcher.Value;
|
||||
|
||||
// Assert
|
||||
Check.That(value).Equals("{}");
|
||||
@@ -90,7 +90,7 @@ public class JsonMatcherTests
|
||||
// Act
|
||||
var result = matcher.IsMatch(new MemoryStream());
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
result.Score.Should().Be(MatchScores.Mismatch);
|
||||
result.Exception.Should().BeAssignableTo<JsonException>();
|
||||
}
|
||||
@@ -102,10 +102,10 @@ public class JsonMatcherTests
|
||||
var bytes = EmptyArray<byte>.Value;
|
||||
var matcher = new JsonMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(bytes).Score;
|
||||
// Act
|
||||
var match = matcher.IsMatch(bytes).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
@@ -116,10 +116,10 @@ public class JsonMatcherTests
|
||||
string? s = null;
|
||||
var matcher = new JsonMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(s).Score;
|
||||
// Act
|
||||
var match = matcher.IsMatch(s).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
@@ -130,215 +130,399 @@ public class JsonMatcherTests
|
||||
object? o = null;
|
||||
var matcher = new JsonMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(o).Score;
|
||||
// Act
|
||||
var match = matcher.IsMatch(o).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JArray()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { "x", "y" });
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"x",
|
||||
"y"
|
||||
};
|
||||
double match = matcher.IsMatch(jArray).Score;
|
||||
var match = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObject()
|
||||
public void JsonMatcher_IsMatch_JObject_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObject_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") },
|
||||
{ "Other", new JValue("abc") }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { id = 1, Name = "test" }, true);
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "NaMe", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Id = 1, Name = "TESt" }, true);
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JArrayAsString()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher("[ \"x\", \"y\" ]");
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"x",
|
||||
"y"
|
||||
};
|
||||
double match = matcher.IsMatch(jArray).Score;
|
||||
var match = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_NormalEnum()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new Test1 { NormalEnum = NormalEnum.Abc});
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new Test1 { NormalEnum = NormalEnum.Abc });
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "NormalEnum", new JValue(0) }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_EnumWithJsonConverter()
|
||||
{
|
||||
// Assign
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new Test2 { EnumWithJsonConverter = EnumWithJsonConverter.Type1 });
|
||||
|
||||
// Act
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "EnumWithJsonConverter", new JValue("Type1") }
|
||||
};
|
||||
double match = matcher.IsMatch(jObject).Score;
|
||||
var match = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithRegexTrue_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Id = "^\\d+$", Name = "Test" }, regex: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(42) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new
|
||||
{
|
||||
Complex = new
|
||||
{
|
||||
Id = "^\\d+$",
|
||||
Name = ".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{
|
||||
"Complex", new JObject
|
||||
{
|
||||
{ "Id", new JValue(42) },
|
||||
{ "Name", new JValue("Test") }
|
||||
}
|
||||
}
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new
|
||||
{
|
||||
Complex = new
|
||||
{
|
||||
Id = "^\\d+$",
|
||||
Name = ".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{
|
||||
"Complex", new JObject
|
||||
{
|
||||
{ "Id", new JValue(42) },
|
||||
{ "Name", new JValue("Test") },
|
||||
{ "Other", new JValue("Other") }
|
||||
}
|
||||
}
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithRegexTrue_Array_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new
|
||||
{
|
||||
Array = new[]
|
||||
{
|
||||
"^\\d+$",
|
||||
".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Array", new JArray("42", "test") }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_WithRegexTrue_Array_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new
|
||||
{
|
||||
Array = new[]
|
||||
{
|
||||
"^\\d+$",
|
||||
".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Array", new JArray("42", "test", "other") }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_GuidAndString()
|
||||
{
|
||||
// Assign
|
||||
var id = Guid.NewGuid();
|
||||
var idAsString = id.ToString();
|
||||
var matcher = new JsonMatcher(new { Id = id });
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(idAsString) }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_StringAndGuid()
|
||||
{
|
||||
// Assign
|
||||
var id = Guid.NewGuid();
|
||||
var idAsString = id.ToString();
|
||||
var matcher = new JsonMatcher(new { Id = idAsString });
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(id) }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,8 @@
|
||||
Pattern: {
|
||||
name: stef
|
||||
},
|
||||
IgnoreCase: false
|
||||
IgnoreCase: false,
|
||||
Regex: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ message HelloReply {
|
||||
Pattern: {
|
||||
name: stef
|
||||
},
|
||||
IgnoreCase: false
|
||||
IgnoreCase: false,
|
||||
Regex: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user