Skip to content

Commit 0209bec

Browse files
authored
[dotnet] Fix network response data encoding (#13576)
* Fix network response data encoding * Fix network interception tests * Avoid unnecessary convertion * Rename to ReadAsByteArray
1 parent 2d177bc commit 0209bec

File tree

7 files changed

+101
-15
lines changed

7 files changed

+101
-15
lines changed

dotnet/src/webdriver/DevTools/v119/V119Network.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ public override async Task AddResponseBody(HttpResponseData responseData)
257257
{
258258
if (bodyResponse.Base64Encoded)
259259
{
260-
responseData.Body = Encoding.UTF8.GetString(Convert.FromBase64String(bodyResponse.Body));
260+
responseData.Content = new HttpResponseContent(Convert.FromBase64String(bodyResponse.Body));
261261
}
262262
else
263263
{
264-
responseData.Body = bodyResponse.Body;
264+
responseData.Content = new HttpResponseContent(bodyResponse.Body);
265265
}
266266
}
267267
}

dotnet/src/webdriver/DevTools/v120/V120Network.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ public override async Task AddResponseBody(HttpResponseData responseData)
257257
{
258258
if (bodyResponse.Base64Encoded)
259259
{
260-
responseData.Body = Encoding.UTF8.GetString(Convert.FromBase64String(bodyResponse.Body));
260+
responseData.Content = new HttpResponseContent(Convert.FromBase64String(bodyResponse.Body));
261261
}
262262
else
263263
{
264-
responseData.Body = bodyResponse.Body;
264+
responseData.Content = new HttpResponseContent(bodyResponse.Body);
265265
}
266266
}
267267
}

dotnet/src/webdriver/DevTools/v121/V121Network.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ public override async Task AddResponseBody(HttpResponseData responseData)
257257
{
258258
if (bodyResponse.Base64Encoded)
259259
{
260-
responseData.Body = Encoding.UTF8.GetString(Convert.FromBase64String(bodyResponse.Body));
260+
responseData.Content = new HttpResponseContent(Convert.FromBase64String(bodyResponse.Body));
261261
}
262262
else
263263
{
264-
responseData.Body = bodyResponse.Body;
264+
responseData.Content = new HttpResponseContent(bodyResponse.Body);
265265
}
266266
}
267267
}

dotnet/src/webdriver/DevTools/v85/V85Network.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ public override async Task AddResponseBody(HttpResponseData responseData)
255255
var bodyResponse = await fetch.GetResponseBody(new Fetch.GetResponseBodyCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
256256
if (bodyResponse.Base64Encoded)
257257
{
258-
responseData.Body = Encoding.UTF8.GetString(Convert.FromBase64String(bodyResponse.Body));
258+
responseData.Content = new HttpResponseContent(Convert.FromBase64String(bodyResponse.Body));
259259
}
260260
else
261261
{
262-
responseData.Body = bodyResponse.Body;
262+
responseData.Content = new HttpResponseContent(bodyResponse.Body);
263263
}
264264
}
265265
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <copyright file="HttpResponseContent.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System.Text;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Represents the content of an HTTP response.
25+
/// </summary>
26+
public class HttpResponseContent
27+
{
28+
private readonly byte[] content;
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="HttpResponseContent"/> class.
32+
/// </summary>
33+
/// <param name="content">The byte array representing the content of the response.</param>
34+
public HttpResponseContent(byte[] content)
35+
{
36+
this.content = content;
37+
}
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="HttpResponseContent"/> class.
41+
/// </summary>
42+
/// <param name="content">The UTF8 encoded string representing the content of the response.</param>
43+
public HttpResponseContent(string content)
44+
{
45+
this.content = Encoding.UTF8.GetBytes(content);
46+
}
47+
48+
/// <summary>
49+
/// Reads the content of the response as a UTF8 encoded string.
50+
/// </summary>
51+
/// <returns>The content of the response as a string.</returns>
52+
public string ReadAsString()
53+
{
54+
return Encoding.UTF8.GetString(content);
55+
}
56+
57+
/// <summary>
58+
/// Reads the content of the response as a byte array.
59+
/// </summary>
60+
/// <returns>The content of the response as a byte array.</returns>
61+
public byte[] ReadAsByteArray()
62+
{
63+
return content;
64+
}
65+
}
66+
}

dotnet/src/webdriver/HttpResponseData.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19-
using System;
2019
using System.Collections.Generic;
21-
using System.Data.Common;
22-
using System.Text;
2320

2421
namespace OpenQA.Selenium
2522
{
@@ -49,7 +46,22 @@ public class HttpResponseData
4946
/// <summary>
5047
/// Gets or sets the body of the HTTP response.
5148
/// </summary>
52-
public string Body { get; set; }
49+
public string Body
50+
{
51+
get
52+
{
53+
return this.Content?.ReadAsString();
54+
}
55+
set
56+
{
57+
this.Content = new HttpResponseContent(value);
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Gets or sets the content of the HTTP response.
63+
/// </summary>
64+
public HttpResponseContent Content { get; set; }
5365

5466
/// <summary>
5567
/// Gets or sets the type of resource for this response.

dotnet/src/webdriver/NetworkResponseReceivedEventArgs.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class NetworkResponseReceivedEventArgs : EventArgs
2929
private readonly string requestId;
3030
private readonly string responseUrl;
3131
private readonly long responseStatusCode;
32-
private readonly string responseBody;
32+
private readonly HttpResponseContent responseContent;
3333
private readonly string responseResourceType;
3434
private readonly Dictionary<string, string> responseHeaders = new Dictionary<string, string>();
3535

@@ -42,7 +42,7 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
4242
this.requestId = responseData.RequestId;
4343
this.responseUrl = responseData.Url;
4444
this.responseStatusCode = responseData.StatusCode;
45-
this.responseBody = responseData.Body;
45+
this.responseContent = responseData.Content;
4646
this.responseResourceType = responseData.ResourceType;
4747
foreach (KeyValuePair<string, string> header in responseData.Headers)
4848
{
@@ -68,7 +68,15 @@ public NetworkResponseReceivedEventArgs(HttpResponseData responseData)
6868
/// <summary>
6969
/// Gets the body of the network response.
7070
/// </summary>
71-
public string ResponseBody => this.responseBody;
71+
/// <remarks>
72+
/// This property is an alias for <see cref="ResponseContent"/>.ReadAsString() to keep backward compatibility.
73+
/// </remarks>
74+
public string ResponseBody => this.ResponseContent?.ReadAsString();
75+
76+
/// <summary>
77+
/// Gets the content of the network response.
78+
/// </summary>
79+
public HttpResponseContent ResponseContent => this.responseContent;
7280

7381
/// <summary>
7482
/// Gets the type of resource of the network response.

0 commit comments

Comments
 (0)