Skip to content

Commit 5113cc3

Browse files
committed
Display request matcher method in console output
Fixes #14
1 parent f7460b1 commit 5113cc3

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

UnitTests/TestMatchHttpMethod.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ public void RegexMatcher()
5959
Assert.False(matcher.MatchesHttpMethod("get"));
6060
}
6161

62+
[Fact]
63+
public void RegexToStringWithHttpMethods()
64+
{
65+
var matcher = new RegexMatcher("foobar");
66+
matcher.SetMatchingHttpMethods("get post");
67+
68+
Assert.Equal("Regex 'foobar' (GET,POST)", matcher.ToString());
69+
}
70+
71+
[Fact]
72+
public void RegexToStringWithoutHttpMethods()
73+
{
74+
var matcher = new RegexMatcher("foobar");
75+
Assert.Equal("Regex 'foobar'", matcher.ToString());
76+
}
77+
78+
[Fact]
79+
public void XPathToStringWithoutHttpMethods()
80+
{
81+
var matcher = new XPathMatcher("/x/path/");
82+
Assert.Equal("XPath '/x/path/'", matcher.ToString());
83+
}
84+
85+
[Fact]
86+
public void XPathToStringWithHttpMethods()
87+
{
88+
var matcher = new XPathMatcher("/x/path/");
89+
matcher.SetMatchingHttpMethods("get post");
90+
Assert.Equal("XPath '/x/path/' (GET,POST)", matcher.ToString());
91+
}
92+
6293
[Fact]
6394
public void TestCaseDefaultMethodIsGet()
6495
{

netmockery/RequestMatcher.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public IEnumerable<string> MatchingHttpMethods
4141
return _matchingHttpMethods;
4242
}
4343
}
44+
45+
protected string AddMatchingMethodsToString(string baseString)
46+
{
47+
Debug.Assert(baseString != null);
48+
if (MatchingHttpMethods != null)
49+
{
50+
return baseString + " (" + string.Join(",", MatchingHttpMethods) + ")";
51+
}
52+
else
53+
{
54+
return baseString;
55+
}
56+
}
4457
}
4558

4659
public class AnyMatcher : RequestMatcher
@@ -72,10 +85,7 @@ public override bool Matches(PathString path, QueryString queryString, string bo
7285
return Regex.IsMatch(path.ToString(), _regex) || Regex.IsMatch(queryString.ToString(), _regex) || Regex.IsMatch(body, _regex);
7386
}
7487

75-
public override string ToString()
76-
{
77-
return $"Regex '{_regex}'";
78-
}
88+
public override string ToString() => AddMatchingMethodsToString($"Regex '{_regex}'");
7989
}
8090

8191
public class XPathMatcher : RequestMatcher
@@ -112,5 +122,7 @@ public override bool Matches(PathString path, QueryString queryString, string bo
112122
}
113123
return (bool) root.XPathEvaluate(_xpath, namespaceManager);
114124
}
125+
126+
public override string ToString() => AddMatchingMethodsToString($"XPath '{_xpath}'");
115127
}
116128
}

0 commit comments

Comments
 (0)