Files
Febe/tests/Febe.Tests/Collection/HttpSiteRequesterTests.cs
T
Alby96andClaude Opus 5.5 c87768766c
verifica / controlli (push) Successful in 22s
verifica / prove (push) Successful in 16m46s
verifica / installatore (push) Successful in 4m43s
verifica / immagine (push) Successful in 21m2s
Sessione salvata cifrata, copiata a «Ho finito» (#36, S4)
Il client del sito ha bisogno della sessione del browser di Febe: ora la copia
c'è, cifrata e dietro un interruttore, senza cambiare nulla di predefinito.

- Opzione «raccolta» (pagine predefinita, richieste): solo con «richieste» e
  almeno un'operazione configurata si costruiscono sessione salvata, client del
  sito e copia. Con Instagram nulla cambia: le dieci operazioni restano non
  configurate (mancano __spin_*, __req, __s, __dyn, __csr e altri).
- Cookie con dominio, percorso, solo-host, Secure, HttpOnly e scadenza, mandati
  solo dove il browser li manderebbe (RFC 6265).
- Sezione «page» del catalogo: dove la pagina tiene ogni valore; nuovi campi rev,
  hs, hsi, dpr, ccg, comet_req, entry_nonce. Il caricatore rifiuta
  un'operazione configurata con un valore di sessione senza fonte nella pagina.
- Dati/sessione/sessione.json: DPAPI su Windows, AES-256-GCM in Docker (chiave
  da FEBE_CHIAVE_SESSIONE o chiave.bin), scrittura atomica, file illeggibile =
  nessuna sessione. «Scollega» la cancella; chiave.bin resta.
- Copia a «Ho finito» con Network.getCookies e una sola Runtime.evaluate in sola
  lettura, solo da una pagina del sito e finché dura il permesso di guida.
- FEBE_PASSWORD e FEBE_CHIAVE_SESSIONE non passano a Chromium, Xvfb, ffmpeg.
- Sito di prova: csrftoken, cookie limitato a /api/, registro di moduli suo.
- Revisione su tre lenti: 11 punti confermati, tutti corretti.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:43:43 +01:00

615 lines
33 KiB
C#

using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Nodes;
using Febe.Core.Abstractions;
using Febe.Core.Collection;
using Febe.Core.Collection.Readers;
using Febe.Core.Collection.Recipes;
using Febe.Core.Collection.Requests;
using Febe.SitoDiProva;
using Febe.Tests.Support;
using VirtualClock = Febe.Tests.Browser.TestSupport.VirtualClock;
namespace Febe.Tests.Collection;
/// <summary>
/// The HTTP site client (issue #36, S2) against the real test site on 127.0.0.1, never Instagram: every configured
/// operation of <c>richieste.prova.json</c> returns the data the pages read, the stop cases of §7.6 come back as answers
/// and signals, redirects are never followed, the session keeps what the site renews, one request at a time, nothing
/// leaves without its permit, and the log holds no value.
/// </summary>
[TestClass]
[TestCategory(SyntheticSite.Category)]
public sealed class HttpSiteRequesterTests
{
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36";
private const string Csrf = "gettone-csrf-di-prova-7";
private const string OtherCookie = "valore-di-un-altro-cookie";
private const string AccountId = "17841400000000077";
private static readonly RequestCatalog Catalog = RequestCatalog.LoadTestSite();
private static readonly SiteAnswerSignals Signals = new(SignalRules.LoadTestSite(), TestSite.SessionCookie);
[TestMethod]
public async Task EveryConfiguredOperationReturnsTheDataThePagesRead()
{
// A fixed clock for the site: the signed addresses of the two answers must be the same to compare them.
await using var site = await SyntheticSite.StartAsync(new FixedTime(DateTimeOffset.UtcNow));
using var bed = new Bed(site);
var cases = Cases(site);
CollectionAssert.AreEquivalent(Catalog.Configured.Select(o => o.Name).ToArray(), cases.Keys.ToArray(), "Every configured operation has a case here.");
var reads = 0;
foreach (var operation in Catalog.Configured)
{
var (values, pagePath) = cases[operation.Name];
var answer = await bed.SendAsync(new SiteRequest(operation.Name, values), operation.Category);
Assert.AreEqual(200, answer.Status, operation.Name);
Assert.AreEqual("application/json", answer.MimeType, operation.Name);
Assert.IsTrue(answer.RootFieldsPresent, operation.Name);
Assert.IsNull(Signals.Classify(operation, answer, bed.Clock.Now), operation.Name);
var page = await site.GetDataAsync(pagePath);
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(page), JsonNode.Parse(answer.Body.Span)), $"{operation.Name}: not the JSON of {pagePath}.");
// Through the delivery to the readers of lettori.prova.json, as the captured answer of the page.
var requested = await ReadAsync(answer.Body, answer, bed.Clock);
var captured = await ReadAsync(System.Text.Encoding.UTF8.GetBytes(page), answer, bed.Clock);
CollectionAssert.AreEqual(captured, requested, operation.Name);
reads += requested.Sum(r => r.Length);
}
Assert.IsTrue(reads > 2000, "The readers keep something of the answers, so the comparison proves something.");
Assert.AreEqual(Catalog.Configured.Count(), site.Control.DirectRequests.Count);
Assert.IsTrue(site.Control.DirectRequests.All(r => r.StartsWith("GET /api/dati/", StringComparison.Ordinal) || r.StartsWith("POST /api/graphql", StringComparison.Ordinal)));
StringAssert.StartsWith(site.Control.DirectRequests.Single(r => r.Contains("graphql", StringComparison.Ordinal)), "POST /api/graphql");
}
[TestMethod]
public async Task TheStopCasesComeBackAsAnswersAndSignalsAndRedirectsAreNeverFollowed()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
var profile = Profile();
site.Control.ReplyToDirect(DirectReply.TooManyRequests);
var slow = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.AreEqual(429, slow.Status);
Assert.AreEqual(TimeSpan.FromSeconds(60), slow.RetryAfter);
var throttle = Classify(slow);
Assert.AreEqual(SignalLevel.Throttle, throttle.Signal.Level);
Assert.AreEqual("http-429", throttle.Signal.Code);
site.Control.ReplyToDirect(DirectReply.RedirectToLogin);
var login = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.AreEqual(302, login.Status);
Assert.IsTrue(login.IsRedirect);
Assert.AreEqual("/{testo}/", login.RedirectPathShape, "Only the shape: no host, no query string with the path asked.");
Assert.AreEqual(SiteAnswerSignals.RedirectCode, Classify(login).Signal.Code);
Assert.AreEqual(SignalLevel.Challenge, Classify(login).Signal.Level);
site.Control.ReplyToDirect(DirectReply.RedirectToChallenge);
var challenge = await bed.SendAsync(Graph(), ActionKind.ProfileOpen);
Assert.AreEqual(302, challenge.Status);
Assert.AreEqual(SiteAnswerSignals.RedirectCode, Classify(challenge).Signal.Code);
site.Control.ReplyToDirect(DirectReply.HtmlInsteadOfJson);
var html = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.IsTrue(html.IsHtml);
Assert.IsFalse(html.RootFieldsPresent);
Assert.AreEqual(SiteAnswerSignals.HtmlCode, Classify(html).Signal.Code);
site.Control.ReplyToDirect(DirectReply.MissingRootField);
var missing = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.AreEqual(200, missing.Status);
Assert.IsTrue(missing.IsDeclaredData);
Assert.IsFalse(missing.RootFieldsPresent);
Assert.AreEqual(SiteAnswerSignals.MissingFieldsCode, Classify(missing).Signal.Code);
Assert.AreEqual(SiteAnswerSignals.H16, Classify(missing).Hypothesis);
// The whole site's stop modes reach the site client too.
site.Control.Set(StopMode.Challenge);
var mode = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.IsTrue(mode.IsRedirect);
site.Control.Set(StopMode.Normal);
var fine = await bed.SendAsync(profile, ActionKind.ProfileOpen);
Assert.AreEqual(200, fine.Status);
Assert.IsNull(Signals.Classify(Operation("profilo"), fine, bed.Clock.Now));
Assert.AreEqual(0, site.Control.LoginPageVisits, "A redirect is a signal, never an address to visit.");
Assert.AreEqual(0, site.Control.ChallengePageVisits);
Assert.HasCount(7, site.Control.DirectRequests);
}
[TestMethod]
public async Task TheSessionKeepsWhatTheSiteRenewsAndADeletedSessionCookieIsReported()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
site.Control.ReplyToDirect(DirectReply.RenewSessionCookie);
var renewed = await bed.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(200, renewed.Status);
CollectionAssert.AreEqual(new[] { TestSite.SessionCookie }, renewed.CookiesSet.ToArray());
Assert.AreEqual("rinnovata-1", bed.Session.Read()!.Cookie(TestSite.SessionCookie));
Assert.IsFalse(site.Control.IsSession(SiteControl.InitialSession), "The old value stopped working on the site.");
Assert.AreEqual(200, (await bed.SendAsync(Profile(), ActionKind.ProfileOpen)).Status, "The renewed cookie is the one sent now.");
site.Control.ReplyToDirect(DirectReply.SetClaim);
var claimed = await bed.SendAsync(Graph(), ActionKind.ProfileOpen);
Assert.IsTrue(claimed.SetsClaim);
Assert.AreEqual("claim-prova-1", bed.Session.Read()!.Fields["claim"]);
Assert.AreEqual(200, (await bed.SendAsync(Graph(), ActionKind.ProfileOpen)).Status);
site.Control.ReplyToDirect(DirectReply.DeleteSessionCookie);
var deleted = await bed.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(200, deleted.Status, "The data came, but the session is over.");
Assert.IsTrue(deleted.Deletes(TestSite.SessionCookie));
var signal = Classify(deleted);
Assert.AreEqual(SiteAnswerSignals.SessionCookieDeletedCode, signal.Signal.Code);
Assert.AreEqual(SignalLevel.Challenge, signal.Signal.Level);
Assert.IsNull(bed.Session.Read()!.Cookie(TestSite.SessionCookie));
Assert.IsTrue(bed.Log.Any(l => l.Contains("ha cancellato il cookie di sessione", StringComparison.Ordinal)));
var sent = site.Control.DirectRequests.Count;
var refused = await Assert.ThrowsExactlyAsync<SiteSessionMissingException>(() => bed.SendAsync(Profile(), ActionKind.ProfileOpen));
Assert.AreEqual(TestSite.SessionCookie, refused.Missing);
Assert.AreEqual(SignalLevel.Disconnect, SiteAnswerSignals.ForMissingSession(refused, bed.Clock.Now).Signal.Level);
Assert.HasCount(sent, site.Control.DirectRequests, "Without the session cookie nothing leaves.");
Assert.AreEqual(3, bed.Session.Updates);
}
[TestMethod]
public async Task AnEndedSessionAMissingValueOrAWrongHeaderIsWhatARealSiteWouldAnswer()
{
await using var site = await SyntheticSite.StartAsync();
// A session cookie the site does not know (expired): a plain address is sent to the login page, a query gets 401.
using (var stale = new Bed(site, Values(session: "scaduta")))
{
var rest = await stale.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(302, rest.Status);
Assert.AreEqual(SiteAnswerSignals.RedirectCode, Classify(rest).Signal.Code);
var graph = await stale.SendAsync(Graph(), ActionKind.ProfileOpen);
Assert.AreEqual(401, graph.Status);
Assert.AreEqual(SiteAnswerSignals.UnauthorizedCode, Classify(graph).Signal.Code);
Assert.AreEqual(0, site.Control.LoginPageVisits);
}
// A header of the catalog with a value the site does not expect: 400 with a JSON without the fields.
using (var wrongApp = new Bed(site, Values(appId: "un-altra-applicazione")))
{
var answer = await wrongApp.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(400, answer.Status);
Assert.AreEqual(SiteAnswerSignals.MissingFieldsCode, Classify(answer).Signal.Code);
}
// Values the catalog asks for and the session does not have: nothing leaves.
var before = site.Control.DirectRequests.Count;
foreach (var (values, missing) in new[]
{
(Values(csrf: null), "x-csrftoken"),
(Values(session: null), TestSite.SessionCookie),
(Values(docIds: false), "doc_id"),
})
{
using var bed = new Bed(site, values);
var refused = await Assert.ThrowsExactlyAsync<SiteSessionMissingException>(() => bed.SendAsync(Graph(), ActionKind.ProfileOpen));
Assert.AreEqual(missing, refused.Missing);
}
using (var none = new Bed(site, noSession: true))
{
var refused = await Assert.ThrowsExactlyAsync<SiteSessionMissingException>(() => none.SendAsync(Profile(), ActionKind.ProfileOpen));
Assert.IsNull(refused.Missing);
}
Assert.HasCount(before, site.Control.DirectRequests);
// The site checks the token against its cookie, as a real site does (H16-generic answer): a plain client here,
// only to show the test site refuses what the site client never sends.
using var plain = new HttpClient(new SocketsHttpHandler { UseProxy = false, UseCookies = false, AllowAutoRedirect = false });
using var request = new HttpRequestMessage(HttpMethod.Get, site.Url("/api/dati/inizio"));
request.Headers.TryAddWithoutValidation("cookie", $"{TestSite.SessionCookie}={SiteControl.InitialSession}; {TestSite.CsrfCookie}={Csrf}");
request.Headers.TryAddWithoutValidation("x-csrftoken", "un-altro-gettone");
request.Headers.TryAddWithoutValidation("x-app-id", TestSite.AppId);
request.Headers.TryAddWithoutValidation("accept", "application/json");
request.Headers.TryAddWithoutValidation("user-agent", UserAgent);
using var forbidden = await plain.SendAsync(request);
Assert.AreEqual(403, (int)forbidden.StatusCode);
Assert.AreEqual("text/html", forbidden.Content.Headers.ContentType?.MediaType);
}
[TestMethod]
public async Task TheSiteChecksTheTypesOfTheVariablesAndTheFriendlyName()
{
await using var site = await SyntheticSite.StartAsync();
// An edited copy of the catalog that writes «primi» as a text: the request is not the page's, the site refuses it.
var file = JsonNode.Parse(Febe.Core.Collection.SiteMap.ReadEmbeddedText("richieste.prova.json"))!.AsObject();
var query = file["operations"]!.AsArray().Single(o => o!["name"]!.GetValue<string>() == TestSite.ProfileQuery)!;
query["variables"]!["primi"] = "12";
using (var typed = new Bed(site, catalog: RequestCatalog.Parse(file.ToJsonString(), "richieste.prova.json (modificato)")))
{
var answer = await typed.SendAsync(Graph(), ActionKind.ProfileOpen);
Assert.AreEqual(400, answer.Status);
Assert.AreEqual(SiteAnswerSignals.MissingFieldsCode, Classify(answer).Signal.Code);
}
// A doc_id of another operation.
var docIds = new Dictionary<string, string> { [TestSite.ProfileQuery] = "8000000000000002" };
var values = Values();
using var wrongDoc = new Bed(site, new SiteSessionValues(values.Cookies, values.UserAgent, values.Fields, docIds));
Assert.AreEqual(400, (await wrongDoc.SendAsync(Graph(), ActionKind.ProfileOpen)).Status);
}
[TestMethod]
public async Task OneRequestAtATimeAndTheOthersAreRefusedNotQueued()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
site.Control.DirectDelay = TimeSpan.FromSeconds(1);
site.Control.ReplyToDirect(DirectReply.TooManyRequests);
var first = bed.SendAsync(Profile(), ActionKind.ProfileOpen);
await WaitUntilAsync(() => site.Control.DirectRequests.Count == 1);
// While the first is in flight: refused at once, not queued behind it (a queued one would leave right after a
// 429 before anybody read it, rule 8).
var others = await Task.WhenAll(Enumerable.Range(0, 3).Select(_ => Task.Run(async () =>
{
var refused = await Assert.ThrowsExactlyAsync<SiteRequestRefusedException>(() => bed.SendAsync(Profile(), ActionKind.ProfileOpen));
return refused.Message;
})));
Assert.AreEqual(429, (await first).Status);
Assert.IsTrue(others.All(m => m.Contains("in corso", StringComparison.Ordinal)));
Assert.HasCount(1, site.Control.DirectRequests, "Nothing left after the 429.");
Assert.AreEqual(1, site.Control.MaxDirectInFlight, "Never two requests in flight.");
// Once the answer is in, the next may leave.
site.Control.DirectDelay = TimeSpan.Zero;
Assert.AreEqual(200, (await bed.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
}
[TestMethod]
public async Task TheStopModesOfTheSiteCountTheRequestsOfTheSiteClientToo()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
// «After one data request»: the site client's requests count, as the pages' do.
site.Control.Set(StopMode.TooManyRequests, afterDataRequests: 1);
Assert.AreEqual(200, (await bed.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
Assert.AreEqual(429, (await bed.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
// The session ends: the cookie goes, and on the site too.
site.Control.Set(StopMode.SessionEnded);
var ended = await bed.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(302, ended.Status);
Assert.IsTrue(ended.Deletes(TestSite.SessionCookie));
Assert.IsFalse(site.Control.IsSession(SiteControl.InitialSession));
Assert.IsNull(bed.Session.Read()!.Cookie(TestSite.SessionCookie));
}
[TestMethod]
public async Task TheSessionThePagesOpenWorksForTheSiteClientAndEndsOnBothSides()
{
await using var site = await SyntheticSite.StartAsync();
using var browser = new HttpClient(new SocketsHttpHandler { UseProxy = false, UseCookies = false, AllowAutoRedirect = false });
// The first visit is a login: a new value, never the fixed one, and the site client accepts it once copied.
var (_, _, first) = await VisitAsync(browser, site, "/", null);
Assert.IsFalse(string.IsNullOrEmpty(first));
Assert.AreNotEqual(SiteControl.InitialSession, first);
Assert.IsTrue(site.Control.IsSession(first));
using (var copied = new Bed(site, Values(session: first)))
{
Assert.AreEqual(200, (await copied.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
}
// The session expires on the page side: the browser loses the cookie and the site ends the value, so the same
// value copied in the site client is refused afterwards.
site.Control.Set(StopMode.SessionEnded);
var (status, location, deleted) = await VisitAsync(browser, site, "/profilo/marta.prova", first);
Assert.AreEqual(302, status);
Assert.AreEqual("/sessione-scaduta", location);
Assert.AreEqual(string.Empty, deleted, "The page deletes the cookie.");
Assert.IsFalse(site.Control.IsSession(first));
site.Control.Set(StopMode.Normal);
using (var stale = new Bed(site, Values(session: first)))
{
var refused = await stale.SendAsync(Profile(), ActionKind.ProfileOpen);
Assert.AreEqual(302, refused.Status);
Assert.AreEqual("/{testo}/", refused.RedirectPathShape);
}
// A new login (a visit without the cookie) opens a new session the site client accepts.
var (_, _, second) = await VisitAsync(browser, site, "/", null);
Assert.IsFalse(string.IsNullOrEmpty(second));
Assert.AreNotEqual(first, second);
using (var renewed = new Bed(site, Values(session: second)))
{
Assert.AreEqual(200, (await renewed.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
}
// A browser that kept a value the site ended gets a new one at its next visit.
var (_, _, third) = await VisitAsync(browser, site, "/", first);
Assert.IsTrue(site.Control.IsSession(third));
Assert.AreNotEqual(first, third);
// The data of the pages end the session too when it expires.
site.Control.Set(StopMode.SessionEnded);
using (var data = new HttpRequestMessage(HttpMethod.Get, site.Url("/dati/inizio")))
{
data.Headers.TryAddWithoutValidation("cookie", $"{TestSite.SessionCookie}={second}");
using var answer = await browser.SendAsync(data);
Assert.AreEqual(401, (int)answer.StatusCode);
}
Assert.IsFalse(site.Control.IsSession(second));
site.Control.Set(StopMode.Normal);
using (var ended = new Bed(site, Values(session: second)))
{
Assert.AreEqual(302, (await ended.SendAsync(Profile(), ActionKind.ProfileOpen)).Status);
}
Assert.AreEqual(0, site.Control.LoginPageVisits);
}
[TestMethod]
public async Task TheLateAnswerOfAReplacedSessionDoesNotTouchTheNewOne()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
site.Control.DirectDelay = TimeSpan.FromMilliseconds(500);
site.Control.ReplyToDirect(DirectReply.DeleteSessionCookie);
var sending = bed.SendAsync(Profile(), ActionKind.ProfileOpen);
await WaitUntilAsync(() => site.Control.DirectRequests.Count == 1);
// A new login while the request is in flight: «Ho finito» copies a new session.
bed.Session.Replace(Values(session: "nuova-sessione"));
var answer = await sending;
Assert.IsTrue(answer.Deletes(TestSite.SessionCookie), "The answer still says what the site did.");
Assert.AreEqual("nuova-sessione", bed.Session.Read()!.Cookie(TestSite.SessionCookie), "But the new session is not changed by the old one's answer.");
Assert.AreEqual(0, bed.Session.Updates);
}
private static async Task WaitUntilAsync(Func<bool> condition)
{
using var limit = new CancellationTokenSource(TimeSpan.FromSeconds(30));
while (!condition())
{
await Task.Delay(10, limit.Token);
}
}
[TestMethod]
public async Task NothingLeavesWithoutAPermitGrantedForThatRequest()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
var denied = Permit.Deny(ActionKind.ProfileOpen, null, DenialReason.HourlyBudgetReached, "Tetto orario raggiunto.");
await Assert.ThrowsExactlyAsync<SiteRequestRefusedException>(() => bed.Requester.SendAsync(Profile(), denied, CancellationToken.None));
await Assert.ThrowsExactlyAsync<SiteRequestRefusedException>(() => bed.Requester.SendAsync(Profile(), Permit.Grant(ActionKind.StoryView, null), CancellationToken.None));
await Assert.ThrowsExactlyAsync<SiteRequestRefusedException>(() => bed.Requester.SendAsync(new SiteRequest("cerca", new Dictionary<string, string> { [CaseValues.SearchText] = "marta" }), Permit.Grant(ActionKind.ProfileSearch, null), CancellationToken.None));
Assert.IsEmpty(site.Control.DirectRequests);
var permit = Permit.Grant(ActionKind.ProfileOpen, null);
await bed.Requester.SendAsync(Profile(), permit, CancellationToken.None);
var again = await Assert.ThrowsExactlyAsync<SiteRequestRefusedException>(() => bed.Requester.SendAsync(Profile(), permit, CancellationToken.None));
StringAssert.Contains(again.Message, "già stato usato");
Assert.HasCount(1, site.Control.DirectRequests);
}
[TestMethod]
public async Task NoAnswerIsAnErrorWithoutTheAddress()
{
await using var site = await SyntheticSite.StartAsync();
site.Control.DirectDelay = TimeSpan.FromSeconds(5);
using var slow = new Bed(site, options: HttpSiteRequesterOptions.TestSite(new Uri(site.SiteUrl + "/")) with { Timeout = TimeSpan.FromMilliseconds(300) });
var timeout = await Assert.ThrowsExactlyAsync<SiteUnreachableException>(() => slow.SendAsync(Profile(), ActionKind.ProfileOpen));
Assert.AreEqual(SiteUnreachableReason.Timeout, timeout.Reason);
AssertNoAddress(timeout.Message, site.SitePort);
var closed = Browser.TestSupport.LocalTestSite.FreePort();
using var nobody = new Bed(site, options: HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{closed}/")));
var unreachable = await Assert.ThrowsExactlyAsync<SiteUnreachableException>(() => nobody.SendAsync(Profile(), ActionKind.ProfileOpen));
Assert.AreEqual(SiteUnreachableReason.Unreachable, unreachable.Reason);
AssertNoAddress(unreachable.Message, closed);
Assert.IsNull(unreachable.InnerException, "The network error may name the address: it is not passed on.");
using var caller = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
using var patient = new Bed(site);
await Assert.ThrowsAsync<OperationCanceledException>(() => patient.Requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), caller.Token));
}
[TestMethod]
public async Task TheLogNamesOnlyOperationsPathShapesAndStatuses()
{
await using var site = await SyntheticSite.StartAsync();
using var bed = new Bed(site);
var cases = Cases(site);
foreach (var operation in Catalog.Configured)
{
await bed.SendAsync(new SiteRequest(operation.Name, cases[operation.Name].Values), operation.Category);
}
foreach (var reply in new[] { DirectReply.RedirectToLogin, DirectReply.SetClaim, DirectReply.RenewSessionCookie, DirectReply.DeleteSessionCookie })
{
site.Control.ReplyToDirect(reply);
await bed.SendAsync(Graph(), ActionKind.ProfileOpen);
}
Assert.IsTrue(bed.Log.Count >= Catalog.Configured.Count() + 4);
Assert.IsTrue(bed.Log.Any(l => l.Contains("«ProvaProfiloQuery» /api/graphql, stato 200", StringComparison.Ordinal)), string.Join("\n", bed.Log));
Assert.IsTrue(bed.Log.Any(l => l.Contains("«profilo» /api/dati/profilo/{testo}, stato 200", StringComparison.Ordinal)), string.Join("\n", bed.Log));
var secrets = new List<string>
{
Csrf, OtherCookie, SiteControl.InitialSession, "rinnovata-1", "claim-prova-1", TestSite.AppId, TestSite.Lsd, TestSite.Dtsg, AccountId,
TestSite.Revision, TestSite.HasteSession, TestSite.HasteSessionId, TestSite.ConnectionClass, TestSite.EntryNonce,
TestSite.DocIds[TestSite.ProfileQuery], UserAgent, "Mozilla", "marta.prova", "luca.prova", "c12", "127.0.0.1", "scheda", "primi", "accesso", "next",
};
secrets.AddRange(cases.Values.SelectMany(c => c.Values.Values));
foreach (var line in bed.Log)
{
foreach (var secret in secrets)
{
Assert.IsFalse(line.Contains(secret, StringComparison.OrdinalIgnoreCase), $"«{secret}» in «{line}».");
}
}
}
internal static Dictionary<string, (Dictionary<string, string> Values, string PagePath)> Cases(TestSite site)
{
string code;
string highlight;
lock (site.World.Gate)
{
var marta = site.World.FindByUsername("marta.prova")!;
code = marta.Posts[0].Code;
highlight = marta.Highlights[0].Id;
}
static Dictionary<string, string> V(params (string Name, string Value)[] values) => values.ToDictionary(v => v.Name, v => v.Value, StringComparer.Ordinal);
return new(StringComparer.Ordinal)
{
["inizio"] = (V(), "/dati/inizio"),
["profilo"] = (V((CaseValues.Username, "marta.prova")), "/dati/profilo/marta.prova?scheda=post"),
["profilo-altri"] = (V((CaseValues.Username, "luca.prova"), (CaseValues.Cursor, "c12")), "/dati/profilo/luca.prova/altri?dopo=c12&scheda=post"),
[TestSite.ProfileQuery] = (V((CaseValues.Username, "marta.prova")), "/dati/profilo/marta.prova?scheda=post"),
["taggati"] = (V((CaseValues.Username, "marta.prova")), "/dati/profilo/marta.prova?scheda=taggati"),
["reel"] = (V((CaseValues.Username, "luca.prova")), "/dati/profilo/luca.prova?scheda=reel"),
["post"] = (V((CaseValues.Code, code)), "/dati/post/" + code),
["storie"] = (V((CaseValues.Username, "marta.prova")), "/dati/storie/marta.prova"),
["evidenza"] = (V((CaseValues.HighlightId, highlight)), "/dati/evidenza/" + Uri.EscapeDataString(highlight)),
["seguiti"] = (V(), "/dati/seguiti"),
};
}
private static async Task<string[]> ReadAsync(ReadOnlyMemory<byte> body, SiteAnswer answer, VirtualClock clock)
{
var collector = new CaptureCollector(new ShapeReader(ReaderVocabulary.LoadTestSite()), clock);
var descriptor = new ResponseDescriptor("r1", HostKind.Site, "http://127.0.0.1" + answer.PathShape, "127.0.0.1", answer.PathShape, answer.Status, answer.MimeType, "Fetch", false, clock.Now);
Assert.AreEqual(1, await new ResponseDelivery("Client del sito").DeliverBodyAsync(descriptor, body, [collector], CancellationToken.None));
return [.. collector.Since(0).Select(r => JsonSerializer.Serialize(r.Result))];
}
/// <summary>
/// A page visit as the browser makes it, with <paramref name="session"/> as its cookie: the status, the redirect and
/// the value of the session cookie the page set (empty when it deleted it, null when it set none).
/// </summary>
private static async Task<(int Status, string? Location, string? Session)> VisitAsync(HttpClient browser, TestSite site, string path, string? session)
{
using var request = new HttpRequestMessage(HttpMethod.Get, site.Url(path));
if (session is not null)
{
request.Headers.TryAddWithoutValidation("cookie", $"{TestSite.SessionCookie}={session}");
}
using var response = await browser.SendAsync(request);
string? set = null;
if (response.Headers.TryGetValues("Set-Cookie", out var cookies))
{
var prefix = TestSite.SessionCookie + "=";
set = cookies.Where(c => c.StartsWith(prefix, StringComparison.Ordinal)).Select(c => c[prefix.Length..].Split(';')[0]).LastOrDefault();
}
return ((int)response.StatusCode, response.Headers.Location?.OriginalString, set);
}
private static SiteRequest Profile() => SiteRequest.For("profilo", (CaseValues.Username, "marta.prova"));
internal static SiteRequest Graph() => SiteRequest.For(TestSite.ProfileQuery, (CaseValues.Username, "marta.prova"));
private static SiteOperation Operation(string name) => Catalog.Operations.Single(o => o.Name == name);
private static SiteAnswerSignal Classify(SiteAnswer answer) =>
Signals.Classify(Operation(answer.Operation), answer, DateTimeOffset.UnixEpoch) ?? throw new AssertFailedException($"Nessun segnale per {answer}.");
private static void AssertNoAddress(string message, int port)
{
Assert.IsFalse(message.Contains("127.0.0.1", StringComparison.Ordinal), message);
Assert.IsFalse(message.Contains(port.ToString(System.Globalization.CultureInfo.InvariantCulture), StringComparison.Ordinal), message);
Assert.IsFalse(message.Contains("marta", StringComparison.Ordinal), message);
Assert.IsFalse(message.Contains("http", StringComparison.Ordinal), message);
}
/// <summary>The saved session of a test: the cookies, tokens and doc_id the pages of the test site hand out.</summary>
internal static SiteSessionValues Values(string? session = SiteControl.InitialSession, string? csrf = Csrf, string appId = TestSite.AppId, bool docIds = true)
{
// Host-only cookies of the test site's host, path «/», as its pages set them.
var cookies = new List<SiteCookie>();
if (csrf is not null)
{
cookies.Add(SiteCookie.ForHost(TestSite.CsrfCookie, csrf, "127.0.0.1"));
}
if (session is not null)
{
cookies.Add(SiteCookie.ForHost(TestSite.SessionCookie, session, "127.0.0.1"));
}
cookies.Add(SiteCookie.ForHost("altro", OtherCookie, "127.0.0.1"));
return new SiteSessionValues(cookies, UserAgent, PageValues(appId), docIds ? TestSite.DocIds : null);
}
/// <summary>The values the pages of the test site hold (the «page» section of richieste.prova.json), and the claim.</summary>
internal static Dictionary<string, string> PageValues(string appId = TestSite.AppId) => new()
{
["app_id"] = appId,
["lsd"] = TestSite.Lsd,
["fb_dtsg"] = TestSite.Dtsg,
["claim"] = "0",
["account_id"] = AccountId,
["rev"] = TestSite.Revision,
["hs"] = TestSite.HasteSession,
["hsi"] = TestSite.HasteSessionId,
["dpr"] = TestSite.Dpr,
["comet_req"] = TestSite.CometReq,
["ccg"] = TestSite.ConnectionClass,
["entry_nonce"] = TestSite.EntryNonce,
};
/// <summary>A site client for the test site with its session, clock and log.</summary>
private sealed class Bed : IDisposable
{
private readonly ConcurrentQueue<string> _log = new();
/// <param name="site">The test site.</param>
/// <param name="values">The saved session; <see cref="Values"/> by default.</param>
/// <param name="options">Options; the test site's by default.</param>
/// <param name="noSession">No saved session at all.</param>
/// <param name="catalog">The catalog; the shipped one by default.</param>
public Bed(TestSite site, SiteSessionValues? values = null, HttpSiteRequesterOptions? options = null, bool noSession = false, RequestCatalog? catalog = null)
{
Session = new InMemorySiteSession(noSession ? null : values ?? Values());
Requester = new HttpSiteRequester(catalog ?? Catalog, Session, options ?? HttpSiteRequesterOptions.TestSite(new Uri(site.SiteUrl + "/")), Clock, _log.Enqueue);
}
public InMemorySiteSession Session { get; }
public VirtualClock Clock { get; } = new();
public HttpSiteRequester Requester { get; }
public IReadOnlyList<string> Log => [.. _log];
public Task<SiteAnswer> SendAsync(SiteRequest request, ActionKind kind) =>
Requester.SendAsync(request, Permit.Grant(kind, null), CancellationToken.None);
public void Dispose() => Requester.Dispose();
}
/// <summary>A clock that does not move.</summary>
private sealed class FixedTime(DateTimeOffset now) : TimeProvider
{
public override DateTimeOffset GetUtcNow() => now;
}
}