Le prove cadevano a ogni carico della CI, ogni volta prove diverse. Dietro alcune c'erano difetti veri: FebeDatabase lasciava i file aperti dopo DisposeAsync (comandi SQLite non chiusi, pool di lettura chiuso dopo lo scrittore), e il WAL spariva durante lo spostamento della cartella Dati; la scheda dell'account senza statistiche leggeva i file di tutta la libreria; l'arresto di FebeApp non aspettava i lavori aggiunti da un avvio in corso. Chromium ha 120 s per avviarsi e aprire la prima scheda, e il ricollegamento smette appena il processo cambia. Nelle prove i limiti di tempo diventano protezioni contro un blocco: attese per condizione con un aiuto comune, limiti per classe nella prova di scala con il tempo migliore, porta 0 e stderr di Chromium nel browser delle prove, e la prova di «Scollega» di WebView2 controlla che il processo esca davvero. Provato su Windows con tutti i core occupati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
478 lines
27 KiB
C#
478 lines
27 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Febe.Browser.Capture;
|
|
using Febe.Browser.Download;
|
|
using Febe.Browser.Hosting;
|
|
using Febe.Browser.RemoteView;
|
|
using Febe.Core.Collection;
|
|
using Febe.Core.Collection.Readers;
|
|
using Febe.Core.Collection.Recipes;
|
|
using Febe.Tests.Browser.TestSupport;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Browser;
|
|
|
|
[TestClass]
|
|
public sealed class CaptureUnitTests
|
|
{
|
|
private static readonly HostClassifier Hosts = new(["127.0.0.1:5000"], ["127.0.0.1:5001"]);
|
|
|
|
[TestMethod]
|
|
public async Task JsonBodiesReachConsumersWithASanitizedDescriptor()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, p) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "r1" => new JsonObject { ["body"] = """{"a":1}""", ["base64Encoded"] = false },
|
|
"Network.getResponseBody" => new JsonObject { ["body"] = Convert.ToBase64String(Encoding.UTF8.GetBytes("""{"b":[2]}""")), ["base64Encoded"] = true },
|
|
_ => null,
|
|
};
|
|
var consumer = new CollectingConsumer();
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddConsumer(consumer);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
Assert.IsTrue(cdp.Sent.Any(c => c.Method == "Network.enable"));
|
|
cdp.Raise("Network.responseReceived", Received("r1", "http://127.0.0.1:5000/api/dati.json?token=segreto", 200, "application/json; charset=utf-8"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r1", ["encodedDataLength"] = 7 });
|
|
cdp.Raise("Network.responseReceived", Received("r2", "http://127.0.0.1:5000/api/b", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r2", ["encodedDataLength"] = 9 });
|
|
cdp.Raise("Network.responseReceived", Received("r3", "http://127.0.0.1:5001/foto.jpg", 200, "image/jpeg"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r3", ["encodedDataLength"] = 10 });
|
|
cdp.Raise("Network.responseReceived", Received("r4", "http://127.0.0.1:5000/troppe", 429, "application/json"));
|
|
|
|
await WaitUntilAsync(() => consumer.Items.Count == 2);
|
|
var first = consumer.Items.Single(i => i.Descriptor.RequestId == "r1");
|
|
Assert.AreEqual("http://127.0.0.1:5000/api/dati.json", first.Descriptor.Url, "No query string.");
|
|
Assert.AreEqual(HostKind.Site, first.Descriptor.HostKind);
|
|
Assert.AreEqual("application/json", first.Descriptor.MimeType);
|
|
Assert.AreEqual("""{"a":1}""", first.Json);
|
|
Assert.AreEqual("""{"b":[2]}""", consumer.Items.Single(i => i.Descriptor.RequestId == "r2").Json, "Base64 bodies are decoded.");
|
|
|
|
Assert.AreEqual(4, observer.Responses.Count, "Observers see every response, bodies or not.");
|
|
Assert.AreEqual(HostKind.Cdn, observer.Responses.Single(r => r.RequestId == "r3").HostKind);
|
|
Assert.AreEqual(429, observer.Responses.Single(r => r.RequestId == "r4").Status);
|
|
Assert.AreEqual(2, cdp.Sent.Count(c => c.Method == "Network.getResponseBody"), "Only JSON bodies are fetched.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task JavaScriptAnswersOfThePageAreDataButScriptFilesAreNot()
|
|
{
|
|
// Instagram serves its GraphQL data as «text/javascript» (HAR of 21/10/2026).
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, p) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "g1" => new JsonObject { ["body"] = """{"data":{"x":[1]}}""", ["base64Encoded"] = false },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "a1" => new JsonObject { ["body"] = """for (;;);{"payload":{}}""", ["base64Encoded"] = false },
|
|
"Network.getResponseBody" => new JsonObject { ["body"] = "var x = 1;", ["base64Encoded"] = false },
|
|
_ => null,
|
|
};
|
|
var consumer = new CollectingConsumer();
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddConsumer(consumer);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
cdp.Raise("Network.responseReceived", Received("g1", "http://127.0.0.1:5000/graphql/query", 200, "text/javascript; charset=utf-8", "XHR"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "g1", ["encodedDataLength"] = 20 });
|
|
cdp.Raise("Network.responseReceived", Received("s1", "http://127.0.0.1:5001/bundle.js", 200, "text/javascript", "Script"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "s1", ["encodedDataLength"] = 10 });
|
|
cdp.Raise("Network.responseReceived", Received("a1", "http://127.0.0.1:5000/ajax/navigation/", 200, "text/javascript", "XHR"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "a1", ["encodedDataLength"] = 24 });
|
|
|
|
await WaitUntilAsync(() => consumer.Items.Count == 1 && observer.Dropped.Contains("a1"));
|
|
Assert.AreEqual("""{"data":{"x":[1]}}""", consumer.Items.Single().Json);
|
|
Assert.AreEqual("XHR", consumer.Items.Single().Descriptor.ResourceType);
|
|
Assert.IsFalse(cdp.Sent.Any(c => c.Method == "Network.getResponseBody" && c.Parameters?["requestId"]?.GetValue<string>() == "s1"), "A script file is never fetched.");
|
|
Assert.AreEqual(1, tap.Stats.ParseErrors, "«for (;;);» is not JSON: skipped, and its waiters are told.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task EveryJsonBlockOfThePageDocumentIsAnAnswerOfItsOwn()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
var tooDeep = new string('[', 300) + new string(']', 300);
|
|
var html = $$"""
|
|
<html><head><script type="application/json" data-sjs>{"a":1}</script><script>var b = 2;</script>
|
|
<script type="application/json">{ non è json </script><script type="application/json">{{tooDeep}}</script>
|
|
<script type="application/json"> </script></head>
|
|
<body><script type="application/json">{"c":[3]}</script></body></html>
|
|
""";
|
|
cdp.Responder = (method, p) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "L7" => new JsonObject { ["body"] = html, ["base64Encoded"] = false },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "r10" => new JsonObject { ["body"] = """{"d":4}""", ["base64Encoded"] = false },
|
|
"Network.getResponseBody" => new JsonObject { ["body"] = "<html><body>niente</body></html>", ["base64Encoded"] = false },
|
|
_ => null,
|
|
};
|
|
var failing = new FailingConsumer();
|
|
var consumer = new CollectingConsumer();
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddConsumer(failing);
|
|
tap.AddConsumer(consumer);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
// The frame commits the new document before its body has finished loading: the body is still read.
|
|
cdp.Raise("Network.responseReceived", Document("L7", "http://127.0.0.1:5000/marta.prova/"));
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["loaderId"] = "L7", ["url"] = "http://127.0.0.1:5000/marta.prova/" } });
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L7", ["encodedDataLength"] = html.Length });
|
|
|
|
// A document without embedded JSON gives nothing; one of another host is not even fetched. A data answer after
|
|
// them: the worker reads in order, so once it has arrived the documents have been dealt with.
|
|
cdp.Raise("Network.responseReceived", Document("L8", "http://127.0.0.1:5000/vuota/"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L8", ["encodedDataLength"] = 30 });
|
|
cdp.Raise("Network.responseReceived", Document("L9", "https://esterno.example/"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L9", ["encodedDataLength"] = 30 });
|
|
cdp.Raise("Network.responseReceived", Received("r10", "http://127.0.0.1:5000/api/dopo", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r10", ["encodedDataLength"] = 7 });
|
|
await WaitUntilAsync(() => consumer.Items.Any(i => i.Descriptor.RequestId == "r10"));
|
|
|
|
// One answer per valid block, in the order of the document, each with the document's descriptor: a block that
|
|
// is not JSON, is too deep or is blank is left out without being a parse error.
|
|
var blocks = consumer.Items.Where(i => i.Descriptor.RequestId == "L7").ToList();
|
|
CollectionAssert.AreEqual(new[] { """{"a":1}""", """{"c":[3]}""" }, blocks.Select(b => b.Json).ToArray());
|
|
Assert.IsTrue(blocks.All(b => b.Descriptor.IsMainFrameDocument && b.Descriptor.MimeType == "text/html" && b.Descriptor.Path == "/marta.prova/"));
|
|
Assert.AreEqual(0, tap.Stats.ParseErrors);
|
|
Assert.AreEqual(3, tap.Stats.JsonBodies, "Two blocks and the answer after them.");
|
|
|
|
// A reader that fails on a block stops neither the other readers, nor the next block, nor the next answer.
|
|
Assert.AreEqual(3, failing.Calls);
|
|
|
|
// Those who wait for a body are told once when nothing reached the readers, never when blocks did.
|
|
Assert.IsFalse(observer.Dropped.Contains("L7"));
|
|
Assert.AreEqual(1, observer.Dropped.Count(id => id == "L8"));
|
|
Assert.IsFalse(cdp.Sent.Any(c => c.Method == "Network.getResponseBody" && c.Parameters?["requestId"]?.GetValue<string>() == "L9"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ThePaginationOfAnEmbeddedBlockStaysWithTheBlockThatCarriesIt()
|
|
{
|
|
// Two real answers (cleaned samples of the HAR of 21/10/2026) embedded in one page document: first the
|
|
// collections, which say «has_next_page: true» of themselves, then a post, which says «more_available: false»
|
|
// of itself. Read as one answer, the post took the first pagination of the document, the collections'.
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
var html = $"""
|
|
<!DOCTYPE html><html><head><script type="application/json">{SampleBody("raccolta-in-evidenza")}</script></head>
|
|
<body><script type="application/json">{SampleBody("post-video")}</script></body></html>
|
|
""";
|
|
cdp.Responder = (method, p) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "L7" => new JsonObject { ["body"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(html)), ["base64Encoded"] = true },
|
|
"Network.getResponseBody" => new JsonObject { ["body"] = """{"segni":[1]}""", ["base64Encoded"] = false },
|
|
_ => null,
|
|
};
|
|
var capture = new CaptureCollector(new ShapeReader(ReaderVocabulary.LoadInstagram()), clock);
|
|
var after = new CollectingConsumer();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddConsumer(capture);
|
|
tap.AddObserver(capture);
|
|
tap.AddConsumer(after);
|
|
await tap.StartAsync();
|
|
|
|
cdp.Raise("Network.responseReceived", Document("L7", "http://127.0.0.1:5000/marta.prova/"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L7", ["encodedDataLength"] = html.Length });
|
|
cdp.Raise("Network.responseReceived", Received("r2", "http://127.0.0.1:5000/api/dopo", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r2", ["encodedDataLength"] = 7 });
|
|
await WaitUntilAsync(() => after.Items.Any(i => i.Descriptor.RequestId == "r2"));
|
|
|
|
var reads = capture.Since(0);
|
|
Assert.AreEqual(2, reads.Count, "One read per block (the answer after them is not about contents).");
|
|
Assert.IsTrue(reads.All(r => r.Response.RequestId == "L7" && r.Response.IsMainFrameDocument));
|
|
var collections = reads[0].Result;
|
|
Assert.AreEqual(2, collections.Highlights.Count);
|
|
Assert.AreEqual(true, collections.Page?.HasMore);
|
|
Assert.IsFalse(string.IsNullOrEmpty(collections.Page?.Cursor));
|
|
var post = reads[1].Result;
|
|
Assert.AreEqual(0, post.Highlights.Count);
|
|
Assert.AreEqual(1, post.Contents.Count);
|
|
Assert.AreEqual(false, post.Page?.HasMore, "The post's own pagination, not the collections'.");
|
|
Assert.IsNull(post.Page?.Cursor);
|
|
Assert.AreEqual(0, capture.Pending, "Nothing is left on its way.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BodiesThatWillNotComeAreReportedToObservers()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, _) => method == "Page.getFrameTree"
|
|
? new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } }
|
|
: null;
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock, options: new ResponseTapOptions { MaxBodyBytes = 100 });
|
|
tap.AddConsumer(new CollectingConsumer());
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
cdp.Raise("Network.responseReceived", Received("big", "http://127.0.0.1:5000/a", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "big", ["encodedDataLength"] = 1000 });
|
|
cdp.Raise("Network.responseReceived", Received("fail", "http://127.0.0.1:5000/b", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFailed", new JsonObject { ["requestId"] = "fail" });
|
|
cdp.Raise("Network.responseReceived", Received("old", "http://127.0.0.1:5000/c", 200, "application/json"));
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["loaderId"] = "L2", ["url"] = "http://127.0.0.1:5000/altra/" } });
|
|
|
|
CollectionAssert.AreEquivalent(new[] { "big", "fail", "old" }, observer.Dropped.ToArray());
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MainFrameNavigationsAreReported()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, _) => method == "Page.getFrameTree"
|
|
? new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } }
|
|
: null;
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "http://127.0.0.1:5000/verifica?da=x" } });
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F2", ["parentId"] = "F1", ["url"] = "http://127.0.0.1:5000/iframe" } });
|
|
|
|
var navigation = observer.Navigations.Single();
|
|
Assert.AreEqual("/verifica", navigation.Path);
|
|
Assert.AreEqual("http://127.0.0.1:5000/verifica", navigation.Url);
|
|
Assert.IsFalse(navigation.SameDocument);
|
|
|
|
// A single-page route change (history.pushState) is a navigation too.
|
|
cdp.Raise("Page.navigatedWithinDocument", new JsonObject { ["frameId"] = "F1", ["url"] = "http://127.0.0.1:5000/accesso/?next=x" });
|
|
cdp.Raise("Page.navigatedWithinDocument", new JsonObject { ["frameId"] = "F2", ["url"] = "http://127.0.0.1:5000/iframe-route" });
|
|
Assert.AreEqual(2, observer.Navigations.Count);
|
|
var route = observer.Navigations.Last();
|
|
Assert.IsTrue(route.SameDocument);
|
|
Assert.AreEqual("/accesso/", route.Path);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherStreamsChunksAndClosesTheHandle()
|
|
{
|
|
var payload = RandomNumberGenerator.GetBytes(300_000);
|
|
var offset = 0;
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, p) =>
|
|
{
|
|
switch (method)
|
|
{
|
|
case "Page.getFrameTree":
|
|
return new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "http://127.0.0.1:5000/" } } };
|
|
case "Network.loadNetworkResource":
|
|
Assert.AreEqual("F1", p!["frameId"]!.GetValue<string>());
|
|
return new JsonObject { ["resource"] = new JsonObject { ["success"] = true, ["httpStatusCode"] = 200, ["stream"] = "S1" } };
|
|
case "IO.read":
|
|
var size = p!["size"]!.GetValue<int>();
|
|
var take = Math.Min(size, payload.Length - offset);
|
|
var chunk = Convert.ToBase64String(payload, offset, take);
|
|
offset += take;
|
|
return new JsonObject { ["base64Encoded"] = true, ["data"] = chunk, ["eof"] = offset >= payload.Length };
|
|
default:
|
|
return null;
|
|
}
|
|
};
|
|
|
|
var fetcher = new BrowserFileFetcher(cdp, Hosts, options: new BrowserFetchOptions { ChunkBytes = 64 * 1024 });
|
|
using var destination = new MemoryStream();
|
|
var progress = new List<long>();
|
|
var result = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5001/f.bin"), destination, new FetchOptions(Progress: new SyncProgress(progress)), CancellationToken.None);
|
|
|
|
Assert.AreEqual(FetchOutcome.Completed, result.Outcome);
|
|
Assert.AreEqual(payload.Length, result.Bytes);
|
|
CollectionAssert.AreEqual(payload, destination.ToArray());
|
|
Assert.AreEqual(5, cdp.Sent.Count(c => c.Method == "IO.read"));
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "IO.close"));
|
|
Assert.AreEqual(payload.Length, progress[^1]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherRefusesTheSiteAndStopsAtTheSizeLimit()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, _) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1" } } },
|
|
"Network.loadNetworkResource" => new JsonObject { ["resource"] = new JsonObject { ["success"] = true, ["httpStatusCode"] = 200, ["stream"] = "S1" } },
|
|
"IO.read" => new JsonObject { ["data"] = Convert.ToBase64String(new byte[10_000]), ["base64Encoded"] = true, ["eof"] = false },
|
|
_ => null,
|
|
};
|
|
var fetcher = new BrowserFileFetcher(cdp, Hosts);
|
|
|
|
var refused = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5000/api"), Stream.Null, null, CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.HostNotAllowed, refused.Outcome);
|
|
Assert.AreEqual(0, cdp.Sent.Count, "Nothing is sent for an address outside the CDN.");
|
|
|
|
using var destination = new MemoryStream();
|
|
var tooLarge = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5001/big"), destination, new FetchOptions(MaxBytes: 25_000), CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.TooLarge, tooLarge.Outcome);
|
|
Assert.AreEqual(25_000, destination.Length);
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "IO.close"), "The stream is closed even when stopping early.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherReportsHttpErrors()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, _) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1" } } },
|
|
"Network.loadNetworkResource" => new JsonObject { ["resource"] = new JsonObject { ["success"] = false, ["httpStatusCode"] = 403, ["netErrorName"] = "net::ERR_HTTP_RESPONSE_CODE_FAILURE" } },
|
|
_ => null,
|
|
};
|
|
var result = await new BrowserFileFetcher(cdp, Hosts).FetchAsync(new Uri("http://127.0.0.1:5001/scaduto.jpg"), Stream.Null, null, CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.HttpError, result.Outcome);
|
|
Assert.AreEqual(403, result.HttpStatus);
|
|
Assert.IsFalse(result.Error!.Contains("5001", StringComparison.Ordinal), "Errors never carry the address.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ScreencastAcknowledgesEveryFrameAndDeliversIt()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
await using var screencast = new ScreencastSession(cdp);
|
|
await screencast.StartAsync(new ScreencastOptions(Quality: 50, MaxWidth: 800, MaxHeight: 600, EveryNthFrame: 2));
|
|
|
|
var start = cdp.Sent.Single(c => c.Method == "Page.startScreencast").Parameters!;
|
|
Assert.AreEqual("jpeg", start["format"]!.GetValue<string>());
|
|
Assert.AreEqual(2, start["everyNthFrame"]!.GetValue<int>());
|
|
|
|
var jpeg = new byte[] { 0xFF, 0xD8, 0xFF, 0xD9 };
|
|
cdp.Raise("Page.screencastFrame", new JsonObject
|
|
{
|
|
["sessionId"] = 7,
|
|
["data"] = Convert.ToBase64String(jpeg),
|
|
["metadata"] = new JsonObject { ["deviceWidth"] = 1280, ["deviceHeight"] = 900, ["pageScaleFactor"] = 1, ["offsetTop"] = 0, ["scrollOffsetX"] = 0, ["scrollOffsetY"] = 120 },
|
|
});
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
|
await using var frames = screencast.ReadFramesAsync(cts.Token).GetAsyncEnumerator(cts.Token);
|
|
Assert.IsTrue(await frames.MoveNextAsync());
|
|
CollectionAssert.AreEqual(jpeg, frames.Current.Jpeg);
|
|
Assert.AreEqual(120, frames.Current.ScrollOffsetY);
|
|
await WaitUntilAsync(() => cdp.Sent.Any(c => c.Method == "Page.screencastFrameAck" && c.Parameters!["sessionId"]!.GetValue<int>() == 7));
|
|
|
|
await screencast.StopAsync();
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "Page.stopScreencast"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task RemoteViewHoldsTheDrivingLeaseAndMapsCoordinates()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
var gate = new UserDrivingGate();
|
|
await using var view = new RemoteViewSession(cdp, gate);
|
|
await Assert.ThrowsExactlyAsync<InvalidOperationException>(() => view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Move, 0.5, 0.5)));
|
|
|
|
await view.StartAsync();
|
|
Assert.IsTrue(gate.IsDriving, "The engine stays still while Alberto uses the remote view.");
|
|
cdp.Raise("Page.screencastFrame", new JsonObject
|
|
{
|
|
["sessionId"] = 1,
|
|
["data"] = Convert.ToBase64String([1, 2, 3]),
|
|
["metadata"] = new JsonObject { ["deviceWidth"] = 1000, ["deviceHeight"] = 800, ["pageScaleFactor"] = 1 },
|
|
});
|
|
|
|
await view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Down, 0.25, 0.5, RemoteMouseButton.Left));
|
|
var press = cdp.Mouse("mousePressed").Single();
|
|
Assert.AreEqual(250, press.X);
|
|
Assert.AreEqual(400, press.Y);
|
|
Assert.AreEqual(1, press.Parameters!["buttons"]!.GetValue<int>());
|
|
|
|
await view.KeyAsync(new RemoteKeyEvent(true, Febe.Browser.Gestures.Keyboard.ForText("a")));
|
|
Assert.AreEqual("keyDown", cdp.Sent.Last(c => c.Method == "Input.dispatchKeyEvent").Type);
|
|
Assert.AreEqual("RemoteKeyEvent(…)", new RemoteKeyEvent(true, Febe.Browser.Gestures.Keyboard.ForText("p")).ToString());
|
|
|
|
await view.StopAsync();
|
|
Assert.IsFalse(gate.IsDriving);
|
|
}
|
|
|
|
private static JsonObject Received(string id, string url, int status, string mime, string type = "Fetch") => new()
|
|
{
|
|
["requestId"] = id,
|
|
["loaderId"] = "L1",
|
|
["frameId"] = "F1",
|
|
["type"] = type,
|
|
["response"] = new JsonObject { ["url"] = url, ["status"] = status, ["mimeType"] = mime },
|
|
};
|
|
|
|
/// <summary>The response of a main-frame navigation: its request id is the loader id.</summary>
|
|
private static JsonObject Document(string loader, string url) => new()
|
|
{
|
|
["requestId"] = loader,
|
|
["loaderId"] = loader,
|
|
["frameId"] = "F1",
|
|
["type"] = "Document",
|
|
["response"] = new JsonObject { ["url"] = url, ["status"] = 200, ["mimeType"] = "text/html; charset=utf-8" },
|
|
};
|
|
|
|
// By condition until a deadline, not a count of polls: bodies and acks run on pool continuations (#46).
|
|
private static Task WaitUntilAsync(Func<bool> condition) => TestWait.UntilAsync(condition, "condizione della prova");
|
|
|
|
/// <summary>The body of a cleaned Instagram sample (<c>tests/campioni/instagram</c>), as JSON text.</summary>
|
|
private static string SampleBody(string name)
|
|
{
|
|
var file = Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "campioni", "instagram"), $"*-{name}.json").Single();
|
|
using var sample = JsonDocument.Parse(File.ReadAllText(file));
|
|
return sample.RootElement.GetProperty("corpo").GetRawText();
|
|
}
|
|
|
|
private sealed class CollectingConsumer : IResponseConsumer
|
|
{
|
|
/// <summary>In arrival order.</summary>
|
|
public ConcurrentQueue<(ResponseDescriptor Descriptor, string Json)> Items { get; } = new();
|
|
|
|
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
|
|
{
|
|
Items.Enqueue((response, body.RootElement.GetRawText()));
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private sealed class FailingConsumer : IResponseConsumer
|
|
{
|
|
private int _calls;
|
|
|
|
public int Calls => Volatile.Read(ref _calls);
|
|
|
|
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
|
|
{
|
|
Interlocked.Increment(ref _calls);
|
|
throw new InvalidOperationException("Lettore di prova che fallisce sempre.");
|
|
}
|
|
}
|
|
|
|
private sealed class CollectingObserver : IResponseObserver
|
|
{
|
|
public ConcurrentQueue<ResponseDescriptor> Responses { get; } = new();
|
|
|
|
public ConcurrentQueue<NavigationDescriptor> Navigations { get; } = new();
|
|
|
|
public void OnResponse(ResponseDescriptor response) => Responses.Enqueue(response);
|
|
|
|
public ConcurrentQueue<string> Dropped { get; } = new();
|
|
|
|
public void OnNavigation(NavigationDescriptor navigation) => Navigations.Enqueue(navigation);
|
|
|
|
public void OnBodyDropped(string requestId) => Dropped.Enqueue(requestId);
|
|
}
|
|
|
|
private sealed class SyncProgress(List<long> values) : IProgress<long>
|
|
{
|
|
public void Report(long value) => values.Add(value);
|
|
}
|
|
}
|