diff --git a/UnitTests/TestInitFromJSON.cs b/UnitTests/TestInitFromJSON.cs index eaf01f4..f646c52 100644 --- a/UnitTests/TestInitFromJSON.cs +++ b/UnitTests/TestInitFromJSON.cs @@ -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'} + ] } ] } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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)); diff --git a/UnitTests/TestTestCommand.cs b/UnitTests/TestTestCommand.cs index b863da2..6b467bc 100644 --- a/UnitTests/TestTestCommand.cs +++ b/UnitTests/TestTestCommand.cs @@ -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'} + ] } ] } @@ -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\")"); } diff --git a/UnitTests/examples/example1/endpoint1/endpoint.json b/UnitTests/examples/example1/endpoint1/endpoint.json index 46a6914..4a2be50 100644 --- a/UnitTests/examples/example1/endpoint1/endpoint.json +++ b/UnitTests/examples/example1/endpoint1/endpoint.json @@ -5,10 +5,9 @@ "responses": [ { "match": {}, - "response": { - "literal": "Hello world", - "contenttype": "text/plain" - } + + "literal": "Hello world", + "contenttype": "text/plain" } ] diff --git a/netmockery/JSONReader.cs b/netmockery/JSONReader.cs index e7d514d..d912a41 100644 --- a/netmockery/JSONReader.cs +++ b/netmockery/JSONReader.cs @@ -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; @@ -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; @@ -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; @@ -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 @@ -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;