From 847745c2563f0589c6ef2a541075a4d7f70d0317 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 19 Jan 2017 23:37:02 +0100 Subject: [PATCH] Added try-catch for return Matchers --- src/WireMock/Matchers/JSONPathMatcher.cs | 18 +++++++++++++----- src/WireMock/Matchers/RegexMatcher.cs | 15 +++++++++++++-- src/WireMock/Matchers/XPathMatcher.cs | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/WireMock/Matchers/JSONPathMatcher.cs b/src/WireMock/Matchers/JSONPathMatcher.cs index e2c6c306..6fa93bcf 100644 --- a/src/WireMock/Matchers/JSONPathMatcher.cs +++ b/src/WireMock/Matchers/JSONPathMatcher.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System; +using JetBrains.Annotations; using Newtonsoft.Json.Linq; using WireMock.Validation; @@ -35,10 +36,17 @@ namespace WireMock.Matchers if (input == null) return false; - JObject o = JObject.Parse(input); - JToken token = o.SelectToken(_pattern); - - return token != null; + try + { + JObject o = JObject.Parse(input); + JToken token = o.SelectToken(_pattern); + + return token != null; + } + catch (Exception) + { + return false; + } } } } \ No newline at end of file diff --git a/src/WireMock/Matchers/RegexMatcher.cs b/src/WireMock/Matchers/RegexMatcher.cs index 657a137d..8c5eb707 100644 --- a/src/WireMock/Matchers/RegexMatcher.cs +++ b/src/WireMock/Matchers/RegexMatcher.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System; +using System.Text.RegularExpressions; using JetBrains.Annotations; using WireMock.Validation; @@ -32,7 +33,17 @@ namespace WireMock.Matchers /// public bool IsMatch(string input) { - return input != null && _expression.IsMatch(input); + if (input == null) + return false; + + try + { + return _expression.IsMatch(input); + } + catch (Exception) + { + return false; + } } } } \ No newline at end of file diff --git a/src/WireMock/Matchers/XPathMatcher.cs b/src/WireMock/Matchers/XPathMatcher.cs index 992b3fea..caa6b7b9 100644 --- a/src/WireMock/Matchers/XPathMatcher.cs +++ b/src/WireMock/Matchers/XPathMatcher.cs @@ -1,4 +1,5 @@ -using System.Xml; +using System; +using System.Xml; using JetBrains.Annotations; using WireMock.Validation; using Wmhelp.XPath2; @@ -36,10 +37,17 @@ namespace WireMock.Matchers if (input == null) return false; - var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); - object result = nav.XPath2Evaluate($"boolean({_pattern})"); + try + { + var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); + object result = nav.XPath2Evaluate($"boolean({_pattern})"); - return true.Equals(result); + return true.Equals(result); + } + catch (Exception) + { + return false; + } } } } \ No newline at end of file