lib/Test/WWW/Mechanize/CGIApp.pm

filter: Statementstmt 80.9% 51 / 63 Branchbran 65.3% 17 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 76.1% 80 / 105 CC 15 SCAR 17.4
CC
15
Cov
75%
_make_request
11.5
_do_request
6.8
new
3.0
CRAP 5.7
↑ Summary
1  
2 package Test::WWW::Mechanize::CGIApp;
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # TODO use Test::WWW::Mechanize;
8 5 ▶use base 'Test::WWW::Mechanize';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use HTTP::Request::AsCGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = "0.05";
13  
14 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
15 4 my ($class, %cnf) = @_;
16 4 my $self;
17 my $app;
18  
19 4 ▶ if (exists($cnf{app})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cnf{"app"}) 2 2 4
20 2 $app = delete $cnf{app};
21 }
22  
23 4 $self = $class->SUPER::new(%cnf);
24  
25 4 ▶ $self->app( $app ) if ($app);
Branch: 1 branch
BranchTrueFalseTotal
if $app 2 2 4
26 4 return $self;
27 }
28  
29 12 ▶sub app {
Subroutine app: called CC=2 SCAR=6.9
Pod documented
30 12 my $self = shift;
31  
32 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 8 12
33 4 $self->{_app} = shift;
34 }
35 12 return $self->{_app};
36 }
37  
38 # copied from Test::WWW:Mechanize::Catalyst and slightly localized.
39 8 ▶sub _make_request {
Subroutine _make_request: called CC=6 SCAR=24.5
40 8 my ( $self, $request ) = @_;
41 8 $request = _cleanup_request($request);
42 8 ▶ $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookie_jar 8 0 8
43  
44 8 my $response = $self->_do_request( $request );
45  
46 8 $response->header( 'Content-Base', $request->uri );
47 8 $response->request($request);
48 8 ▶ $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookie_jar 8 0 8
49  
50 # check if that was a redirect
51 8 ▶ if ( $response->header('Location')
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("Location") and $self->redirect_ok($request, $response)) 0 8 8
Condition33%1/3
$response->header("Location") and $self->redirect_ok($request, $response)
!ll&&!rl&&r
800
MC/DC0%0/2
$response->header("Location") and $self->redirect_ok($request, $response)
$response->header("Location") $self->redirect_ok($request, $response)
Truth table33%1/3
A and B
A= $response->header("Location")B= $self->redirect_ok($request, $response)
ABresult
0X0
100
111
52 && $self->redirect_ok( $request, $response ) )
53 {
54  
55 # remember the old response
56 0 my $old_response = $response;
57  
58 # *where* do they want us to redirect to?
59 0 my $location = $old_response->header('Location');
60  
61 # no-one *should* be returning non-absolute URLs, but if they
62 # are then we'd better cope with it. Let's create a new URI, using
63 # our request as the base.
64 0 my $uri = URI->new_abs( $location, $request->uri )->as_string;
65  
66 # make a new response, and save the old response in it
67 0 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
68 0 my $end_of_chain = $response;
69 0 ▶ while ( $end_of_chain->previous ) # keep going till the end
Branch: 1 branch
BranchTrueFalseTotal
if ($end_of_chain->previous) 0 0 0
70 {
71 0 $end_of_chain = $end_of_chain->previous;
72 } # of the chain...
73 0 $end_of_chain->previous($old_response); # ...and add us to it
74 }
75  
76 8 return $response;
77 }
78  
79 8 ▶sub _cleanup_request {
Subroutine _cleanup_request: called CC=2 SCAR=6.9
80 8 my $request = shift;
81  
82 8 ▶ $request->uri('http://localhost' . $request->uri())
Branch: 1 branch
BranchTrueFalseTotal
unless $request->uri =~ /^http/ 4 4 8
83 unless ( $request->uri() =~ m|^http| );
84  
85 8 return($request);
86 }
87  
88 8 ▶sub _do_request {
Subroutine _do_request: called CC=6 SCAR=19.2
89 8 my $self = shift;
90 8 my $request = shift;
91  
92 8 my $cgi = HTTP::Request::AsCGI->new($request, %ENV)->setup;
93 8 my $app = $self->app();
94  
95 8 ▶ if (defined ($app)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $app) { } 8 0 8
96 8 ▶ if (ref $app) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app) { } 3 5 8
97 3 ▶ if (ref $app eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app eq 'CODE') { } 3 0 3
98 3 &{$app};
99 }
100 else {
101 0 die "The app value is a ref to something that isn't implemented.";
102 }
103 }
104 else {
105 # use eval since the module name isn't a BAREWORD
106 5 eval "require " . $app;
107  
108 5 ▶ if ($app->isa("CGI::Application::Dispatch")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($app->isa('CGI::Application::Dispatch')) { } 0 5 5
elsif ($app->isa('CGI::Application')) { } 5 0 5
109 0 $app->dispatch();
110 }
111 elsif ($app->isa("CGI::Application")) {
112 5 my $app = $app->new();
113 5 $app->run();
114 }
115 else {
116 0 die "Unable to use the value of app.";
117 }
118 }
119 }
120 else {
121 0 die "App was not defined.";
122 }
123  
124 8 return $cgi->restore->response;
125 }
126  
127  
128 5 1;
129  
130 __END__
↑ Summary