Skip to content

Commit 48ba3a1

Browse files
[dotnet] Annotate nullability on HttpCommandExecutor aside from Response (#14871)
* [dotnet] Annotate nullability on `HttpCommandExecutor` aside from `Response` * Remove nullability annotation from `HttpCommandExecutor.CreateHttpClient`
1 parent 1e86898 commit 48ba3a1

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,9 @@ public class HttpCommandExecutor : ICommandExecutor
4343
private const string RequestAcceptHeader = JsonMimeType + ", " + PngMimeType;
4444
private const string RequestContentTypeHeader = JsonMimeType + "; charset=" + Utf8CharsetType;
4545
private const string UserAgentHeaderTemplate = "selenium/{0} (.net {1})";
46-
private Uri remoteServerUri;
47-
private TimeSpan serverResponseTimeout;
48-
private string userAgent;
49-
private bool enableKeepAlive;
46+
private readonly Uri remoteServerUri;
47+
private readonly TimeSpan serverResponseTimeout;
5048
private bool isDisposed;
51-
private IWebProxy proxy;
5249
private CommandInfoRepository commandInfoRepository = new W3CWireProtocolCommandInfoRepository();
5350
private HttpClient client;
5451

@@ -71,6 +68,7 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout)
7168
/// <param name="timeout">The timeout within which the server must respond.</param>
7269
/// <param name="enableKeepAlive"><see langword="true"/> if the KeepAlive header should be sent
7370
/// with HTTP requests; otherwise, <see langword="false"/>.</param>
71+
/// <exception cref="ArgumentNullException">If <paramref name="addressOfRemoteServer"/> is <see langword="null"/>.</exception>
7472
public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool enableKeepAlive)
7573
{
7674
if (addressOfRemoteServer == null)
@@ -83,10 +81,10 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
8381
addressOfRemoteServer = new Uri(addressOfRemoteServer.ToString() + "/");
8482
}
8583

86-
this.userAgent = string.Format(CultureInfo.InvariantCulture, UserAgentHeaderTemplate, ResourceUtilities.ProductVersion, ResourceUtilities.PlatformFamily);
84+
this.UserAgent = string.Format(CultureInfo.InvariantCulture, UserAgentHeaderTemplate, ResourceUtilities.ProductVersion, ResourceUtilities.PlatformFamily);
8785
this.remoteServerUri = addressOfRemoteServer;
8886
this.serverResponseTimeout = timeout;
89-
this.enableKeepAlive = enableKeepAlive;
87+
this.IsKeepAliveEnabled = enableKeepAlive;
9088
}
9189

9290
/// <summary>
@@ -100,41 +98,30 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
10098
/// between this <see cref="HttpCommandExecutor"/> and the remote end WebDriver
10199
/// implementation.
102100
/// </summary>
103-
public IWebProxy Proxy
104-
{
105-
get { return this.proxy; }
106-
set { this.proxy = value; }
107-
}
101+
public IWebProxy Proxy { get; set; }
108102

109103
/// <summary>
110104
/// Gets or sets a value indicating whether keep-alive is enabled for HTTP
111105
/// communication between this <see cref="HttpCommandExecutor"/> and the
112106
/// remote end WebDriver implementation.
113107
/// </summary>
114-
public bool IsKeepAliveEnabled
115-
{
116-
get { return this.enableKeepAlive; }
117-
set { this.enableKeepAlive = value; }
118-
}
108+
public bool IsKeepAliveEnabled { get; set; }
119109

120110
/// <summary>
121111
/// Gets or sets the user agent string used for HTTP communication
122112
/// batween this <see cref="HttpCommandExecutor"/> and the remote end
123113
/// WebDriver implementation
124114
/// </summary>
125-
public string UserAgent
126-
{
127-
get { return this.userAgent; }
128-
set { this.userAgent = value; }
129-
}
115+
public string UserAgent { get; set; }
130116

131117
/// <summary>
132118
/// Gets the repository of objects containing information about commands.
133119
/// </summary>
120+
/// <exception cref="ArgumentNullException">If the value is set to <see langword="null"/>.</exception>
134121
protected CommandInfoRepository CommandInfoRepository
135122
{
136-
get { return this.commandInfoRepository; }
137-
set { this.commandInfoRepository = value; }
123+
get => this.commandInfoRepository;
124+
set => this.commandInfoRepository = value ?? throw new ArgumentNullException(nameof(value), "CommandInfoRepository cannot be null");
138125
}
139126

