File Coverage

blib/lib/HTTP/RecordedSession.pm
Criterion Covered Total %
statement 26 67 38.8
branch 2 18 11.1
condition 1 3 33.3
subroutine 7 11 63.6
pod 2 3 66.6
total 38 102 37.2


line stmt bran cond sub pod time code
1             package HTTP::RecordedSession;
2 1     1   1093 use strict;
  1         2  
  1         40  
3 1     1   5 use vars qw( $VERSION );
  1         1  
  1         299  
4             $VERSION = '0.05';
5              
6             sub new {
7 1     1 1 105 my ( $proto ) = shift;
8 1   33     10 my ( $class ) = ref( $proto ) || $proto;
9 1         3 my ( $self ) = {};
10 1         3 bless( $self, $class );
11 1         6 $self->_init( @_ );
12 0         0 return $self;
13             }
14              
15             sub _init {
16 1     1   3 my ( $self ) = shift;
17              
18 1         9 my ( %args ) = (@_);
19 1         17 $self->{uc($_)} = $args{$_} foreach (keys %args);
20              
21             #assign defaults, if necessary
22 1 50       5 $self->{ PATH } = "/usr/tmp" unless ( defined $self->{ PATH } );
23 1 50       4 $self->{ TEST_MOD } = "Monkeywrench" unless ( defined $self->{ TEST_MOD } );
24              
25 1         5 $self->{ CLICK_AREF } = $self->_deserialize_clicks;
26             }
27              
28             sub get_id {
29 0     0 0 0 my ( $self ) = shift;
30 0         0 return $self->{ CONFIG_ID };
31             }
32              
33             sub get_clicks {
34 0     0 1 0 my ( $self ) = shift;
35 0         0 return $self->{ CLICK_AREF };
36             }
37              
38             sub _deserialize_clicks {
39 1     1   3 my ( $self ) = shift;
40              
41 1     1   1113 use Storable qw( lock_retrieve );
  1         6450  
  1         136  
42 1         3 my ( $file_path ) = $self->{ PATH } . "recorder_conf_" . $self->{ CONFIG_ID };
43 1         6 my ( $hashref ) = lock_retrieve( $file_path );
44              
45 0           my ( $session_aref );
46 1     1   6149 use Data::Dumper;
  1         29376  
  1         663  
47 0 0         if ( $self->{ TEST_MOD } =~ /Monkeywrench/i ) { $session_aref = $self->_format_for_mw( $hashref ) }
  0 0          
48 0           elsif ( $self->{ TEST_MOD } =~ /WebTest/i ) { $session_aref = $self->_format_for_wt( $hashref ) }
49 0           else { die "Please specify either WebTest or Monkeywrench as the value of the test_mod hash element" }
50 0           return $session_aref;
51             }
52              
53             sub _format_for_mw {
54 0     0     my ( $self ) = shift;
55 0           my ( $hashref ) = shift;
56 0           my ( @keys ) = sort keys %$hashref;
57 0           my ( @session );
58 0           foreach my $key ( @keys ) {
59 0           push( @session, $hashref->{ $key } );
60             }
61 0           return \@session;
62             }
63              
64             sub _format_for_wt {
65 0     0     my ( $self ) = shift;
66 0           my ( $hashref ) = shift;
67 0           my ( @keys ) = sort keys %$hashref;
68 0           my ( @session );
69 0           foreach my $key ( @keys ) {
70 0           foreach my $element (keys %{ $hashref->{ $key } } ) {
  0            
71 0 0         if ( $element eq 'acceptcookie' ) {
72 0           $hashref->{ $key }{ 'accept_cookies' } = $hashref->{ $key }{ $element };
73 0 0         if ( $hashref->{ $key }{ 'accept_cookies' } == 1 ) {
74 0           $hashref->{ $key }{ 'accept_cookies' } = 'yes';
75             }
76             else {
77 0           $hashref->{ $key }{ 'accept_cookies' } = 'no';
78             }
79 0           delete $hashref->{ $key }{ $element };
80             }
81 0 0         if ( $element eq 'sendcookie' ) {
82 0           $hashref->{ $key }{ 'send_cookies' } = $hashref->{ $key }{ $element };
83 0 0         if ( $hashref->{ $key }{ 'send_cookies' } == 1 ) {
84 0           $hashref->{ $key }{ 'send_cookies' } = 'yes';
85             }
86             else {
87 0           $hashref->{ $key }{ 'send_cookies' } = 'no';
88             }
89 0           delete $hashref->{ $key }{ $element };
90             }
91 0 0         $hashref->{ $key }{ $element } = lc( $hashref->{ $key }{ $element } ) if ( $element eq 'method' );
92 0           $hashref->{ $key }{ test_name } = "$key";
93             }
94 0           push( @session, $hashref->{ $key } );
95             }
96 0           return \@session;
97             }
98             1;
99              
100             =head1 NAME
101              
102             HTTP::RecordedSession - Class to interface with serialized clicks from Apache::Recorder
103              
104             =head1 SYNOPSIS
105              
106             Two sample scripts are provided below: one for HTTP::Monkeywrench, and one for
107             HTTP::WebTest.
108              
109             ###################### Monkeywrench #####################
110              
111             use strict;
112              
113             use HTTP::RecordedSession;
114              
115             use HTTP::Monkeywrench;
116              
117             my ( $config_id ) = '1WFmxpCj'; #ID from recorder.pl
118              
119             my ( $conf ) = new HTTP::RecordedSession(
120             config_id => $config_id,
121             path => "/usr/tmp/", # optional
122             test_mod => "Monkeywrench", # optional
123             );
124              
125             my ( $clicks ) = $conf->get_clicks;
126              
127             my ( %settings ) = ( #See Monkeywrench docs
128             show_cookies => '1',
129             print_results => '1',
130             );
131              
132             my ( $wrench ) = HTTP::Monkeywrench->new( \%settings );
133              
134             $wrench->test( $clicks );
135              
136             ###################### WebTest #########################
137              
138             use strict;
139              
140             use HTTP::RecordedSession;
141              
142             use HTTP::WebTest qw( run_web_test );
143              
144             my ( $config_id ) = '1WFmxpCj'; #ID from recorder.pl
145              
146             my ( $conf ) = new HTTP::RecordedSession(
147             config_id => $config_id,
148             path => "/usr/tmp/", # optional
149             test_mod => "WebTest", # optional
150             );
151              
152             my ( $clicks ) = $conf->get_clicks;
153              
154             my ( %options ) = ( #See WebTest docs
155             show_cookies => 'yes',
156             terse => 'summary',
157             );
158              
159             my ( $num_fail, $num_succeed );
160              
161             my ( $results ) = run_web_test($clicks, \$num_fail, \$num_succeed, \%options);
162              
163             #######################
164              
165             =head1 DESCRIPTION
166              
167             HTTP::RecordedSession will correctly format the output of Apache::Recorder
168             for a script that uses either HTTP::Monkeywrench or HTTP::WebTest.
169              
170             The HTTP::RecordedSession::new() method accepts a hashref with three
171             possible elements:
172              
173             =over 4
174              
175             =item * config_id: This is the id provided by recorder.pl when you first
176             begin recording an HTTP session. This element is required.
177              
178             =item * path: This is intended to provide greater portability -- you do
179             not have to use the (Linux-based) default path of "/usr/tmp/",
180             although RecordedSession will default to this to ensure backwards
181             compatibility if no path is provided.
182            
183              
184             =item * test_mod: This option allows you to choose between HTTP::
185             Monkeywrench and HTTP::WebTest to actually test your recorded
186             session. HTTP::RecordedSession will default to 'Monkeywrench'
187             to ensure backwards compatibility.
188              
189             =back
190              
191             There are only three public methods:
192              
193             =over 4
194              
195             =item * new()
196              
197             =item * get_clicks: this method returns the clicks formatted for either HTTP::Monkeywrench
198             or HTTP::WebTest.
199              
200             =item * get_id: this method returns the config_id that is passed to the
201             HTTP::RecordedSession constructor.
202              
203             =back
204              
205             =head2 Notes:
206              
207             =over 4
208              
209             =item * Scripts that were written using HTTP::RecordedSession version 0.03 are not
210             compatible out of the box with scripts written using version 0.04. The $self->get_clicks
211             method returned an arrayref to an arrayref of hashrefs in version 0.03. This has been
212             fixed in version 0.04, so that $self->get_clicks returns a simple arrayref of hashrefs.
213             In terms of code, you need to change:
214              
215              
216             my ( $clicks ) = @{ $conf->get_clicks };
217              
218             to:
219              
220             my ( $clicks ) = $conf->get_clicks;
221              
222             Apologies for the inconvenience.
223              
224             =item * By default HTTP::RecordedSession sets the acceptcookie / sendcookie (Monkeywrench) and
225             accept_cookies / send_cookies (WebTest) parameters to 1 and yes respectively. You can change
226             this behavior for the entire test in the %options hash. However, if you only want to change
227             it for a subset of the clicks in $clicks, you will (at present) need to loop through the
228             clicks and set them by hand.
229              
230             =back
231              
232             =head1 AUTHOR
233              
234             Chris Brooks
235              
236             =head1 SEE ALSO
237              
238             Apache::Recorder
239              
240             HTTP::Monkeywrench
241              
242             HTTP::WebTest
243              
244             =cut