Skip to content

Commit ed1b942

Browse files
committed
Now with PROPER querystring support
1 parent 0a6b7c9 commit ed1b942

File tree

12 files changed

+53
-28
lines changed

12 files changed

+53
-28
lines changed

UnitTests/TestEndpoint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ResolveMoreThanOneMatcher()
3131
endpoint.Add(new AnyMatcher(), new LiteralResponse("foobar"));
3232

3333
bool singleMatch;
34-
var firstmatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(""), "foo", null, out singleMatch);
34+
var firstmatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(""), new Microsoft.AspNetCore.Http.QueryString(""), "foo", null, out singleMatch);
3535
Assert.False(singleMatch);
3636
Assert.IsType(typeof(RegexMatcher), firstmatch.Item1);
3737
}
@@ -44,7 +44,7 @@ public void ResolveOnlyOne()
4444
endpoint.Add(new AnyMatcher(), new LiteralResponse("foobar"));
4545

4646
bool singleMatch;
47-
var firstmatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(""), "bar", null, out singleMatch);
47+
var firstmatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(""), new Microsoft.AspNetCore.Http.QueryString(), "bar", null, out singleMatch);
4848
Assert.True(singleMatch);
4949
Assert.IsType(typeof(AnyMatcher), firstmatch.Item1);
5050

UnitTests/TestRegExMatcher.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,29 @@ public class TestRegExMatcher
2727
public void CanMatchBody()
2828
{
2929
var matcher = new RegexMatcher("<ssn>13116900216</ssn>");
30-
Assert.True(matcher.Matches(null, BODY, null));
30+
Assert.True(matcher.Matches(null, new QueryString(), BODY, null));
3131
}
3232

3333
[Fact]
3434
public void NoMatch()
3535
{
3636
var matcher = new RegexMatcher("<ssn>13116900217</ssn>");
37-
Assert.False(matcher.Matches(null, BODY, null));
37+
Assert.False(matcher.Matches(null, new QueryString(), BODY, null));
3838
}
3939

4040
[Fact]
4141
public void MatchesBothUrlAndBody()
4242
{
4343
var matcher = new RegexMatcher("abcde");
44-
Assert.True(matcher.Matches(new PathString("/foo/bar/ae/fx/"), "content in body: abcde", null));
45-
Assert.True(matcher.Matches(new PathString("/foo/bar/abcde/fx/"), "", null));
44+
Assert.True(matcher.Matches(new PathString("/foo/bar/ae/fx/"), new QueryString(), "content in body: abcde", null));
45+
Assert.True(matcher.Matches(new PathString("/foo/bar/abcde/fx/"), new QueryString(), "", null));
46+
}
47+
48+
[Fact]
49+
public void MatchesQueryString()
50+
{
51+
var matcher = new RegexMatcher("abcde");
52+
Assert.True(matcher.Matches(new PathString("/foo/bar/"), new QueryString("?value=abcde"), "", null));
4653
}
4754

4855
}

UnitTests/TestTestCommand.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async public void CanCheckExpectedRequestMatcherSuccess()
186186
}
187187

188188
[Fact]
189-
async public void CanCheckExpectedResponseCreatorFailureError()
189+
async public void CanCheckExpectedResponseCreatorError()
190190
{
191191
var testcase =
192192
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedresponsecreator = "File content.txt" })
@@ -200,7 +200,7 @@ async public void CanCheckExpectedResponseCreatorFailureError()
200200
}
201201

202202
[Fact]
203-
async public void CanCheckExpectedResponseCreatorFailureSuccess()
203+
async public void CanCheckExpectedResponseCreatorSuccess()
204204
{
205205
var testcase =
206206
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedresponsecreator = "File content.txt" })
@@ -210,5 +210,23 @@ async public void CanCheckExpectedResponseCreatorFailureSuccess()
210210
Assert.True(result.OK, result.Message);
211211
Assert.Null(result.Message);
212212
}
213+
214+
[Fact]
215+
public void CanReadQueryString()
216+
{
217+
var testcase = (new JSONTest { querystring = "?foo=bar" }).CreateTestCase(".");
218+
Assert.Equal("?foo=bar", testcase.QueryString);
219+
}
220+
221+
[Fact]
222+
async public void CanExecuteWithQueryStringFailure()
223+
{
224+
var testcase =
225+
(new JSONTest { name = "checksomething", requestpath = "/foo/", querystring = "?a=test", requestbody = "foobar", expectedresponsecreator = "File content.txt" })
226+
.Validated().CreateTestCase(".");
227+
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
228+
Assert.True(result.OK, result.Message);
229+
Assert.Null(result.Message);
230+
}
213231
}
214232
}

UnitTests/TestXPathMatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public void CanMatchBody()
2727
{
2828
var matcher = new XPathMatcher("//nhn:ssn = '13116900216'");
2929
matcher.AddNamespace("nhn", "http://register.nhn.no/Orchestration");
30-
Assert.True(matcher.Matches(null, BODY, null));
30+
Assert.True(matcher.Matches(null, new Microsoft.AspNetCore.Http.QueryString(), BODY, null));
3131
}
3232

