Skip to content

Commit ccec1a3

Browse files
committed
Log HTTP method
1 parent b83e080 commit ccec1a3

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

UnitTests/TestWebUI.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public async Task CheckAllUrlsAfterTestRequestsMade()
7171
Assert.Equal(EXPECTED_NUMBER_OF_URLS + testRunner.Tests.Count() * EXPECTED_EXTRA_URLS_PER_REQUEST, urls.Count);
7272

7373
Assert.Equal(testRunner.Tests.Count(), GetResponseRegistry().Responses.Count());
74+
75+
var response0 = GetResponseRegistry().Responses.First();
76+
Assert.Equal("GET", response0.Method);
7477
}
7578

7679
ResponseRegistry GetResponseRegistry()

netmockery/ResponseRegistry.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,36 @@ public class ResponseRegistryItem
1414
public Endpoint Endpoint;
1515
public RequestMatcher RequestMatcher;
1616
public ResponseCreator ResponseCreator;
17+
public string Method;
1718
public string RequestPath;
1819
public string QueryString;
1920
public string RequestBody;
2021
public string ResponseBody;
2122
public string Error;
2223
public bool SingleMatch;
2324

24-
public void WriteToConsole()
25+
public void WriteIncomingInfoToConsole()
2526
{
26-
Console.WriteLine($"{Timestamp.ToString("HH:mm:ss.fff")} {RequestPath} {Endpoint?.Name} {Error}");
27+
Console.WriteLine($"[{Id}] {Timestamp.ToString("HH:mm:ss.fff")} {Method} {RequestPath}");
28+
}
29+
30+
public void WriteResolvedInfoToConsole()
31+
{
32+
if (Endpoint != null)
33+
{
34+
Console.WriteLine($"[{Id}] Endpoint: {Endpoint.Name}");
35+
}
2736
if (RequestMatcher != null)
2837
{
29-
Console.WriteLine(" " + RequestMatcher.ToString());
38+
Console.WriteLine($"[{Id}] Request matcher: {RequestMatcher}");
3039
}
3140
if (ResponseCreator != null)
3241
{
33-
Console.WriteLine(" " + ResponseCreator.ToString());
42+
Console.WriteLine($"[{Id}] Response creator: {ResponseCreator}");
43+
}
44+
if (Error != null)
45+
{
46+
Console.WriteLine($"[{Id}] Error: {Error}");
3447
}
3548
}
3649
}
@@ -65,7 +78,7 @@ public void Clear()
6578
public IEnumerable<ResponseRegistryItem> Responses => _items.Reverse<ResponseRegistryItem>();
6679

6780
[MethodImpl(MethodImplOptions.Synchronized)]
68-
public void Add(ResponseRegistryItem responseRegistryItem)
81+
public ResponseRegistryItem Add(ResponseRegistryItem responseRegistryItem)
6982
{
7083
Debug.Assert(responseRegistryItem.Id == 0);
7184
responseRegistryItem.Id = ++_nextId;
@@ -74,6 +87,7 @@ public void Add(ResponseRegistryItem responseRegistryItem)
7487
_items.Dequeue();
7588
}
7689
_items.Enqueue(responseRegistryItem);
90+
return responseRegistryItem;
7791
}
7892
}
7993
}

netmockery/Startup.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ public void ConfigureServices(IServiceCollection services)
4545

4646
public async Task HandleRequest(HttpContext context, string requestBody, byte[] requestBodyBytes)
4747
{
48-
var responseRegistryItem = new ResponseRegistryItem
48+
Debug.Assert(context != null);
49+
var responseRegistryItem = _responseRegistry.Add(new ResponseRegistryItem
4950
{
5051
Timestamp = DateTime.Now,
5152
RequestBody = requestBody,
53+
Method = context.Request.Method,
5254
RequestPath = context.Request.Path.ToString(),
5355
QueryString = context.Request.QueryString.ToString()
54-
};
56+
});
57+
responseRegistryItem.WriteIncomingInfoToConsole();
5558

5659
try
5760
{
@@ -64,8 +67,7 @@ public async Task HandleRequest(HttpContext context, string requestBody, byte[]
6467
}
6568
finally
6669
{
67-
responseRegistryItem.WriteToConsole();
68-
_responseRegistry.Add(responseRegistryItem);
70+
responseRegistryItem.WriteResolvedInfoToConsole();
6971
}
7072

7173
}

0 commit comments

Comments
 (0)