Skip to content

Commit

Permalink
Now with PROPER querystring support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeape2 committed Nov 10, 2016
1 parent 0a6b7c9 commit ed1b942
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions UnitTests/TestEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void ResolveMoreThanOneMatcher()
endpoint.Add(new AnyMatcher(), new LiteralResponse("foobar"));

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

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

Expand Down
15 changes: 11 additions & 4 deletions UnitTests/TestRegExMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ public class TestRegExMatcher
public void CanMatchBody()
{
var matcher = new RegexMatcher("<ssn>13116900216</ssn>");
Assert.True(matcher.Matches(null, BODY, null));
Assert.True(matcher.Matches(null, new QueryString(), BODY, null));
}

[Fact]
public void NoMatch()
{
var matcher = new RegexMatcher("<ssn>13116900217</ssn>");
Assert.False(matcher.Matches(null, BODY, null));
Assert.False(matcher.Matches(null, new QueryString(), BODY, null));
}

[Fact]
public void MatchesBothUrlAndBody()
{
var matcher = new RegexMatcher("abcde");
Assert.True(matcher.Matches(new PathString("/foo/bar/ae/fx/"), "content in body: abcde", null));
Assert.True(matcher.Matches(new PathString("/foo/bar/abcde/fx/"), "", null));
Assert.True(matcher.Matches(new PathString("/foo/bar/ae/fx/"), new QueryString(), "content in body: abcde", null));
Assert.True(matcher.Matches(new PathString("/foo/bar/abcde/fx/"), new QueryString(), "", null));
}

[Fact]
public void MatchesQueryString()
{
var matcher = new RegexMatcher("abcde");
Assert.True(matcher.Matches(new PathString("/foo/bar/"), new QueryString("?value=abcde"), "", null));
}

}
Expand Down
22 changes: 20 additions & 2 deletions UnitTests/TestTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async public void CanCheckExpectedRequestMatcherSuccess()
}

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

[Fact]
async public void CanCheckExpectedResponseCreatorFailureSuccess()
async public void CanCheckExpectedResponseCreatorSuccess()
{
var testcase =
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedresponsecreator = "File content.txt" })
Expand All @@ -210,5 +210,23 @@ async public void CanCheckExpectedResponseCreatorFailureSuccess()
Assert.True(result.OK, result.Message);
Assert.Null(result.Message);
}

[Fact]
public void CanReadQueryString()
{
var testcase = (new JSONTest { querystring = "?foo=bar" }).CreateTestCase(".");
Assert.Equal("?foo=bar", testcase.QueryString);
}

[Fact]
async public void CanExecuteWithQueryStringFailure()
{
var testcase =
(new JSONTest { name = "checksomething", requestpath = "/foo/", querystring = "?a=test", requestbody = "foobar", expectedresponsecreator = "File content.txt" })
.Validated().CreateTestCase(".");
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
Assert.True(result.OK, result.Message);
Assert.Null(result.Message);
}
}
}
4 changes: 2 additions & 2 deletions UnitTests/TestXPathMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void CanMatchBody()
{
var matcher = new XPathMatcher("//nhn:ssn = '13116900216'");
matcher.AddNamespace("nhn", "http://register.nhn.no/Orchestration");
Assert.True(matcher.Matches(null, BODY, null));
Assert.True(matcher.Matches(null, new Microsoft.AspNetCore.Http.QueryString(), BODY, null));
}

[Fact]
public void NoMatch()
{
var matcher = new XPathMatcher("//nhn:ssn = '13116900217'");
matcher.AddNamespace("nhn", "http://register.nhn.no/Orchestration");
Assert.False(matcher.Matches(null, BODY, null));
Assert.False(matcher.Matches(null, new Microsoft.AspNetCore.Http.QueryString(), BODY, null));
}
}
}
4 changes: 2 additions & 2 deletions netmockery/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public void Add(RequestMatcher requestMatcher, ResponseCreator responseCreator)
}
}

