Files
av-parser/tests/AvParser.Infrastructure.Tests/ProxyHandlerFactoryTests.cs
T
Leonid PershinandClaude Opus 5 9bf2ea5532 Add a proxy pool with rotation, liveness checks and a management page
The parser will need to move between proxies, so this adds the module it will
sit on: pluggable sources, a pool that hands proxies out and learns from the
outcome, three rotation strategies, and a page to drive it.

Sources are IProxySource implementations. The public proxifly/free-proxy-list
feed is fetched as the combined all/data.json through jsDelivr and filtered
locally — one conditional request beats four per-protocol ones that can disagree
mid-publish — and cached for the five minutes upstream takes to regenerate. A
feed that is down keeps serving its last payload rather than emptying the pool.
The user's own list lives in proxies.custom.json beside the settings, takes a
pasted blob, and names the lines it could not parse instead of quietly dropping
them.

Both knobs the pool exposes are settings, as asked: rotation is Sticky (default,
the only one that keeps site sessions coherent), RoundRobin or WeightedRandom;
liveness is either a parallel sweep of the whole pool or a probe at hand-out
time. Free lists are a few percent alive, so skipping verification entirely
means mostly waiting on timeouts.

Two invariants worth keeping, both of which cost a bug to find:

Availability is decided by the quarantine, never by Health. Excluding everything
that has ever failed made the quarantine window dead code and discarded proxies
permanently on their first hiccup, which is exactly wrong for addresses that
flap constantly. Health only orders the candidates now.

A probe verdict does not touch the success/failure counters. Those are about
real requests, and letting a sweep over a few thousand proxies rewrite them
would drown the evidence weighted selection reads.

SOCKS needs no extra package — .NET resolves socks4/socks4a/socks5 in WebProxy —
but a proxifly record with "protocol": "https" is still an HTTP proxy reached
over http:// with CONNECT, not an https:// scheme.

115 new tests. Also fixes a pre-existing flake: a command gated on another
command's IsExecuting cannot be driven straight after its Execute() completes,
because IsExecuting is published on the output scheduler.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-13 17:22:30 +03:00

83 lines
2.7 KiB
C#

using System.Net;
using AvParser.Core.Proxies;
using AvParser.Infrastructure.Proxies;
namespace AvParser.Infrastructure.Tests;
public class ProxyHandlerFactoryTests
{
[Theory]
[InlineData(ProxyProtocol.Http, "http://1.2.3.4:8080/")]
[InlineData(ProxyProtocol.Https, "http://1.2.3.4:8080/")]
[InlineData(ProxyProtocol.Socks4, "socks4://1.2.3.4:8080/")]
[InlineData(ProxyProtocol.Socks5, "socks5://1.2.3.4:8080/")]
public void Maps_the_protocol_onto_a_scheme_dotnet_understands(ProxyProtocol protocol, string expected)
{
var proxy = ProxyHandlerFactory.CreateWebProxy(new ProxyEndpoint(protocol, "1.2.3.4", 8080));
proxy.Address!.ToString().ShouldBe(expected);
}
[Fact]
public void Attaches_credentials_when_present()
{
var endpoint = new ProxyEndpoint(ProxyProtocol.Http, "1.2.3.4", 8080)
{
Username = "alice",
Password = "s3cret",
};
var credentials = ProxyHandlerFactory.CreateWebProxy(endpoint).Credentials.ShouldBeOfType<NetworkCredential>();
credentials.UserName.ShouldBe("alice");
credentials.Password.ShouldBe("s3cret");
}
[Fact]
public void Leaves_credentials_alone_when_there_are_none() =>
ProxyHandlerFactory
.CreateWebProxy(new ProxyEndpoint(ProxyProtocol.Http, "1.2.3.4", 8080))
.Credentials.ShouldBeNull();
[Fact]
public void A_null_endpoint_produces_a_direct_handler()
{
using var handler = ProxyHandlerFactory.CreateHandler(null, TimeSpan.FromSeconds(5));
handler.UseProxy.ShouldBeFalse();
handler.Proxy.ShouldBeNull();
}
[Fact]
public void An_endpoint_produces_a_proxied_handler()
{
using var handler = ProxyHandlerFactory.CreateHandler(
new ProxyEndpoint(ProxyProtocol.Socks5, "1.2.3.4", 1080),
TimeSpan.FromSeconds(5)
);
handler.UseProxy.ShouldBeTrue();
handler.Proxy.ShouldNotBeNull();
handler.ConnectTimeout.ShouldBe(TimeSpan.FromSeconds(5));
}
[Fact]
public void The_client_factory_honours_the_requested_timeout()
{
var factory = new ProxiedHttpClientFactory(new ProxyPool([], new NeverProbe(), new ProxyOptions()));
using var client = factory.Create(null, TimeSpan.FromSeconds(3));
client.Timeout.ShouldBe(TimeSpan.FromSeconds(3));
}
private sealed class NeverProbe : IProxyProbe
{
public Task<ProxyProbeResult> ProbeAsync(
ProxyEndpoint endpoint,
ProxyOptions options,
CancellationToken cancellationToken = default
) => Task.FromResult(ProxyProbeResult.Failure("not used"));
}
}