File: 03-handlers.t

package info (click to toggle)
libtest-lwp-useragent-perl 0.036-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: perl: 530; makefile: 10
file content (55 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use strict;
use warnings;

use Test::More 0.88;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::Deep;
use Test::LWP::UserAgent;


# all phases as defined in the LWP::UserAgent documentation
my @request_phases = qw(request_preprepare request_prepare request_send);
# TODO: I'm not sure how to get response_header and response_data to be called...
my @response_phases = qw(response_done response_redirect);

{
    my $useragent = Test::LWP::UserAgent->new;
    $useragent->map_response(
        qr/localhost/,
        HTTP::Response->new('200', 'OK', ['Content-Type' => 'text/plain'], 'all good!'),
    );

    my %phase_called;

    foreach my $phase (@request_phases)
    {
        $useragent->add_handler($phase => sub {
                my ($request, $ua, $h, $data) = @_;
                isa_ok($request, 'HTTP::Request');
                isa_ok($ua, 'LWP::UserAgent');
                $phase_called{$phase} = 1;
                return;
            }, m_host => 'localhost');
    }

    foreach my $phase (@response_phases)
    {
        $useragent->add_handler($phase => sub {
                my ($response, $ua, $h, $data) = @_;
                isa_ok($response, 'HTTP::Response');
                isa_ok($ua, 'LWP::UserAgent');
                $phase_called{$phase} = 1;
                return;
            }, m_host => 'localhost');
    }

    my $response = $useragent->get('http://localhost');

    cmp_deeply(
        \%phase_called,
        { map +($_ => 1), @request_phases, @response_phases },
        'all handlers called',
    );
}

done_testing;