Skip to content

[dotnet] Simplify user creation of network types #15267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions dotnet/src/webdriver/DevTools/v130/V130Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
var requestData = new HttpRequestData
(
requestId: e.RequestId,
method: e.Request.Method,
url: e.Request.Url,
postData: e.Request.PostData,
headers: new Dictionary<string, string>(e.Request.Headers)
);
{
RequestId = e.RequestId,
Method = e.Request.Method,
Url = e.Request.Url,
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};

RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
this.OnRequestPaused(wrapped);
}
else
{
var responseData = new HttpResponseData
(
requestId: e.RequestId,
url: e.Request.Url,
resourceType: e.ResourceType.ToString(),
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
errorReason: e.ResponseErrorReason?.ToString()
);
var responseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString(),
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
ErrorReason = e.ResponseErrorReason?.ToString()
};

if (e.ResponseHeaders != null)
{
Expand Down
30 changes: 15 additions & 15 deletions dotnet/src/webdriver/DevTools/v131/V131Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
var requestData = new HttpRequestData
(
requestId: e.RequestId,
method: e.Request.Method,
url: e.Request.Url,
postData: e.Request.PostData,
headers: new Dictionary<string, string>(e.Request.Headers)
);
{
RequestId = e.RequestId,
Method = e.Request.Method,
Url = e.Request.Url,
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};

RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
this.OnRequestPaused(wrapped);
}
else
{
var responseData = new HttpResponseData
(
requestId: e.RequestId,
url: e.Request.Url,
resourceType: e.ResourceType.ToString(),
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
errorReason: e.ResponseErrorReason?.ToString()
);
var responseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString(),
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
ErrorReason = e.ResponseErrorReason?.ToString()
};

if (e.ResponseHeaders != null)
{
Expand Down
30 changes: 15 additions & 15 deletions dotnet/src/webdriver/DevTools/v132/V132Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
var requestData = new HttpRequestData
(
requestId: e.RequestId,
method: e.Request.Method,
url: e.Request.Url,
postData: e.Request.PostData,
headers: new Dictionary<string, string>(e.Request.Headers)
);
{
RequestId = e.RequestId,
Method = e.Request.Method,
Url = e.Request.Url,
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};

RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
this.OnRequestPaused(wrapped);
}
else
{
var responseData = new HttpResponseData
(
requestId: e.RequestId,
url: e.Request.Url,
resourceType: e.ResourceType.ToString(),
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
errorReason: e.ResponseErrorReason?.ToString()
);
var responseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString(),
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
ErrorReason = e.ResponseErrorReason?.ToString()
};

if (e.ResponseHeaders != null)
{
Expand Down
28 changes: 14 additions & 14 deletions dotnet/src/webdriver/DevTools/v85/V85Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,27 +335,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
var requestData = new HttpRequestData
(
requestId: e.RequestId,
method: e.Request.Method,
url: e.Request.Url,
postData: e.Request.PostData,
headers: new Dictionary<string, string>(e.Request.Headers)
);
{
RequestId = e.RequestId,
Method = e.Request.Method,
Url = e.Request.Url,
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};

RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
this.OnRequestPaused(wrapped);
}
else
{
var responseData = new HttpResponseData
(
requestId: e.RequestId,
url: e.Request.Url,
resourceType: e.ResourceType.ToString(),
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
errorReason: e.ResponseErrorReason?.ToString()
);
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString(),
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
ErrorReason = e.ResponseErrorReason?.ToString()
};