public Tuple<RequestMatcher, ResponseCreator> Resolve(PathString path, string body, IHeaderDictionary headers, out bool singleMatch)
public Tuple<RequestMatcher, ResponseCreator> Resolve(PathString path, QueryString queryString, string body, IHeaderDictionary headers, out bool singleMatch)
{
singleMatch = true;
var candidates = (from t in _responses where t.Item1.Matches(path, body, headers) select t).Take(2);
var candidates = (from t in _responses where t.Item1.Matches(path, queryString, body, headers) select t).Take(2);
if (! candidates.Any())
{
return null;
Expand Down
7 changes: 2 additions & 5 deletions netmockery/JSONReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ public static Endpoint ReadEndpoint(string jsonString, string rootDir)

public class JSONTest
{
/*
* Typer tester vi boer stoette:
* - expectedendpointname
* - expectedrequestmatcher
*/
public string name;
public string requestpath;
public string querystring;
public string requestbody;
public string expectedresponsebody;
public string expectedrequestmatcher;
Expand All @@ -50,6 +46,7 @@ public NetmockeryTestCase CreateTestCase(string directory)
return new NetmockeryTestCase {
Name = name,
RequestPath = requestpath,
QueryString = querystring,
RequestBody =
requestbody != null && requestbody.StartsWith("file:")
?
Expand Down
3 changes: 2 additions & 1 deletion netmockery/NetmockeryTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class NetmockeryTestCase
{
public string Name;
public string RequestPath;
public string QueryString;
public string RequestBody;

public string ExpectedRequestMatcher;
Expand Down Expand Up @@ -140,7 +141,7 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
else
{
bool singleMatch;
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), RequestBody, null, out singleMatch);
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody, null, out singleMatch);
if (matcher_and_creator != null)
{
var responseCreator = matcher_and_creator.Item2;
Expand Down
2 changes: 1 addition & 1 deletion netmockery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static void Match(string[] args)
}
WriteLine($"Endpoint: {endpoint.Name}");
bool singleMatch;
var responseMatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(path), body, null, out singleMatch);
var responseMatch = endpoint.Resolve(new Microsoft.AspNetCore.Http.PathString(path), new Microsoft.AspNetCore.Http.QueryString(), body, null, out singleMatch);
if (responseMatch == null)
{
WriteLine("No match");
Expand Down
10 changes: 5 additions & 5 deletions netmockery/RequestMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace netmockery
public abstract class RequestMatcher
{
public int Index = -1;
public abstract bool Matches(PathString path, string body, IHeaderDictionary headers);
public abstract bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers);
}

public class AnyMatcher : RequestMatcher
{
public override bool Matches(PathString path, string body, IHeaderDictionary headers)
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
{
return true;
}
Expand All @@ -43,9 +43,9 @@ public RegexMatcher(string regex)

public string Expression => _regex;

public override bool Matches(PathString path, string body, IHeaderDictionary headers)
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
{
return Regex.IsMatch(path.ToString(), _regex) || Regex.IsMatch(body, _regex);
return Regex.IsMatch(path.ToString(), _regex) || Regex.IsMatch(queryString.ToString(), _regex) || Regex.IsMatch(body, _regex);
}

public override string ToString()
Expand Down Expand Up @@ -75,7 +75,7 @@ public void AddNamespace(string prefix, string ns)
_namespaces.Add(ns);
}

public override bool Matches(PathString path, string body, IHeaderDictionary headers)
public override bool Matches(PathString path, QueryString queryString, string body, IHeaderDictionary headers)
{
var reader = XmlReader.Create(new StringReader(body));
var root = XElement.Load(reader);
Expand Down
1 change: 1 addition & 0 deletions netmockery/ResponseRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ResponseRegistryItem
public RequestMatcher RequestMatcher;
public ResponseCreator ResponseCreator;
public string RequestPath;
public string QueryString;
public string RequestBody;
public string ResponseBody;
public string Error;
Expand Down
5 changes: 3 additions & 2 deletions netmockery/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task HandleRequest(ResponseRegistryItem responseRegistryItem, HttpC
if (endpoint != null)
{
bool singleMatch;
var matcher_and_creator = endpoint.Resolve(context.Request.Path, requestBody, context.Request.Headers, out singleMatch);
var matcher_and_creator = endpoint.Resolve(context.Request.Path, context.Request.QueryString, requestBody, context.Request.Headers, out singleMatch);
if (matcher_and_creator != null)
{
var responseCreator = matcher_and_creator.Item2;
Expand Down Expand Up @@ -94,7 +94,8 @@ public void Configure(IApplicationBuilder app)
{
Timestamp = DateTime.Now,
RequestBody = requestBody,
RequestPath = context.Request.Path.ToString()
RequestPath = context.Request.Path.ToString(),
QueryString = context.Request.QueryString.ToString()
};
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<thead>
<tr>
<th>Timestamp</th>
<th>Request Path</th>
<th>Request</th>
<th>Endpoint</th>
<th>Request Matcher</th>
<th>Response Creator</th>
Expand All @@ -20,7 +20,7 @@
{
<tr>
<td>@item.Timestamp.ToString("HH:mm:ss.fff")</td>
<td>@item.RequestPath</td>
<td>@(item.RequestPath)@(item.QueryString)</td>
<td>
@if (item.Endpoint != null)
{
Expand Down

0 comments on commit ed1b942

Please sign in to comment.