#1256 Check HasStarted for StatusCode and ReasonPhrase

This commit is contained in:
Chris Ross (ASP.NET) 2017-11-08 11:10:07 -08:00
parent 64596d538b
commit 05fd382b93
2 changed files with 55 additions and 2 deletions

View File

@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.TestHost
private Func<Task> _responseStartingAsync = () => Task.FromResult(true);
private Func<Task> _responseCompletedAsync = () => Task.FromResult(true);
private HeaderDictionary _headers = new HeaderDictionary();
private int _statusCode;
private string _reasonPhrase;
public ResponseFeature()
{
@ -25,9 +27,37 @@ namespace Microsoft.AspNetCore.TestHost
StatusCode = 200;
}
public int StatusCode { get; set; }
public int StatusCode
{
get => _statusCode;
set
{
if (HasStarted)
{
throw new InvalidOperationException("The status code cannot be set, the response has already started.");
}
if (value < 100)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The status code cannot be set to a value less than 100");
}
public string ReasonPhrase { get; set; }
_statusCode = value;
}
}
public string ReasonPhrase
{
get => _reasonPhrase;
set
{
if (HasStarted)
{
throw new InvalidOperationException("The reason phrase cannot be set, the response has already started.");
}
_reasonPhrase = value;
}
}
public IHeaderDictionary Headers { get; set; }

View File

@ -41,5 +41,28 @@ namespace Microsoft.AspNetCore.TestHost
}, state: "string");
});
}
[Fact]
public void StatusCode_ThrowsWhenHasStarted()
{
var responseInformation = new ResponseFeature();
responseInformation.HasStarted = true;
Assert.Throws<InvalidOperationException>(() => responseInformation.StatusCode = 400);
Assert.Throws<InvalidOperationException>(() => responseInformation.ReasonPhrase = "Hello World");
}
[Fact]
public void StatusCode_MustBeGreaterThan99()
{
var responseInformation = new ResponseFeature();
Assert.Throws<ArgumentOutOfRangeException>(() => responseInformation.StatusCode = 99);
Assert.Throws<ArgumentOutOfRangeException>(() => responseInformation.StatusCode = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => responseInformation.StatusCode = -200);
responseInformation.StatusCode = 100;
responseInformation.StatusCode = 200;
responseInformation.StatusCode = 1000;
}
}
}