(() => HarImporter.Import(temp.Combine("elenco.har"), temp.Combine("nuova4")));
Assert.IsFalse(Directory.Exists(temp.Combine("nuova4")), "Nothing is created for a file that is not a HAR.");
}
[TestMethod]
public void OnlyNamesOfTheSiteCodeNameAFile()
{
Assert.AreEqual("PolarisStoriesV3HighlightsPageQuery", HarImporter.OperationName("PolarisStoriesV3HighlightsPageQuery"));
Assert.AreEqual("usePolarisRegisterInRecentSearchesMutation", HarImporter.OperationName(" usePolarisRegisterInRecentSearchesMutation "));
Assert.AreEqual("ProvaAggiornamentiSubscription", HarImporter.OperationName("ProvaAggiornamentiSubscription"));
Assert.AreEqual("ProvaGrigliaQuery_connection", HarImporter.OperationName("ProvaGrigliaQuery_connection"), "The following page of a connection.");
foreach (var value in new[] { null, "", "Query", "mario.rossi", "mario_rossi", "Pagina di mario Query", "Profilo12345Query", "/mario.rossi/Query", "ProvaElenco", "ProvaQuery_mario.rossi", "ProvaQuery_Mario" })
{
Assert.IsNull(HarImporter.OperationName(value), value ?? "null");
}
}
[TestMethod]
public void TheTargetsSnippetOnlyListensAndReads()
{
var path = Febe.Tests.Desktop.Support.Repository.PathOf("tools", "bersagli", "raccogli-bersagli.js");
var script = File.ReadAllText(path);
// The same deny-list as Febe's own page scripts: no gesture, no DOM change, no request, no cookie or storage.
Febe.Browser.Cdp.ReadOnlyScriptGuard.EnsureReadOnly(script);
foreach (var word in new[] { "preventDefault", "stopPropagation", "stopImmediatePropagation", "postMessage", "document.cookie", "cookieStore", "Storage", "indexedDB", "fetch(", "XMLHttpRequest", "sendBeacon", "WebSocket", "new Image", "innerHTML", "outerHTML", "classList", ".style", "setAttribute", "removeAttribute", "eval(", "Function(", "open(" })
{
Assert.IsFalse(script.Contains(word, StringComparison.Ordinal), word);
}
// One capturing click listener, and the function that copies the records.
foreach (var part in new[] { "addEventListener('click', onClick, { capture: true, passive: true })", "globalThis.febeBersagli = ", "copy(text)" })
{
Assert.IsTrue(script.Contains(part, StringComparison.Ordinal), part);
}
Assert.AreEqual(1, script.Split("addEventListener").Length - 1, "A single listener.");
Assert.IsTrue(File.Exists(Path.Combine(Path.GetDirectoryName(path)!, "LEGGIMI.md")));
}
private static JsonNode Sample(string folder, string name) => JsonNode.Parse(File.ReadAllText(Path.Combine(folder, name)))!;
private static string Base64(string text) => Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
private static JsonObject SyntheticHar()
{
static JsonArray Headers(params (string Name, string Value)[] headers) =>
[.. headers.Select(h => (JsonNode)new JsonObject { ["name"] = h.Name, ["value"] = h.Value })];
static JsonObject Entry(string resourceType, string method, string url, int status, JsonObject content, JsonObject? postData = null, string started = "2026-10-21T17:35:04.512+02:00")
{
var request = new JsonObject
{
["method"] = method,
["url"] = url,
["httpVersion"] = "h3",
["headers"] = Headers(("Cookie", "sessionid=segreto-cookie; ds_user_id=segreto-cookie"), ("X-Prova", "segreto-intestazione")),
["queryString"] = new JsonArray(new JsonObject { ["name"] = "variables", ["value"] = "segreto-indirizzo" }),
["cookies"] = new JsonArray(new JsonObject { ["name"] = "sessionid", ["value"] = "segreto-cookie" }),
["headersSize"] = -1,
["bodySize"] = 0,
};
if (postData is not null)
{
request["postData"] = postData;
}
return new JsonObject
{
["_resourceType"] = resourceType,
["startedDateTime"] = started,
["time"] = 123.4,
["request"] = request,
["response"] = new JsonObject
{
["status"] = status,
["statusText"] = "",
["httpVersion"] = "h3",
["headers"] = Headers(("Set-Cookie", "prova=segreto-risposta"), ("Content-Type", "text/plain; segreto-intestazione")),
["cookies"] = new JsonArray(new JsonObject { ["name"] = "prova", ["value"] = "segreto-risposta" }),
["content"] = content,
["redirectURL"] = "",
},
};
}
static JsonObject Content(string mime, string? text, bool base64 = false)
{
var content = new JsonObject { ["size"] = text?.Length ?? 0, ["mimeType"] = mime };
if (text is not null)
{
content["text"] = base64 ? Base64(text) : text;
if (base64)
{
content["encoding"] = "base64";
}
}
return content;
}
static JsonObject Form(string text, params (string Name, string Value)[] parameters)
{
var form = new JsonObject { ["mimeType"] = "application/x-www-form-urlencoded", ["text"] = text };
if (parameters.Length > 0)
{
form["params"] = Headers(parameters);
}
return form;
}
var page = $$"""
segreto-testo-della-pagina
""";
return new JsonObject
{
["log"] = new JsonObject
{
["version"] = "1.2",
["creator"] = new JsonObject { ["name"] = "WebInspector", ["version"] = "537.36" },
["pages"] = new JsonArray(),
["entries"] = new JsonArray(
Entry("xhr", "POST", "https://www.instagram.com/graphql/query?variables=segreto-indirizzo", 200, Content("text/javascript; charset=utf-8", FirstBody, base64: true),
Form($"av=segreto-corpo&{Operation}=ProvaElencoQuery&variables=segreto-corpo", ("av", "segreto-corpo"), (Operation, "ProvaElencoQuery"), ("variables", "segreto-corpo"))),
Entry("fetch", "GET", "https://www.instagram.com/api/v1/prova/mario.rossi/info/?segreto=1", 200, Content("application/json", SecondBody)),
Entry("xhr", "POST", "https://www.instagram.com/graphql/query", 200, Content("application/json", """{"pagina":{"cursore":"QVFIUjBz","altre":true}}"""),
Form($"{Operation}=ProvaPaginaSuccessivaQuery&x=segreto-corpo")),
Entry("xhr", "POST", "https://www.instagram.com/api/graphql", 200, Content("text/javascript", """{"dati":{"vuoto":null}}"""),
Form($"{Operation}=mario.rossi", (Operation, "mario.rossi"))),
Entry("script", "GET", "https://www.instagram.com/static/pacchetto.js", 200, Content("text/javascript", "console.log('segreto-script')")),
Entry("image", "GET", "https://scontent.cdninstagram.com/v/foto.jpg?firma=segreto-indirizzo", 200, Content("image/jpeg", "segreto-immagine", base64: true)),
Entry("document", "GET", "https://www.instagram.com/mario.rossi/?igsh=segreto-indirizzo", 200, Content("text/html; charset=utf-8", page)),
Entry("xhr", "POST", "https://www.instagram.com/ajax/prova/", 200, Content("text/javascript", """for (;;);{"payload":{"segreto":1}}""")),
Entry("document", "GET", "https://www.instagram.com/altro/", 200, Content("text/html", "segreto-senza-dati")),
Entry("xhr", "GET", "https://www.instagram.com/api/v1/vuota/", 200, Content("application/json", null)),
Entry("fetch", "GET", "https://esterno.example/api/?segreto=1", 200, Content("application/json", """{"segreto":1}""")),
Entry("fetch", "GET", "https://scontent.cdninstagram.com/v/dati.json", 200, Content("application/json", """{"segreto":1}"""))),
},
};
}
}