blib/lib/Test/WWW/Mechanize/HSS.pm

filter: Statementstmt 93.1% 82 / 88 Branchbran 54.7% 23 / 42 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.3% 12 / 13 Podpod 25.0% 1 / 4 totaltotal 77.7% 122 / 157 CC 24 SCAR 24.7
CC
24
Cov
79%
_do_hssimple_request
21.3
_make_request
6.3
host
6.0
CRAP 11.8
↑ Summary
1 package Test::WWW::Mechanize::HSS;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use parent 'Test::WWW::Mechanize';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 $VERSION = '0.02';
6  
7 =head1 NAME
8  
9 Test::WWW::Mechanize::HSS - Test HTTP::Server::Simple programs using WWW::Mechanize
10  
11 =head1 SYNOPSIS
12  
13 use Test::WWW::Mechanize::HSS;
14 use MyApp::Server; # A descendant from HTTP::Server::Simple
15  
16 # Construct your server
17 my $s = MyApp::Server->new(
18 ...
19 );
20  
21 my $mech = Test::WWW::Mechanize::HSS->new(
22 server => $s
23 );
24
25 $mech->get_ok('/');
26  
27 =head1 ABSTRACT
28  
29 This module implements the necessary glue to run code written for
30 L<HTTP::Server::Simple> and L<HTTP::Server::Simple::CGI>
31 using L<Test::WWW::Mechanize> without needing
32 to fire up an actual webserver.
33  
34 =head1 STATUS
35  
36 This is an early release, hacked together to test
37 one of my applications. So far it has worked well
38 for me, but there sure are some corners that
39 I haven't tested well. Tests and patches
40 are welcome.
41  
42 =cut
43  
44 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 9 ▶sub server { $_[0]->{server} };
Subroutine server: called CC=1 SCAR=0.0
Pod undocumented
49 0 ▶sub host { $_[1] ? $_[0]->{host} = $_[1] : $_[0] };
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
50 9 ▶sub has_host { exists $_[0]->{host} };
Subroutine has_host: called CC=1 SCAR=0.0
Pod undocumented
51  
52 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
53 7 my ($class,%args) = @_;
54  
55 7 my $s = delete $args{server};
56 7 my $self = $class->SUPER::new(%args);
57 7 $self->{server} = $s;
58
59 7 ▶ $s->after_setup_listener if $s->can('after_setup_listener');
Branch: 1 branch
BranchTrueFalseTotal
if $s->can("after_setup_listener") 7 0 7
60
61 # Save our known good environment
62 7 HTTP::Server::Simple::CGI::Environment->setup_environment;
63  
64 7 $self
65 };
66  
67 9 ▶sub _make_request {
Subroutine _make_request: called CC=6 SCAR=18.4
68 9 my ($self,$request) = @_;
69  
70 9 my $response = $self->_do_hssimple_request($request);
71
72 9 $response->header( 'Content-Base', $request->uri );
73 9 $response->request($request);
74 9 ▶ if ( $request->uri->as_string =~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->uri->as_string =~ m[^/]) 0 9 9
75 0 $request->uri(
76 URI->new( 'http://localhost:80/' . $request->uri->as_string ) );
77 }
78 9 ▶ $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookie_jar 9 0 9
79  
80 # check if that was a redirect
81 9 ▶ if ( $response->header('Location')
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header('Location') and $self->redirect_ok($request, $response)) { } 1 8 9
Condition66%2/3
$response->header("Location") and $self->redirect_ok($request, $response)
!ll&&!rl&&r
801
MC/DC50%1/2
$response->header("Location") and $self->redirect_ok($request, $response)
$response->header("Location") $self->redirect_ok($request, $response)
Truth table66%2/3
A and B
A= $response->header("Location")B= $self->redirect_ok($request, $response)
ABresult
0X0
100
111
82 && $self->redirect_ok( $request, $response ) )
83 {
84 1 my $old_response = $response;
85  
86 # *where* do they want us to redirect to?
87 1 my $location = $old_response->header('Location');
88  
89 # no-one *should* be returning relative URLs, but if they
90 # are then we'd better cope with it. Let's create a new URI, using
91 # our request as the base.
92 1 my $uri = URI->new_abs( $location, $request->uri )->as_string;
93  
94 # make a new response, and save the old response in it
95 1 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
96 1 my $end_of_chain = $response;
97 1 ▶ while ( $end_of_chain->previous ) # keep going till the end
Branch: 1 branch
BranchTrueFalseTotal
if ($end_of_chain->previous) 0 1 1
98 {
99 0 $end_of_chain = $end_of_chain->previous;
100 } # of the chain...
101 1 $end_of_chain->previous($old_response); # ...and add us to it
102 } else {
103 8 $response->{_raw_content} = $response->content;
104 }
105  
106 9 return $response;
107 }
108  
109 9 ▶sub _do_hssimple_request {
Subroutine _do_hssimple_request: called CC=17 SCAR=30.6
110 9 my ($self,$request) = @_;
111 9 my $uri = $request->uri;
112
113 9 ▶ $uri->scheme('http') unless defined $uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->scheme 0 9 9
114 9 ▶ $uri->host('localhost') unless defined $uri->host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->host 0 9 9
115 9 ▶ $self->cookie_jar->add_cookie_header($request)
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookie_jar 9 0 9
116 if $self->cookie_jar;
117  
118 9 ▶ unless ($request->header('Host')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->header("Host")) 9 0 9
119 9 ▶ my $host = $self->has_host
Branch: 1 branch
BranchTrueFalseTotal
$self->has_host ? : 0 9 9
120 ? $self->host
121 : $uri->host;
122  
123 9 $request->header('Host', $host);
124 }
125  
126 9 my @creds = $self->get_basic_credentials( "Basic", $uri );
127 9 ▶ $request->authorization_basic( @creds ) if @creds;
Branch: 1 branch
BranchTrueFalseTotal
if @creds 0 9 9
128  
129 # Run a HTTP::Server::Simple::CGI request
130 # Currently neglects all the fancy features
131 9 my $rs = $request->content;
132 9 ▶ open my $rsh, '<', \$rs
Branch: 1 branch
BranchTrueFalseTotal
open my $rsh, "<", \$rs or die "Couldn't create read-only memory file? $!" 9 0 9
133 or die "Couldn't create read-only memory file? $!";
134 9 binmode $rsh;
135 9 local *STDIN = *$rsh;
136
137 9 ▶ open my $STDOUT, '>', \my $stdout
Branch: 1 branch
BranchTrueFalseTotal
open my $STDOUT, ">", \my $stdout or die "Open failed? $!" 9 0 9
138 or die "Open failed? $!";
139 9 local *STDOUT = $STDOUT;
140
141 #warn "<<<$rs>>>";
142
143 9 my $s = $self->server;
144
145 9 $s->stdio_handle($STDOUT);
146  
147 9 ▶ $self->accept_hook() if $self->can("accept_hook");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("accept_hook") 0 9 9
148  
149 9 ▶ my ( $method, $request_uri, $proto ) = (
Branch: 1 branch
BranchTrueFalseTotal
$request->uri->query ? : 1 8 9
150 $request->method,
151 $request->uri->query
152 ? $request->uri->path . '?' . $request->uri->query
153 : $request->uri->path,
154 "HTTP/1.1"
155 );
156
157 # cut-n-paste from HTTP::Server::Simple::_process_request
158 9 ▶ unless ($s->valid_http_method($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->valid_http_method($method)) 0 9 9
159 0 $s->bad_request;
160 0 goto REQUEST_DONE;
161 }
162  
163 #$proto ||= "HTTP/0.9";
164  
165 9 my ( $file, $query_string )
166 = ( $request_uri =~ /([^?]*)(?:\?(.*))?/s ); # split at ?
167  
168 9 ▶ $s->setup(
Branch: 1 branch
BranchTrueFalseTotal
defined $query_string ? : 1 8 9
169 method => $method,
170 protocol => $proto,
171 query_string => ( defined($query_string) ? $query_string : '' ),
172 request_uri => $request_uri,
173 path => $file,
174 localname => $s->host,
175 localport => $s->port,
176 peername => 'testing',
177 peeraddr => '127.0.0.1',
178 );
179  
180 # HTTP/0.9 didn't have any headers (I think)
181 9 ▶ if ( $proto =~ m{HTTP/(\d(\.\d)?)$} and $1 >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ m[HTTP/(\d(\.\d)?)$] and $1 >= 1) 9 0 9
Condition33%1/3
$proto =~ m[HTTP/(\d(\.\d)?)$] and $1 >= 1
!ll&&!rl&&r
009
MC/DC0%0/2
$proto =~ m[HTTP/(\d(\.\d)?)$] and $1 >= 1
$proto =~ m[HTTP/(\d(\.\d)?)$] $1 >= 1
Truth table33%1/3
A and B
A= $proto =~ m[HTTP/(\d(\.\d)?)$]B= $1 >= 1
ABresult
0X0
100
111
182 9 my $headers = [];
183 9 ▶ $request->headers->scan( sub { push @$headers, @_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
184  
185 9 $s->headers($headers);
186 }
187  
188 9 ▶ $s->post_setup_hook if $s->can("post_setup_hook");
Branch: 1 branch
BranchTrueFalseTotal
if $s->can("post_setup_hook") 9 0 9
189
190 9 my $ok = eval {
191 9 $s->handler;
192 9 1
193 };
194 9 my $err = $@;
195 9 REQUEST_DONE:
196
197 #warn "[[[$stdout]]]";
198 my $response;
199 9 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 9 0 9
200 9 $response = HTTP::Response->parse($stdout);
201 } else {
202 0 $response = HTTP::Response->new(500, $err, ['X-Internal','Internal error'], $err);
203 };
204 9 return $response
205 };
206  
207 8 1;
208  
209 __END__
↑ Summary