Skip to content

Commit e833288

Browse files
committed
[dotnet] Serialize command parameters directly to UTF-8
1 parent 1475d55 commit e833288

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

dotnet/src/webdriver/Command.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,29 @@ public string ParametersAsJsonString
101101
}
102102
}
103103

104+
/// <summary>
105+
/// Serializes the parameters of the comand to JSON as UTF-8 bytes.
106+
/// </summary>
107+
/// <returns></returns>
108+
public byte[] GetParametersAsUtf8Bytes()
109+
{
110+
if (this.Parameters != null && this.Parameters.Count > 0)
111+
{
112+
return JsonSerializer.SerializeToUtf8Bytes(this.Parameters, s_jsonSerializerOptions);
113+
}
114+
else
115+
{
116+
return "{}"u8.ToArray();
117+
}
118+
}
119+
104120
/// <summary>
105121
/// Returns a string of the Command object
106122
/// </summary>
107123
/// <returns>A string representation of the Command Object</returns>
108124
public override string ToString()
109125
{
110-
return string.Concat("[", this.SessionId, "]: ", this.Name, " ", this.ParametersAsJsonString);
126+
return $"[{this.SessionId}]: {this.Name} Parameters: {this.ParametersAsJsonString}";
111127
}
112128

113129
/// <summary>

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
280280
acceptHeader.CharSet = Utf8CharsetType;
281281
requestMessage.Headers.Accept.Add(acceptHeader);
282282

283-
byte[] bytes = Encoding.UTF8.GetBytes(requestInfo.RequestBody);
283+
byte[] bytes = requestInfo.RequestBody;
284284
requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);
285285

286286
MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
@@ -370,12 +370,12 @@ public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo
370370

371371
this.FullUri = commandInfo.CreateCommandUri(serverUri, commandToExecute);
372372
this.HttpMethod = commandInfo.Method;
373-
this.RequestBody = commandToExecute.ParametersAsJsonString;
373+
this.RequestBody = commandToExecute.GetParametersAsUtf8Bytes();
374374
}
375375

376376
public Uri FullUri { get; set; }
377377
public string HttpMethod { get; set; }
378-
public string RequestBody { get; set; }
378+
public byte[] RequestBody { get; set; }
379379
}
380380

381381
private class HttpResponseInfo

dotnet/src/webdriver/Remote/SendingRemoteHttpRequestEventArgs.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22+
using System.Text;
2223

2324
#nullable enable
2425

@@ -29,6 +30,8 @@ namespace OpenQA.Selenium.Remote
2930
/// </summary>
3031
public class SendingRemoteHttpRequestEventArgs : EventArgs
3132
{
33+
private string? _requestBody;
34+
private readonly byte[]? _utf8RequestBody;
3235
private readonly Dictionary<string, string> headers = new Dictionary<string, string>();
3336

3437
/// <summary>
@@ -42,7 +45,21 @@ public SendingRemoteHttpRequestEventArgs(string method, string fullUrl, string?
4245
{
4346
this.Method = method ?? throw new ArgumentNullException(nameof(method));
4447
this.FullUrl = fullUrl ?? throw new ArgumentNullException(nameof(fullUrl));
45-
this.RequestBody = requestBody;
48+
_utf8RequestBody = requestBody is null ? null : Encoding.UTF8.GetBytes(requestBody);
49+
}
50+
51+
/// <summary>
52+
/// Initializes a new instance of the <see cref="SendingRemoteHttpRequestEventArgs"/> class.
53+
/// </summary>
54+
/// <param name="method">The HTTP method of the request being sent.</param>
55+
/// <param name="fullUrl">The full URL of the request being sent.</param>
56+
/// <param name="requestBody">The body of the request.</param>
57+
/// <exception cref="ArgumentNullException">If <paramref name="method"/>, <paramref name="fullUrl"/> are null.</exception>
58+
public SendingRemoteHttpRequestEventArgs(string method, string fullUrl, byte[]? requestBody)
59+
{
60+
this.Method = method ?? throw new ArgumentNullException(nameof(method));
61+
this.FullUrl = fullUrl ?? throw new ArgumentNullException(nameof(fullUrl));
62+
_utf8RequestBody = requestBody;
4663
}
4764

4865
/// <summary>
@@ -58,7 +75,18 @@ public SendingRemoteHttpRequestEventArgs(string method, string fullUrl, string?
5875
/// <summary>
5976
/// Gets the body of the HTTP request as a string.
6077
/// </summary>
61-
public string? RequestBody { get; }
78+
public string? RequestBody
79+
{
80+
get
81+
{
82+
if (_requestBody is not null)
83+
{
84+
return _requestBody;
85+
}
86+
87+
return _requestBody = _utf8RequestBody is null ? null : Encoding.UTF8.GetString(_utf8RequestBody);
88+
}
89+
}
6290

6391
/// <summary>
6492
/// Gets a read-only dictionary of the headers of the HTTP request.

dotnet/test/common/CommandTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ namespace OpenQA.Selenium
2525
[TestFixture]
2626
public class CommandTests
2727
{
28+
[Test]
29+
public void CommandSerializesNullParameters()
30+
{
31+
var command = new Command(new SessionId("session"), "test command", parameters: null);
32+
33+
Assert.That(command.GetParametersAsUtf8Bytes(), Is.EqualTo("{}"u8.ToArray()));
34+
}
35+
36+
[Test]
37+
public void CommandSerializesEmptyParameters()
38+
{
39+
var command = new Command(new SessionId("session"), "test command", parameters: new Dictionary<string, object>());
40+
41+
Assert.That(command.GetParametersAsUtf8Bytes(), Is.EqualTo("{}"u8.ToArray()));
42+
}
43+
2844
[Test]
2945
public void CommandSerializesAnonymousType()
3046
{
@@ -35,7 +51,7 @@ public void CommandSerializesAnonymousType()
3551

3652
var command = new Command(new SessionId("session"), "test command", parameters);
3753

38-
Assert.That(command.ParametersAsJsonString, Is.EqualTo("""{"arg":{"param1":true,"param2":false}}"""));
54+
Assert.That(command.GetParametersAsUtf8Bytes(), Is.EqualTo("""{"arg":{"param1":true,"param2":false}}"""u8.ToArray()));
3955
}
4056
}
4157
}

0 commit comments

Comments
 (0)