| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # check if that was a redirect | ||||||||||||||||||||||||||||
| 51 | 8 | ▶ | if ( $response->header('Location') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->header("Location") and $self->redirect_ok($request, $response)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | 8 | ▶ | if (ref $app) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | if (ref $app eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||