From 58bfb3ea33d2188769830fe4e3088cd08f5f72d9 Mon Sep 17 00:00:00 2001 From: Tymur Nesterenko Date: Wed, 19 Jul 2023 23:34:02 +0300 Subject: [PATCH] JsonPartialMatcher - match guid and string (#972) * JsonPartialMatcher - match guid and string (#971) * JsonPartialMatcher - match guid and string. Add Regex with Guid test (#971) --- .../Matchers/AbstractJsonPartialMatcher.cs | 7 ++++ .../Matchers/JsonPartialMatcherTests.cs | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/WireMock.Net/Matchers/AbstractJsonPartialMatcher.cs b/src/WireMock.Net/Matchers/AbstractJsonPartialMatcher.cs index f9c3c669..0aed05a8 100644 --- a/src/WireMock.Net/Matchers/AbstractJsonPartialMatcher.cs +++ b/src/WireMock.Net/Matchers/AbstractJsonPartialMatcher.cs @@ -74,6 +74,13 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher } } + if (input != null && + ((value.Type == JTokenType.Guid && input.Type == JTokenType.String) || + (value.Type == JTokenType.String && input.Type == JTokenType.Guid))) + { + return IsMatch(value.ToString(), input.ToString()); + } + if (input == null || value.Type != input.Type) { return false; diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index 7d4b33b4..3a2d3043 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -199,6 +199,27 @@ public class JsonPartialMatcherTests Assert.Equal(0.0, match); } + [Fact] + public void JsonPartialMatcher_IsMatch_GuidAsString_UsingRegex() + { + var guid = new Guid("1111238e-b775-44a9-a263-95e570135c94"); + var matcher = new JsonPartialMatcher(new { + Id = 1, + Name = "^1111[a-fA-F0-9]{4}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$" + }, false, false, true); + + // Act + var jObject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue(guid) } + }; + double match = matcher.IsMatch(jObject); + + // Assert + Assert.Equal(1.0, match); + } + [Fact] public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject() { @@ -281,6 +302,25 @@ public class JsonPartialMatcherTests Assert.Equal(1.0, match); } + [Fact] + public void JsonPartialMatcher_IsMatch_GuidAsString() + { + // Assign + var guid = Guid.NewGuid(); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = guid }); + + // Act + var jObject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue(guid.ToString()) } + }; + double match = matcher.IsMatch(jObject); + + // Assert + Assert.Equal(1.0, match); + } + [Fact] public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() {