File Coverage

blib/lib/Test/MockHTTP.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 2 2 100.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             package Test::MockHTTP;
2              
3 6     6   324008 use warnings;
  6         15  
  6         196  
4 6     6   34 use strict;
  6         10  
  6         213  
5              
6             =head1 NAME
7              
8             Test::MockHTTP - utility to test LWP usage without actual HTTP requests
9              
10             =cut
11              
12 6     6   32 use base qw/Exporter/;
  6         9  
  6         10639  
13              
14             our @EXPORT = qw/http_cmp http_test_setup/;
15              
16 6     6   5913 use Test::MockObject::Extends;
  6         61587  
  6         62  
17 6     6   748 use Test::Deep;
  6         14  
  6         1513  
18 6     6   7323 use LWP::UserAgent;
  6         340303  
  6         1299  
19              
20             my $Http_test_setup;
21             my $Mock_ua;
22              
23             =head1 SYNOPSIS
24              
25             use Test::MockHTTP;
26              
27             my $ua;
28             http_test_setup { $ua = $_[0] };
29              
30             http_cmp( sub { $ua->do_some_http_request() },
31             [
32             as_string => re('image1_link=\S+mail\.rambler\.ru'),
33             # these are pairs of method => test againt its return
34             # value
35             ]
36             );
37             ...
38              
39             =cut
40              
41             =head1 FUNCTIONS
42              
43             =head2 http_test_setup
44              
45             This function takes one coderef argument which gets called with a
46             specially crafted (with mocked methods) LWP::UserAgent instance before
47             each test. All actions should use this UA to be tested.
48              
49             =cut
50              
51             sub http_test_setup(&) {
52 6     6 1 5454 $Http_test_setup = shift;
53             }
54              
55             =head2 http_cmp
56             This is a wrapper around Test::Deep::cmp_deeply which does the actual
57             tests.
58              
59             It takes 4 arguments.
60              
61             =over 2
62              
63             =item $code
64              
65             This is the code to test. It gets called.
66              
67             =item $methods
68              
69             This is an arrayref of pairs 'method' => 'test against its return
70             value'. It gets passed into Test::Deep::methods to construct a test
71             against HTTP::Request which is provided by LWP.
72              
73             =item $msg
74              
75             TAP message, is directly passed to Test::Deep::cmp_deeply.
76              
77             =item $resp
78              
79             HTTP::Response instance which is returned as a fake response to HTTP::Request
80             from simple_request() method. Defaults to simple '200 Ok' empty
81             response.
82              
83             =back
84              
85             =cut
86              
87             sub http_cmp {
88 41     41 1 111105 my ($code, $methods, $msg, $resp) = @_;
89              
90 41 50       222 $Http_test_setup->($Mock_ua) if $Http_test_setup;
91              
92             $Mock_ua->mock(simple_request => sub {
93 41     41   4579 cmp_deeply($_[1], methods(@$methods), $msg);
94              
95 41   66     53622 return $resp || HTTP::Response->new(200, 'Ok',
96             [Server => 'mock'], '{"result": "ok"}'); # JSON
97 41         1146 });
98              
99 41         2260 $code->();
100             }
101              
102             $Mock_ua = Test::MockObject::Extends->new(LWP::UserAgent->new);