blib/lib/HTTP/RecordedSession.pm

filter: Statementstmt 40.5% 28 / 69 Branchbran 11.1% 2 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 63.6% 7 / 11 Podpod 66.6% 2 / 3 totaltotal 37.7% 40 / 106 CC 15 SCAR 32.3
CC
15
Cov
34%
_format_for_wt
72.0
_format_for_mw
6.0
_deserialize_clicks
4.3
CRAP 25.2
↑ Summary
1 package HTTP::RecordedSession;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION = '0.05';
5  
6 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
7 1 my ( $proto ) = shift;
8 1 ▶ my ( $class ) = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
9 1 my ( $self ) = {};
10 1 bless( $self, $class );
11 1 $self->_init( @_ );
12 0 return $self;
13 }
14  
15 1 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.2
16 1 my ( $self ) = shift;
17  
18 1 my ( %args ) = (@_);
19 1 $self->{uc($_)} = $args{$_} foreach (keys %args);
20  
21 #assign defaults, if necessary
22 1 ▶ $self->{ PATH } = "/usr/tmp" unless ( defined $self->{ PATH } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"PATH"} 0 1 1
23 1 ▶ $self->{ TEST_MOD } = "Monkeywrench" unless ( defined $self->{ TEST_MOD } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"TEST_MOD"} 0 1 1
24  
25 1 $self->{ CLICK_AREF } = $self->_deserialize_clicks;
26 }
27  
28 0 ▶sub get_id {
Subroutine get_id: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my ( $self ) = shift;
30 0 return $self->{ CONFIG_ID };
31 }
32  
33 0 ▶sub get_clicks {
Subroutine get_clicks: not called CC=1 SCAR=6.9
Pod documented
34 0 my ( $self ) = shift;
35 0 return $self->{ CLICK_AREF };
36 }
37  
38 1 ▶sub _deserialize_clicks {
Subroutine _deserialize_clicks: called CC=3 SCAR=14.6
39 1 my ( $self ) = shift;
40  
41 1 ▶ use Storable qw( lock_retrieve );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 my ( $file_path ) = $self->{ PATH } . "recorder_conf_" . $self->{ CONFIG_ID };
43 1 my ( $hashref ) = lock_retrieve( $file_path );
44  
45 0 my ( $session_aref );
46 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 0 ▶ if ( $self->{ TEST_MOD } =~ /Monkeywrench/i ) { $session_aref = $self->_format_for_mw( $hashref ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'TEST_MOD'} =~ /Monkeywrench/i) { } 0 0 0
elsif ($self->{'TEST_MOD'} =~ /WebTest/i) { } 0 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 0 ▶sub _format_for_mw {
Subroutine _format_for_mw: not called CC=2 SCAR=17.9
54 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 0 ▶sub _format_for_wt {
Subroutine _format_for_wt: not called CC=8 SCAR=42.8
65 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 } } ) {
71 0 ▶ if ( $element eq 'acceptcookie' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq "acceptcookie") 0 0 0
72 0 $hashref->{ $key }{ 'accept_cookies' } = $hashref->{ $key }{ $element };
73 0 ▶ if ( $hashref->{ $key }{ 'accept_cookies' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashref->{$key}{'accept_cookies'} == 1) { } 0 0 0
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 ▶ if ( $element eq 'sendcookie' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq "sendcookie") 0 0 0
82 0 $hashref->{ $key }{ 'send_cookies' } = $hashref->{ $key }{ $element };
83 0 ▶ if ( $hashref->{ $key }{ 'send_cookies' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashref->{$key}{'send_cookies'} == 1) { } 0 0 0
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 ▶ $hashref->{ $key }{ $element } = lc( $hashref->{ $key }{ $element } ) if ( $element eq 'method' );
Branch: 1 branch
BranchTrueFalseTotal
if $element eq "method" 0 0 0
92 0 $hashref->{ $key }{ test_name } = "$key";
93 }
94 0 push( @session, $hashref->{ $key } );
95 }
96 0 return \@session;
97 }
98 1 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 <cbrooks@organiccodefarm.com>
235  
236 =head1 SEE ALSO
237  
238 Apache::Recorder
239  
240 HTTP::Monkeywrench
241  
242 HTTP::WebTest
243  
244 =cut
↑ Summary