EV-WebKit

 view release on metacpan or  search on metacpan

eg/intercept.pl  view on Meta::CPAN

        push @log, "$req->{method} $req->{url}";

        # 1. BLOCK: refuse a whole class of request, with a visible answer.
        if ($req->{host} =~ /(?:^|\.)(?:doubleclick|googlesyndication)\./) {
            return { status => 403, body => 'blocked by eg/intercept.pl' };
        }

        # 2. MOCK: answer locally, without the network seeing anything.
        if ($req->{url} =~ m{/robots\.txt$}) {
            return { status => 200,
                     headers => { 'content-type' => 'text/plain' },
                     body    => "User-agent: *\nDisallow: /\n" };
        }

        # 3. REWRITE: change the request that does go out. Anything set here
        #    overrides curl-impersonate's own template header of the same name.
        $req->{headers}{'x-intercepted-by'} = 'eg/intercept.pl';
        return;   # ...and let it proceed
    },
);

t/76-on-request.t  view on Meta::CPAN

    $b->quit;
}

# --- the hook is really wired into that proxy: rewrite + mock, driven by a
# direct client request, so no external network is touched ---
SKIP: {
    my @seen;
    my $b = EV::WebKit->new(window => [200,150], on_request => sub {
        my ($req) = @_;
        push @seen, { method => $req->{method}, url => $req->{url}, host => $req->{host} };
        return { status => 418, headers => { 'content-type' => 'text/plain' },
                 body => 'intercepted' };
    });

    # skip, NOT plan skip_all: assertions have already run in this file, and a
    # plan emitted after them is a BAD PLAN -- the harness reports "You planned
    # 0 tests but ran N" and the whole run FAILS. Which would mean failing on
    # exactly the restricted hosts this guard exists to be gentle with.
    my $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $b->proxy_port,
                                     Proto => 'tcp', Timeout => 5)
        or skip "cannot connect to the interception proxy: $!", 7;

t/97-network-fingerprint.t  view on Meta::CPAN

# the proxy the on_request hook answers it, and not reached through the proxy
# there is nothing to resolve. (A 127.0.0.1 server cannot show this -- WebKit
# routes local addresses direct, bypassing the proxy entirely.)
{
    my @seen;
    my $p = EV::WebKit->new(window => [400,300], fingerprint => 'windows-chrome',
                            network_fingerprint => 1,
                            on_request => sub {
                                push @seen, ($_[0]{url} // '(no url)');
                                return { status  => 200,
                                         headers => { 'content-type' => 'text/html' },
                                         body    => '<html><head><title>viaproxy</title></head><body>ok</body></html>' };
                            });
    is($p->{session}->get_tls_errors_policy, 'ignore',
       'the proxy self-signed certificate is accepted');

    my ($err, $title);
    $p->go('http://probe.invalid/x', sub { (undef, $err) = @_; $title = $p->title; EV::break });
    TWK::run_with_timeout(30);

    is($err, undef, 'a navigation to an unresolvable host succeeds through the proxy') or diag $err;



( run in 1.877 second using v1.01-cache-2.11-cpan-800906f7e73 )