Skip to content

Commit

Permalink
[dotnet] Annotate nullability on network interactions (#15209)
Browse files Browse the repository at this point in the history
* [dotnet] Annotate nullability on network interactions

* Add XML doc to network implementation constructors
  • Loading branch information
RenderMichael authored Feb 3, 2025
1 parent 7b45115 commit 9a3390d
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 44 deletions.
27 changes: 18 additions & 9 deletions dotnet/src/webdriver/DevTools/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
// under the License.
// </copyright>

using System;
using System.Linq;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand All @@ -30,17 +33,17 @@ public abstract class Network
/// <summary>
/// Occurs when a network request requires authorization.
/// </summary>
public event AsyncEventHandler<AuthRequiredEventArgs> AuthRequired;
public event AsyncEventHandler<AuthRequiredEventArgs>? AuthRequired;

/// <summary>
/// Occurs when a network request is intercepted.
/// </summary>
public event AsyncEventHandler<RequestPausedEventArgs> RequestPaused;
public event AsyncEventHandler<RequestPausedEventArgs>? RequestPaused;

/// <summary>
/// Occurs when a network response is received.
/// </summary>
public event AsyncEventHandler<ResponsePausedEventArgs> ResponsePaused;
public event AsyncEventHandler<ResponsePausedEventArgs>? ResponsePaused;

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

/// <summary>
/// Asynchronously diables the fetch domain.
/// Asynchronously disables the fetch domain.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public abstract Task DisableNetwork();
Expand All @@ -79,18 +82,19 @@ public abstract class Network
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task SetUserAgentOverride(string userAgent)
{
await SetUserAgentOverride(new UserAgent() { UserAgentString = userAgent }).ConfigureAwait(false);
await SetUserAgentOverride(new UserAgent(userAgent)).ConfigureAwait(false);
}

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

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

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

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

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

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

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

/// <summary>
Expand Down Expand Up @@ -200,7 +209,7 @@ protected virtual void OnResponsePaused(ResponsePausedEventArgs e)

/// <returns>A task that represents the asynchronous operation.</returns>
/// <summary>
/// Am asynchrounous delegate for handling network events.
/// Am asynchronous delegate for handling network events.
/// </summary>
/// <typeparam name="TEventArgs">The type of event args the event raises.</typeparam>
/// <param name="sender">The sender of the event.</param>
Expand Down
58 changes: 51 additions & 7 deletions dotnet/src/webdriver/DevTools/v130/V130Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using System.Text;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium.DevTools.V130
{
/// <summary>
Expand All @@ -39,10 +41,11 @@ public class V130Network : DevTools.Network
/// </summary>
/// <param name="network">The adapter for the Network domain.</param>
/// <param name="fetch">The adapter for the Fetch domain.</param>
/// <exception cref="ArgumentNullException">If <paramref name="network"/> or <paramref name="fetch"/> are <see langword="null"/>.</exception>
public V130Network(NetworkAdapter network, FetchAdapter fetch)
{
this.network = network;
this.fetch = fetch;
this.network = network ?? throw new ArgumentNullException(nameof(network));
this.fetch = fetch ?? throw new ArgumentNullException(nameof(fetch));
fetch.AuthRequired += OnFetchAuthRequired;
fetch.RequestPaused += OnFetchRequestPaused;
}
Expand Down Expand Up @@ -101,7 +104,7 @@ await fetch.Enable(new Fetch.EnableCommandSettings()
}

/// <summary>
/// Asynchronously diables the fetch domain.
/// Asynchronously disables the fetch domain.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisableFetch()
Expand All @@ -114,8 +117,14 @@ public override async Task DisableFetch()
/// </summary>
/// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="userAgent"/> is null.</exception>
public override async Task SetUserAgentOverride(UserAgent userAgent)
{
if (userAgent is null)
{
throw new ArgumentNullException(nameof(userAgent));
}

await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
{
UserAgent = userAgent.UserAgentString,
Expand All @@ -129,8 +138,14 @@ await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
/// </summary>
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
public override async Task ContinueRequest(HttpRequestData requestData)
{
if (requestData is null)
{
throw new ArgumentNullException(nameof(requestData));
}

var commandSettings = new ContinueRequestCommandSettings()
{
RequestId = requestData.RequestId,
Expand Down Expand Up @@ -163,8 +178,19 @@ public override async Task ContinueRequest(HttpRequestData requestData)
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
/// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> or <paramref name="responseData"/> are <see langword="null"/>.</exception>
public override async Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData)
{
if (requestData is null)
{
throw new ArgumentNullException(nameof(requestData));
}

if (responseData is null)
{
throw new ArgumentNullException(nameof(responseData));
}

var commandSettings = new FulfillRequestCommandSettings()
{
RequestId = requestData.RequestId,
Expand Down Expand Up @@ -196,12 +222,18 @@ public override async Task ContinueRequestWithResponse(HttpRequestData requestDa
}

/// <summary>
/// Asynchronously contines an intercepted network call without modification.
/// Asynchronously continues an intercepted network call without modification.
/// </summary>
/// <param name="requestData">The <see cref="HttpRequestData"/> of the network call.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="requestData"/> is <see langword="null"/>.</exception>
public override async Task ContinueRequestWithoutModification(HttpRequestData requestData)
{
if (requestData is null)
{
throw new ArgumentNullException(nameof(requestData));
}

await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = requestData.RequestId }).ConfigureAwait(false);
}

Expand All @@ -212,7 +244,7 @@ public override async Task ContinueRequestWithoutModification(HttpRequestData re
/// <param name="userName">The user name with which to authenticate.</param>
/// <param name="password">The password with which to authenticate.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueWithAuth(string requestId, string userName, string password)
public override async Task ContinueWithAuth(string requestId, string? userName, string? password)
{
await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
{
Expand Down Expand Up @@ -248,8 +280,14 @@ await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
/// </summary>
/// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
public override async Task AddResponseBody(HttpResponseData responseData)
{
if (responseData is null)
{
throw new ArgumentNullException(nameof(responseData));
}

// If the response is a redirect, retrieving the body will throw an error in CDP.
if (responseData.StatusCode < 300 || responseData.StatusCode > 399)
{
Expand All @@ -273,12 +311,18 @@ public override async Task AddResponseBody(HttpResponseData responseData)
/// </summary>
/// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="responseData"/> is <see langword="null"/>.</exception>
public override async Task ContinueResponseWithoutModification(HttpResponseData responseData)
{
if (responseData is null)
{
throw new ArgumentNullException(nameof(responseData));
}

await fetch.ContinueResponse(new ContinueResponseCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
}

private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
private void OnFetchAuthRequired(object? sender, Fetch.AuthRequiredEventArgs e)
{
AuthRequiredEventArgs wrapped = new AuthRequiredEventArgs
(
Expand All @@ -289,7 +333,7 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
this.OnAuthRequired(wrapped);
}

private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e)
{
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
Expand Down
Loading

0 comments on commit 9a3390d

Please sign in to comment.