Skip to content

Commit 9a3390d

Browse files
[dotnet] Annotate nullability on network interactions (SeleniumHQ#15209)
* [dotnet] Annotate nullability on network interactions * Add XML doc to network implementation constructors
1 parent 7b45115 commit 9a3390d

File tree

9 files changed

+255
-44
lines changed

9 files changed

+255
-44
lines changed

dotnet/src/webdriver/DevTools/Network.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Linq;
2122
using System.Threading.Tasks;
2223

24+
#nullable enable
25+
2326
namespace OpenQA.Selenium.DevTools
2427
{
2528
/// <summary>
@@ -30,17 +33,17 @@ public abstract class Network
3033
/// <summary>
3134
/// Occurs when a network request requires authorization.
3235
/// </summary>
33-
public event AsyncEventHandler<AuthRequiredEventArgs> AuthRequired;
36+
public event AsyncEventHandler<AuthRequiredEventArgs>? AuthRequired;
3437

3538
/// <summary>
3639
/// Occurs when a network request is intercepted.
3740
/// </summary>
38-
public event AsyncEventHandler<RequestPausedEventArgs> RequestPaused;
41+
public event AsyncEventHandler<RequestPausedEventArgs>? RequestPaused;
3942

4043
/// <summary>
4144
/// Occurs when a network response is received.
4245
/// </summary>
43-
public event AsyncEventHandler<ResponsePausedEventArgs> ResponsePaused;
46+
public event AsyncEventHandler<ResponsePausedEventArgs>? ResponsePaused;
4447

4548
/// <summary>
4649
/// Asynchronously disables network caching.
@@ -61,7 +64,7 @@ public abstract class Network
6164
public abstract Task EnableNetwork();
6265

6366
/// <summary>
64-
/// Asynchronously diables the fetch domain.
67+
/// Asynchronously disables the fetch domain.
6568
/// </summary>
6669
/// <returns>A task that represents the asynchronous operation.</returns>
6770
public abstract Task DisableNetwork();
@@ -79,18 +82,19 @@ public abstract class Network
7982
/// <returns>A task that represents the asynchronous operation.</returns>
8083
public async Task SetUserAgentOverride(string userAgent)
8184
{
82-
await SetUserAgentOverride(new UserAgent() { UserAgentString = userAgent }).ConfigureAwait(false);
85+
await SetUserAgentOverride(new UserAgent(userAgent)).ConfigureAwait(false);
8386
}
8487

8588
/// <summary>
8689
/// Asynchronously sets the override of the user agent settings.
8790
/// </summary>
8891
/// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
8992
/// <returns>A task that represents the asynchronous operation.</returns>
93+
/// <exception cref="ArgumentNullException">If <paramref name="userAgent"/> is null.</exception>
9094
public abstract Task SetUserAgentOverride(UserAgent userAgent);
9195

9296
/// <summary>
93-
/// Asynchronously diables the fetch domain.
97+
/// Asynchronously disables the fetch domain.
9498
/// </summary>
9599
/// <returns>A task that represents the asynchronous operation.</returns>
96100
public abstract Task DisableFetch();
@@ -100,6 +104,7 @@ public async Task SetUserAgentOverride(string userAgent)
100104
/// </summary>
101105
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
102106
/// <returns>A task that represents the asynchronous operation.</returns>
107+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
103108
public abstract Task ContinueRequest(HttpRequestData requestData);
104109

105110
/// <summary>
@@ -108,13 +113,15 @@ public async Task SetUserAgentOverride(string userAgent)
108113
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
109114
/// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
110115
/// <returns>A task that represents the asynchronous operation.</returns>
116+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> or <paramref name="responseData"/> are <see langword="null"/>.</exception>
111117
public abstract Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData);
112118

113119
/// <summary>
114-
/// Asynchronously contines an intercepted network request without modification.
120+
/// Asynchronously continues an intercepted network request without modification.
115121
/// </summary>
116122
/// <param name="requestData">The <see cref="HttpRequestData"/> of the network request.</param>
117123
/// <returns>A task that represents the asynchronous operation.</returns>
124+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
118125
public abstract Task ContinueRequestWithoutModification(HttpRequestData requestData);
119126

120127
/// <summary>
@@ -124,7 +131,7 @@ public async Task SetUserAgentOverride(string userAgent)
124131
/// <param name="userName">The user name with which to authenticate.</param>
125132
/// <param name="password">The password with which to authenticate.</param>
126133
/// <returns>A task that represents the asynchronous operation.</returns>
127-
public abstract Task ContinueWithAuth(string requestId, string userName, string password);
134+
public abstract Task ContinueWithAuth(string requestId, string? userName, string? password);
128135

129136
/// <summary>
130137
/// Asynchronously cancels authorization of an intercepted network request.
@@ -138,13 +145,15 @@ public async Task SetUserAgentOverride(string userAgent)
138145
/// </summary>
139146
/// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
140147
/// <returns>A task that represents the asynchronous operation.</returns>
148+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
141149
public abstract Task AddResponseBody(HttpResponseData responseData);
142150

143151
/// <summary>
144152
/// Asynchronously continues an intercepted network response without modification.
145153
/// </summary>
146154
/// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
147155
/// <returns>A task that represents the asynchronous operation.</returns>
156+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
148157
public abstract Task ContinueResponseWithoutModification(HttpResponseData responseData);
149158

150159
/// <summary>
@@ -200,7 +209,7 @@ protected virtual void OnResponsePaused(ResponsePausedEventArgs e)
200209

201210
/// <returns>A task that represents the asynchronous operation.</returns>
202211
/// <summary>
203-
/// Am asynchrounous delegate for handling network events.
212+
/// Am asynchronous delegate for handling network events.
204213
/// </summary>
205214
/// <typeparam name="TEventArgs">The type of event args the event raises.</typeparam>
206215
/// <param name="sender">The sender of the event.</param>

dotnet/src/webdriver/DevTools/v130/V130Network.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
using System.Text;
2525
using System.Threading.Tasks;
2626

27+
#nullable enable
28+
2729
namespace OpenQA.Selenium.DevTools.V130
2830
{
2931
/// <summary>
@@ -39,10 +41,11 @@ public class V130Network : DevTools.Network
3941
/// </summary>
4042
/// <param name="network">The adapter for the Network domain.</param>
4143
/// <param name="fetch">The adapter for the Fetch domain.</param>
44+
/// <exception cref="ArgumentNullException">If <paramref name="network"/> or <paramref name="fetch"/> are <see langword="null"/>.</exception>
4245
public V130Network(NetworkAdapter network, FetchAdapter fetch)
4346
{
44-
this.network = network;
45-
this.fetch = fetch;
47+
this.network = network ?? throw new ArgumentNullException(nameof(network));
48+
this.fetch = fetch ?? throw new ArgumentNullException(nameof(fetch));
4649
fetch.AuthRequired += OnFetchAuthRequired;
4750
fetch.RequestPaused += OnFetchRequestPaused;
4851
}
@@ -101,7 +104,7 @@ await fetch.Enable(new Fetch.EnableCommandSettings()
101104
}
102105

103106
/// <summary>
104-
/// Asynchronously diables the fetch domain.
107+
/// Asynchronously disables the fetch domain.
105108
/// </summary>
106109
/// <returns>A task that represents the asynchronous operation.</returns>
107110
public override async Task DisableFetch()
@@ -114,8 +117,14 @@ public override async Task DisableFetch()
114117
/// </summary>
115118
/// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
116119
/// <returns>A task that represents the asynchronous operation.</returns>
120+
/// <exception cref="ArgumentNullException">If <paramref name="userAgent"/> is null.</exception>
117121
public override async Task SetUserAgentOverride(UserAgent userAgent)
118122
{
123+
if (userAgent is null)
124+
{
125+
throw new ArgumentNullException(nameof(userAgent));
126+
}
127+
119128
await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
120129
{
121130
UserAgent = userAgent.UserAgentString,
@@ -129,8 +138,14 @@ await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
129138
/// </summary>
130139
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
131140
/// <returns>A task that represents the asynchronous operation.</returns>
141+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
132142
public override async Task ContinueRequest(HttpRequestData requestData)
133143
{
144+
if (requestData is null)
145+
{
146+
throw new ArgumentNullException(nameof(requestData));
147+
}
148+
134149
var commandSettings = new ContinueRequestCommandSettings()
135150
{
136151
RequestId = requestData.RequestId,
@@ -163,8 +178,19 @@ public override async Task ContinueRequest(HttpRequestData requestData)
163178
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
164179
/// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
165180
/// <returns>A task that represents the asynchronous operation.</returns>
181+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> or <paramref name="responseData"/> are <see langword="null"/>.</exception>
166182
public override async Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData)
167183
{
184+
if (requestData is null)
185+
{
186+
throw new ArgumentNullException(nameof(requestData));
187+
}
188+
189+
if (responseData is null)
190+
{
191+
throw new ArgumentNullException(nameof(responseData));
192+
}
193+
168194
var commandSettings = new FulfillRequestCommandSettings()
169195
{
170196
RequestId = requestData.RequestId,
@@ -196,12 +222,18 @@ public override async Task ContinueRequestWithResponse(HttpRequestData requestDa
196222
}
197223

198224
/// <summary>
199-
/// Asynchronously contines an intercepted network call without modification.
225+
/// Asynchronously continues an intercepted network call without modification.
200226
/// </summary>
201227
/// <param name="requestData">The <see cref="HttpRequestData"/> of the network call.</param>
202228
/// <returns>A task that represents the asynchronous operation.</returns>
229+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
203230
public override async Task ContinueRequestWithoutModification(HttpRequestData requestData)
204231
{
232+
if (requestData is null)
233+
{
234+
throw new ArgumentNullException(nameof(requestData));
235+
}
236+
205237
await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = requestData.RequestId }).ConfigureAwait(false);
206238
}
207239

@@ -212,7 +244,7 @@ public override async Task ContinueRequestWithoutModification(HttpRequestData re
212244
/// <param name="userName">The user name with which to authenticate.</param>
213245
/// <param name="password">The password with which to authenticate.</param>
214246
/// <returns>A task that represents the asynchronous operation.</returns>
215-
public override async Task ContinueWithAuth(string requestId, string userName, string password)
247+
public override async Task ContinueWithAuth(string requestId, string? userName, string? password)
216248
{
217249
await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
218250
{
@@ -248,8 +280,14 @@ await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
248280
/// </summary>
249281
/// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
250282
/// <returns>A task that represents the asynchronous operation.</returns>
283+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
251284
public override async Task AddResponseBody(HttpResponseData responseData)
252285
{
286+
if (responseData is null)
287+
{
288+
throw new ArgumentNullException(nameof(responseData));
289+
}
290+
253291
// If the response is a redirect, retrieving the body will throw an error in CDP.
254292
if (responseData.StatusCode < 300 || responseData.StatusCode > 399)
255293
{
@@ -273,12 +311,18 @@ public override async Task AddResponseBody(HttpResponseData responseData)
273311
/// </summary>
274312
/// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
275313
/// <returns>A task that represents the asynchronous operation.</returns>
314+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
276315
public override async Task ContinueResponseWithoutModification(HttpResponseData responseData)
277316
{
317+
if (responseData is null)
318+
{
319+
throw new ArgumentNullException(nameof(responseData));
320+
}
321+
278322
await fetch.ContinueResponse(new ContinueResponseCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
279323
}
280324

281-
private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
325+
private void OnFetchAuthRequired(object? sender, Fetch.AuthRequiredEventArgs e)
282326
{
283327
AuthRequiredEventArgs wrapped = new AuthRequiredEventArgs
284328
(
@@ -289,7 +333,7 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
289333
this.OnAuthRequired(wrapped);
290334
}
291335

292-
private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
336+
private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e)
293337
{
294338
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
295339
{

0 commit comments

Comments
 (0)