3333
[Fact]
3434
public void NoMatch()
3535
{
3636
var matcher = new XPathMatcher("//nhn:ssn = '13116900217'");
3737
matcher.AddNamespace("nhn", "http://register.nhn.no/Orchestration");
38-
Assert.False(matcher.Matches(null, BODY, null));
38+
Assert.False(matcher.Matches(null, new Microsoft.AspNetCore.Http.QueryString(), BODY, null));
3939
}
4040
}
4141
}

netmockery/Endpoint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public void Add(RequestMatcher requestMatcher, ResponseCreator responseCreator)
6262
}
6363
}
6464

65-
public Tuple<RequestMatcher, ResponseCreator> Resolve(PathString path, string body, IHeaderDictionary headers, out bool singleMatch)
65+
public Tuple<RequestMatcher, ResponseCreator> Resolve(PathString path, QueryString queryString, string body, IHeaderDictionary headers, out bool singleMatch)
6666
{
6767
singleMatch = true;
68-
var candidates = (from t in _responses where t.Item1.Matches(path, body, headers) select t).Take(2);
68+
var candidates = (from t in _responses where t.Item1.Matches(path, queryString, body, headers) select t).Take(2);
6969
if (! candidates.Any())
7070
{
7171
return null;

netmockery/JSONReader.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ public static Endpoint ReadEndpoint(string jsonString, string rootDir)
1818

1919
public class JSONTest
2020
{
21-
/*
22-
* Typer tester vi boer stoette:
23-
* - expectedendpointname
24-
* - expectedrequestmatcher
25-
*/
2621
public string name;
2722
public string requestpath;
23+
public string querystring;
2824
public string requestbody;
2925
public string expectedresponsebody;
3026
public string expectedrequestmatcher;
@@ -50,6 +46,7 @@ public NetmockeryTestCase CreateTestCase(string directory)
5046
return new NetmockeryTestCase {
5147
Name = name,
5248
RequestPath = requestpath,
49+
QueryString = querystring,
5350
RequestBody =
5451
requestbody != null && requestbody.StartsWith("file:")
5552
?

netmockery/NetmockeryTestCase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class NetmockeryTestCase
7272
{
7373
public string Name;
7474
public string RequestPath;
75+
public string QueryString;
7576
public string RequestBody;
7677

7778
public string ExpectedRequestMatcher;
@@ -140,7 +141,7 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
140141
else
141142
{
142143
bool singleMatch;
143-
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), RequestBody, null, out singleMatch);
144+
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody, null, out singleMatch);
144145
if (matcher_and_creator != null)
145146
{
146147
var responseCreator = matcher_and_creator.Item2;

netmockery/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public static void Match(string[] args)
187187
}
188188
WriteLine($"Endpoint: {endpoint.Name}");
189189
bool singleMatch;
190-
var responseMatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(path), body, null, out singleMatch);
190+
var responseMatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(path), new Microsoft.AspNetCore.Http.QueryString(), body, null, out singleMatch);
191191
if (responseMatch == null)
192192
{
193193
WriteLine("No match");

netmockery/RequestMatcher.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace netmockery
1616
public abstract class RequestMatcher
1717
{
1818
public int Index = -1;
19-
public abstract bool Matches(PathString path, string body, IHeaderDictionary headers);
19+
public abstract bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers);
2020
}
2121

2222
public class AnyMatcher : RequestMatcher
2323
{
24-
public override bool Matches(PathString path, string body, IHeaderDictionary headers)
24+
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
2525
{
2626
return true;
2727
}
@@ -43,9 +43,9 @@ public RegexMatcher(string regex)
4343

4444
public string Expression => _regex;
4545

46-
public override bool Matches(PathString path, string body, IHeaderDictionary headers)
46+
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
4747
{
48-
return Regex.IsMatch(path.ToString(), _regex) || Regex.IsMatch(body, _regex);
48+
return Regex.IsMatch(path.ToString(), _regex) || Regex.IsMatch(queryString.ToString(), _regex) || Regex.IsMatch(body, _regex);
4949
}
5050

5151
public override string ToString()
@@ -75,7 +75,7 @@ public void AddNamespace(string prefix, string ns)
7575
_namespaces.Add(ns);
7676
}
7777

78-
public override bool Matches(PathString path, string body, IHeaderDictionary headers)
78+
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
7979
{
8080
var reader = XmlReader.Create(new StringReader(body));
8181
var root = XElement.Load(reader);

netmockery/ResponseRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ResponseRegistryItem
1515
public RequestMatcher RequestMatcher;
1616
public ResponseCreator ResponseCreator;
1717
public string RequestPath;
18+
public string QueryString;
1819
public string RequestBody;
1920
public string ResponseBody;
2021
public string Error;

0 commit comments

Comments
 (0)