EV-YACurl

 view release on metacpan or  search on metacpan

t/07-edge-cases.t  view on Meta::CPAN

        CURLOPT_URL => "$base/binary",
        CURLOPT_WRITEFUNCTION => sub { $body .= $_[0] },
    });

    EV::run until $done;
    ok($res, "Binary: got response");
    is(length($body), 256, "Binary: correct length");

    my $expected = join('', map { chr($_) } 0..255);
    is($body, $expected, "Binary: all bytes correct");
}

# A server that never answers must be abandoned on the timeout.
# TestServer forks per connection, so the stuck one costs nothing here.
{
    my $silent = TestServer->new(sub { sleep 30; (200, [], 'too late') });
    my $client = EV::YACurl->new({});
    my ($res, $err);
    my $done = 0;
    my $started = EV::time();

    $client->request(sub {
        ($res, $err) = @_;
        $done = 1;
    }, {
        CURLOPT_URL => $silent->base_url . '/',
        CURLOPT_TIMEOUT_MS => 400,
        CURLOPT_WRITEFUNCTION => sub { },
    });

    EV::run until $done;
    ok(!$res, "Timeout: no response");
    like($err // '', qr/timed?\s*out/i, "Timeout: error message ($err)");
    cmp_ok(EV::time() - $started, '<', 5, 'Timeout: gave up promptly');
}

{
    my $client = EV::YACurl->new({});
    my $completed = 0;
    my @connect_times;

    for (1..3) {
        my $done = 0;
        my $response;

        $client->request(sub {
            ($response, my $err) = @_;
            if ($response) {
                push @connect_times, $response->getinfo(CURLINFO_NUM_CONNECTS);
            }
            $done = 1;
            $completed++;
        }, {
            CURLOPT_URL => "$base/",
            CURLOPT_WRITEFUNCTION => sub { },
        });

        EV::run until $done;
    }

    is($completed, 3, "Keep-alive: all requests completed");
    # On localhost a reconnect is as fast as the first connect, so timing proves
    # nothing here; the count of new connections does.
    is_deeply([@connect_times[1, 2]], [0, 0], "Keep-alive: later requests opened no connection")
        or diag "num_connects: @connect_times";
}

{
    my $client = EV::YACurl->new({});
    my ($res, $err);
    my $done = 0;
    my @headers;

    $client->request(sub {
        ($res, $err) = @_;
        $done = 1;
    }, {
        CURLOPT_URL => "$base/multi-header",
        CURLOPT_WRITEFUNCTION => sub { },
        CURLOPT_HEADERFUNCTION => sub { push @headers, $_[0] },
    });

    EV::run until $done;
    ok($res, "Headers: got response");
    ok(scalar @headers >= 2, "Headers: captured " . scalar(@headers) . " headers");
}



( run in 3.104 seconds using v1.01-cache-2.11-cpan-c221a9de4ec )