line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Mock::LWP::Conditional; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
1112093
|
use 5.008001; |
|
7
|
|
|
|
|
25
|
|
|
7
|
|
|
|
|
267
|
|
4
|
7
|
|
|
7
|
|
36
|
use strict; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
198
|
|
5
|
7
|
|
|
7
|
|
33
|
use warnings; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
158
|
|
6
|
7
|
|
|
7
|
|
9431
|
use LWP::UserAgent; |
|
7
|
|
|
|
|
159367
|
|
|
7
|
|
|
|
|
257
|
|
7
|
7
|
|
|
7
|
|
74
|
use Scalar::Util qw(blessed refaddr); |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
577
|
|
8
|
7
|
|
|
7
|
|
6409
|
use Sub::Install qw(install_sub); |
|
7
|
|
|
|
|
12104
|
|
|
7
|
|
|
|
|
46
|
|
9
|
7
|
|
|
7
|
|
7652
|
use Class::Method::Modifiers qw(install_modifier); |
|
7
|
|
|
|
|
11735
|
|
|
7
|
|
|
|
|
664
|
|
10
|
7
|
|
|
7
|
|
3893
|
use Test::Mock::LWP::Conditional::Stubs; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
3561
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $Stubs = +{ __GLOBAL__ => +{} }; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _set_stub { |
18
|
13
|
|
|
13
|
|
41
|
my ($key, $uri, $res) = @_; |
19
|
|
|
|
|
|
|
|
20
|
13
|
|
100
|
|
|
171
|
$Stubs->{$key} ||= +{}; |
21
|
13
|
|
66
|
|
|
224
|
$Stubs->{$key}->{$uri} ||= Test::Mock::LWP::Conditional::Stubs->new; |
22
|
|
|
|
|
|
|
|
23
|
13
|
|
|
|
|
81
|
$Stubs->{$key}->{$uri}->add($res); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _get_stub { |
27
|
31
|
|
|
31
|
|
253
|
my ($key, $uri) = @_; |
28
|
|
|
|
|
|
|
|
29
|
31
|
100
|
66
|
|
|
400
|
if (exists $Stubs->{$key} && exists $Stubs->{$key}->{$uri}) { |
|
|
100
|
|
|
|
|
|
30
|
6
|
|
|
|
|
141
|
return $Stubs->{$key}->{$uri}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
elsif (exists $Stubs->{__GLOBAL__}->{$uri}) { |
33
|
17
|
|
|
|
|
207
|
return $Stubs->{__GLOBAL__}->{$uri}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub stub_request { |
38
|
13
|
|
|
13
|
1
|
599618
|
my ($self, $uri, $res) = @_; |
39
|
13
|
100
|
|
|
|
182
|
my $key = blessed($self) ? refaddr($self) : '__GLOBAL__'; |
40
|
13
|
|
|
|
|
62
|
_set_stub($key, $uri, $res); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub reset_all { |
44
|
1
|
|
|
1
|
1
|
536
|
$Stubs = +{ __GLOBAL__ => +{} }; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
{ # LWP::UserAgent injection |
48
|
|
|
|
|
|
|
install_modifier('LWP::UserAgent', 'around', 'simple_request', sub { |
49
|
|
|
|
|
|
|
my $orig = shift; |
50
|
|
|
|
|
|
|
my ($self, $req, @rest) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $stub = _get_stub(refaddr($self), $req->uri); |
53
|
|
|
|
|
|
|
return $stub ? $stub->execute($req) : $orig->(@_); |
54
|
|
|
|
|
|
|
}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
install_sub({ |
57
|
|
|
|
|
|
|
code => __PACKAGE__->can('stub_request'), |
58
|
|
|
|
|
|
|
into => 'LWP::UserAgent', |
59
|
|
|
|
|
|
|
as => 'stub_request', |
60
|
|
|
|
|
|
|
}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Test::Mock::LWP::Conditional - stubbing on LWP request |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
use LWP::UserAgent; |
72
|
|
|
|
|
|
|
use HTTP::Response; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Test::More |
75
|
|
|
|
|
|
|
use Test::Mock::LWP::Conditional; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $uri = 'http://example.com/'; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# global |
80
|
|
|
|
|
|
|
Test::Mock::LWP::Conditional->stub_request($uri => HTTP::Response->new(503)); |
81
|
|
|
|
|
|
|
is LWP::UserAgent->new->get($uri)->code => 503; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# lexical |
84
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
85
|
|
|
|
|
|
|
$ua->stub_request($uri => sub { HTTP::Response->new(500) }); |
86
|
|
|
|
|
|
|
is $ua->get($uri)->code => 500; |
87
|
|
|
|
|
|
|
is LWP::UserAgent->new->get($uri)->code => 503; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# reset |
90
|
|
|
|
|
|
|
Test::Mock::LWP::Conditional->reset_all; |
91
|
|
|
|
|
|
|
is $ua->get($uri)->code => 200; |
92
|
|
|
|
|
|
|
is LWP::UserAgent->new->get($uri)->code => 200; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This module stubs out LWP::UserAgent's request. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over 4 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item * stub_request($uri, $res) |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Sets stub response for requesed URI. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * reset_all |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Clear all stub requests. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=back |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 AUTHOR |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
NAKAGAWA Masaki Emasaki@cpan.orgE |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 LICENSE |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
119
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 SEE ALSO |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
L, L, L, L |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
L |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L, L |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |