This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 527
Call OnStarting before verifying response length (#1289) #1302
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,7 +485,7 @@ await connection.Receive( | |
} | ||
|
||
[Fact] | ||
public async Task WhenAppWritesMoreThanContentLengthWriteThrowsAndConnectionCloses() | ||
public async Task ThrowsAndClosesConnectionWhenAppWritesMoreThanContentLengthWrite() | ||
{ | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; | ||
|
@@ -520,7 +520,7 @@ await connection.ReceiveEnd( | |
} | ||
|
||
[Fact] | ||
public async Task WhenAppWritesMoreThanContentLengthWriteAsyncThrowsAndConnectionCloses() | ||
public async Task ThrowsAndClosesConnectionWhenAppWritesMoreThanContentLengthWriteAsync() | ||
{ | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; | ||
|
@@ -554,15 +554,52 @@ await connection.ReceiveForcedEnd( | |
} | ||
|
||
[Fact] | ||
public async Task WhenAppWritesMoreThanContentLengthAndResponseNotStarted500ResponseSentAndConnectionCloses() | ||
public async Task InternalServerErrorAndConnectionClosedOnWriteWithMoreThanContentLengthAndResponseNotStarted() | ||
{ | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; | ||
|
||
using (var server = new TestServer(async httpContext => | ||
using (var server = new TestServer(httpContext => | ||
{ | ||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = 5; | ||
await httpContext.Response.WriteAsync("hello, world"); | ||
httpContext.Response.Body.Write(response, 0, response.Length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no longer async? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
return TaskCache.CompletedTask; | ||
}, serviceContext)) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.ReceiveForcedEnd( | ||
$"HTTP/1.1 500 Internal Server Error", | ||
"Connection: close", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
"Content-Length: 0", | ||
"", | ||
""); | ||
} | ||
} | ||
|
||
var logMessage = Assert.Single(testLogger.Messages, message => message.LogLevel == LogLevel.Error); | ||
Assert.Equal( | ||
$"Response Content-Length mismatch: too many bytes written (12 of 5).", | ||
logMessage.Exception.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task InternalServerErrorAndConnectionClosedOnWriteAsyncWithMoreThanContentLengthAndResponseNotStarted() | ||
{ | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; | ||
|
||
using (var server = new TestServer(httpContext => | ||
{ | ||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = 5; | ||
return httpContext.Response.Body.WriteAsync(response, 0, response.Length); | ||
}, serviceContext)) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
|
@@ -631,7 +668,7 @@ await connection.ReceiveEnd( | |
} | ||
|
||
[Fact] | ||
public async Task WhenAppSetsContentLengthButDoesNotWriteBody500ResponseSentAndConnectionCloses() | ||
public async Task WhenAppSetsContentLengthButDoesNotWriteBody500ResponseSentAndConnectionDoesNotClose() | ||
{ | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; | ||
|
@@ -645,23 +682,27 @@ public async Task WhenAppSetsContentLengthButDoesNotWriteBody500ResponseSentAndC | |
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.ReceiveForcedEnd( | ||
$"HTTP/1.1 500 Internal Server Error", | ||
"Connection: close", | ||
await connection.Receive( | ||
"HTTP/1.1 500 Internal Server Error", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
"Content-Length: 0", | ||
"", | ||
"HTTP/1.1 500 Internal Server Error", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
"Content-Length: 0", | ||
"", | ||
""); | ||
} | ||
} | ||
|
||
var errorMessage = Assert.Single(testLogger.Messages, message => message.LogLevel == LogLevel.Error); | ||
Assert.Equal( | ||
$"Response Content-Length mismatch: too few bytes written (0 of 5).", | ||
errorMessage.Exception.Message); | ||
var error = testLogger.Messages.Where(message => message.LogLevel == LogLevel.Error); | ||
Assert.Equal(2, error.Count()); | ||
Assert.All(error, message => message.Equals("Response Content-Length mismatch: too few bytes written (0 of 5).")); | ||
} | ||
|
||
[Theory] | ||
|
@@ -1065,6 +1106,170 @@ await connection.ReceiveEnd( | |
} | ||
} | ||
|
||
[Fact] | ||
public async Task FirstWriteVerifiedAfterOnStarting() | ||
{ | ||
using (var server = new TestServer(httpContext => | ||
{ | ||
httpContext.Response.OnStarting(() => | ||
{ | ||
// Change response to chunked | ||
httpContext.Response.ContentLength = null; | ||
return TaskCache.CompletedTask; | ||
}); | ||
|
||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = response.Length - 1; | ||
|
||
// If OnStarting is not run before verifying writes, an error response will be sent. | ||
httpContext.Response.Body.Write(response, 0, response.Length); | ||
return TaskCache.CompletedTask; | ||
}, new TestServiceContext())) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.Receive( | ||
"HTTP/1.1 200 OK", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
$"Transfer-Encoding: chunked", | ||
"", | ||
"c", | ||
"hello, world", | ||
"0", | ||
"", | ||
""); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task SubsequentWriteVerifiedAfterOnStarting() | ||
{ | ||
using (var server = new TestServer(httpContext => | ||
{ | ||
httpContext.Response.OnStarting(() => | ||
{ | ||
// Change response to chunked | ||
httpContext.Response.ContentLength = null; | ||
return TaskCache.CompletedTask; | ||
}); | ||
|
||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = response.Length - 1; | ||
|
||
// If OnStarting is not run before verifying writes, an error response will be sent. | ||
httpContext.Response.Body.Write(response, 0, response.Length / 2); | ||
httpContext.Response.Body.Write(response, response.Length / 2, response.Length - response.Length / 2); | ||
return TaskCache.CompletedTask; | ||
}, new TestServiceContext())) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.Receive( | ||
"HTTP/1.1 200 OK", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
$"Transfer-Encoding: chunked", | ||
"", | ||
"6", | ||
"hello,", | ||
"6", | ||
" world", | ||
"0", | ||
"", | ||
""); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task FirstWriteAsyncVerifiedAfterOnStarting() | ||
{ | ||
using (var server = new TestServer(httpContext => | ||
{ | ||
httpContext.Response.OnStarting(() => | ||
{ | ||
// Change response to chunked | ||
httpContext.Response.ContentLength = null; | ||
return TaskCache.CompletedTask; | ||
}); | ||
|
||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = response.Length - 1; | ||
|
||
// If OnStarting is not run before verifying writes, an error response will be sent. | ||
return httpContext.Response.Body.WriteAsync(response, 0, response.Length); | ||
}, new TestServiceContext())) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.Receive( | ||
"HTTP/1.1 200 OK", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
$"Transfer-Encoding: chunked", | ||
"", | ||
"c", | ||
"hello, world", | ||
"0", | ||
"", | ||
""); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task SubsequentWriteAsyncVerifiedAfterOnStarting() | ||
{ | ||
using (var server = new TestServer(async httpContext => | ||
{ | ||
httpContext.Response.OnStarting(() => | ||
{ | ||
// Change response to chunked | ||
httpContext.Response.ContentLength = null; | ||
return TaskCache.CompletedTask; | ||
}); | ||
|
||
var response = Encoding.ASCII.GetBytes("hello, world"); | ||
httpContext.Response.ContentLength = response.Length - 1; | ||
|
||
// If OnStarting is not run before verifying writes, an error response will be sent. | ||
await httpContext.Response.Body.WriteAsync(response, 0, response.Length / 2); | ||
await httpContext.Response.Body.WriteAsync(response, response.Length / 2, response.Length - response.Length / 2); | ||
}, new TestServiceContext())) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
await connection.Send( | ||
"GET / HTTP/1.1", | ||
"", | ||
""); | ||
await connection.Receive( | ||
"HTTP/1.1 200 OK", | ||
$"Date: {server.Context.DateHeaderValue}", | ||
$"Transfer-Encoding: chunked", | ||
"", | ||
"6", | ||
"hello,", | ||
"6", | ||
" world", | ||
"0", | ||
"", | ||
""); | ||
} | ||
} | ||
} | ||
|
||
public static TheoryData<string, StringValues, string> NullHeaderData | ||
{ | ||
get | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change InitializeResponse to not call VerifyAndUpdateWrite, and just call VerifyAndUpdateWrite afterwards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the check in
InitializeResponse
so we can return a 500 if the first write exceeds Content-Length. Without the check there a 200 response still started, despite the code failing later.