Files
Febe/tests/Febe.Tests/Collection/Support/ScriptedRequester.cs
T
Alby96andClaude Opus 5.5 76dd1b6032
verifica / controlli (push) Successful in 17s
verifica / prove (push) Successful in 18m22s
verifica / installatore (push) Successful in 4m43s
verifica / immagine (push) Successful in 17m20s
Giunto per le richieste dirette, senza cambiare comportamento (#36, S1)
Le risposte catturate dal browser e quelle che il client del sito chiederà
devono arrivare ai lettori e ai segnali nello stesso modo: la consegna passa
in ResponseDelivery (Core), che ResponseTap usa con gli stessi conteggi e
messaggi. Arrivano i contratti SiteRequest, SiteAnswer e ISiteRequester con
SiteRequestGuard (un permesso concesso, della categoria giusta, per una sola
richiesta) e RequestCatalog, che legge richieste.<sito>.json e tiene nel
codice i divieti: nomi con Mutation, chat, telemetria, /ajax/, /ws/, percorsi
con segmenti strani. Nessun doc_id nel repository; nessuna operazione di
Instagram è ancora configurata perché le forme della sonda non dicono da dove
vengano alcuni campi (tappa S4). ScriptedRequester risponde dai campioni
ripuliti per le prove. Nulla di nuovo è collegato al motore.

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

167 lines
6.6 KiB
C#

using System.Collections.Concurrent;
using System.Text;
using System.Text.Json.Nodes;
using Febe.Core.Abstractions;
using Febe.Core.Collection.Requests;
namespace Febe.Tests.Collection.Support;
/// <summary>What <see cref="ScriptedRequester"/> answers to the next request, instead of the sample.</summary>
public enum ScriptedReply
{
/// <summary>The cleaned sample of the operation (<see cref="SiteOperation.Sample"/>).</summary>
Sample,
/// <summary>A 429 without body (§7.6, Rallentamento).</summary>
TooManyRequests,
/// <summary>A 302 to the login page of the site, never followed.</summary>
RedirectToLogin,
/// <summary>A 302 to the challenge page of the site, never followed.</summary>
RedirectToChallenge,
/// <summary>A 200 with an HTML page where the catalog expects JSON.</summary>
HtmlInsteadOfJson,
/// <summary>The sample without its expected root field (the last one of <see cref="SiteOperation.RootFields"/>).</summary>
MissingRootField,
/// <summary>The sample, with the session cookie deleted by the site.</summary>
SessionCookieDeleted,
}
/// <summary>
/// The site client of the tests (issue #36): answers the requests of the catalog from the cleaned samples of
/// <c>tests/Febe.Tests/campioni/&lt;sito&gt;</c>, by operation name, and can be scripted to answer the stop cases of
/// §7.6. It honours the <see cref="ISiteRequester"/> contract: <see cref="SiteRequestGuard"/> before anything (a permit
/// used once is spent, <see cref="SpentPermits"/>), and one request at a time. Nothing leaves the process.
/// </summary>
public sealed class ScriptedRequester : ISiteRequester
{
private readonly RequestCatalog _catalog;
private readonly string _samples;
private readonly ConcurrentQueue<ScriptedReply> _script = new();
private readonly ConcurrentQueue<SiteRequest> _sent = new();
private readonly SpentPermits _spent = new();
private int _busy;
private int _refused;
/// <param name="catalog">The catalog the requests are checked against.</param>
/// <param name="sampleFolder">Folder of the cleaned samples; by default <c>campioni/&lt;site of the catalog&gt;</c> next to the tests.</param>
public ScriptedRequester(RequestCatalog catalog, string? sampleFolder = null)
{
_catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
_samples = sampleFolder ?? Path.Combine(AppContext.BaseDirectory, "campioni", catalog.Site);
}
/// <summary>Path of the login page a redirect points to.</summary>
public string LoginLocation { get; init; } = "https://www.instagram.com/accounts/login/?next=%2F";
/// <summary>Path of the challenge page a redirect points to.</summary>
public string ChallengeLocation { get; init; } = "/challenge/AbCdEf123/?next=%2F";
/// <summary>Name of the session cookie (<see cref="ScriptedReply.SessionCookieDeleted"/>).</summary>
public string SessionCookie { get; init; } = "sessionid";
/// <summary>Requests that passed the guard, in order.</summary>
public IReadOnlyList<SiteRequest> Sent => [.. _sent];
/// <summary>Requests refused before anything was answered.</summary>
public int Refused => Volatile.Read(ref _refused);
/// <summary>The replies of the next requests, in order; after them, the samples again.</summary>
public ScriptedRequester Then(params ScriptedReply[] replies)
{
foreach (var reply in replies)
{
_script.Enqueue(reply);
}
return this;
}
public async Task<SiteAnswer> SendAsync(SiteRequest request, Permit permit, CancellationToken ct)
{
SiteOperation operation;
try
{
operation = SiteRequestGuard.Check(_catalog, request, permit, _spent);
}
catch (SiteRequestRefusedException)
{
Interlocked.Increment(ref _refused);
throw;
}
if (Interlocked.Exchange(ref _busy, 1) == 1)
{
Interlocked.Increment(ref _refused);
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: un'altra richiesta è ancora in corso (una per volta).");
}
try
{
ct.ThrowIfCancellationRequested();
await Task.Yield();
_sent.Enqueue(request);
var reply = _script.TryDequeue(out var next) ? next : ScriptedReply.Sample;
return Answer(operation, reply);
}
finally
{
Volatile.Write(ref _busy, 0);
}
}
/// <summary>The descriptor and the body of the sample of <paramref name="operation"/>.</summary>
public (string MimeType, JsonNode Body) Sample(SiteOperation operation)
{
ArgumentNullException.ThrowIfNull(operation);
if (operation.Sample is not { } name)
{
throw new InvalidOperationException($"«{operation.Name}» non ha un campione.");
}
var file = Directory.EnumerateFiles(_samples, $"{name}.json").Single();
var sample = JsonNode.Parse(File.ReadAllText(file))!;
return (sample["descrittore"]!["tipo"]!.GetValue<string>(), sample["corpo"]!.DeepClone());
}
private SiteAnswer Answer(SiteOperation operation, ScriptedReply reply)
{
switch (reply)
{
case ScriptedReply.TooManyRequests:
return new SiteAnswer(operation, 429, "text/plain", ReadOnlyMemory<byte>.Empty);
case ScriptedReply.RedirectToLogin:
return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory<byte>.Empty, redirectTo: LoginLocation);
case ScriptedReply.RedirectToChallenge:
return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory<byte>.Empty, redirectTo: ChallengeLocation);
case ScriptedReply.HtmlInsteadOfJson:
return new SiteAnswer(operation, 200, "text/html; charset=utf-8", Encoding.UTF8.GetBytes("<!DOCTYPE html><html><head><title>Pagina</title></head><body></body></html>"));
}
var (mime, body) = Sample(operation);
if (reply == ScriptedReply.MissingRootField)
{
var parts = operation.RootFields[^1].Split('.');
var parent = body;
foreach (var part in parts[..^1])
{
parent = parent[part]!;
}
parent.AsObject().Remove(parts[^1]);
}
var bytes = Encoding.UTF8.GetBytes(body.ToJsonString());
return new SiteAnswer(
operation,
200,
mime,
bytes,
cookiesDeleted: reply == ScriptedReply.SessionCookieDeleted ? [SessionCookie] : null);
}
}