Skip to content

Commit bbb695f

Browse files
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.
1 parent 0bc8ad7 commit bbb695f

File tree

9 files changed

+19
-19
lines changed

9 files changed

+19
-19
lines changed

src/Hosting/TestHost/src/AsyncStreamWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.TestHost
1010
{
1111
internal class AsyncStreamWrapper : Stream
1212
{
13-
private Stream _inner;
14-
private Func<bool> _allowSynchronousIO;
13+
private readonly Stream _inner;
14+
private readonly Func<bool> _allowSynchronousIO;
1515

1616
internal AsyncStreamWrapper(Stream inner, Func<bool> allowSynchronousIO)
1717
{

src/Hosting/TestHost/src/ClientHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal ClientHandler(PathString pathBase, ApplicationWrapper application)
3939
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
4040
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
4141
{
42-
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
42+
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character
4343
}
4444
_pathBase = pathBase;
4545
}

src/Hosting/TestHost/src/HttpContextBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal class HttpContextBuilder : IHttpBodyControlFeature, IHttpResetFeature
2626
private bool _pipelineFinished;
2727
private bool _returningResponse;
2828
private object _testContext;
29-
private Pipe _requestPipe;
29+
private readonly Pipe _requestPipe;
3030

3131
private Action<HttpContext> _responseReadCompleteCallback;
3232
private Task _sendRequestStreamTask;

src/Hosting/TestHost/src/RequestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public RequestBuilder AddHeader(string name, string value)
6464
if (!_req.Content.Headers.TryAddWithoutValidation(name, value))
6565
{
6666
// TODO: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidHeaderName, name), "name");
67-
throw new ArgumentException("Invalid header name: " + name, "name");
67+
throw new ArgumentException("Invalid header name: " + name, nameof(name));
6868
}
6969
}
7070
return this;

src/Hosting/TestHost/src/ResponseBodyReaderStream.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ private static void VerifyBuffer(byte[] buffer, int offset, int count)
105105
{
106106
if (buffer == null)
107107
{
108-
throw new ArgumentNullException("buffer");
108+
throw new ArgumentNullException(nameof(buffer));
109109
}
110110
if (offset < 0 || offset > buffer.Length)
111111
{
112-
throw new ArgumentOutOfRangeException("offset", offset, string.Empty);
112+
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
113113
}
114114
if (count <= 0 || count > buffer.Length - offset)
115115
{
116-
throw new ArgumentOutOfRangeException("count", count, string.Empty);
116+
throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
117117
}
118118
}
119119

src/Hosting/TestHost/src/ResponseFeature.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ internal class ResponseFeature : IHttpResponseFeature, IHttpResponseBodyFeature
1616
private readonly HeaderDictionary _headers = new HeaderDictionary();
1717
private readonly Action<Exception> _abort;
1818

19-
private Func<Task> _responseStartingAsync = () => Task.FromResult(true);
20-
private Func<Task> _responseCompletedAsync = () => Task.FromResult(true);
19+
private Func<Task> _responseStartingAsync = () => Task.CompletedTask;
20+
private Func<Task> _responseCompletedAsync = () => Task.CompletedTask;
2121
private int _statusCode;
2222
private string _reasonPhrase;
2323

src/Hosting/TestHost/src/TestServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public async Task<HttpContext> SendAsync(Action<HttpContext> configureContext, C
150150
var pathBase = PathString.FromUriComponent(BaseAddress);
151151
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
152152
{
153-
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
153+
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character.
154154
}
155155
request.PathBase = pathBase;
156156
});

src/Hosting/TestHost/src/TestWebSocket.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace Microsoft.AspNetCore.TestHost
1212
{
1313
internal class TestWebSocket : WebSocket
1414
{
15-
private ReceiverSenderBuffer _receiveBuffer;
16-
private ReceiverSenderBuffer _sendBuffer;
15+
private readonly ReceiverSenderBuffer _receiveBuffer;
16+
private readonly ReceiverSenderBuffer _sendBuffer;
1717
private readonly string _subProtocol;
1818
private WebSocketState _state;
1919
private WebSocketCloseStatus? _closeStatus;
@@ -165,7 +165,7 @@ public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType m
165165
throw new ArgumentOutOfRangeException(nameof(messageType), messageType, string.Empty);
166166
}
167167

168-
var message = new Message(buffer, messageType, endOfMessage, cancellationToken);
168+
var message = new Message(buffer, messageType, endOfMessage);
169169
return _sendBuffer.SendAsync(message, cancellationToken);
170170
}
171171

@@ -225,7 +225,7 @@ private TestWebSocket(string subProtocol, ReceiverSenderBuffer readBuffer, Recei
225225

226226
private class Message
227227
{
228-
public Message(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken token)
228+
public Message(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage)
229229
{
230230
Buffer = buffer;
231231
CloseStatus = null;
@@ -236,7 +236,7 @@ public Message(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool
236236

237237
public Message(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
238238
{
239-
Buffer = new ArraySegment<byte>(new byte[0]);
239+
Buffer = new ArraySegment<byte>(Array.Empty<byte>());
240240
CloseStatus = closeStatus;
241241
CloseStatusDescription = closeStatusDescription;
242242
MessageType = WebSocketMessageType.Close;
@@ -255,8 +255,8 @@ private class ReceiverSenderBuffer
255255
private bool _receiverClosed;
256256
private bool _senderClosed;
257257
private bool _disposed;
258-
private SemaphoreSlim _sem;
259-
private Queue<Message> _messageQueue;
258+
private readonly SemaphoreSlim _sem;
259+
private readonly Queue<Message> _messageQueue;
260260

261261
public ReceiverSenderBuffer()
262262
{

src/Hosting/TestHost/src/WebSocketClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal WebSocketClient(PathString pathBase, ApplicationWrapper application)
2727
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
2828
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
2929
{
30-
pathBase = new PathString(pathBase.Value.Substring(0, pathBase.Value.Length - 1));
30+
pathBase = new PathString(pathBase.Value[..^1]); // All but the last character.
3131
}
3232
_pathBase = pathBase;
3333

0 commit comments

Comments
 (0)