mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-30 03:30:42 +02:00
Add SystemTextJsonMatchers (#1447)
* SystemTextJsonMatcher * , * . * new projectx * Update test/WireMock.Net.Tests/Pact/PactTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net/WireMock.Net.csproj Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.Minimal/Properties/AssemblyInfo.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * . * more tests * . * . * x * ... * . * fix tests * 0.11.0 * . * delete jsonutils.cs * s * fix findings * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Missing Dispose call on local IDisposable' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * JsonConverter 0.12.0 * -- tools --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,370 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Text.Json;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class SystemTextJsonMatcherTests
|
||||
{
|
||||
public enum NormalEnumStj
|
||||
{
|
||||
Abc
|
||||
}
|
||||
|
||||
public class Test1Stj
|
||||
{
|
||||
public NormalEnumStj NormalEnum { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
var name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
name.Should().Be("SystemTextJsonMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_GetValue()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
var value = matcher.Value;
|
||||
|
||||
// Assert
|
||||
value.Should().Be("{}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_WithInvalidStringValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_WithInvalidObjectValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
using var stream = new MemoryStream();
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(stream);
|
||||
|
||||
// Assert
|
||||
result.Score.Should().Be(MatchScores.Mismatch);
|
||||
result.Exception.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[0];
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(bytes).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string? s = null;
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(s).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object? o = null;
|
||||
var matcher = new SystemTextJsonMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(o).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_JsonArrayAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("[ \"x\", \"y\" ]");
|
||||
|
||||
// Act
|
||||
var jsonElement = JsonDocument.Parse("[ \"x\", \"y\" ]").RootElement;
|
||||
var match = matcher.IsMatch(jsonElement).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_JsonObjectAsString_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }").RootElement;
|
||||
var match = matcher.IsMatch(jsonElement).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_AnonymousObject_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_AnonymousObject_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\", \"Other\" : \"abc\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithIgnoreCaseTrue_JsonObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { id = 1, Name = "test" }, true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"NaMe\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithIgnoreCaseTrue_JsonObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = 1, Name = "TESt" }, true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_JsonObjectAsString_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_JsonObjectWithDateTimeOffsetAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_NormalEnum()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new Test1Stj { NormalEnum = NormalEnumStj.Abc });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"NormalEnum\" : 0 }").Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithRegexTrue_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = "^\\d+$", Name = "Test" }, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : \"42\", \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new
|
||||
{
|
||||
Complex = new
|
||||
{
|
||||
Id = "^\\d+$",
|
||||
Name = ".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Complex\" : { \"Id\" : \"42\", \"Name\" : \"Test\" } }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new
|
||||
{
|
||||
Complex = new
|
||||
{
|
||||
Id = "^\\d+$",
|
||||
Name = ".*"
|
||||
}
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Complex\" : { \"Id\" : \"42\", \"Name\" : \"Test\", \"Other\" : \"Other\" } }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithRegexTrue_Array_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new
|
||||
{
|
||||
Array = new[] { "^\\d+$", ".*" }
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Array\" : [ \"42\", \"test\" ] }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_WithRegexTrue_Array_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new
|
||||
{
|
||||
Array = new[] { "^\\d+$", ".*" }
|
||||
}, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Array\" : [ \"42\", \"test\", \"other\" ] }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_GuidAndString()
|
||||
{
|
||||
// Assign
|
||||
var id = Guid.NewGuid();
|
||||
var idAsString = id.ToString();
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = id });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch($"{{ \"Id\" : \"{idAsString}\" }}").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_StringAndGuid()
|
||||
{
|
||||
// Assign
|
||||
var id = Guid.NewGuid();
|
||||
var idAsString = id.ToString();
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = idAsString });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch($"{{ \"Id\" : \"{id}\" }}").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonMatcher_IsMatch_JsonElement_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }").RootElement;
|
||||
var match = matcher.IsMatch(jsonElement).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Text.Json;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class SystemTextJsonPartialMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
name.Should().Be("SystemTextJsonPartialMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_GetValue()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
|
||||
// Act
|
||||
object value = matcher.Value;
|
||||
|
||||
// Assert
|
||||
value.Should().Be("{}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_WithInvalidStringValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonPartialMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_WithInvalidObjectValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonPartialMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
using var stream = new MemoryStream();
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch(stream);
|
||||
|
||||
// Assert
|
||||
result.Score.Should().Be(MatchScores.Mismatch);
|
||||
result.Exception.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[0];
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(bytes).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string? s = null;
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(s).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object? o = null;
|
||||
var matcher = new SystemTextJsonPartialMatcher("{}");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(o).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonArray()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new[] { "x", "y" });
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("[ \"x\", \"y\" ]").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithRegexTrue()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" }, false, true);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : \"1\", \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithRegexFalse()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" });
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_GuidAsString_UsingRegex()
|
||||
{
|
||||
var guid = new Guid("1111238e-b775-44a9-a263-95e570135c94");
|
||||
var matcher = new SystemTextJsonPartialMatcher(new
|
||||
{
|
||||
Id = 1,
|
||||
Name = "^1111[a-fA-F0-9]{4}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"
|
||||
}, false, true);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch($"{{ \"Id\" : 1, \"Name\" : \"{guid}\" }}").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JsonObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { id = 1, Name = "test" }, true);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"NaMe\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JsonObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = 1, Name = "TESt" }, true);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonArrayAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("[ \"x\", \"y\" ]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("[ \"x\", \"y\" ]").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObjectAsStringWithDottedPropertyName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{ \"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_GuidAsString()
|
||||
{
|
||||
// Assign
|
||||
var guid = Guid.NewGuid();
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = 1, Name = guid });
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch($"{{ \"Id\" : 1, \"Name\" : \"{guid}\" }}").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JsonObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObjectAsString_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonObjectWithDateTimeOffsetAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}")]
|
||||
[InlineData("\"test\"", "\"test\"")]
|
||||
[InlineData("123", "123")]
|
||||
[InlineData("[\"test\"]", "[\"test\"]")]
|
||||
[InlineData("[\"test\"]", "[\"test\", \"other\"]")]
|
||||
[InlineData("[123]", "[123]")]
|
||||
[InlineData("[123]", "[123, 456]")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\"}")]
|
||||
[InlineData("{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}")]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_StringInputValidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(value);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("\"test\"", null)]
|
||||
[InlineData("\"test1\"", "\"test2\"")]
|
||||
[InlineData("123", "1234")]
|
||||
[InlineData("[\"test\"]", "[\"test1\"]")]
|
||||
[InlineData("[\"test\"]", "[\"test1\", \"test2\"]")]
|
||||
[InlineData("[123]", "[1234]")]
|
||||
[InlineData("{}", "\"test\"")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value2\"}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
|
||||
[InlineData("{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, string? input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(value);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":123}}")]
|
||||
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[123, 456]}}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}")]
|
||||
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]")]
|
||||
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]")]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_ValueAsPathValidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(value);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":456}}")]
|
||||
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[1, 2]}}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
|
||||
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value1\"}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
|
||||
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value1\"}]")]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_ValueAsPathInvalidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(value);
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialMatcher_IsMatch_JsonElement_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\", \"Extra\" : \"value\" }").RootElement;
|
||||
double match = matcher.IsMatch(jsonElement).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Text.Json;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class SystemTextJsonPartialWildcardMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
var name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
name.Should().Be("SystemTextJsonPartialWildcardMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_GetValue()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
var value = matcher.Value;
|
||||
|
||||
// Assert
|
||||
value.Should().Be("{}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_WithInvalidStringValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_WithInvalidObjectValue_Should_ThrowException()
|
||||
{
|
||||
// Act
|
||||
Action action = () => new SystemTextJsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
using var stream = new MemoryStream();
|
||||
var result = matcher.IsMatch(stream);
|
||||
|
||||
// Assert
|
||||
result.Score.Should().Be(MatchScores.Mismatch);
|
||||
result.Exception.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[0];
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(bytes).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string? s = null;
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(s).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object? o = null;
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{}");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(o).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonArray()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new[] { "x", "y" });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("[ \"x\", \"y\" ]").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JsonObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { id = 1, Name = "test" }, true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"NaMe\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JsonObjectParsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "TESt" }, true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonArrayAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("[ \"x\", \"y\" ]");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("[ \"x\", \"y\" ]").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JsonObjectAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonObjectAsString_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Name\" : \"Test\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonObjectWithDateTimeOffsetAsString()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}")]
|
||||
[InlineData("\"test\"", "\"test\"")]
|
||||
[InlineData("123", "123")]
|
||||
[InlineData("[\"test\"]", "[\"test\"]")]
|
||||
[InlineData("[\"test\"]", "[\"test\", \"other\"]")]
|
||||
[InlineData("[123]", "[123]")]
|
||||
[InlineData("[123]", "[123, 456]")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\"}")]
|
||||
[InlineData("{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}")]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_StringInput_IsValidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(value);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"test\":\"*\"}", "{\"test\":\"xxx\",\"other\":\"xyz\"}")]
|
||||
[InlineData("\"t*t\"", "\"test\"")]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_StringInputWithWildcard_IsValidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(value);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("\"test\"", null)]
|
||||
[InlineData("\"test1\"", "\"test2\"")]
|
||||
[InlineData("123", "1234")]
|
||||
[InlineData("[\"test\"]", "[\"test1\"]")]
|
||||
[InlineData("[\"test\"]", "[\"test1\", \"test2\"]")]
|
||||
[InlineData("[123]", "[1234]")]
|
||||
[InlineData("{}", "\"test\"")]
|
||||
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value2\"}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
|
||||
[InlineData("{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_StringInputWithInvalidMatch(string value, string? input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(value);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":123}}")]
|
||||
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[123, 456]}}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}")]
|
||||
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]")]
|
||||
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]")]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_ValueAsPathValidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(value);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":456}}")]
|
||||
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[1, 2]}}")]
|
||||
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
|
||||
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value1\"}")]
|
||||
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
|
||||
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value1\"}]")]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_ValueAsPathInvalidMatch(string value, string input)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(value);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrueAndRegexTrue_JsonObject1()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { id = 1, Number = "^\\d+$" }, ignoreCase: true, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{ \"Id\" : 1, \"Number\" : \"42\" }").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrueAndRegexTrue_JsonObject2()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||
|
||||
// Act
|
||||
var match = matcher.IsMatch("{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}").Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonElement_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Act
|
||||
var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\", \"Extra\" : \"value\" }").RootElement;
|
||||
var match = matcher.IsMatch(jsonElement).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class SystemTextJsonPathMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_GetName()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("X");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
name.Should().Be("SystemTextJsonPathMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_GetPatterns()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("X");
|
||||
|
||||
// Act
|
||||
var patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
patterns.Should().ContainSingle("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = new byte[0];
|
||||
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(bytes).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(null).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_EmptyString()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(string.Empty).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Arrange
|
||||
object? o = null;
|
||||
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(o).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_String_Exception_Mismatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("not-json").Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_AnonymousObject()
|
||||
{
|
||||
// Arrange - RFC 9535: filter expression requires an array context
|
||||
var matcher = new SystemTextJsonPathMatcher("$[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(new[] { new { Id = 1, Name = "Test" } }).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_AnonymousObject_WithNestedObject()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(new { things = new { name = "x" } }).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_String_WithNestedObject()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{ \"things\": { \"name\": \"x\" } }";
|
||||
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(json).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsNoMatch_String_WithNestedObject()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{ \"things\": { \"name\": \"y\" } }";
|
||||
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(json).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_JsonNode()
|
||||
{
|
||||
// Arrange - RFC 9535: filter expression requires an array context
|
||||
string[] patterns = { "$[?(@.Id == 1)]" };
|
||||
var matcher = new SystemTextJsonPathMatcher(patterns);
|
||||
|
||||
// Act
|
||||
var node = JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]");
|
||||
double match = matcher.IsMatch(node).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_JsonNode_Parsed()
|
||||
{
|
||||
// Arrange - RFC 9535: filter expression requires an array context
|
||||
var matcher = new SystemTextJsonPathMatcher("$[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Arrange - RFC 9535: filter expression requires an array context
|
||||
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "$[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_ArrayOneLevel()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1""
|
||||
}]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_ObjectMatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.test");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [
|
||||
{
|
||||
""line1"": ""line1""
|
||||
}
|
||||
]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.test3");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [
|
||||
{
|
||||
""line1"": ""line1""
|
||||
}
|
||||
]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatchInArray()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": []
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatchNoObjectsInArray()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$arr[2].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": []
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_NestedArrays()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.arr[0].sub[0].subline1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1"",
|
||||
""sub"":[
|
||||
{
|
||||
""subline1"":""subline1""
|
||||
}]
|
||||
}]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorAnd()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.And, "$.arr[0].sub[0].subline1", "$.arr[0].line2");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1"",
|
||||
""sub"":[
|
||||
{
|
||||
""subline1"":""subline1""
|
||||
}]
|
||||
}]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorOr()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "$.arr[0].sub[0].subline2", "$.arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1"",
|
||||
""sub"":[
|
||||
{
|
||||
""subline1"":""subline1""
|
||||
}]
|
||||
}]
|
||||
}")).Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_String_ArrayOneLevel()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1""
|
||||
}]
|
||||
}").Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SystemTextJsonPathMatcher_IsMatch_String_DoesntMatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new SystemTextJsonPathMatcher("$.test3");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(@"{ ""test"": ""test"" }").Score;
|
||||
|
||||
// Assert
|
||||
match.Should().Be(0.0);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using WireMock.Server;
|
||||
|
||||
namespace WireMock.Net.Tests.Pact;
|
||||
|
||||
[Collection(nameof(PactTests))]
|
||||
public class PactTests
|
||||
{
|
||||
[Fact]
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
@@ -323,7 +322,7 @@ public class RequestBuilderWithBodyTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyJson_PathMatcher_false()
|
||||
public void Request_WithBodyJson_JsonPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
@@ -368,10 +367,10 @@ public class RequestBuilderWithBodyTests
|
||||
public void Request_WithBody_Array_JsonPathMatcher_1()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]"));
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$[?(@.Id == 1)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";
|
||||
string jsonString = "[{\"Id\": 1, \"Name\": \"Test\"}, {\"Id\": 2, \"Name\": \"Test2\"}]";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
@@ -391,10 +390,10 @@ public class RequestBuilderWithBodyTests
|
||||
public void Request_WithBody_Array_JsonPathMatcher_2()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]"));
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.test"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"Id\": 1, \"Name\": \"Test\" }";
|
||||
string jsonString = "{\"name\": \"PathSelectorTest\", \"test\": \"test\", \"test2\": \"test2\", \"arr\": [{\"line1\": \"line1\"}]}";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
@@ -479,5 +478,132 @@ public class RequestBuilderWithBodyTests
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
requestBuilder.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_SystemTextJsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyJson_SystemTextJsonPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().NotBe(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_Object_SystemTextJsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$.arr[0].line1"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{\"name\": \"PathSelectorTest\", \"test\": \"test\", \"test2\": \"test2\", \"arr\": [{\"line1\": \"line1\"}]}";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_Array_SystemTextJsonPathMatcher_1()
|
||||
{
|
||||
// Arrange - RFC 9535: filter expression requires an array context
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$[?(@.Id == 1)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "[{\"Id\": 1, \"Name\": \"Test\"}, {\"Id\": 2, \"Name\": \"Test2\"}]";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_Array_SystemTextJsonPathMatcher_2()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$.test"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{\"name\": \"PathSelectorTest\", \"test\": \"test\", \"test2\": \"test2\", \"arr\": [{\"line1\": \"line1\"}]}";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
double result = spec.GetMatchingScore(request, requestMatchResult);
|
||||
result.Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_SystemTextJsonPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new SystemTextJsonPathMatcher("$.nonexistent"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{\"name\": \"Test\", \"arr\": [{\"line1\": \"line1\"}]}";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().NotBe(1.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
|
||||
@@ -18,7 +19,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_Arguments()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "one", "--test2", "2", "--test3", "3", "--test4", "true", "--test5", "Https" });
|
||||
_parser.Parse(["--test1", "one", "--test2", "2", "--test3", "3", "--test4", "true", "--test5", "Https"]);
|
||||
|
||||
// Act
|
||||
string? stringValue = _parser.GetStringValue("test1");
|
||||
@@ -46,7 +47,7 @@ public class SimpleSettingsParserTests
|
||||
{ "WireMockServerSettings__test1", "one" },
|
||||
{ "WireMockServerSettings__test2", "two" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
_parser.Parse([], env);
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
@@ -61,7 +62,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsAsCombinedKeyAndValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2 two", "--test3 three" });
|
||||
_parser.Parse(["--test1 one", "--test2 two", "--test3 three"]);
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
@@ -78,7 +79,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsMixed()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2", "two", "--test3 three" });
|
||||
_parser.Parse(["--test1 one", "--test2", "two", "--test3 three"]);
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
@@ -95,7 +96,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_GetBoolValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "'--test1", "false'", "--test2 true" });
|
||||
_parser.Parse(["'--test1", "false'", "--test2 true"]);
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolValue("test1");
|
||||
@@ -112,7 +113,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_GetBoolWithDefault()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "true", "--test2", "false" });
|
||||
_parser.Parse(["--test1", "true", "--test2", "false"]);
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolWithDefault("test1", "test1_fallback", defaultValue: false);
|
||||
@@ -134,7 +135,7 @@ public class SimpleSettingsParserTests
|
||||
{ "WireMockServerSettings__test1", "false" },
|
||||
{ "WireMockServerSettings__test2", "true" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
_parser.Parse([], env);
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolValue("test1");
|
||||
@@ -151,7 +152,7 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_GetIntValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "42", "--test2 55" });
|
||||
_parser.Parse(["--test1", "42", "--test2 55"]);
|
||||
|
||||
// Act
|
||||
int? value1 = _parser.GetIntValue("test1");
|
||||
@@ -175,7 +176,7 @@ public class SimpleSettingsParserTests
|
||||
{ "WireMockServerSettings__test1", "42" },
|
||||
{ "WireMockServerSETTINGS__TEST2", "55" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
_parser.Parse([], env);
|
||||
|
||||
// Act
|
||||
int? value1 = _parser.GetIntValue("test1");
|
||||
@@ -194,10 +195,10 @@ public class SimpleSettingsParserTests
|
||||
public void SimpleCommandLineParser_Parse_GetObjectValueFromJson()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { @"--json {""k1"":""v1"",""k2"":""v2""}" });
|
||||
_parser.Parse([@"--json {""k1"":""v1"",""k2"":""v2""}"]);
|
||||
|
||||
// Act
|
||||
var value = _parser.GetObjectValueFromJson<Dictionary<string, string>>("json");
|
||||
var value = _parser.GetObjectValueFromJson<Dictionary<string, string>>("json", new NewtonsoftJsonConverter());
|
||||
|
||||
// Assert
|
||||
var expected = new Dictionary<string, string>
|
||||
@@ -207,4 +208,4 @@ public class SimpleSettingsParserTests
|
||||
};
|
||||
value.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Transformers;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Net.Tests.Transformers;
|
||||
|
||||
public class JsonBodyTransformerTests
|
||||
{
|
||||
public static TheoryData<JsonBodyTransformerTestContext> Transformers
|
||||
{
|
||||
get
|
||||
{
|
||||
var settings = new WireMockServerSettings();
|
||||
|
||||
return
|
||||
[
|
||||
new JsonBodyTransformerTestContext(
|
||||
() => new NewtonsoftJsonBodyTransformer(settings),
|
||||
JObject.Parse,
|
||||
body => ((JToken)body).ToString(Formatting.None)),
|
||||
|
||||
new JsonBodyTransformerTestContext(
|
||||
() => new SystemTextJsonBodyTransformer(settings),
|
||||
json => JsonNode.Parse(json)!,
|
||||
body => ((JsonNode)body).ToJsonString())
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Transformers))]
|
||||
public void TransformBodyAsJson_Replaces_String_Value_And_Preserves_Original(JsonBodyTransformerTestContext testContext)
|
||||
{
|
||||
// Arrange
|
||||
var transformer = testContext.CreateTransformer();
|
||||
var originalJson = testContext.ParseJson("{\"value\":\"{{number}}\"}");
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
DetectedBodyTypeFromContentType = BodyType.Json,
|
||||
ProtoBufMessageType = "My.Message",
|
||||
BodyAsJson = originalJson
|
||||
};
|
||||
|
||||
var transformerContext = new FakeTransformerContext(
|
||||
text => text,
|
||||
text => text == "{{number}}" ? "123" : text);
|
||||
|
||||
// Act
|
||||
var result = transformer.TransformBodyAsJson(transformerContext, ReplaceNodeOptions.EvaluateAndTryToConvert, new { }, bodyData);
|
||||
|
||||
// Assert
|
||||
result.Encoding.Should().Be(Encoding.UTF8);
|
||||
result.DetectedBodyType.Should().Be(BodyType.Json);
|
||||
result.DetectedBodyTypeFromContentType.Should().Be(BodyType.Json);
|
||||
result.ProtoBufMessageType.Should().Be("My.Message");
|
||||
result.BodyAsJson.Should().NotBeNull();
|
||||
testContext.SerializeJson(result.BodyAsJson).Should().Be("{\"value\":123}");
|
||||
testContext.SerializeJson(originalJson).Should().Be("{\"value\":\"{{number}}\"}");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Transformers))]
|
||||
public void TransformBodyAsJson_With_String_Body_Replaces_Single_Node_With_Object(JsonBodyTransformerTestContext testContext)
|
||||
{
|
||||
// Arrange
|
||||
var transformer = testContext.CreateTransformer();
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.Json,
|
||||
BodyAsJson = "{{json}}"
|
||||
};
|
||||
|
||||
var transformerContext = new FakeTransformerContext(
|
||||
text => text == "{{json}}" ? "{\"name\":\"test\"}" : text,
|
||||
text => text);
|
||||
|
||||
// Act
|
||||
var result = transformer.TransformBodyAsJson(transformerContext, ReplaceNodeOptions.EvaluateAndTryToConvert, new { }, bodyData);
|
||||
|
||||
// Assert
|
||||
result.BodyAsJson.Should().NotBeNull();
|
||||
testContext.SerializeJson(result.BodyAsJson).Should().Be("{\"name\":\"test\"}");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Transformers))]
|
||||
public void TransformBodyAsJson_Replaces_String_Value_With_WireMockList_As_Array(JsonBodyTransformerTestContext testContext)
|
||||
{
|
||||
// Arrange
|
||||
var transformer = testContext.CreateTransformer();
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.Json,
|
||||
BodyAsJson = testContext.ParseJson("{\"values\":\"{{list}}\"}")
|
||||
};
|
||||
|
||||
var transformerContext = new FakeTransformerContext(
|
||||
text => text,
|
||||
text => text == "{{list}}" ? new WireMockList<string>(["a", "b"]) : text);
|
||||
|
||||
// Act
|
||||
var result = transformer.TransformBodyAsJson(transformerContext, ReplaceNodeOptions.EvaluateAndTryToConvert, new { }, bodyData);
|
||||
|
||||
// Assert
|
||||
result.BodyAsJson.Should().NotBeNull();
|
||||
testContext.SerializeJson(result.BodyAsJson).Should().Be("{\"values\":[\"a\",\"b\"]}");
|
||||
}
|
||||
|
||||
public sealed class JsonBodyTransformerTestContext
|
||||
{
|
||||
private readonly Func<IJsonBodyTransformer> _createTransformer;
|
||||
private readonly Func<string, object> _parseJson;
|
||||
private readonly Func<object, string> _serializeJson;
|
||||
|
||||
public JsonBodyTransformerTestContext(
|
||||
Func<IJsonBodyTransformer> createTransformer,
|
||||
Func<string, object> parseJson,
|
||||
Func<object, string> serializeJson)
|
||||
{
|
||||
_createTransformer = createTransformer;
|
||||
_parseJson = parseJson;
|
||||
_serializeJson = serializeJson;
|
||||
}
|
||||
|
||||
public IJsonBodyTransformer CreateTransformer()
|
||||
{
|
||||
return _createTransformer();
|
||||
}
|
||||
|
||||
public object ParseJson(string json)
|
||||
{
|
||||
return _parseJson(json);
|
||||
}
|
||||
|
||||
public string SerializeJson(object body)
|
||||
{
|
||||
return _serializeJson(body);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FakeTransformerContext(Func<string, string> parseAndRender, Func<string, object> parseAndEvaluate) : ITransformerContext
|
||||
{
|
||||
public IFileSystemHandler FileSystemHandler { get; private set; } = Mock.Of<IFileSystemHandler>();
|
||||
|
||||
public string ParseAndRender(string text, object model)
|
||||
{
|
||||
return parseAndRender(text);
|
||||
}
|
||||
|
||||
public object ParseAndEvaluate(string text, object model)
|
||||
{
|
||||
return parseAndEvaluate(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -750,6 +750,8 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc
|
||||
{
|
||||
await client.SendAsync(testMessage, cancellationToken: _ct);
|
||||
|
||||
await Task.Delay(250, _ct);
|
||||
|
||||
var received = await client.ReceiveAsTextAsync(cancellationToken: _ct);
|
||||
received.Should().Be(testMessage, $"message '{testMessage}' should be proxied and echoed back");
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.AwesomeAssertions\WireMock.Net.AwesomeAssertions.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.Matchers.CSharpCode\WireMock.Net.Matchers.CSharpCode.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.Matchers.SystemTextJsonPath\WireMock.Net.Matchers.SystemTextJsonPath.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.RestClient.AwesomeAssertions\WireMock.Net.RestClient.AwesomeAssertions.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.RestClient\WireMock.Net.RestClient.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.xUnit.v3\WireMock.Net.xUnit.v3.csproj" />
|
||||
@@ -75,7 +76,6 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.33.5" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.76.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.78.0">
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using JsonConverter.System.Text.Json;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Net.Xunit;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
@@ -232,9 +233,13 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new SystemTextJsonConverter());
|
||||
using var server = WireMockServer.Start(settings =>
|
||||
{
|
||||
settings.Logger = new TestOutputHelperWireMockLogger(testOutputHelper);
|
||||
settings.DefaultJsonSerializer = new SystemTextJsonConverter();
|
||||
});
|
||||
|
||||
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||
var matcher = new SystemTextJsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||
server.Given(Request.Create()
|
||||
.WithHeader("Content-Type", "application/json*")
|
||||
.UsingPost()
|
||||
|
||||
Reference in New Issue
Block a user