Skip to content

Commit

Permalink
Merge branch 'fileformat'
Browse files Browse the repository at this point in the history
  • Loading branch information
codeape2 committed Nov 21, 2016
2 parents d3c47cf + 5ceeed8 commit 2afa007
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 83 deletions.
34 changes: 15 additions & 19 deletions UnitTests/TestInitFromJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ public class TestInitFromJSON
'responses': [
{
'match': {'regex': 'test'},
'response': {
'file': 'content.txt',
'contenttype': 'text/plain',
'delay': 10
}
'file': 'content.txt',
'contenttype': 'text/plain',
'delay': 10
},
{
'match': {},
'response': {
'script': 'myscript.csscript',
'contenttype': 'text/xml',
'replacements': [
{'search': 'a', 'replace': 'b'},
{'search': 'foo', 'replace': 'bar'}
]
}
'script': 'myscript.csscript',
'contenttype': 'text/xml',
'replacements': [
{'search': 'a', 'replace': 'b'},
{'search': 'foo', 'replace': 'bar'}
]
}
]
}
Expand Down Expand Up @@ -86,7 +82,7 @@ public void Responses()
[Fact]
public void DeserializeXPathRequestMatcher()
{
var response = ParseResponse("{'match': {'xpath': 'foo', 'namespaces': [{'prefix': 'foo', 'ns': 'urn:foo'}]}, 'response': {'file': 'foo.txt'}}");
var response = ParseResponse("{'match': {'xpath': 'foo', 'namespaces': [{'prefix': 'foo', 'ns': 'urn:foo'}]}, 'file': 'foo.txt'}");
var xpathMatcher = response.Item1 as XPathMatcher;
Assert.NotNull(xpathMatcher);
Assert.Equal("foo", xpathMatcher.XPathExpresssion);
Expand All @@ -97,7 +93,7 @@ public void DeserializeXPathRequestMatcher()
[Fact]
public void DeserializeRegExRequestMatcher()
{
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'response': {'file': 'foo.txt'}}");
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'file': 'foo.txt'}");
var regexMatcher = response.Item1 as RegexMatcher;
Assert.NotNull(regexMatcher);
Assert.Equal("foobar", regexMatcher.Expression);
Expand All @@ -106,7 +102,7 @@ public void DeserializeRegExRequestMatcher()
[Fact]
public void DeserializeCompiledCreator()
{
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'response': {'assembly': 'foo.dll', 'class': 'Foo', 'method': 'Bar'}}");
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'assembly': 'foo.dll', 'class': 'Foo', 'method': 'Bar'}");
var responseCreator = response.Item2 as AssemblyResponseCreator;
Assert.NotNull(responseCreator);
Assert.Equal("r:\\oot\\directory\\foo.dll", responseCreator.AssemblyFilename);
Expand All @@ -117,7 +113,7 @@ public void DeserializeCompiledCreator()
[Fact]
public void DeserializeForwardCreator()
{
var response = ParseResponse("{'match': {}, 'response': {'forward': 'http://foo.bar'}}");
var response = ParseResponse("{'match': {}, 'forward': 'http://foo.bar'}");
var responseCreator = response.Item2 as ForwardResponseCreator;
Assert.NotNull(responseCreator);
Assert.Equal("http://foo.bar", responseCreator.Url);
Expand All @@ -128,7 +124,7 @@ public void DeserializeForwardCreator()
[Fact]
public void DeserializeForwardCreatorWithProxyAndStripPath()
{
var response = ParseResponse("{'match': {}, 'response': {'forward': 'http://foo.bar', 'proxy': 'http://localhost:1234', 'strippath': 'foo'}}");
var response = ParseResponse("{'match': {}, 'forward': 'http://foo.bar', 'proxy': 'http://localhost:1234', 'strippath': 'foo'}");
var responseCreator = response.Item2 as ForwardResponseCreator;
Assert.NotNull(responseCreator);
Assert.Equal("http://foo.bar", responseCreator.Url);
Expand All @@ -139,7 +135,7 @@ public void DeserializeForwardCreatorWithProxyAndStripPath()
[Fact]
public void DeserializeLiteralResponse()
{
var response = ParseResponse("{'match': {}, 'response': {'literal': 'Hello world', 'contenttype': 'text/plain'}}");
var response = ParseResponse("{'match': {}, 'literal': 'Hello world', 'contenttype': 'text/plain'}");
var responseCreator = response.Item2 as LiteralResponse;
Assert.NotNull(responseCreator);
Assert.Equal("Hello world", responseCreator.GetBody(null));
Expand Down
22 changes: 9 additions & 13 deletions UnitTests/TestTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ public static class TESTCOMMAND_CONSTANTS
'responses': [
{
'match': {'regex': 'test'},
'response': {
'file': 'content.txt',
'contenttype': 'text/plain'
}
'file': 'content.txt',
'contenttype': 'text/plain'
},
{
'match': {},
'response': {
'script': 'myscript.csscript',
'contenttype': 'text/xml',
'replacements': [
{'search': 'a', 'replace': 'b'},
{'search': 'foo', 'replace': 'bar'}
]
}
'script': 'myscript.csscript',
'contenttype': 'text/xml',
'replacements': [
{'search': 'a', 'replace': 'b'},
{'search': 'foo', 'replace': 'bar'}
]
}
]
}
Expand Down Expand Up @@ -110,7 +106,7 @@ public TestTestCommand()
dc.AddFile("tests\\tests.json", TESTCOMMAND_CONSTANTS.TESTS);
dc.AddFile("tests\\example.txt", "FOOBARBOOBAR");
dc.AddFile("tests\\now.txt", "2015-01-01 12:01:31");
dc.AddFile("getnow\\endpoint.json", "{'name': 'GetNow', 'pathregex': '/getnow/', 'responses': [{'match': {}, 'response': {'script':'getnow.csscript'}}]}");
dc.AddFile("getnow\\endpoint.json", "{'name': 'GetNow', 'pathregex': '/getnow/', 'responses': [{'match': {}, 'script':'getnow.csscript'}]}");
dc.AddFile("getnow\\getnow.csscript", "GetNow().ToString(\"yyyy-MM-dd HH:mm:ss\")");
}

Expand Down
7 changes: 3 additions & 4 deletions UnitTests/examples/example1/endpoint1/endpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
"responses": [
{
"match": {},
"response": {
"literal": "Hello world",
"contenttype": "text/plain"
}

"literal": "Hello world",
"contenttype": "text/plain"
}
]

Expand Down
91 changes: 44 additions & 47 deletions netmockery/JSONReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,51 +70,7 @@ public NetmockeryTestCase CreateTestCase(string directory)
public class JSONResponse
{
public JSONRequestMatcher match;
public JSONResponseDefinition response;
}

public class JSONRequestMatcher
{
public string xpath;
public string regex;
public JSONXPathNamespace[] namespaces;

public RequestMatcher CreateRequestMatcher()
{
if (xpath != null)
{
var xpathMatcher = new XPathMatcher(xpath);
if (namespaces != null)
{
foreach (var jsonNs in namespaces)
{
xpathMatcher.AddNamespace(jsonNs.prefix, jsonNs.ns);
}
}
return xpathMatcher;
}
else if (regex != null)
{
return new RegexMatcher(regex);
}
return new AnyMatcher();
}
}

public class JSONXPathNamespace
{
public string prefix;
public string ns;
}

public class JSONReplacement
{
public string search;
public string replace;
}

public class JSONResponseDefinition
{
public string literal;

public string file;
Expand All @@ -123,7 +79,7 @@ public class JSONResponseDefinition
public string assembly;
public string @class;
public string method;

public string forward;
public string proxy;
public string strippath;
Expand All @@ -132,7 +88,7 @@ public class JSONResponseDefinition
public JSONReplacement[] replacements;
public int delay;

public JSONResponseDefinition Validated()
public JSONResponse Validated()
{
var mutuallyExclusive = new[] { literal, file, script, assembly, forward };
var mutExWithValues = from value in mutuallyExclusive where value != null select value;
Expand Down Expand Up @@ -199,6 +155,47 @@ from jsonreplacement in replacements

return responseCreator;
}

}

public class JSONRequestMatcher
{
public string xpath;
public string regex;
public JSONXPathNamespace[] namespaces;

public RequestMatcher CreateRequestMatcher()
{
if (xpath != null)
{
var xpathMatcher = new XPathMatcher(xpath);
if (namespaces != null)
{
foreach (var jsonNs in namespaces)
{
xpathMatcher.AddNamespace(jsonNs.prefix, jsonNs.ns);
}
}
return xpathMatcher;
}
else if (regex != null)
{
return new RegexMatcher(regex);
}
return new AnyMatcher();
}
}

public class JSONXPathNamespace
{
public string prefix;
public string ns;
}

public class JSONReplacement
{
public string search;
public string replace;
}

public class JSONEndpoint
Expand All @@ -213,7 +210,7 @@ public Endpoint CreateEndpoint(string rootDir)
var endpoint = new Endpoint(name, pathregex);
foreach (var jsonResponse in responses)
{
endpoint.Add(jsonResponse.match.CreateRequestMatcher(), jsonResponse.response.Validated().CreateResponseCreator(rootDir));
endpoint.Add(jsonResponse.match.CreateRequestMatcher(), jsonResponse.Validated().CreateResponseCreator(rootDir));
}
endpoint.Directory = rootDir;
return endpoint;
Expand Down

0 comments on commit 2afa007

Please sign in to comment.