Skip to content

Commit c16a3c3

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Simplify user creation of network types (SeleniumHQ#15267)
* [dotnet] Simplify user creation of network types * add test
1 parent a1b7009 commit c16a3c3

9 files changed

+95
-87
lines changed

Diff for: dotnet/src/webdriver/DevTools/v130/V130Network.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
338338
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
339339
{
340340
var requestData = new HttpRequestData
341-
(
342-
requestId: e.RequestId,
343-
method: e.Request.Method,
344-
url: e.Request.Url,
345-
postData: e.Request.PostData,
346-
headers: new Dictionary<string, string>(e.Request.Headers)
347-
);
341+
{
342+
RequestId = e.RequestId,
343+
Method = e.Request.Method,
344+
Url = e.Request.Url,
345+
PostData = e.Request.PostData,
346+
Headers = new Dictionary<string, string>(e.Request.Headers)
347+
};
348348

349349
RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
350350
this.OnRequestPaused(wrapped);
351351
}
352352
else
353353
{
354-
var responseData = new HttpResponseData
355-
(
356-
requestId: e.RequestId,
357-
url: e.Request.Url,
358-
resourceType: e.ResourceType.ToString(),
359-
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
360-
errorReason: e.ResponseErrorReason?.ToString()
361-
);
354+
var responseData = new HttpResponseData()
355+
{
356+
RequestId = e.RequestId,
357+
Url = e.Request.Url,
358+
ResourceType = e.ResourceType.ToString(),
359+
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
360+
ErrorReason = e.ResponseErrorReason?.ToString()
361+
};
362362

363363
if (e.ResponseHeaders != null)
364364
{

Diff for: dotnet/src/webdriver/DevTools/v131/V131Network.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
338338
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
339339
{
340340
var requestData = new HttpRequestData
341-
(
342-
requestId: e.RequestId,
343-
method: e.Request.Method,
344-
url: e.Request.Url,
345-
postData: e.Request.PostData,
346-
headers: new Dictionary<string, string>(e.Request.Headers)
347-
);
341+
{
342+
RequestId = e.RequestId,
343+
Method = e.Request.Method,
344+
Url = e.Request.Url,
345+
PostData = e.Request.PostData,
346+
Headers = new Dictionary<string, string>(e.Request.Headers)
347+
};
348348

349349
RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
350350
this.OnRequestPaused(wrapped);
351351
}
352352
else
353353
{
354-
var responseData = new HttpResponseData
355-
(
356-
requestId: e.RequestId,
357-
url: e.Request.Url,
358-
resourceType: e.ResourceType.ToString(),
359-
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
360-
errorReason: e.ResponseErrorReason?.ToString()
361-
);
354+
var responseData = new HttpResponseData()
355+
{
356+
RequestId = e.RequestId,
357+
Url = e.Request.Url,
358+
ResourceType = e.ResourceType.ToString(),
359+
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
360+
ErrorReason = e.ResponseErrorReason?.ToString()
361+
};
362362

363363
if (e.ResponseHeaders != null)
364364
{

Diff for: dotnet/src/webdriver/DevTools/v132/V132Network.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -338,27 +338,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
338338
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
339339
{
340340
var requestData = new HttpRequestData
341-
(
342-
requestId: e.RequestId,
343-
method: e.Request.Method,
344-
url: e.Request.Url,
345-
postData: e.Request.PostData,
346-
headers: new Dictionary<string, string>(e.Request.Headers)
347-
);
341+
{
342+
RequestId = e.RequestId,
343+
Method = e.Request.Method,
344+
Url = e.Request.Url,
345+
PostData = e.Request.PostData,
346+
Headers = new Dictionary<string, string>(e.Request.Headers)
347+
};
348348

349349
RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
350350
this.OnRequestPaused(wrapped);
351351
}
352352
else
353353
{
354-
var responseData = new HttpResponseData
355-
(
356-
requestId: e.RequestId,
357-
url: e.Request.Url,
358-
resourceType: e.ResourceType.ToString(),
359-
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
360-
errorReason: e.ResponseErrorReason?.ToString()
361-
);
354+
var responseData = new HttpResponseData()
355+
{
356+
RequestId = e.RequestId,
357+
Url = e.Request.Url,
358+
ResourceType = e.ResourceType.ToString(),
359+
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
360+
ErrorReason = e.ResponseErrorReason?.ToString()
361+
};
362362

363363
if (e.ResponseHeaders != null)
364364
{

Diff for: dotnet/src/webdriver/DevTools/v85/V85Network.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -335,27 +335,27 @@ private void OnFetchRequestPaused(object? sender, Fetch.RequestPausedEventArgs e
335335
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
336336
{
337337
var requestData = new HttpRequestData
338-
(
339-
requestId: e.RequestId,
340-
method: e.Request.Method,
341-
url: e.Request.Url,
342-
postData: e.Request.PostData,
343-
headers: new Dictionary<string, string>(e.Request.Headers)
344-
);
338+
{
339+
RequestId = e.RequestId,
340+
Method = e.Request.Method,
341+
Url = e.Request.Url,
342+
PostData = e.Request.PostData,
343+
Headers = new Dictionary<string, string>(e.Request.Headers)
344+
};
345345

346346
RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
347347
this.OnRequestPaused(wrapped);
348348
}
349349
else
350350
{
351351
var responseData = new HttpResponseData
352-
(
353-
requestId: e.RequestId,
354-
url: e.Request.Url,
355-
resourceType: e.ResourceType.ToString(),
356-
statusCode: e.ResponseStatusCode.GetValueOrDefault(),
357-
errorReason: e.ResponseErrorReason?.ToString()
358-
);
352+
{
353+
RequestId = e.RequestId,
354+
Url = e.Request.Url,
355+
ResourceType = e.ResourceType.ToString(),
356+
StatusCode = e.ResponseStatusCode.GetValueOrDefault(),
357+
ErrorReason = e.ResponseErrorReason?.ToString()
358+
};
359359

360360
if (e.ResponseHeaders != null)
361361
{

Diff for: dotnet/src/webdriver/HttpRequestData.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ public HttpRequestData()
3535
{
3636
}
3737

38-
internal HttpRequestData(string? method, string? url, string? postData, Dictionary<string, string>? headers, string? requestId)
39-
{
40-
this.Method = method;
41-
this.Url = url;
42-
this.PostData = postData;
43-
this.Headers = headers;
44-
this.RequestId = requestId;
45-
}
46-
4738
/// <summary>
4839
/// Gets the method of the HTTP request.
4940
/// </summary>
@@ -67,6 +58,6 @@ internal HttpRequestData(string? method, string? url, string? postData, Dictiona
6758
/// <summary>
6859
/// Gets the ID of the HTTP request.
6960
/// </summary>
70-
public string? RequestId { get; }
61+
public string? RequestId { get; internal set; }
7162
}
7263
}

Diff for: dotnet/src/webdriver/HttpResponseData.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,19 @@ public class HttpResponseData
3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="HttpResponseData"/> type.
3434
/// </summary>
35-
/// <param name="requestId">The ID of the request that generated this response.</param>
36-
/// <param name="url">The URL of the HTTP response.</param>
37-
/// <param name="resourceType">The type of resource for this response.</param>
38-
/// <param name="statusCode">The numeric status code of the HTTP response.</param>
39-
/// <param name="errorReason">The reason for an error response.</param>
40-
public HttpResponseData(string requestId, string url, string resourceType, long statusCode, string? errorReason)
35+
public HttpResponseData()
4136
{
42-
RequestId = requestId;
43-
Url = url;
44-
ResourceType = resourceType;
45-
StatusCode = statusCode;
46-
ErrorReason = errorReason;
4737
}
4838

4939
/// <summary>
5040
/// Gets or sets the ID of the request that generated this response.
5141
/// </summary>
52-
public string RequestId { get; set; }
42+
public string? RequestId { get; set; }
5343

5444
/// <summary>
5545
/// Gets or sets the URL of the HTTP response.
5646
/// </summary>
57-
public string Url { get; set; }
47+
public string? Url { get; set; }
5848

5949
/// <summary>
6050
/// Gets or sets the numeric status code of the HTTP response.
@@ -79,7 +69,7 @@ public string? Body
7969
/// <summary>
8070
/// Gets or sets the type of resource for this response.
8171
/// </summary>
82-
public string ResourceType { get; set; }
72+
public string? ResourceType { get; set; }
8373

8474
/// <summary>
8575
/// Gets or sets the reason for an error response.

Diff for: dotnet/src/webdriver/NetworkManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private async Task OnResponsePaused(object sender, ResponsePausedEventArgs e)
262262
// method demands one; however, the only property used by that method is the RequestId property.
263263
// It might be better to refactor that method signature to simply pass the request ID, or
264264
// alternatively, just pass the response data, which should also contain the request ID anyway.
265-
HttpRequestData requestData = new HttpRequestData(null, null, null, null, requestId: e.ResponseData.RequestId);
265+
HttpRequestData requestData = new HttpRequestData { RequestId = e.ResponseData.RequestId };
266266
await this.session.Value.Domains.Network.ContinueRequestWithResponse(requestData, handler.ResponseTransformer(e.ResponseData)).ConfigureAwait(false);
267267
return;
268268
}

Diff for: dotnet/src/webdriver/NetworkResponseReceivedEventArgs.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
5151
/// <summary>
5252
/// Gets the request ID of the network request that generated this response.
5353
/// </summary>
54-
public string RequestId { get; }
54+
public string? RequestId { get; }
5555

5656
/// <summary>
5757
/// Gets the URL of the network response.
5858
/// </summary>
59-
public string ResponseUrl { get; }
59+
public string? ResponseUrl { get; }
6060

6161
/// <summary>
6262
/// Gets the HTTP status code of the network response.
@@ -79,7 +79,7 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
7979
/// <summary>
8080
/// Gets the type of resource of the network response.
8181
/// </summary>
82-
public string ResponseResourceType { get; }
82+
public string? ResponseResourceType { get; }
8383

8484
/// <summary>
8585
/// Gets the headers associated with this network response.

Diff for: dotnet/test/common/NetworkInterceptionTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,32 @@ public async Task TestCanUseAuthorizationHandler()
8181
Assert.That(text, Is.EqualTo("authorized"));
8282
}
8383
}
84+
85+
[Test]
86+
[IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
87+
public async Task TransformNetworkResponse()
88+
{
89+
if (driver is IDevTools)
90+
{
91+
var handler = new NetworkResponseHandler()
92+
{
93+
ResponseMatcher = _ => true,
94+
ResponseTransformer = _ => new HttpResponseData
95+
{
96+
StatusCode = 200,
97+
Body = "Creamy, delicious cheese!"
98+
}
99+
};
100+
INetwork networkInterceptor = driver.Manage().Network;
101+
networkInterceptor.AddResponseHandler(handler);
102+
await networkInterceptor.StartMonitoring();
103+
104+
driver.Navigate().GoToUrl("https://www.selenium.dev");
105+
await networkInterceptor.StopMonitoring();
106+
107+
var body = driver.FindElement(By.TagName("body"));
108+
Assert.AreEqual("Creamy, delicious cheese!", body.Text);
109+
}
110+
}
84111
}
85112
}

0 commit comments

Comments
 (0)