Skip to content

Commit f410ed4

Browse files
committed
[dotnet] Annotate nullability on network interactions
1 parent 01a96d9 commit f410ed4

File tree

9 files changed

+251
-44
lines changed

9 files changed

+251
-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: 50 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>
@@ -41,8 +43,8 @@ public class V130Network : DevTools.Network
4143
/// <param name="fetch">The adapter for the Fetch domain.</param>
4244
public V130Network(NetworkAdapter network, FetchAdapter fetch)
4345
{
44-
this.network = network;
45-
this.fetch = fetch;
46+
this.network = network ?? throw new ArgumentNullException(nameof(network));
47+
this.fetch = fetch ?? throw new ArgumentNullException(nameof(fetch));
4648
fetch.AuthRequired += OnFetchAuthRequired;
4749
fetch.RequestPaused += OnFetchRequestPaused;
4850
}
@@ -101,7 +103,7 @@ await fetch.Enable(new Fetch.EnableCommandSettings()
101103
}
102104

103105
/// <summary>
104-
/// Asynchronously diables the fetch domain.
106+
/// Asynchronously disables the fetch domain.
105107
/// </summary>
106108
/// <returns>A task that represents the asynchronous operation.</returns>
107109
public override async Task DisableFetch()
@@ -114,8 +116,14 @@ public override async Task DisableFetch()
114116
/// </summary>
115117
/// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
116118
/// <returns>A task that represents the asynchronous operation.</returns>
119+
/// <exception cref="ArgumentNullException">If <paramref name="userAgent"/> is null.</exception>
117120
public override async Task SetUserAgentOverride(UserAgent userAgent)
118121
{
122+
if (userAgent is null)
123+
{
124+
throw new ArgumentNullException(nameof(userAgent));
125+
}
126+
119127
await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
120128
{
121129
UserAgent = userAgent.UserAgentString,
@@ -129,8 +137,14 @@ await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
129137
/// </summary>
130138
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
131139
/// <returns>A task that represents the asynchronous operation.</returns>
140+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
132141
public override async Task ContinueRequest(HttpRequestData requestData)
133142
{
143+
if (requestData is null)
144+
{
145+
throw new ArgumentNullException(nameof(requestData));
146+
}
147+
134148
var commandSettings = new ContinueRequestCommandSettings()
135149
{
136150
RequestId = requestData.RequestId,
@@ -163,8 +177,19 @@ public override async Task ContinueRequest(HttpRequestData requestData)
163177
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
164178
/// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
165179
/// <returns>A task that represents the asynchronous operation.</returns>
180+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> or <paramref name="responseData"/> are <see langword="null"/>.</exception>
166181
public override async Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData)
167182
{
183+
if (requestData is null)
184+
{
185+
throw new ArgumentNullException(nameof(requestData));
186+
}
187+
188+
if (responseData is null)
189+
{
190+
throw new ArgumentNullException(nameof(responseData));
191+
}
192+
168193
var commandSettings = new FulfillRequestCommandSettings()
169194
{
170195
RequestId = requestData.RequestId,
@@ -196,12 +221,18 @@ public override async Task ContinueRequestWithResponse(HttpRequestData requestDa
196221
}
197222

198223
/// <summary>
199-
/// Asynchronously contines an intercepted network call without modification.
224+
/// Asynchronously continues an intercepted network call without modification.
200225
/// </summary>
201226
/// <param name="requestData">The <see cref="HttpRequestData"/> of the network call.</param>
202227
/// <returns>A task that represents the asynchronous operation.</returns>
228+
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
203229
public override async Task ContinueRequestWithoutModification(HttpRequestData requestData)
204230
{
231+
if (requestData is null)
232+
{
233+
throw new ArgumentNullException(nameof(requestData));
234+
}
235+
205236
await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = requestData.RequestId }).ConfigureAwait(false);
206237
}
207238

@@ -212,7 +243,7 @@ public override async Task ContinueRequestWithoutModification(HttpRequestData re
212243
/// <param name="userName">The user name with which to authenticate.</param>
213244
/// <param name="password">The password with which to authenticate.</param>
214245
/// <returns>A task that represents the asynchronous operation.</returns>
215-
public override async Task ContinueWithAuth(string requestId, string userName, string password)
246+
public override async Task ContinueWithAuth(string requestId, string? userName, string? password)
216247
{
217248
await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
218249
{
@@ -248,8 +279,14 @@ await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
248279
/// </summary>
249280
/// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
250281
/// <returns>A task that represents the asynchronous operation.</returns>
282+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
251283
public override async Task AddResponseBody(HttpResponseData responseData)
252284
{
285+
if (responseData is null)
286+
{
287+
throw new ArgumentNullException(nameof(responseData));
288+
}
289+
253290
// If the response is a redirect, retrieving the body will throw an error in CDP.
254291
if (responseData.StatusCode < 300 || responseData.StatusCode > 399)
255292
{
@@ -273,12 +310,18 @@ public override async Task AddResponseBody(HttpResponseData responseData)
273310
/// </summary>
274311
/// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
275312
/// <returns>A task that represents the asynchronous operation.</returns>
313+
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
276314
public override async Task ContinueResponseWithoutModification(HttpResponseData responseData)
277315
{
316+
if (responseData is null)
317+
{
318+
throw new ArgumentNullException(nameof(responseData));
319+
}
320+
278321
await fetch.ContinueResponse(new ContinueResponseCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
279322
}
280323

281-
private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
324+
private void OnFetchAuthRequired(object? sender, Fetch.AuthRequiredEventArgs e)
282325
{
283326
AuthRequiredEventArgs wrapped = new AuthRequiredEventArgs
284327
(
@@ -289,7 +332,7 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
289332
this.OnAuthRequired(wrapped);
290333
}
291334

292-
private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
335+
private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e)
293336
{
294337
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
295338
{

0 commit comments

Comments
 (0)