Throw when setting OnStarting after response has already started (#806).

This commit is contained in:
Cesar Blum Silveira 2016-08-08 11:38:06 -07:00
parent 55f409b38b
commit a5aacd6307
2 changed files with 25 additions and 0 deletions

View File

@ -382,6 +382,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{
lock (_onStartingSync)
{
if (HasResponseStarted)
{
ThrowResponseAlreadyStartedException(nameof(OnStarting));
}
if (_onStarting == null)
{
_onStarting = new List<KeyValuePair<Func<object, Task>, object>>();

View File

@ -440,6 +440,26 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Assert.Throws<InvalidOperationException>(() => ((IHttpResponseFeature)frame).ReasonPhrase = "Reason phrase");
}
[Fact]
public void ThrowsWhenOnStartingIsSetAfterResponseStarted()
{
// Arrange
var connectionContext = new ConnectionContext()
{
DateHeaderValueManager = new DateHeaderValueManager(),
ServerAddress = ServerAddress.FromUrl("http://localhost:5000"),
ServerOptions = new KestrelServerOptions(),
SocketOutput = new MockSocketOuptut()
};
var frame = new Frame<object>(application: null, context: connectionContext);
frame.InitializeHeaders();
frame.Write(new ArraySegment<byte>(new byte[1]));
// Act/Assert
Assert.True(frame.HasResponseStarted);
Assert.Throws<InvalidOperationException>(() => ((IHttpResponseFeature)frame).OnStarting(_ => TaskUtilities.CompletedTask, null));
}
[Fact]
public void InitializeHeadersResetsRequestHeaders()
{