Skip to content

Commit e6d4ca8

Browse files
Add tests.
1 parent 9d38e0e commit e6d4ca8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Servers/Kestrel/test/FunctionalTests/RequestTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,46 @@ await connection.Receive(
399399
Assert.False(loggedHigherThanDebug);
400400
}
401401

402+
[Fact]
403+
public async Task IncompleteRequestBodyDoesNotLogAsApplicationError()
404+
{
405+
var logMessageReceived = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
406+
407+
// Listen for the expected log message
408+
TestSink.MessageLogged += context =>
409+
{
410+
if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel"
411+
&& context.EventId.Id == 13
412+
&& context.LogLevel > LogLevel.Debug)
413+
{
414+
logMessageReceived.SetResult();
415+
}
416+
};
417+
418+
await using var server = new TestServer(async context =>
419+
{
420+
var buffer = new byte[1024];
421+
422+
// Attempt to read more of the body than will show up.
423+
await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
424+
}, new TestServiceContext(LoggerFactory));
425+
426+
using (var connection = server.CreateConnection())
427+
{
428+
await connection.Send(
429+
"POST / HTTP/1.1",
430+
"Host:",
431+
"Connection: keep-alive",
432+
"Content-Type: application/json",
433+
"Content-Length: 100", // Declare a larger body than will be sent
434+
"",
435+
"");
436+
}
437+
438+
// Log message should not have appeared.
439+
await Assert.ThrowsAsync<TimeoutException>(() => logMessageReceived.Task.TimeoutAfter(TimeSpan.FromSeconds(1)));
440+
}
441+
402442
[Fact]
403443
public async Task ConnectionResetBetweenRequestsIsLoggedAsDebug()
404444
{

src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ await connection.ReceiveEnd(
740740
"",
741741
"");
742742
}
743+
744+
// The 400 response should have been logged by default at Error level since the connection wasn't aborted when the bad request was handled.
745+
Assert.Contains(TestSink.Writes, w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel" && w.EventId.Id == 13 && w.LogLevel > LogLevel.Debug);
743746
}
744747
}
745748

0 commit comments

Comments
 (0)