From 4f7259d27aa3bfc8d3c518640ce857fd699e306f Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 7 Aug 2018 19:27:10 +0200 Subject: [PATCH] Fix JsonMatcher and JsonPathMatcher --- .../MainApp.cs | 24 +++++++++---------- src/WireMock.Net/Matchers/JSONPathMatcher.cs | 4 +++- src/WireMock.Net/Matchers/JsonMatcher.cs | 4 +++- .../Matchers/JsonMatcherTests.cs | 14 +++++++++++ .../Matchers/JsonPathMatcherTests.cs | 14 +++++++++++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index a44bf4aa..a291198d 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -142,11 +142,11 @@ namespace WireMock.Net.ConsoleApplication .WithBodyFromFile(@"c:\temp\x.json") ); - server - .Given(Request.Create().WithPath("/file_rel").UsingGet()) - .RespondWith(Response.Create() - .WithBodyFromFile("Program.cs", false) - ); + //server + // .Given(Request.Create().WithPath("/file_rel").UsingGet()) + // .RespondWith(Response.Create() + // .WithBodyFromFile("Program.cs", false) + // ); server .Given(Request.Create().WithHeader("ProxyThis", "true") @@ -176,13 +176,13 @@ namespace WireMock.Net.ConsoleApplication .WithStatusCode(200) .WithBody("hi")); - server - .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) - .AtPriority(4) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); + //server + // .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) + // .AtPriority(4) + // .RespondWith(Response.Create() + // .WithStatusCode(200) + // .WithHeader("Content-Type", "application/json") + // .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); server .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs index 5a4a6241..0b25790e 100644 --- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs @@ -63,7 +63,9 @@ namespace WireMock.Matchers public double IsMatch(object input) { double match = MatchScores.Mismatch; - if (input != null) + + // When input is null or byte[], return Mismatch. + if (input != null && !(input is byte[])) { try { diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index d39477c3..4cfff630 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -65,7 +65,9 @@ namespace WireMock.Matchers public double IsMatch(object input) { bool match = false; - if (input != null) + + // When input is null or byte[], return Mismatch. + if (input != null && !(input is byte[])) { try { diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index 85bc18d6..0dca72b5 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -33,6 +33,20 @@ namespace WireMock.Net.Tests.Matchers Check.That(value).Equals("{}"); } + [Fact] + public void JsonMatcher_IsMatch_ByteArray() + { + // Assign + var bytes = new byte[0]; + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(bytes); + + // Assert + Check.That(match).IsEqualTo(0); + } + [Fact] public void JsonMatcher_IsMatch_NullString() { diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index 8d25cc1b..a1ffe2ce 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -33,6 +33,20 @@ namespace WireMock.Net.Tests.Matchers Check.That(patterns).ContainsExactly("X"); } + [Fact] + public void JsonPathMatcher_IsMatch_ByteArray() + { + // Assign + var bytes = new byte[0]; + var matcher = new JsonPathMatcher(""); + + // Act + double match = matcher.IsMatch(bytes); + + // Assert + Check.That(match).IsEqualTo(0); + } + [Fact] public void JsonPathMatcher_IsMatch_NullString() {