File Coverage

lib/Test/WWW/Mechanize/CGIApp.pm
Criterion Covered Total %
statement 49 61 80.3
branch 17 24 70.8
condition 1 3 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 78 99 78.7


line stmt bran cond sub pod time code
1              
2             package Test::WWW::Mechanize::CGIApp;
3              
4 4     4   106170 use strict;
  4         10  
  4         138  
5 4     4   22 use warnings;
  4         8  
  4         119  
6              
7             # TODO use Test::WWW::Mechanize;
8 4     4   19 use base 'Test::WWW::Mechanize';
  4         11  
  4         4628  
9              
10 4     4   1025435 use HTTP::Request::AsCGI;
  4         31249  
  4         44  
11              
12             our $VERSION = "0.05";
13              
14             sub new {
15 4     4 1 16661 my ($class, %cnf) = @_;
16 4         10 my $self;
17             my $app;
18              
19 4 100       24 if (exists($cnf{app})) {
20 2         11 $app = delete $cnf{app};
21             }
22              
23 4         44 $self = $class->SUPER::new(%cnf);
24              
25 4 100       51349 $self->app( $app ) if ($app);
26 4         21 return $self;
27             }
28              
29             sub app {
30 12     12 1 4465 my $self = shift;
31              
32 12 100       51 if (@_) {
33 4         14 $self->{_app} = shift;
34             }
35 12         42 return $self->{_app};
36             }
37              
38             # copied from Test::WWW:Mechanize::Catalyst and slightly localized.
39             sub _make_request {
40 8     8   105302 my ( $self, $request ) = @_;
41 8         30 $request = _cleanup_request($request);
42 8 50       48 $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
43              
44 8         2210 my $response = $self->_do_request( $request );
45              
46 8         7501 $response->header( 'Content-Base', $request->uri );
47 8         1601 $response->request($request);
48 8 50       124 $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
49              
50             # check if that was a redirect
51 8 50 33     978 if ( $response->header('Location')
52             && $self->redirect_ok( $request, $response ) )
53             {
54              
55             # remember the old response
56 0         0 my $old_response = $response;
57              
58             # *where* do they want us to redirect to?
59 0         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         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         0 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
68 0         0 my $end_of_chain = $response;
69 0         0 while ( $end_of_chain->previous ) # keep going till the end
70             {
71 0         0 $end_of_chain = $end_of_chain->previous;
72             } # of the chain...
73 0         0 $end_of_chain->previous($old_response); # ...and add us to it
74             }
75              
76 8         365 return $response;
77             }
78              
79             sub _cleanup_request {
80 8     8   21 my $request = shift;
81              
82 8 100       31 $request->uri('http://localhost' . $request->uri())
83             unless ( $request->uri() =~ m|^http| );
84              
85 8         10704 return($request);
86             }
87              
88             sub _do_request {
89 8     8   20 my $self = shift;
90 8         17 my $request = shift;
91              
92 8         153 my $cgi = HTTP::Request::AsCGI->new($request, %ENV)->setup;
93 8         12168 my $app = $self->app();
94              
95 8 50       33 if (defined ($app)) {
96 8 100       31 if (ref $app) {
97 3 50       9 if (ref $app eq 'CODE') {
98 3         6 &{$app};
  3         14  
99             }
100             else {
101 0         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         402 eval "require " . $app;
107              
108 5 50       16485 if ($app->isa("CGI::Application::Dispatch")) {
    50          
109 0         0 $app->dispatch();
110             }
111             elsif ($app->isa("CGI::Application")) {
112 5         37 my $app = $app->new();
113 5         1285 $app->run();
114             }
115             else {
116 0         0 die "Unable to use the value of app.";
117             }
118             }
119             }
120             else {
121 0         0 die "App was not defined.";
122             }
123              
124 8         77914 return $cgi->restore->response;
125             }
126              
127              
128             1;
129              
130             __END__