Skip to content

Commit 5ceeed8

Browse files
committed
Refactored JSON endpoint definition format
1 parent 0c2b42d commit 5ceeed8

File tree

4 files changed

+71
-83
lines changed

4 files changed

+71
-83
lines changed

UnitTests/TestInitFromJSON.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ public class TestInitFromJSON
1717
'responses': [
1818
{
1919
'match': {'regex': 'test'},
20-
'response': {
21-
'file': 'content.txt',
22-
'contenttype': 'text/plain',
23-
'delay': 10
24-
}
20+
'file': 'content.txt',
21+
'contenttype': 'text/plain',
22+
'delay': 10
2523
},
2624
{
2725
'match': {},
28-
'response': {
29-
'script': 'myscript.csscript',
30-
'contenttype': 'text/xml',
31-
'replacements': [
32-
{'search': 'a', 'replace': 'b'},
33-
{'search': 'foo', 'replace': 'bar'}
34-
]
35-
}
26+
'script': 'myscript.csscript',
27+
'contenttype': 'text/xml',
28+
'replacements': [
29+
{'search': 'a', 'replace': 'b'},
30+
{'search': 'foo', 'replace': 'bar'}
31+
]
3632
}
3733
]
3834
}
@@ -86,7 +82,7 @@ public void Responses()
8682
[Fact]
8783
public void DeserializeXPathRequestMatcher()
8884
{
89-
var response = ParseResponse("{'match': {'xpath': 'foo', 'namespaces': [{'prefix': 'foo', 'ns': 'urn:foo'}]}, 'response': {'file': 'foo.txt'}}");
85+
var response = ParseResponse("{'match': {'xpath': 'foo', 'namespaces': [{'prefix': 'foo', 'ns': 'urn:foo'}]}, 'file': 'foo.txt'}");
9086
var xpathMatcher = response.Item1 as XPathMatcher;
9187
Assert.NotNull(xpathMatcher);
9288
Assert.Equal("foo", xpathMatcher.XPathExpresssion);
@@ -97,7 +93,7 @@ public void DeserializeXPathRequestMatcher()
9793
[Fact]
9894
public void DeserializeRegExRequestMatcher()
9995
{
100-
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'response': {'file': 'foo.txt'}}");
96+
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'file': 'foo.txt'}");
10197
var regexMatcher = response.Item1 as RegexMatcher;
10298
Assert.NotNull(regexMatcher);
10399
Assert.Equal("foobar", regexMatcher.Expression);
@@ -106,7 +102,7 @@ public void DeserializeRegExRequestMatcher()
106102
[Fact]
107103
public void DeserializeCompiledCreator()
108104
{
109-
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'response': {'assembly': 'foo.dll', 'class': 'Foo', 'method': 'Bar'}}");
105+
var response = ParseResponse("{'match': {'regex': 'foobar'}, 'assembly': 'foo.dll', 'class': 'Foo', 'method': 'Bar'}");
110106
var responseCreator = response.Item2 as AssemblyResponseCreator;
111107
Assert.NotNull(responseCreator);
112108
Assert.Equal("r:\\oot\\directory\\foo.dll", responseCreator.AssemblyFilename);
@@ -117,7 +113,7 @@ public void DeserializeCompiledCreator()
117113
[Fact]
118114
public void DeserializeForwardCreator()
119115
{
120-
var response = ParseResponse("{'match': {}, 'response': {'forward': 'http://foo.bar'}}");
116+
var response = ParseResponse("{'match': {}, 'forward': 'http://foo.bar'}");
121117
var responseCreator = response.Item2 as ForwardResponseCreator;
122118
Assert.NotNull(responseCreator);
123119
Assert.Equal("http://foo.bar", responseCreator.Url);
@@ -128,7 +124,7 @@ public void DeserializeForwardCreator()
128124
[Fact]
129125
public void DeserializeForwardCreatorWithProxyAndStripPath()
130126
{
131-
var response = ParseResponse("{'match': {}, 'response': {'forward': 'http://foo.bar', 'proxy': 'http://localhost:1234', 'strippath': 'foo'}}");
127+
var response = ParseResponse("{'match': {}, 'forward': 'http://foo.bar', 'proxy': 'http://localhost:1234', 'strippath': 'foo'}");
132128
var responseCreator = response.Item2 as ForwardResponseCreator;
133129
Assert.NotNull(responseCreator);
134130
Assert.Equal("http://foo.bar", responseCreator.Url);
@@ -139,7 +135,7 @@ public void DeserializeForwardCreatorWithProxyAndStripPath()
139135
[Fact]
140136
public void DeserializeLiteralResponse()
141137
{
142-
var response = ParseResponse("{'match': {}, 'response': {'literal': 'Hello world', 'contenttype': 'text/plain'}}");
138+
var response = ParseResponse("{'match': {}, 'literal': 'Hello world', 'contenttype': 'text/plain'}");
143139
var responseCreator = response.Item2 as LiteralResponse;
144140
Assert.NotNull(responseCreator);
145141
Assert.Equal("Hello world", responseCreator.GetBody(null));

UnitTests/TestTestCommand.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ public static class TESTCOMMAND_CONSTANTS
1616
'responses': [
1717
{
1818
'match': {'regex': 'test'},
19-
'response': {
20-
'file': 'content.txt',
21-
'contenttype': 'text/plain'
22-
}
19+
'file': 'content.txt',
20+
'contenttype': 'text/plain'
2321
},
2422
{
2523
'match': {},
26-
'response': {
27-
'script': 'myscript.csscript',
28-
'contenttype': 'text/xml',
29-
'replacements': [
30-
{'search': 'a', 'replace': 'b'},
31-
{'search': 'foo', 'replace': 'bar'}
32-
]
33-
}
24+
'script': 'myscript.csscript',
25+
'contenttype': 'text/xml',
26+
'replacements': [
27+
{'search': 'a', 'replace': 'b'},
28+
{'search': 'foo', 'replace': 'bar'}
29+
]
3430
}
3531
]
3632
}
@@ -110,7 +106,7 @@ public TestTestCommand()
110106
dc.AddFile("tests\\tests.json", TESTCOMMAND_CONSTANTS.TESTS);
111107
dc.AddFile("tests\\example.txt", "FOOBARBOOBAR");
112108
dc.AddFile("tests\\now.txt", "2015-01-01 12:01:31");
113-
dc.AddFile("getnow\\endpoint.json", "{'name': 'GetNow', 'pathregex': '/getnow/', 'responses': [{'match': {}, 'response': {'script':'getnow.csscript'}}]}");
109+
dc.AddFile("getnow\\endpoint.json", "{'name': 'GetNow', 'pathregex': '/getnow/', 'responses': [{'match': {}, 'script':'getnow.csscript'}]}");
114110
dc.AddFile("getnow\\getnow.csscript", "GetNow().ToString(\"yyyy-MM-dd HH:mm:ss\")");
115111
}
116112

UnitTests/examples/example1/endpoint1/endpoint.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
"responses": [
66
{
77
"match": {},
8-
"response": {
9-
"literal": "Hello world",
10-
"contenttype": "text/plain"
11-
}
8+
9+
"literal": "Hello world",
10+
"contenttype": "text/plain"
1211
}
1312
]
1413

netmockery/JSONReader.cs

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,51 +70,7 @@ public NetmockeryTestCase CreateTestCase(string directory)
7070
public class JSONResponse
7171
{
7272
public JSONRequestMatcher match;
73-
public JSONResponseDefinition response;
74-
}
75-
76-
public class JSONRequestMatcher
77-
{
78-
public string xpath;
79-
public string regex;
80-
public JSONXPathNamespace[] namespaces;
81-
82-
public RequestMatcher CreateRequestMatcher()
83-
{
84-
if (xpath != null)
85-
{
86-
var xpathMatcher = new XPathMatcher(xpath);
87-
if (namespaces != null)
88-
{
89-
foreach (var jsonNs in namespaces)
90-
{
91-
xpathMatcher.AddNamespace(jsonNs.prefix, jsonNs.ns);
92-
}
93-
}
94-
return xpathMatcher;
95-
}
96-
else if (regex != null)
97-
{
98-
return new RegexMatcher(regex);
99-
}
100-
return new AnyMatcher();
101-
}
102-
}
10373

104-
public class JSONXPathNamespace
105-
{
106-
public string prefix;
107-
public string ns;
108-
}
109-
110-
public class JSONReplacement
111-
{
112-
public string search;
113-
public string replace;
114-
}
115-
116-
public class JSONResponseDefinition
117-
{
11874
public string literal;
11975

12076
public string file;
@@ -123,7 +79,7 @@ public class JSONResponseDefinition
12379
public string assembly;
12480
public string @class;
12581
public string method;
126-
82+
12783
public string forward;
12884
public string proxy;
12985
public string strippath;
@@ -132,7 +88,7 @@ public class JSONResponseDefinition
13288
public JSONReplacement[] replacements;
13389
public int delay;
13490

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

200156
return responseCreator;
201157
}
158+
159+
}
160+
161+
public class JSONRequestMatcher
162+
{
163+
public string xpath;
164+
public string regex;
165+
public JSONXPathNamespace[] namespaces;
166+
167+
public RequestMatcher CreateRequestMatcher()
168+
{
169+
if (xpath != null)
170+
{
171+
var xpathMatcher = new XPathMatcher(xpath);
172+
if (namespaces != null)
173+
{
174+
foreach (var jsonNs in namespaces)
175+
{
176+
xpathMatcher.AddNamespace(jsonNs.prefix, jsonNs.ns);
177+
}
178+
}
179+
return xpathMatcher;
180+
}
181+
else if (regex != null)
182+
{
183+
return new RegexMatcher(regex);
184+
}
185+
return new AnyMatcher();
186+
}
187+
}
188+
189+
public class JSONXPathNamespace
190+
{
191+
public string prefix;
192+
public string ns;
193+
}
194+
195+
public class JSONReplacement
196+
{
197+
public string search;
198+
public string replace;
202199
}
203200

204201
public class JSONEndpoint
@@ -213,7 +210,7 @@ public Endpoint CreateEndpoint(string rootDir)
213210
var endpoint = new Endpoint(name, pathregex);
214211
foreach (var jsonResponse in responses)
215212
{
216-
endpoint.Add(jsonResponse.match.CreateRequestMatcher(), jsonResponse.response.Validated().CreateResponseCreator(rootDir));
213+
endpoint.Add(jsonResponse.match.CreateRequestMatcher(), jsonResponse.Validated().CreateResponseCreator(rootDir));
217214
}
218215
endpoint.Directory = rootDir;
219216
return endpoint;

0 commit comments

Comments
 (0)