if (e.ResponseHeaders != null)
{
Expand Down
11 changes: 1 addition & 10 deletions dotnet/src/webdriver/HttpRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ public HttpRequestData()
{
}

internal HttpRequestData(string? method, string? url, string? postData, Dictionary<string, string>? headers, string? requestId)
{
this.Method = method;
this.Url = url;
this.PostData = postData;
this.Headers = headers;
this.RequestId = requestId;
}

/// <summary>
/// Gets the method of the HTTP request.
/// </summary>
Expand All @@ -67,6 +58,6 @@ internal HttpRequestData(string? method, string? url, string? postData, Dictiona
/// <summary>
/// Gets the ID of the HTTP request.
/// </summary>
public string? RequestId { get; }
public string? RequestId { get; internal set; }
}
}
18 changes: 4 additions & 14 deletions dotnet/src/webdriver/HttpResponseData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,19 @@ public class HttpResponseData
/// <summary>
/// Initializes a new instance of the <see cref="HttpResponseData"/> type.
/// </summary>
/// <param name="requestId">The ID of the request that generated this response.</param>
/// <param name="url">The URL of the HTTP response.</param>
/// <param name="resourceType">The type of resource for this response.</param>
/// <param name="statusCode">The numeric status code of the HTTP response.</param>
/// <param name="errorReason">The reason for an error response.</param>
public HttpResponseData(string requestId, string url, string resourceType, long statusCode, string? errorReason)
public HttpResponseData()
{
RequestId = requestId;
Url = url;
ResourceType = resourceType;
StatusCode = statusCode;
ErrorReason = errorReason;
}

/// <summary>
/// Gets or sets the ID of the request that generated this response.
/// </summary>
public string RequestId { get; set; }
public string? RequestId { get; set; }

/// <summary>
/// Gets or sets the URL of the HTTP response.
/// </summary>
public string Url { get; set; }
public string? Url { get; set; }

/// <summary>
/// Gets or sets the numeric status code of the HTTP response.
Expand All @@ -79,7 +69,7 @@ public string? Body
/// <summary>
/// Gets or sets the type of resource for this response.
/// </summary>
public string ResourceType { get; set; }
public string? ResourceType { get; set; }

/// <summary>
/// Gets or sets the reason for an error response.
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private async Task OnResponsePaused(object sender, ResponsePausedEventArgs e)
// method demands one; however, the only property used by that method is the RequestId property.
// It might be better to refactor that method signature to simply pass the request ID, or
// alternatively, just pass the response data, which should also contain the request ID anyway.
HttpRequestData requestData = new HttpRequestData(null, null, null, null, requestId: e.ResponseData.RequestId);
HttpRequestData requestData = new HttpRequestData { RequestId = e.ResponseData.RequestId };
await this.session.Value.Domains.Network.ContinueRequestWithResponse(requestData, handler.ResponseTransformer(e.ResponseData)).ConfigureAwait(false);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/NetworkResponseReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
/// <summary>
/// Gets the request ID of the network request that generated this response.
/// </summary>
public string RequestId { get; }
public string? RequestId { get; }

/// <summary>
/// Gets the URL of the network response.
/// </summary>
public string ResponseUrl { get; }
public string? ResponseUrl { get; }

/// <summary>
/// Gets the HTTP status code of the network response.
Expand All @@ -79,7 +79,7 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
/// <summary>
/// Gets the type of resource of the network response.
/// </summary>
public string ResponseResourceType { get; }
public string? ResponseResourceType { get; }

/// <summary>
/// Gets the headers associated with this network response.
Expand Down
27 changes: 27 additions & 0 deletions dotnet/test/common/NetworkInterceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,32 @@ public async Task TestCanUseAuthorizationHandler()
Assert.That(text, Is.EqualTo("authorized"));
}
}

[Test]
[IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
public async Task TransformNetworkResponse()
{
if (driver is IDevTools)
{
var handler = new NetworkResponseHandler()
{
ResponseMatcher = _ => true,
ResponseTransformer = _ => new HttpResponseData
{
StatusCode = 200,
Body = "Creamy, delicious cheese!"
}
};
INetwork networkInterceptor = driver.Manage().Network;
networkInterceptor.AddResponseHandler(handler);
await networkInterceptor.StartMonitoring();

driver.Navigate().GoToUrl("https://www.selenium.dev");
await networkInterceptor.StopMonitoring();

var body = driver.FindElement(By.TagName("body"));
Assert.AreEqual("Creamy, delicious cheese!", body.Text);
}
}
}
}
Loading