Skip to content

Commit

Permalink
Implemented optional request/response log disable
Browse files Browse the repository at this point in the history
  • Loading branch information
codeape2 committed Aug 9, 2017
1 parent 5d91ff2 commit d217036
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
8 changes: 8 additions & 0 deletions UnitTests/TestInitFromJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public void SimpleEndpointAttributes()
var endpoint = JSONReader.ReadEndpoint(ENDPOINTJSON, "p:\\ath\\to\\endpoint\\directory", globalDefaults: null);
Assert.Equal("foo", endpoint.Name);
Assert.Equal("^/foo/$", endpoint.PathRegex);
Assert.True(endpoint.RecordRequests);
}

[Fact]
public void DoNotRecordRequestsAttribute()
{
var endpoint = JSONReader.ReadEndpoint("{'name': 'foo', 'pathregex': 'bar', 'responses': [], 'record': false}", "p:\\ath\\to\\endpoint\\directory", globalDefaults: null);
Assert.False(endpoint.RecordRequests);
}

private Endpoint endpoint;
Expand Down
3 changes: 2 additions & 1 deletion UnitTests/TestJSONAdditionalData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void AdditionalDataIsNotValid()
Assert.Equal("nmae", endpoint.AdditionalData.Keys.Single());

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


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

var as_str = JsonConvert.SerializeObject(endpoint);
Assert.Equal("{\"name\":\"foobar\",\"pathregex\":null,\"responses\":null}", as_str);
Assert.Equal("{\"name\":\"foobar\",\"pathregex\":null,\"record\":true,\"responses\":null}", as_str);
}
}
}
1 change: 1 addition & 0 deletions netmockery/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Endpoint(string name, string pathregex)
//public IEnumerable<EndpointParameter> Parameters => _parameters;

public string Directory { get; set; }
public bool RecordRequests { get; set; }

public string Name => _name;
public string PathRegex => _pathregex;
Expand Down
4 changes: 3 additions & 1 deletion netmockery/JSONReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public class JSONEndpoint : JSONObjectWithAdditionalData
{
public string name;
public string pathregex;
public bool record = true;
public JSONResponse[] responses;


Expand All @@ -300,7 +301,8 @@ public Endpoint CreateEndpoint(string rootDir, JSONDefaults globalDefaults)
ThrowExceptionIfAdditionalData();
var endpoint = new Endpoint(name, pathregex)
{
Directory = rootDir
Directory = rootDir,
RecordRequests = record
};

var endpointDefaultsFile = Path.Combine(rootDir, "defaults.json");
Expand Down
10 changes: 10 additions & 0 deletions netmockery/ResponseRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ public class ResponseRegistryItem
public string Error;
public bool SingleMatch;

public bool HasBeenAddedToRegistry => Id != 0;

public void WriteIncomingInfoToConsole()
{
Debug.Assert(HasBeenAddedToRegistry);
Console.WriteLine($"[{Id}] {Timestamp.ToString("HH:mm:ss.fff")} {Method} {RequestPath}");
}

public void WriteResolvedInfoToConsole()
{
Debug.Assert(HasBeenAddedToRegistry);
if (Endpoint != null)
{
Console.WriteLine($"[{Id}] Endpoint: {Endpoint.Name}");
Expand Down Expand Up @@ -88,5 +92,11 @@ public ResponseRegistryItem Add(ResponseRegistryItem responseRegistryItem)
}
return responseRegistryItem;
}

public void AddAndWriteIncomingInfoToConsole(ResponseRegistryItem responseRegistryItem)
{
Add(responseRegistryItem);
responseRegistryItem.WriteIncomingInfoToConsole();
}
}
}
22 changes: 17 additions & 5 deletions netmockery/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public void ConfigureServices(IServiceCollection services)
public async Task HandleRequest(HttpContext context, string requestBody, byte[] requestBodyBytes)
{
Debug.Assert(context != null);
var responseRegistryItem = _responseRegistry.Add(new ResponseRegistryItem
var responseRegistryItem = new ResponseRegistryItem
{
Timestamp = DateTime.Now,
RequestBody = requestBody,
Method = context.Request.Method,
RequestPath = context.Request.Path.ToString(),
QueryString = context.Request.QueryString.ToString()
});
responseRegistryItem.WriteIncomingInfoToConsole();
};
Debug.Assert(responseRegistryItem.Id == 0);

try
{
Expand All @@ -64,25 +64,35 @@ public async Task HandleRequest(HttpContext context, string requestBody, byte[]
{
Debug.WriteLine(e);
responseRegistryItem.Error = e.ToString();
if (! responseRegistryItem.HasBeenAddedToRegistry)
{
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
}
}
finally
{
responseRegistryItem.WriteResolvedInfoToConsole();
if (responseRegistryItem.HasBeenAddedToRegistry)
{
responseRegistryItem.WriteResolvedInfoToConsole();
}
}

}

public async Task HandleRequestInner(ResponseRegistryItem responseRegistryItem, HttpContext context, string requestBody, byte[] requestBodyBytes)
{
Debug.Assert(_endpointCollectionProvider != null);
Debug.Assert(responseRegistryItem != null);
var endpointCollection = _endpointCollectionProvider.EndpointCollection;
var endpoint = endpointCollection.Resolve(context.Request.Path.ToString());
responseRegistryItem.Endpoint = endpoint;
if (endpoint != null)
{
{
var matcher_and_creator = endpoint.Resolve(context.Request.Method, context.Request.Path, context.Request.QueryString, requestBody, context.Request.Headers);
if (matcher_and_creator != null)
{
//TODO: Only if configured
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
var responseCreator = matcher_and_creator.ResponseCreator;

responseRegistryItem.RequestMatcher = matcher_and_creator.RequestMatcher;
Expand All @@ -106,11 +116,13 @@ public async Task HandleRequestInner(ResponseRegistryItem responseRegistryItem,
}
else
{
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
responseRegistryItem.Error = "Endpoint has no match for request";
}
}
else
{
_responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem);
responseRegistryItem.Error = "No endpoint matches request path";
}
}
Expand Down
1 change: 1 addition & 0 deletions netmockery/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Example directory structure:
* ``name``: The endpoint's name. The name is for display in the web UI only.
* ``pathregex``: A request path reqular expression, used in the first step of the incoming request handling.
* ``responses``: A list of request matching rules and response creation steps for the endpoint.
* TODO: Document ``record`` property

Example ``endpoint.json``:

Expand Down

0 comments on commit d217036

Please sign in to comment.