Minor code refactoring to TestHost library (#23692)

* Make fields readonly

Make a number of fields that aren't changed readonly.

* Use range to trim string

Use a range to trim the last character of the paths.

* Use nameof()

Use nameof() for exceptions rather than literals.

* Use Task.CompletedTask

Use Task.CompletedTask instead of Task.FromResult().

* Use Array.Empty()

Use Array.Empty() instead of allocating an empty array.

* Remove unused parameter

Remove unused CancellationToken parameter.

* Fix compilation error

Fix compilation error.

* Apply suggestions from code review

Add comment explaining range syntax.
This commit is contained in:
Martin Costello 2020-07-06 19:52:53 +01:00 committed by GitHub
parent 0bc8ad7e6e
commit bbb695fe16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 19 deletions

View File

@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.TestHost
{
internal class AsyncStreamWrapper : Stream
{
private Stream _inner;
private Func<bool> _allowSynchronousIO;
private readonly Stream _inner;
private readonly Func<bool> _allowSynchronousIO;
internal AsyncStreamWrapper(Stream inner, Func<bool> allowSynchronousIO)
{

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.TestHost
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
{
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character
}
_pathBase = pathBase;
}

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.TestHost
private bool _pipelineFinished;
private bool _returningResponse;
private object _testContext;
private Pipe _requestPipe;
private readonly Pipe _requestPipe;
private Action<HttpContext> _responseReadCompleteCallback;
private Task _sendRequestStreamTask;

View File

@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.TestHost
if (!_req.Content.Headers.TryAddWithoutValidation(name, value))
{
// TODO: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidHeaderName, name), "name");
throw new ArgumentException("Invalid header name: " + name, "name");
throw new ArgumentException("Invalid header name: " + name, nameof(name));
}
}
return this;

View File

@ -105,15 +105,15 @@ namespace Microsoft.AspNetCore.TestHost
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
throw new ArgumentNullException(nameof(buffer));
}
if (offset < 0 || offset > buffer.Length)
{
throw new ArgumentOutOfRangeException("offset", offset, string.Empty);
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
}
if (count <= 0 || count > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException("count", count, string.Empty);
throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
}
}

View File

@ -16,8 +16,8 @@ namespace Microsoft.AspNetCore.TestHost
private readonly HeaderDictionary _headers = new HeaderDictionary();
private readonly Action<Exception> _abort;
private Func<Task> _responseStartingAsync = () => Task.FromResult(true);
private Func<Task> _responseCompletedAsync = () => Task.FromResult(true);
private Func<Task> _responseStartingAsync = () => Task.CompletedTask;
private Func<Task> _responseCompletedAsync = () => Task.CompletedTask;
private int _statusCode;
private string _reasonPhrase;

View File

@ -150,7 +150,7 @@ namespace Microsoft.AspNetCore.TestHost
var pathBase = PathString.FromUriComponent(BaseAddress);
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
{
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character.
}
request.PathBase = pathBase;
});

View File

@ -12,8 +12,8 @@ namespace Microsoft.AspNetCore.TestHost
{
internal class TestWebSocket : WebSocket
{
private ReceiverSenderBuffer _receiveBuffer;
private ReceiverSenderBuffer _sendBuffer;
private readonly ReceiverSenderBuffer _receiveBuffer;
private readonly ReceiverSenderBuffer _sendBuffer;
private readonly string _subProtocol;
private WebSocketState _state;
private WebSocketCloseStatus? _closeStatus;
@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.TestHost
throw new ArgumentOutOfRangeException(nameof(messageType), messageType, string.Empty);
}
var message = new Message(buffer, messageType, endOfMessage, cancellationToken);
var message = new Message(buffer, messageType, endOfMessage);
return _sendBuffer.SendAsync(message, cancellationToken);
}
@ -225,7 +225,7 @@ namespace Microsoft.AspNetCore.TestHost
private class Message
{
public Message(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken token)
public Message(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage)
{
Buffer = buffer;
CloseStatus = null;
@ -236,7 +236,7 @@ namespace Microsoft.AspNetCore.TestHost
public Message(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
{
Buffer = new ArraySegment<byte>(new byte[0]);
Buffer = new ArraySegment<byte>(Array.Empty<byte>());
CloseStatus = closeStatus;
CloseStatusDescription = closeStatusDescription;
MessageType = WebSocketMessageType.Close;
@ -255,8 +255,8 @@ namespace Microsoft.AspNetCore.TestHost
private bool _receiverClosed;
private bool _senderClosed;
private bool _disposed;
private SemaphoreSlim _sem;
private Queue<Message> _messageQueue;
private readonly SemaphoreSlim _sem;
private readonly Queue<Message> _messageQueue;
public ReceiverSenderBuffer()
{

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.TestHost
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
{
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character.
}
_pathBase = pathBase;