| 1 | package Test::Mock::LWP::Conditional; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 7 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 7 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 7 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 7 | ▶ | use Sub::Install qw(install_sub); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 7 | ▶ | use Class::Method::Modifiers qw(install_modifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 7 | ▶ | use Math::Random::Secure qw(irand); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 7 | ▶ | use Test::Mock::LWP::Conditional::Stubs; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 7 | our $VERSION = '0.04'; | |||||||||||||||||||
| 14 | 7 | $VERSION = eval $VERSION; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 7 | our $Stubs = +{ __GLOBAL__ => +{} }; | |||||||||||||||||||
| 17 | 7 | our $Regex = +{ __GLOBAL__ => +{} }; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 15 | ▶ | sub _set_stub { | ||||||||||||||||||
|
Subroutine
_set_stub: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 20 | 15 | my ($key, $uri, $res) = @_; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 15 | ▶ | $Stubs->{$key} ||= +{}; | ||||||||||||||||||
|
Condition100%2/2
$Test::Mock::LWP::Conditional::Stubs->{$key} ||= {}
MC/DC100%1/1
$Test::Mock::LWP::Conditional::Stubs->{$key} ||= {}
$Test::Mock::LWP::Conditional::Stubs->{$key}
Truth table100%2/2
A
A= $Test::Mock::LWP::Conditional::Stubs->{$key}
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 15 | ▶ | if (ref $uri eq 'Regexp') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 2 | my $rng = irand('9999999999999999'); | |||||||||||||||||||
| 26 | 2 | $Regex->{$key}->{$rng} = $uri; | |||||||||||||||||||
| 27 | 2 | $uri = $rng; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 15 | ▶ | $Stubs->{$key}->{$uri} ||= Test::Mock::LWP::Conditional::Stubs->new; | ||||||||||||||||||
|
Condition66%2/3
$Test::Mock::LWP::Conditional::Stubs->{$key}{$uri} ||= "Test::Mock::LWP::Conditional::Stubs"->new
MC/DC0%0/2
$Test::Mock::LWP::Conditional::Stubs->{$key}{$uri} ||= "Test::Mock::LWP::Conditional::Stubs"->new
$Test::Mock::LWP::Conditional::Stubs->{$key}{$uri}
"Test::Mock::LWP::Conditional::Stubs"->new
Truth table66%2/3
A ||= B
A= $Test::Mock::LWP::Conditional::Stubs->{$key}{$uri}B= "Test::Mock::LWP::Conditional::Stubs"->new
| |||||||||||||||||||||
| 31 | 15 | $Stubs->{$key}->{$uri}->add($res); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 37 | ▶ | sub _get_stub { | ||||||||||||||||||
|
Subroutine
_get_stub: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 35 | 37 | my ($key, $uri) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 37 | ▶ | if (exists $Stubs->{$key}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 38 | 9 | ▶ | if (exists $Stubs->{$key}->{$uri}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 6 | return $Stubs->{$key}->{$uri}; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | 3 | return _check_regex($key, $uri); | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | elsif (exists $Stubs->{__GLOBAL__}->{$uri}) { | ||||||||||||||||||||
| 44 | 17 | return $Stubs->{__GLOBAL__}->{$uri}; | |||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||
| 46 | 11 | return _check_regex("__GLOBAL__", $uri); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 14 | ▶ | sub _check_regex { | ||||||||||||||||||
|
Subroutine
_check_regex: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 51 | 14 | my ($key, $uri) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 14 | foreach my $marker (keys %{$Regex->{$key}}) { | |||||||||||||||||||
| 54 | 5 | ▶ | if ($uri =~ $Regex->{$key}->{$marker}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 4 | return $Stubs->{$key}->{$marker}; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 15 | ▶ | sub stub_request { | ||||||||||||||||||
|
Subroutine
stub_request: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | 15 | my ($self, $uri, $res) = @_; | |||||||||||||||||||
| 62 | 15 | ▶ | my $key = blessed($self) ? refaddr($self) : '__GLOBAL__'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 15 | _set_stub($key, $uri, $res); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 1 | ▶ | sub reset_all { | ||||||||||||||||||
|
Subroutine
reset_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 67 | 1 | $Stubs = +{ __GLOBAL__ => +{} }; | |||||||||||||||||||
| 68 | 1 | $Regex = +{ __GLOBAL__ => +{} }; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | { # LWP::UserAgent injection | ||||||||||||||||||||
| 72 | 7 | ▶ | install_modifier('LWP::UserAgent', 'around', 'simple_request', sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 73 | 37 | my $orig = shift; | |||||||||||||||||||
| 74 | 37 | my ($self, $req, @rest) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 37 | my $stub = _get_stub(refaddr($self), $req->uri); | |||||||||||||||||||
| 77 | 37 | ▶ | return $stub ? $stub->execute($req) : $orig->(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 7 | }); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 7 | install_sub({ | |||||||||||||||||||
| 81 | code => __PACKAGE__->can('stub_request'), | ||||||||||||||||||||
| 82 | into => 'LWP::UserAgent', | ||||||||||||||||||||
| 83 | as => 'stub_request', | ||||||||||||||||||||
| 84 | }); | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 7 | 1; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 NAME | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Test::Mock::LWP::Conditional - stubbing on LWP request | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head1 SYNOPSIS | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | use LWP::UserAgent; | ||||||||||||||||||||
| 96 | use HTTP::Response; | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | use Test::More | ||||||||||||||||||||
| 99 | use Test::Mock::LWP::Conditional; | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | my $uri = 'http://example.com/'; | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # global | ||||||||||||||||||||
| 104 | Test::Mock::LWP::Conditional->stub_request($uri => HTTP::Response->new(503)); | ||||||||||||||||||||
| 105 | is LWP::UserAgent->new->get($uri)->code => 503; | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # lexical | ||||||||||||||||||||
| 108 | my $ua = LWP::UserAgent->new; | ||||||||||||||||||||
| 109 | $ua->stub_request($uri => sub { HTTP::Response->new(500) }); | ||||||||||||||||||||
| 110 | is $ua->get($uri)->code => 500; | ||||||||||||||||||||
| 111 | is LWP::UserAgent->new->get($uri)->code => 503; | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | # reset | ||||||||||||||||||||
| 114 | Test::Mock::LWP::Conditional->reset_all; | ||||||||||||||||||||
| 115 | is $ua->get($uri)->code => 200; | ||||||||||||||||||||
| 116 | is LWP::UserAgent->new->get($uri)->code => 200; | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head1 DESCRIPTION | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | This module stubs out LWP::UserAgent's request. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 METHODS | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =over 4 | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =item * stub_request($uri, $res) | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | Sets stub response for requesed URI. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =item * reset_all | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Clear all stub requests. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =back | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 AUTHOR | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 LICENSE | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 143 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 SEE ALSO | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | L<Test::Mock::LWP>, L<Test::Mock::LWP::Dispatch>, L<Test::MockHTTP>, L<Test::LWP::MockSocket::http> | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | L<LWP::UserAgent> | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | L<https://github.com/bblimke/webmock>, L<https://github.com/chrisk/fakeweb> | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||