Skip to content

Commit d217036

Browse files
committed
Implemented optional request/response log disable
1 parent 5d91ff2 commit d217036

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

UnitTests/TestInitFromJSON.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public void SimpleEndpointAttributes()
4444
var endpoint = JSONReader.ReadEndpoint(ENDPOINTJSON, "p:\\ath\\to\\endpoint\\directory", globalDefaults: null);
4545
Assert.Equal("foo", endpoint.Name);
4646
Assert.Equal("^/foo/$", endpoint.PathRegex);
47+
Assert.True(endpoint.RecordRequests);
48+
}
49+
50+
[Fact]
51+
public void DoNotRecordRequestsAttribute()
52+
{
53+
var endpoint = JSONReader.ReadEndpoint("{'name': 'foo', 'pathregex': 'bar', 'responses': [], 'record': false}", "p:\\ath\\to\\endpoint\\directory", globalDefaults: null);
54+
Assert.False(endpoint.RecordRequests);
4755
}
4856

4957
private Endpoint endpoint;

UnitTests/TestJSONAdditionalData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void AdditionalDataIsNotValid()
3939
Assert.Equal("nmae", endpoint.AdditionalData.Keys.Single());
4040

4141
var exception = Assert.Throws<ArgumentException>(() => endpoint.ThrowExceptionIfAdditionalData());
42+
Assert.Equal("Unknown JSON attributes: 'nmae'", exception.Message);
4243
}
4344

4445

@@ -49,7 +50,7 @@ public void NullAdditionalDataIsNotSerialized()
4950
Assert.Null(endpoint.AdditionalData);
5051

5152
var as_str = JsonConvert.SerializeObject(endpoint);
52-
Assert.Equal("{\"name\":\"foobar\",\"pathregex\":null,\"responses\":null}", as_str);
53+
Assert.Equal("{\"name\":\"foobar\",\"pathregex\":null,\"record\":true,\"responses\":null}", as_str);
5354
}
5455
}
5556
}

netmockery/Endpoint.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public Endpoint(string name, string pathregex)
4343
//public IEnumerable<EndpointParameter> Parameters => _parameters;
4444

4545
public string Directory { get; set; }
46+
public bool RecordRequests { get; set; }
4647

4748
public string Name => _name;
4849
public string PathRegex => _pathregex;

netmockery/JSONReader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public class JSONEndpoint : JSONObjectWithAdditionalData
292292
{
293293
public string name;
294294
public string pathregex;
295+
public bool record = true;
295296
public JSONResponse[] responses;
296297

297298

@@ -300,7 +301,8 @@ public Endpoint CreateEndpoint(string rootDir, JSONDefaults globalDefaults)
300301
ThrowExceptionIfAdditionalData();
301302
var endpoint = new Endpoint(name, pathregex)
302303
{
303-
Directory = rootDir
304+
Directory = rootDir,
305+
RecordRequests = record
304306
};
305307

306308
var endpointDefaultsFile = Path.Combine(rootDir, "defaults.json");

netmockery/ResponseRegistry.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public class ResponseRegistryItem
2222
public string Error;
2323
public bool SingleMatch;
2424

25+
public bool HasBeenAddedToRegistry => Id != 0;
26+
2527
public void WriteIncomingInfoToConsole()
2628
{
29+
Debug.Assert(HasBeenAddedToRegistry);
2730
Console.WriteLine($"[{Id}] {Timestamp.ToString("HH:mm:ss.fff")} {Method} {RequestPath}");
2831
}
2932

3033
public void WriteResolvedInfoToConsole()
3134
{
35+
Debug.Assert(HasBeenAddedToRegistry);
3236
if (Endpoint != null)
3337
{
3438
Console.WriteLine($"[{Id}] Endpoint: {Endpoint.Name}");
@@ -88,5 +92,11 @@ public ResponseRegistryItem Add(ResponseRegistryItem responseRegistryItem)
8892
}
8993
return responseRegistryItem;
9094
}
95+
96+
public void AddAndWriteIncomingInfoToConsole(ResponseRegistryItem responseRegistryItem)
97+
{
98+
Add(responseRegistryItem);
99+
responseRegistryItem.WriteIncomingInfoToConsole();
100+
}
91101
}
92102
}

netmockery/Startup.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public void ConfigureServices(IServiceCollection services)
4646
public async Task HandleRequest(HttpContext context, string requestBody, byte[] requestBodyBytes)
4747
{
4848
Debug.Assert(context != null);
49-
var responseRegistryItem = _responseRegistry.Add(new ResponseRegistryItem
49+
var responseRegistryItem = new ResponseRegistryItem
5050
{
5151
Timestamp = DateTime.Now,
5252
RequestBody = requestBody,
5353
Method = context.Request.Method,
5454
RequestPath = context.Request.Path.ToString(),
5555
QueryString = context.Request.QueryString.ToString()
56-
});
57-
responseRegistryItem.WriteIncomingInfoToConsole();
56+
};
57+
Debug.Assert(responseRegistryItem.Id == 0);
5858

5959
try
6060
{
@@ -64,25 +64,35 @@ public async Task HandleRequest(HttpContext context, string requestBody, byte[]
6464
{
6565
Debug.WriteLine(e);
6666
responseRegistryItem.Error = e.ToString();
67+
if (! responseRegistryItem.HasBeenAddedToRegistry)
68+
{
69+
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
70+
}
6771
}
6872
finally
6973
{
70-
responseRegistryItem.WriteResolvedInfoToConsole();
74+
if (responseRegistryItem.HasBeenAddedToRegistry)
75+
{
76+
responseRegistryItem.WriteResolvedInfoToConsole();
77+
}
7178
}
7279

7380
}
7481

7582
public async Task HandleRequestInner(ResponseRegistryItem responseRegistryItem, HttpContext context, string requestBody, byte[] requestBodyBytes)
7683
{
7784
Debug.Assert(_endpointCollectionProvider != null);
85+
Debug.Assert(responseRegistryItem != null);
7886
var endpointCollection = _endpointCollectionProvider.EndpointCollection;
7987
var endpoint = endpointCollection.Resolve(context.Request.Path.ToString());
8088
responseRegistryItem.Endpoint = endpoint;
8189
if (endpoint != null)
82-
{
90+
{
8391
var matcher_and_creator = endpoint.Resolve(context.Request.Method, context.Request.Path, context.Request.QueryString, requestBody, context.Request.Headers);
8492
if (matcher_and_creator != null)
8593
{
94+
//TODO: Only if configured
95+
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
8696
var responseCreator = matcher_and_creator.ResponseCreator;
8797

8898
responseRegistryItem.RequestMatcher = matcher_and_creator.RequestMatcher;
@@ -106,11 +116,13 @@ public async Task HandleRequestInner(ResponseRegistryItem responseRegistryItem,
106116
}
107117
else
108118
{
119+
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
109120
responseRegistryItem.Error = "Endpoint has no match for request";
110121
}
111122
}
112123
else
113124
{
125+
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
114126
responseRegistryItem.Error = "No endpoint matches request path";
115127
}
116128
}

netmockery/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Example directory structure:
5959
* ``name``: The endpoint's name. The name is for display in the web UI only.
6060
* ``pathregex``: A request path reqular expression, used in the first step of the incoming request handling.
6161
* ``responses``: A list of request matching rules and response creation steps for the endpoint.
62+
* TODO: Document ``record`` property
6263

6364
Example ``endpoint.json``:
6465

0 commit comments

Comments
 (0)