Chrome ha troncato due esportazioni del giro A quater (a 200 000 byte e a 57 MB, a meta di un corpo di risposta) e la sonda le rifiutava come file sbagliati. Ora da-har e richieste tengono le voci complete, scartano quella dentro cui il file finisce e lo dicono; un file rotto in qualunque altro modo resta rifiutato. Per richieste la voce scartata conta comunque come dati: si chiude dove si ferma e passa per le stesse regole di una voce intera, e ogni stringa JSON del testo interrotto conta come valore, cosi un nome che e un dato solo li non arriva nel file delle forme. In A quater: come esportare senza che il file si tronchi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
299 lines
14 KiB
C#
299 lines
14 KiB
C#
using System.Text;
|
|
using System.Text.Json.Nodes;
|
|
using Febe.Sonda;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Collection;
|
|
|
|
/// <summary>
|
|
/// A HAR export that the browser cut short. On 12 November 2026 Chrome stopped writing the same round twice: once at
|
|
/// exactly 200 000 bytes, once at 57 MB in the middle of a response body. The probe keeps the complete entries, leaves
|
|
/// out the one the file ends inside and says so; a file broken in any other way is still refused. For <c>richieste</c>
|
|
/// the left-out entry still counts as data, so that a name that is someone's value only there is removed all the same.
|
|
/// The HAR is synthetic, written by the test with invented values.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class HarFileTests
|
|
{
|
|
/// <summary>Where the synthetic HAR is cut: inside the body of its last entry.</summary>
|
|
private const string CutMark = "CORPO-INTERROTTO";
|
|
|
|
/// <summary>A key of the first request's variables that is someone's name: a value only in the last entry.</summary>
|
|
private const string Person = "carla_verdi";
|
|
|
|
[TestMethod]
|
|
public void AWholeFileIsReadAsItIsEvenWithAByteOrderMark()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
var har = temp.Combine("intero.har");
|
|
File.WriteAllText(har, Har().ToJsonString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: true));
|
|
|
|
using var file = HarFile.Open(har, 256);
|
|
Assert.IsFalse(file.Truncated);
|
|
Assert.IsFalse(file.DroppedAnEntry);
|
|
Assert.AreEqual(3, file.Entries.GetArrayLength());
|
|
Assert.IsNull(file.ReadDroppedEntry());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AFileCutInsideAnEntryKeepsTheEntriesBeforeIt()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
using var file = HarFile.Open(Write(temp, "tagliato.har", CutInsideTheLastBody(Har())), 256);
|
|
Assert.IsTrue(file.Truncated);
|
|
Assert.IsTrue(file.DroppedAnEntry);
|
|
Assert.AreEqual(2, file.Entries.GetArrayLength());
|
|
Assert.AreEqual("https://www.instagram.com/api/v1/prima/", file.Entries[0].GetProperty("request").GetProperty("url").GetString());
|
|
Assert.AreEqual("https://www.instagram.com/api/v1/seconda/", file.Entries[1].GetProperty("request").GetProperty("url").GetString());
|
|
|
|
// The left-out entry, closed where it stops: the cut body is kept up to the cut.
|
|
using var dropped = file.ReadDroppedEntry()!;
|
|
var entry = dropped.Document.RootElement[0];
|
|
Assert.AreEqual("https://www.instagram.com/api/v1/terza/", entry.GetProperty("request").GetProperty("url").GetString());
|
|
var body = entry.GetProperty("response").GetProperty("content").GetProperty("text").GetString()!;
|
|
Assert.StartsWith("{\"utente\":{\"username\":\"" + Person + "\"}", body);
|
|
Assert.AreEqual(body, dropped.CutText);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AFileCutBetweenEntriesOrAfterThemKeepsThemAll()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
var text = Har().ToJsonString();
|
|
|
|
// Right after the comma that follows the second entry.
|
|
var third = text.IndexOf("https://www.instagram.com/api/v1/terza/", StringComparison.Ordinal);
|
|
var between = text.LastIndexOf("},{", third, StringComparison.Ordinal) + 2;
|
|
using (var file = HarFile.Open(Write(temp, "fra.har", text[..between]), 256))
|
|
{
|
|
Assert.IsTrue(file.Truncated);
|
|
Assert.IsFalse(file.DroppedAnEntry);
|
|
Assert.AreEqual(2, file.Entries.GetArrayLength());
|
|
Assert.IsNull(file.ReadDroppedEntry());
|
|
}
|
|
|
|
// After the array of entries, before the closing braces and the rest of «log».
|
|
var whole = Har();
|
|
whole["log"]!["pages"] = new JsonArray(new JsonObject { ["id"] = "pagina" });
|
|
var withPages = whole.ToJsonString();
|
|
var afterEntries = withPages.IndexOf(",\"pages\"", StringComparison.Ordinal) + 3;
|
|
using (var file = HarFile.Open(Write(temp, "dopo.har", withPages[..afterEntries]), 256))
|
|
{
|
|
Assert.IsTrue(file.Truncated);
|
|
Assert.IsFalse(file.DroppedAnEntry);
|
|
Assert.AreEqual(3, file.Entries.GetArrayLength());
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AFileBrokenAnywhereElseIsStillRefused()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
var text = Har().ToJsonString();
|
|
var cut = CutInsideTheLastBody(Har());
|
|
|
|
// A syntax error in the second entry only, the first one whole, and the file also cut later.
|
|
var second = cut.IndexOf("/api/v1/seconda/", StringComparison.Ordinal);
|
|
var status = cut.IndexOf("\"status\":200,", second, StringComparison.Ordinal);
|
|
var broken = cut[..status] + "\"status\":200;" + cut[(status + "\"status\":200,".Length)..];
|
|
StringAssert.Contains(Assert.ThrowsExactly<InvalidDataException>(() => HarFile.Open(Write(temp, "sbagliato.har", broken), 256)).Message, "non è un file HAR valido");
|
|
|
|
// An entry that is not an object, after a whole one.
|
|
Assert.ThrowsExactly<InvalidDataException>(() => HarFile.Open(Write(temp, "numeri.har", """{"log":{"entries":[{"a":1},2,{"b":"""), 256));
|
|
|
|
// Cut before the first entry is complete: nothing to keep.
|
|
var first = text[..(text.IndexOf("/api/v1/prima/", StringComparison.Ordinal) + 5)];
|
|
Assert.ThrowsExactly<InvalidDataException>(() => HarFile.Open(Write(temp, "presto.har", first), 256));
|
|
|
|
// Something after a whole file.
|
|
Assert.ThrowsExactly<InvalidDataException>(() => HarFile.Open(Write(temp, "coda.har", text + "{}"), 256));
|
|
|
|
// Not a HAR: entries that are not under «log».
|
|
Assert.ThrowsExactly<InvalidDataException>(() => HarFile.Open(Write(temp, "altro.har", """{"altro":{"entries":[{"a":1},{"b":"""), 256));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TextThatIsNotValidUnicodeInTheDroppedEntryDoesNotStopTheReading()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
var head = Encoding.UTF8.GetBytes("""{"log":{"entries":[{"request":{"url":"https://www.instagram.com/api/v1/prima/"}},{"request":{"url":"x""");
|
|
byte[] badUtf8 = [0xC3, 0x28];
|
|
var tail = Encoding.UTF8.GetBytes("""y","headers":[{"name":"a","value":"\ud800"}]},"response":{"content":{"text":"CORPO""");
|
|
var har = temp.Combine("unicode.har");
|
|
File.WriteAllBytes(har, [.. head, .. badUtf8, .. tail]);
|
|
|
|
using (var file = HarFile.Open(har, 256))
|
|
{
|
|
Assert.IsTrue(file.DroppedAnEntry);
|
|
Assert.AreEqual(1, file.Entries.GetArrayLength());
|
|
using var dropped = file.ReadDroppedEntry()!;
|
|
Assert.AreEqual("CORPO", dropped.CutText);
|
|
}
|
|
|
|
Assert.AreEqual(1, HarImporter.Import(har, temp.Combine("grezzi")).Entries);
|
|
Assert.IsTrue(RequestShapes.Extract(har, temp.Combine("richieste.json")).DroppedEntry);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("abc", "abc")]
|
|
[DataRow("abc\\", "abc")]
|
|
[DataRow("abc\\u00", "abc")]
|
|
[DataRow("abc\\u00e8", "abcè")]
|
|
[DataRow("a\\\\", "a\\")]
|
|
[DataRow("a\\n", "a\n")]
|
|
public void AStringCutShortKeepsWhatWasWhole(string raw, string expected) =>
|
|
Assert.AreEqual(expected, HarFile.PartialString(Encoding.UTF8.GetBytes(raw)));
|
|
|
|
[TestMethod]
|
|
public void AStringCutInsideACharacterKeepsTheCharactersBeforeIt()
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes("perché");
|
|
Assert.AreEqual("perch", HarFile.PartialString(bytes.AsSpan(0, bytes.Length - 1)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheConversionOfACutFileWritesTheCompleteAnswersAndSaysItIsTruncated()
|
|
{
|
|
using var temp = new TempFolder("har");
|
|
var har = Write(temp, "tagliato.har", CutInsideTheLastBody(Har()));
|
|
|
|
var report = HarImporter.Import(har, temp.Combine("grezzi"));
|
|
Assert.IsTrue(report.Truncated);
|
|
Assert.IsTrue(report.DroppedEntry);
|
|
Assert.AreEqual(2, report.Entries);
|
|
Assert.AreEqual(2, report.DataResponses);
|
|
Assert.AreEqual(2, Directory.GetFiles(temp.Combine("grezzi"), "*.json").Length);
|
|
|
|
var whole = Write(temp, "intero.har", Har().ToJsonString());
|
|
Assert.IsFalse(HarImporter.Import(whole, temp.Combine("grezzi-interi")).Truncated);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ANameThatIsAValueOnlyInsideTheCutBodyIsStillRemoved()
|
|
{
|
|
using var temp = new TempFolder("richieste");
|
|
var report = RequestShapes.Extract(Write(temp, "tagliato.har", CutInsideTheLastBody(Har())), temp.Combine("tagliato.json"));
|
|
Assert.IsTrue(report.Truncated);
|
|
Assert.IsTrue(report.DroppedEntry);
|
|
Assert.AreEqual(2, report.Entries);
|
|
var output = File.ReadAllText(temp.Combine("tagliato.json"));
|
|
Assert.IsFalse(output.Contains(Person, StringComparison.Ordinal), output);
|
|
|
|
// Control: without the third entry at all, the same key is a name like any other and stays.
|
|
var two = Har();
|
|
two["log"]!["entries"]!.AsArray().RemoveAt(2);
|
|
Assert.IsFalse(RequestShapes.Extract(Write(temp, "corto.har", two.ToJsonString()), temp.Combine("corto.json")).Truncated);
|
|
StringAssert.Contains(File.ReadAllText(temp.Combine("corto.json")), Person);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ANameInsideTheCutPartOfAnEmbeddedBlockIsStillRemoved()
|
|
{
|
|
using var temp = new TempFolder("richieste");
|
|
|
|
// A page cut inside the JSON block it embeds: the block never closes, so only its literals still name it.
|
|
var page = Har(body: "<html><script type=\"application/json\">{\"utente\":{\"username\":\"" + Person + "\",\"testo\":\"" + CutMark + new string('x', 200) + "\"}}</script></html>", mime: "text/html");
|
|
RequestShapes.Extract(Write(temp, "pagina.har", CutInsideTheLastBody(page)), temp.Combine("pagina.json"));
|
|
Assert.IsFalse(File.ReadAllText(temp.Combine("pagina.json")).Contains(Person, StringComparison.Ordinal));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheDroppedEntryCountsWithTheSameRulesAsAWholeOne()
|
|
{
|
|
using var temp = new TempFolder("richieste");
|
|
|
|
// A body in base64 and a page with an embedded block, both whole, in an entry cut later (in its timings).
|
|
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("{\"utente\":{\"username\":\"" + Person + "\"}}"));
|
|
var page = "<html><script type=\"application/json\">{\"utente\":{\"username\":\"" + Person + "\"}}</script></html>";
|
|
foreach (var (name, har) in new[] { ("base64", Har(body: base64, encoding: "base64")), ("pagina", Har(body: page, mime: "text/html")) })
|
|
{
|
|
var entries = har["log"]!["entries"]!.AsArray();
|
|
entries[2]!["timings"] = new JsonObject { ["wait"] = CutMark + new string('x', 200) };
|
|
var text = har.ToJsonString();
|
|
var cut = text[..(text.LastIndexOf(CutMark, StringComparison.Ordinal) + 6)];
|
|
var output = temp.Combine(name + ".json");
|
|
Assert.IsTrue(RequestShapes.Extract(Write(temp, name + ".har", cut), output).DroppedEntry, name);
|
|
Assert.IsFalse(File.ReadAllText(output).Contains(Person, StringComparison.Ordinal), name);
|
|
}
|
|
|
|
// A base64 body cut in the middle: its first whole groups still decode.
|
|
var long64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("{\"utente\":{\"username\":\"" + Person + "\",\"testo\":\"" + new string('x', 300) + "\"}}"));
|
|
var cut64 = Har(body: long64, encoding: "base64").ToJsonString();
|
|
cut64 = cut64[..(cut64.IndexOf(long64, StringComparison.Ordinal) + 120)];
|
|
RequestShapes.Extract(Write(temp, "base64-tagliato.har", cut64), temp.Combine("base64-tagliato.json"));
|
|
Assert.IsFalse(File.ReadAllText(temp.Combine("base64-tagliato.json")).Contains(Person, StringComparison.Ordinal));
|
|
}
|
|
|
|
private static string Write(TempFolder temp, string name, string text)
|
|
{
|
|
var path = temp.Combine(name);
|
|
File.WriteAllText(path, text);
|
|
return path;
|
|
}
|
|
|
|
/// <summary>The HAR as text, cut a few characters after the mark in the body of its last entry.</summary>
|
|
private static string CutInsideTheLastBody(JsonObject har)
|
|
{
|
|
var text = har.ToJsonString();
|
|
return text[..(text.LastIndexOf(CutMark, StringComparison.Ordinal) + 6)];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Three answers of the site. The first is a GraphQL POST whose variables are keyed by <see cref="Person"/>; that text
|
|
/// is a value only in the body of the third entry, which a cut file ends inside.
|
|
/// </summary>
|
|
private static JsonObject Har(string? body = null, string mime = "application/json", string? encoding = null)
|
|
{
|
|
static JsonObject Entry(string method, string url, string body, string mime = "application/json", string? encoding = null, JsonObject? postData = null)
|
|
{
|
|
var request = new JsonObject
|
|
{
|
|
["method"] = method,
|
|
["url"] = url,
|
|
["headers"] = new JsonArray(new JsonObject { ["name"] = "Referer", ["value"] = "https://www.instagram.com/" }),
|
|
};
|
|
if (postData is not null)
|
|
{
|
|
request["postData"] = postData;
|
|
}
|
|
|
|
var content = new JsonObject { ["mimeType"] = mime, ["text"] = body };
|
|
if (encoding is not null)
|
|
{
|
|
content["encoding"] = encoding;
|
|
}
|
|
|
|
return new JsonObject
|
|
{
|
|
["_resourceType"] = "xhr",
|
|
["startedDateTime"] = "2026-11-12T23:40:00.000+01:00",
|
|
["request"] = request,
|
|
["response"] = new JsonObject
|
|
{
|
|
["status"] = 200,
|
|
["headers"] = new JsonArray(),
|
|
["content"] = content,
|
|
},
|
|
};
|
|
}
|
|
|
|
body ??= "{\"utente\":{\"username\":\"" + Person + "\"},\"testo\":\"" + CutMark + new string('x', 200) + "\"}";
|
|
return new JsonObject
|
|
{
|
|
["log"] = new JsonObject
|
|
{
|
|
["version"] = "1.2",
|
|
["entries"] = new JsonArray(
|
|
Entry("POST", "https://www.instagram.com/api/v1/prima/", """{"elementi":[{"nome":"mario"}]}""",
|
|
postData: new JsonObject
|
|
{
|
|
["mimeType"] = "application/x-www-form-urlencoded",
|
|
["text"] = "doc_id=1000000000001&variables=" + Uri.EscapeDataString("{\"seen\":{\"" + Person + "\":1}}"),
|
|
}),
|
|
Entry("GET", "https://www.instagram.com/api/v1/seconda/", """{"elementi":[{"nome":"lucia"}]}"""),
|
|
Entry("GET", "https://www.instagram.com/api/v1/terza/", body, mime, encoding)),
|
|
},
|
|
};
|
|
}
|
|
}
|