140127
/// <summary>
@@ -145,8 +132,7 @@ protected CommandInfoRepository CommandInfoRepository
145132
/// <returns><see langword="true"/> if the new command has been added successfully; otherwise, <see langword="false"/>.</returns>
146133
public bool TryAddCommand(string commandName, CommandInfo info)
147134
{
148-
HttpCommandInfo commandInfo = info as HttpCommandInfo;
149-
if (commandInfo == null)
135+
if (info is not HttpCommandInfo commandInfo)
150136
{
151137
return false;
152138
}
@@ -193,7 +179,7 @@ public virtual async Task<Response> ExecuteAsync(Command commandToExecute)
193179
}
194180

195181
HttpRequestInfo requestInfo = new HttpRequestInfo(this.remoteServerUri, commandToExecute, info);
196-
HttpResponseInfo responseInfo = null;
182+
HttpResponseInfo responseInfo;
197183
try
198184
{
199185
responseInfo = await this.MakeHttpRequest(requestInfo).ConfigureAwait(false);
@@ -304,12 +290,11 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
304290

305291
using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage).ConfigureAwait(false))
306292
{
307-
HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
308-
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
309-
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType?.ToString();
310-
httpResponseInfo.StatusCode = responseMessage.StatusCode;
293+
var responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
294+
var responseContentType = responseMessage.Content.Headers.ContentType?.ToString();
295+
var responseStatusCode = responseMessage.StatusCode;
311296

312-
return httpResponseInfo;
297+
return new HttpResponseInfo(responseBody, responseContentType, responseStatusCode);
313298
}
314299
}
315300
}
@@ -346,12 +331,15 @@ private Response CreateResponse(HttpResponseInfo responseInfo)
346331
return response;
347332
}
348333

334+
#nullable enable
335+
349336
/// <summary>
350337
/// Releases all resources used by the <see cref="HttpCommandExecutor"/>.
351338
/// </summary>
352339
public void Dispose()
353340
{
354341
this.Dispose(true);
342+
GC.SuppressFinalize(this);
355343
}
356344

357345
/// <summary>
@@ -364,10 +352,7 @@ protected virtual void Dispose(bool disposing)
364352
{
365353
if (!this.isDisposed)
366354
{
367-
if (this.client != null)
368-
{
369-
this.client.Dispose();
370-
}
355+
this.client?.Dispose();
371356

372357
this.isDisposed = true;
373358
}
@@ -377,6 +362,11 @@ private class HttpRequestInfo
377362
{
378363
public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo commandInfo)
379364
{
365+
if (commandInfo is null)
366+
{
367+
throw new ArgumentNullException(nameof(commandInfo));
368+
}
369+
380370
this.FullUri = commandInfo.CreateCommandUri(serverUri, commandToExecute);
381371
this.HttpMethod = commandInfo.Method;
382372
this.RequestBody = commandToExecute.ParametersAsJsonString;
@@ -389,9 +379,16 @@ public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo
389379

390380
private class HttpResponseInfo
391381
{
382+
public HttpResponseInfo(string body, string? contentType, HttpStatusCode statusCode)
383+
{
384+
this.Body = body ?? throw new ArgumentNullException(nameof(body));
385+
this.ContentType = contentType;
386+
this.StatusCode = statusCode;
387+
}
388+
392389
public HttpStatusCode StatusCode { get; set; }
393390
public string Body { get; set; }
394-
public string ContentType { get; set; }
391+
public string? ContentType { get; set; }
395392
}
396393

397394
/// <summary>
@@ -404,7 +401,12 @@ private class DiagnosticsHttpHandler : DelegatingHandler
404401
public DiagnosticsHttpHandler(HttpMessageHandler messageHandler, ILogger logger)
405402
: base(messageHandler)
406403
{
407-
_logger = logger;
404+
if (messageHandler is null)
405+
{
406+
throw new ArgumentNullException(nameof(messageHandler));
407+
}
408+
409+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
408410
}
409411

410412
/// <summary>

0 commit comments

Comments
 (0)