mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 03:32:37 +00:00
DevTools - Support strongly typed events (#3669)
Includes various other DevTools fixes: - JavaScript "number"s are represented as doubles instead of longs - Headers are now correctly serialized / deserialized - DevToolsDomainErrorResponse is now correctly deserialized
This commit is contained in:
@@ -151,6 +151,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="BindingRedirectAssemblyResolver.cs" />
|
||||
<Compile Include="DevTools\DevToolsClientFacts.cs" />
|
||||
<Compile Include="DevTools\HeadersFacts.cs" />
|
||||
<Compile Include="Framework\BinderFacts.cs" />
|
||||
<Compile Include="Framework\AsyncExtensionFacts.cs" />
|
||||
<Compile Include="Framework\ConcurrentMethodRunnerQueueFacts.cs" />
|
||||
|
||||
@@ -248,5 +248,71 @@ namespace CefSharp.Test.DevTools
|
||||
Assert.Equal("89.0.4389.114", highEntropyValuesResult.uaFullVersion);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanSetExtraHTTPHeaders()
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false))
|
||||
{
|
||||
await browser.CreateBrowserAsync();
|
||||
|
||||
RequestWillBeSentEventArgs requestWillBeSentEventArgs = null;
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
var extraHeaders = new Headers();
|
||||
extraHeaders.SetCommaSeparatedValues("TEST", "0");
|
||||
extraHeaders.AppendCommaSeparatedValues("test", " 1 ", "\" 2 \"");
|
||||
extraHeaders.AppendCommaSeparatedValues("Test", " 2,5 ");
|
||||
|
||||
await devToolsClient.Network.SetExtraHTTPHeadersAsync(extraHeaders);
|
||||
|
||||
devToolsClient.Network.RequestWillBeSent += (sender, args) =>
|
||||
{
|
||||
if (requestWillBeSentEventArgs == null)
|
||||
{
|
||||
requestWillBeSentEventArgs = args;
|
||||
}
|
||||
};
|
||||
|
||||
// enable events
|
||||
await devToolsClient.Network.EnableAsync();
|
||||
|
||||
await browser.LoadUrlAsync("www.google.com");
|
||||
}
|
||||
|
||||
Assert.NotNull(requestWillBeSentEventArgs);
|
||||
Assert.NotEmpty(requestWillBeSentEventArgs.RequestId);
|
||||
Assert.NotEqual(0, requestWillBeSentEventArgs.Timestamp);
|
||||
Assert.NotEqual(0, requestWillBeSentEventArgs.WallTime);
|
||||
Assert.NotNull(requestWillBeSentEventArgs.Request);
|
||||
Assert.True(requestWillBeSentEventArgs.Request.Headers.TryGetValues("TeSt", out var values));
|
||||
Assert.Collection(values,
|
||||
v => Assert.Equal("0", v),
|
||||
v => Assert.Equal("1", v),
|
||||
v => Assert.Equal(" 2 ", v),
|
||||
v => Assert.Equal(" 2,5 ", v)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteDevToolsMethodThrowsExceptionWithInvalidMethod()
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
var ex = await Assert.ThrowsAsync<CefSharp.DevTools.DevToolsClientException>(
|
||||
() => devToolsClient.ExecuteDevToolsMethodAsync("methoddoesnotexist"));
|
||||
|
||||
Assert.NotNull(ex.Response);
|
||||
Assert.NotEqual(0, ex.Response.MessageId);
|
||||
Assert.NotEqual(0, ex.Response.Code);
|
||||
Assert.NotNull(ex.Response.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// Copyright © 2021 The CefSharp Authors. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using CefSharp.DevTools.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace CefSharp.Test.DevTools
|
||||
{
|
||||
public class HeadersFacts
|
||||
{
|
||||
[Fact]
|
||||
public void HeadersTryGetValues()
|
||||
{
|
||||
var headers = new Headers();
|
||||
headers["EMPTY"] = " ";
|
||||
headers["SIMPLE"] = " 1 ";
|
||||
headers["MULTIPLE"] = " 1 , \" 1,5 \",2, \" 2 , 5 \" , 3 , ,";
|
||||
|
||||
string[] values;
|
||||
|
||||
Assert.False(headers.TryGetValues("doesnotexist", out values));
|
||||
Assert.Null(values);
|
||||
|
||||
Assert.True(headers.TryGetValues("EMPTY", out values));
|
||||
Assert.Collection(values,
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
|
||||
Assert.True(headers.TryGetValues("SIMPLE", out values));
|
||||
Assert.Collection(values,
|
||||
v => Assert.Equal("1", v)
|
||||
);
|
||||
|
||||
Assert.True(headers.TryGetValues("MULTIPLE", out values));
|
||||
Assert.Collection(values,
|
||||
v => Assert.Equal("1", v),
|
||||
v => Assert.Equal(" 1,5 ", v),
|
||||
v => Assert.Equal("2", v),
|
||||
v => Assert.Equal(" 2 , 5 ", v),
|
||||
v => Assert.Equal("3", v),
|
||||
v => Assert.Equal("", v),
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HeadersGetCommaSeparatedValues()
|
||||
{
|
||||
var headers = new Headers();
|
||||
headers["EMPTY"] = " ";
|
||||
headers["SIMPLE"] = " 1 ";
|
||||
headers["MULTIPLE"] = " 1 , \" 1,5 \",2, \" 2 , 5 \" , 3 , ,";
|
||||
|
||||
Assert.Null(headers.GetCommaSeparatedValues("doesnotexist"));
|
||||
Assert.Collection(headers.GetCommaSeparatedValues("EMPTY"),
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
Assert.Collection(headers.GetCommaSeparatedValues("SIMPLE"),
|
||||
v => Assert.Equal("1", v)
|
||||
);
|
||||
Assert.Collection(headers.GetCommaSeparatedValues("MULTIPLE"),
|
||||
v => Assert.Equal("1", v),
|
||||
v => Assert.Equal(" 1,5 ", v),
|
||||
v => Assert.Equal("2", v),
|
||||
v => Assert.Equal(" 2 , 5 ", v),
|
||||
v => Assert.Equal("3", v),
|
||||
v => Assert.Equal("", v),
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HeadersAppendCommaSeparatedValues()
|
||||
{
|
||||
var headers = new Headers();
|
||||
headers.AppendCommaSeparatedValues("TEST", " 1 ");
|
||||
|
||||
headers.AppendCommaSeparatedValues("test", "\" 1,5 \"", "2", " 2 , 5 ", "3", " ", "");
|
||||
|
||||
Assert.Collection(headers.GetCommaSeparatedValues("Test"),
|
||||
v => Assert.Equal("1", v),
|
||||
v => Assert.Equal(" 1,5 ", v),
|
||||
v => Assert.Equal("2", v),
|
||||
v => Assert.Equal(" 2 , 5 ", v),
|
||||
v => Assert.Equal("3", v),
|
||||
v => Assert.Equal("", v),
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HeadersSetCommaSeparatedValues()
|
||||
{
|
||||
var headers = new Headers();
|
||||
headers["TEST"] = " 1 ";
|
||||
|
||||
headers.SetCommaSeparatedValues("test", "\" 1,5 \"", "2", " 2 , 5 ", "3", " ", "");
|
||||
|
||||
Assert.Collection(headers.GetCommaSeparatedValues("Test"),
|
||||
v => Assert.Equal(" 1,5 ", v),
|
||||
v => Assert.Equal("2", v),
|
||||
v => Assert.Equal(" 2 , 5 ", v),
|
||||
v => Assert.Equal("3", v),
|
||||
v => Assert.Equal("", v),
|
||||
v => Assert.Equal("", v)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,7 @@
|
||||
<Compile Include="DevTools\DevToolsDomainBase.cs" />
|
||||
<Compile Include="DevTools\DevToolsDomainEntityBase.cs" />
|
||||
<Compile Include="DevTools\DevToolsDomainErrorResponse.cs" />
|
||||
<Compile Include="DevTools\DevToolsDomainEventArgsBase.cs" />
|
||||
<Compile Include="DevTools\DevToolsDomainResponseBase.cs" />
|
||||
<Compile Include="DevTools\DevToolsEventArgs.cs" />
|
||||
<Compile Include="DevTools\DevToolsMethodResponse.cs" />
|
||||
|
||||
+11002
-1146
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -34,7 +34,7 @@ namespace CefSharp.DevTools
|
||||
/// <summary>
|
||||
/// DevToolsEvent
|
||||
/// </summary>
|
||||
public EventHandler<DevToolsEventArgs> DevToolsEvent;
|
||||
public event EventHandler<DevToolsEventArgs> DevToolsEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Capture the current <see cref="SynchronizationContext"/> so
|
||||
@@ -226,7 +226,7 @@ namespace CefSharp.DevTools
|
||||
{
|
||||
execute = () =>
|
||||
{
|
||||
var errorObj = methodResult.DeserializeJson<DevToolsDomainErrorResponse>();
|
||||
var errorObj = methodResult.DeserializeJson<DevToolsDomainErrorResponse>(ignoreSuccess: true);
|
||||
errorObj.MessageId = messageId;
|
||||
|
||||
//Make sure continuation runs on Thread Pool
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace CefSharp.DevTools
|
||||
/// Error Message parsed from JSON
|
||||
/// e.g. {"code":-32601,"message":"'Browser.getWindowForTarget' wasn't found"}
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public class DevToolsDomainErrorResponse
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright © 2021 The CefSharp Authors. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace CefSharp.DevTools
|
||||
{
|
||||
[DataContract]
|
||||
public abstract class DevToolsDomainEventArgsBase : EventArgs
|
||||
{
|
||||
#if !NETCOREAPP
|
||||
public static object StringToEnum(Type enumType, string input)
|
||||
{
|
||||
return DevToolsDomainEntityBase.StringToEnum(enumType, input);
|
||||
}
|
||||
|
||||
public static string EnumToString(Enum e)
|
||||
{
|
||||
return DevToolsDomainEntityBase.EnumToString(e);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,11 @@
|
||||
|
||||
|
||||
using System;
|
||||
#if !NETCOREAPP
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
#endif
|
||||
|
||||
namespace CefSharp.DevTools
|
||||
{
|
||||
@@ -13,7 +18,7 @@ namespace CefSharp.DevTools
|
||||
public class DevToolsEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Method
|
||||
/// Event Name
|
||||
/// </summary>
|
||||
public string EventName { get; private set; }
|
||||
|
||||
@@ -27,5 +32,30 @@ namespace CefSharp.DevTools
|
||||
EventName = eventName;
|
||||
ParametersAsJsonString = paramsAsJsonString;
|
||||
}
|
||||
|
||||
internal T DeserializeJson<T>()
|
||||
{
|
||||
#if NETCOREAPP
|
||||
var options = new System.Text.Json.JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
IgnoreNullValues = true,
|
||||
};
|
||||
|
||||
options.Converters.Add(new Internals.Json.JsonEnumConverterFactory());
|
||||
|
||||
return System.Text.Json.JsonSerializer.Deserialize<T>(ParametersAsJsonString, options);
|
||||
#else
|
||||
var bytes = Encoding.UTF8.GetBytes(ParametersAsJsonString);
|
||||
using (var ms = new MemoryStream(bytes))
|
||||
{
|
||||
var settings = new DataContractJsonSerializerSettings();
|
||||
settings.UseSimpleDictionaryFormat = true;
|
||||
|
||||
var dcs = new DataContractJsonSerializer(typeof(T), settings);
|
||||
return (T)dcs.ReadObject(ms);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ namespace CefSharp.DevTools
|
||||
/// </summary>
|
||||
public string ResponseAsJsonString { get; set; }
|
||||
|
||||
internal T DeserializeJson<T>()
|
||||
internal T DeserializeJson<T>(bool ignoreSuccess = false)
|
||||
{
|
||||
if (Success)
|
||||
if (Success || ignoreSuccess)
|
||||
{
|
||||
#if NETCOREAPP
|
||||
var options = new System.Text.Json.JsonSerializerOptions
|
||||
@@ -48,7 +48,10 @@ namespace CefSharp.DevTools
|
||||
var bytes = Encoding.UTF8.GetBytes(ResponseAsJsonString);
|
||||
using (var ms = new MemoryStream(bytes))
|
||||
{
|
||||
var dcs = new DataContractJsonSerializer(typeof(T));
|
||||
var settings = new DataContractJsonSerializerSettings();
|
||||
settings.UseSimpleDictionaryFormat = true;
|
||||
|
||||
var dcs = new DataContractJsonSerializer(typeof(T), settings);
|
||||
return (T)dcs.ReadObject(ms);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2,16 +2,160 @@
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System.Collections.Specialized;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace CefSharp.DevTools.Network
|
||||
{
|
||||
//TODO: Properly implement this type
|
||||
public class Headers : NameValueCollection
|
||||
/// <summary>
|
||||
/// Request / response headers as keys / values of JSON object.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// CDP uses comma seperated values to store multiple header values.
|
||||
/// Use <see cref="TryGetValues(string, out string[])"/> or <see cref="GetCommaSeparatedValues(string)"/> to get a string[]
|
||||
/// for headers that have multiple values.
|
||||
/// </remarks>
|
||||
/// Helper methods for dealing with comma separated header values based on https://github.com/dotnet/aspnetcore/blob/52eff90fbcfca39b7eb58baad597df6a99a542b0/src/Http/Http.Abstractions/src/Extensions/HeaderDictionaryExtensions.cs
|
||||
public class Headers : Dictionary<string, string>
|
||||
{
|
||||
public NameValueCollection ToDictionary()
|
||||
public Headers() : base(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
}
|
||||
|
||||
public Dictionary<string, string> ToDictionary()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an array of values for the specified key. Values are comma seperated and will be split into a string[].
|
||||
/// Quoted values will not be split, and the quotes will be removed.
|
||||
/// </summary>
|
||||
/// <param name="key">The header name.</param>
|
||||
/// <param name="values">the associated values from the dictionary separated into individual values, or null if the key is not present.</param>
|
||||
/// <returns>true if the Dictionary contains an element with the specified key; otherwise, false.</returns>
|
||||
public bool TryGetValues(string key, out string[] values)
|
||||
{
|
||||
values = null;
|
||||
string value;
|
||||
|
||||
if (TryGetValue(key, out value))
|
||||
{
|
||||
var list = new List<string>();
|
||||
|
||||
var valueStartIndex = -1;
|
||||
var valueEndIndex = -1;
|
||||
var inQuote = false;
|
||||
for (var i = 0; i < value.Length; i++)
|
||||
{
|
||||
var c = value[i];
|
||||
|
||||
if (c == '\"')
|
||||
{
|
||||
inQuote = !inQuote;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inQuote && char.IsWhiteSpace(c))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (valueStartIndex == -1)
|
||||
{
|
||||
valueStartIndex = i;
|
||||
}
|
||||
|
||||
if (!inQuote && c == ',')
|
||||
{
|
||||
if (valueEndIndex == -1)
|
||||
{
|
||||
list.Add(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(value.Substring(valueStartIndex, valueEndIndex + 1 - valueStartIndex));
|
||||
}
|
||||
valueStartIndex = -1;
|
||||
valueEndIndex = -1;
|
||||
continue;
|
||||
}
|
||||
|
||||
valueEndIndex = i;
|
||||
}
|
||||
if (valueEndIndex == -1)
|
||||
{
|
||||
list.Add(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(value.Substring(valueStartIndex, valueEndIndex + 1 - valueStartIndex));
|
||||
}
|
||||
|
||||
values = list.ToArray();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the associated values from the dictionary separated into individual values.
|
||||
/// Quoted values will not be split, and the quotes will be removed.
|
||||
/// </summary>
|
||||
/// <param name="key">The header name.</param>
|
||||
/// <returns>the associated values from the dictionary separated into individual values, or null if the key is not present.</returns>
|
||||
public string[] GetCommaSeparatedValues(string key)
|
||||
{
|
||||
string[] values;
|
||||
|
||||
if (TryGetValues(key, out values))
|
||||
{
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Quotes any values containing commas, and then comma joins all of the values with any existing values.
|
||||
/// </summary>
|
||||
/// <param name="key">The header name.</param>
|
||||
/// <param name="values">The header values.</param>
|
||||
public void AppendCommaSeparatedValues(string key, params string[] values)
|
||||
{
|
||||
if (TryGetValue(key, out var existingValue))
|
||||
{
|
||||
this[key] = existingValue + "," + string.Join(",", values.Select(QuoteIfNeeded));
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCommaSeparatedValues(key, values);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Quotes any values containing commas, and then comma joins all of the values.
|
||||
/// </summary>
|
||||
/// <param name="key">The header name.</param>
|
||||
/// <param name="values">The header values.</param>
|
||||
public void SetCommaSeparatedValues(string key, params string[] values)
|
||||
{
|
||||
this[key] = string.Join(",", values.Select(QuoteIfNeeded));
|
||||
}
|
||||
|
||||
private static string QuoteIfNeeded(string value)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value) &&
|
||||
value.Contains(',') &&
|
||||
(value[0] != '"' || value[value.Length - 1] != '"'))
|
||||
{
|
||||
return "\"" + value + "\"";
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -12,6 +13,11 @@ namespace CefSharp.DevTools
|
||||
/// </summary>
|
||||
public interface IDevToolsClient
|
||||
{
|
||||
/// <summary>
|
||||
/// DevToolsEvent
|
||||
/// </summary>
|
||||
event EventHandler<DevToolsEventArgs> DevToolsEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Execute a method call over the DevTools protocol. This method can be called on any thread.
|
||||
/// See the DevTools protocol documentation at https://chromedevtools.github.io/devtools-protocol/ for details
|
||||
|
||||
Reference in New Issue
Block a user