From bbb695fe16bcd82f9c4d4c967fb365f388cdda52 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Mon, 6 Jul 2020 19:52:53 +0100 Subject: [PATCH] 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. --- src/Hosting/TestHost/src/AsyncStreamWrapper.cs | 4 ++-- src/Hosting/TestHost/src/ClientHandler.cs | 2 +- src/Hosting/TestHost/src/HttpContextBuilder.cs | 2 +- src/Hosting/TestHost/src/RequestBuilder.cs | 2 +- .../TestHost/src/ResponseBodyReaderStream.cs | 6 +++--- src/Hosting/TestHost/src/ResponseFeature.cs | 4 ++-- src/Hosting/TestHost/src/TestServer.cs | 2 +- src/Hosting/TestHost/src/TestWebSocket.cs | 14 +++++++------- src/Hosting/TestHost/src/WebSocketClient.cs | 2 +- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Hosting/TestHost/src/AsyncStreamWrapper.cs b/src/Hosting/TestHost/src/AsyncStreamWrapper.cs index 705d5a7a0e..545667701e 100644 --- a/src/Hosting/TestHost/src/AsyncStreamWrapper.cs +++ b/src/Hosting/TestHost/src/AsyncStreamWrapper.cs @@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.TestHost { internal class AsyncStreamWrapper : Stream { - private Stream _inner; - private Func _allowSynchronousIO; + private readonly Stream _inner; + private readonly Func _allowSynchronousIO; internal AsyncStreamWrapper(Stream inner, Func allowSynchronousIO) { diff --git a/src/Hosting/TestHost/src/ClientHandler.cs b/src/Hosting/TestHost/src/ClientHandler.cs index fb54d69bbf..5c4a42a5a8 100644 --- a/src/Hosting/TestHost/src/ClientHandler.cs +++ b/src/Hosting/TestHost/src/ClientHandler.cs @@ -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; } diff --git a/src/Hosting/TestHost/src/HttpContextBuilder.cs b/src/Hosting/TestHost/src/HttpContextBuilder.cs index 736b0458a6..525cc54624 100644 --- a/src/Hosting/TestHost/src/HttpContextBuilder.cs +++ b/src/Hosting/TestHost/src/HttpContextBuilder.cs @@ -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 _responseReadCompleteCallback; private Task _sendRequestStreamTask; diff --git a/src/Hosting/TestHost/src/RequestBuilder.cs b/src/Hosting/TestHost/src/RequestBuilder.cs index c8499fc025..579ba66906 100644 --- a/src/Hosting/TestHost/src/RequestBuilder.cs +++ b/src/Hosting/TestHost/src/RequestBuilder.cs @@ -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; diff --git a/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs b/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs index 6110c4a264..1af5169314 100644 --- a/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs +++ b/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs @@ -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); } } diff --git a/src/Hosting/TestHost/src/ResponseFeature.cs b/src/Hosting/TestHost/src/ResponseFeature.cs index a4c9a07e09..f56632cbed 100644 --- a/src/Hosting/TestHost/src/ResponseFeature.cs +++ b/src/Hosting/TestHost/src/ResponseFeature.cs @@ -16,8 +16,8 @@ namespace Microsoft.AspNetCore.TestHost private readonly HeaderDictionary _headers = new HeaderDictionary(); private readonly Action _abort; - private Func _responseStartingAsync = () => Task.FromResult(true); - private Func _responseCompletedAsync = () => Task.FromResult(true); + private Func _responseStartingAsync = () => Task.CompletedTask; + private Func _responseCompletedAsync = () => Task.CompletedTask; private int _statusCode; private string _reasonPhrase; diff --git a/src/Hosting/TestHost/src/TestServer.cs b/src/Hosting/TestHost/src/TestServer.cs index 1fe56ec919..cb41f75f6e 100644 --- a/src/Hosting/TestHost/src/TestServer.cs +++ b/src/Hosting/TestHost/src/TestServer.cs @@ -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; }); diff --git a/src/Hosting/TestHost/src/TestWebSocket.cs b/src/Hosting/TestHost/src/TestWebSocket.cs index d1a77e5af6..51297e0509 100644 --- a/src/Hosting/TestHost/src/TestWebSocket.cs +++ b/src/Hosting/TestHost/src/TestWebSocket.cs @@ -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 buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken token) + public Message(ArraySegment 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(new byte[0]); + Buffer = new ArraySegment(Array.Empty()); 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 _messageQueue; + private readonly SemaphoreSlim _sem; + private readonly Queue _messageQueue; public ReceiverSenderBuffer() { diff --git a/src/Hosting/TestHost/src/WebSocketClient.cs b/src/Hosting/TestHost/src/WebSocketClient.cs index cfb6ceda32..7743e51630 100644 --- a/src/Hosting/TestHost/src/WebSocketClient.cs +++ b/src/Hosting/TestHost/src/WebSocketClient.cs @@ -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;