lib/Apache/TestRequest.pm

filter: Statementstmt 20.0% 76 / 379 Branchbran 5.0% 9 / 178 Conditioncond 1.2% 1 / 80 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 21.8% 14 / 64 Podpod 23.6% 9 / 38 totaltotal 13.8% 109 / 785 CC 113 SCAR 43.2
CC
113
Cov
14%
user_agent
272.0
lwp_call
272.0
same_interp_do
132.0
CRAP 75.1
« lib/Apache/TestConfig.pm ↑ Summary blib/lib/Apache/TestHarnessPHP.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 package Apache::TestRequest;
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
22 1 $ENV{PERL_LWP_USE_HTTP_10} = 1; # default to http/1.0
23 1 ▶ $ENV{APACHE_TEST_HTTP_09_OK} ||= 0; # 0.9 responses are ok
Condition50%1/2
$ENV{"APACHE_TEST_HTTP_09_OK"} ||= 0
l!l
01
MC/DC0%0/1
$ENV{"APACHE_TEST_HTTP_09_OK"} ||= 0
$ENV{"APACHE_TEST_HTTP_09_OK"}
Truth table50%1/2
A
A= $ENV{"APACHE_TEST_HTTP_09_OK"}
Aresult
11
00
24 }
25  
26 1 ▶use Apache::Test ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Apache::TestConfig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use constant TRY_TIMES => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant INTERP_KEY => 'X-PerlInterpreter';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant UA_TIMEOUT => 60 * 10; #longer timeout for debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 my $have_lwp = 0;
36  
37 # APACHE_TEST_PRETEND_NO_LWP=1 pretends that LWP is not available so
38 # one can test whether the test suite survives if the user doesn't
39 # have lwp installed
40 1 ▶unless ($ENV{APACHE_TEST_PRETEND_NO_LWP}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"APACHE_TEST_PRETEND_NO_LWP"}) 1 0 1
41 1 $have_lwp = eval {
42 1 require LWP::UserAgent;
43 1 require HTTP::Request::Common;
44  
45 1 ▶ unless (defined &HTTP::Request::Common::OPTIONS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &HTTP::Request::Common::OPTIONS) 0 1 1
46 package HTTP::Request::Common;
47 1 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 0 ▶ *OPTIONS = sub { _simple_req(OPTIONS => @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 push @EXPORT, 'OPTIONS';
50 }
51 1 1;
52 };
53 }
54  
55 1 ▶unless ($have_lwp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_lwp) 0 1 1
56 0 require Apache::TestClient;
57 }
58  
59 0 ▶sub has_lwp { $have_lwp }
Subroutine has_lwp: not called CC=1 SCAR=6.9
Pod undocumented
60  
61 1 ▶unless ($have_lwp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_lwp) 0 1 1
62 #need to define the shortcuts even though the wont be used
63 #so Perl can parse test scripts
64 0 @HTTP::Request::Common::EXPORT = qw(GET HEAD POST PUT OPTIONS);
65 }
66  
67 0 ▶sub install_http11 {
Subroutine install_http11: not called CC=2 SCAR=17.9
Pod undocumented
68 0 eval {
69 0 ▶ die "no LWP" unless $have_lwp;
Branch: 1 branch
BranchTrueFalseTotal
unless $have_lwp 0 0 0
70 0 LWP->VERSION(5.60); #minimal version
71 0 require LWP::Protocol::http;
72 #LWP::Protocol::http10 is used by default
73 0 LWP::Protocol::implementor('http', 'LWP::Protocol::http');
74 };
75 }
76  
77 1 ▶use vars qw(@EXPORT @ISA $RedirectOK $DebugLWP);
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 1 require Exporter;
80 1 *import = \&Exporter::import;
81 1 @EXPORT = @HTTP::Request::Common::EXPORT;
82  
83 1 @ISA = qw(LWP::UserAgent);
84  
85 1 my $UA;
86 1 ▶my $REDIR = $have_lwp ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$have_lwp ? : 1 0 1
87 1 my $conn_opts = {};
88  
89 0 ▶sub module {
Subroutine module: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my $module = shift;
91 0 ▶ $Apache::TestRequest::Module = $module if $module;
Branch: 1 branch
BranchTrueFalseTotal
if $module 0 0 0
92 0 $Apache::TestRequest::Module;
93 }
94  
95 0 ▶sub scheme {
Subroutine scheme: not called CC=2 SCAR=17.9
Pod undocumented
96 0 my $scheme = shift;
97 0 ▶ $Apache::TestRequest::Scheme = $scheme if $scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $scheme 0 0 0
98 0 $Apache::TestRequest::Scheme;
99 }
100  
101 0 ▶sub module2path {
Subroutine module2path: not called CC=1 SCAR=6.9
Pod documented
102 0 my $package = shift;
103  
104 # httpd (1.3 && 2) / winFU have problems when the first path's
105 # segment includes ':' (security precaution which breaks the rfc)
106 # so we can't use /TestFoo::bar as path_info
107 0 (my $path = $package) =~ s/::/__/g;
108  
109 0 return $path;
110 }
111  
112 0 ▶sub module2url {
Subroutine module2url: not called CC=4 SCAR=30.0
Pod documented
113 0 my $module = shift;
114 0 ▶ my $opt = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
115 0 ▶ my $scheme = $opt->{scheme} || 'http';
Condition0%0/2
$opt->{'scheme'} || 'http'
l!l
00
MC/DC0%0/1
$opt->{'scheme'} || 'http'
$opt->{'scheme'}
Truth table0%0/2
A
A= $opt->{'scheme'}
Aresult
11
00
116 0 ▶ my $path = exists $opt->{path} ? $opt->{path} : module2path($module);
Branch: 1 branch
BranchTrueFalseTotal
exists $opt->{'path'} ? : 0 0 0
117  
118 0 module($module);
119  
120 0 my $config = Apache::Test::config();
121 0 my $hostport = hostport($config);
122  
123 0 $path =~ s|^/||;
124 0 return "$scheme://$hostport/$path";
125 }
126  
127 0 ▶sub user_agent {
Subroutine user_agent: not called CC=16 SCAR=56.1
Pod undocumented
128 0 my $args = {@_};
129  
130 0 ▶ if (delete $args->{reset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args->{"reset"}) 0 0 0
131 0 $UA = undef;
132 }
133  
134 0 ▶ if (exists $args->{requests_redirectable}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"requests_redirectable"}) 0 0 0
135 0 my $redir = $args->{requests_redirectable};
136 0 ▶ if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $redir and @$redir > 1 || $redir->[0] ne 'POST') { } 0 0 0
elsif ($redir) { } 0 0 0
Condition0%0/6
ref $redir and @$redir > 1 || $redir->[0] ne "POST"
!ll&&!rl&&r
000
@$redir > 1 or $redir->[0] ne "POST"
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $redir and @$redir > 1 || $redir->[0] ne "POST"
ref $redir @$redir > 1 $redir->[0] ne "POST"
Truth table0%0/4
A and B or C
A= ref $redirB= @$redir > 1C= $redir->[0] ne "POST"
ABCresult
0XX0
1000
11X1
1011
137 # Set our internal flag if there's no LWP.
138 0 ▶ $REDIR = $have_lwp ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$have_lwp ? : 0 0 0
139 } elsif ($redir) {
140 0 ▶ if ($have_lwp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_lwp) { } 0 0 0
141 0 $args->{requests_redirectable} = [ qw/GET HEAD POST/ ];
142 0 $REDIR = undef;
143 } else {
144 # Set our internal flag.
145 0 $REDIR = 1;
146 }
147 } else {
148 # Make sure our internal flag is false if there's no LWP.
149 0 ▶ $REDIR = $have_lwp ? undef : 0;
Branch: 1 branch
BranchTrueFalseTotal
$have_lwp ? : 0 0 0
150 }
151 }
152  
153 0 ▶ $args->{keep_alive} ||= $ENV{APACHE_TEST_HTTP11};
Condition0%0/3
$args->{"keep_alive"} ||= $ENV{"APACHE_TEST_HTTP11"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"keep_alive"} ||= $ENV{"APACHE_TEST_HTTP11"}
$args->{"keep_alive"} $ENV{"APACHE_TEST_HTTP11"}
Truth table0%0/3
A ||= B
A= $args->{"keep_alive"}B= $ENV{"APACHE_TEST_HTTP11"}
ABresult
1X1
011
000
154  
155 0 ▶ if ($args->{keep_alive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"keep_alive"}) 0 0 0
156 0 install_http11();
157 0 eval {
158 0 require LWP::Protocol::https; #https10 is the default
159 0 LWP::Protocol::implementor('https', 'LWP::Protocol::https');
160 };
161 }
162  
163 # in LWP 6, verify_hostname defaults to on, so SSL_ca_file
164 # needs to be set accordingly
165 0 ▶ if ($have_lwp and $LWP::VERSION >= 6.0 and not exists $args->{ssl_opts}->{SSL_ca_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_lwp and $LWP::VERSION >= 6 and not exists $args->{"ssl_opts"}{"SSL_ca_file"}) 0 0 0
Condition0%0/6
$have_lwp and $LWP::VERSION >= 6 and not exists $args->{"ssl_opts"}{"SSL_ca_file"}
!ll&&!rl&&r
000
$have_lwp and $LWP::VERSION >= 6
!ll&&!rl&&r
000
MC/DC0%0/3
$have_lwp and $LWP::VERSION >= 6 and not exists $args->{"ssl_opts"}{"SSL_ca_file"}
$have_lwp $LWP::VERSION >= 6 not exists $args->{"ssl_opts"}{"SSL_ca_file"}
Truth table0%0/4
A and B and not(C)
A= $have_lwpB= $LWP::VERSION >= 6C= not exists $args->{"ssl_opts"}{"SSL_ca_file"}
ABCresult
0XX0
10X0
1100
1111
166 0 my $vars = Apache::Test::vars();
167 0 my $cafile = "$vars->{sslca}/$vars->{sslcaorg}/certs/ca.crt";
168 0 $args->{ssl_opts}->{SSL_ca_file} = $cafile;
169 # IO::Socket:SSL raw socket compatibility
170 0 $conn_opts->{SSL_ca_file} = $cafile;
171 }
172  
173 0 ▶ eval { $UA ||= __PACKAGE__->new(%$args); };
Condition0%0/3
$UA ||= __PACKAGE__->new(%$args)
l!l&&r!l&&!r
000
MC/DC0%0/2
$UA ||= __PACKAGE__->new(%$args)
$UA __PACKAGE__->new(%$args)
Truth table0%0/3
A ||= B
A= $UAB= __PACKAGE__->new(%$args)
ABresult
1X1
011
000
174 }
175  
176 0 ▶sub user_agent_request_num {
Subroutine user_agent_request_num: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my $res = shift;
178 0 ▶ $res->header('Client-Request-Num') || #lwp 5.60
Branch: 1 branch
BranchTrueFalseTotal
$res->header("Client-Request-Num") or $res->header("Client-Response-Num") 0 0 0
179 $res->header('Client-Response-Num'); #lwp 5.62+
180 }
181  
182 0 ▶sub user_agent_keepalive {
Subroutine user_agent_keepalive: not called CC=1 SCAR=6.9
Pod undocumented
183 0 $ENV{APACHE_TEST_HTTP11} = shift;
184 }
185  
186 0 ▶sub do_request {
Subroutine do_request: not called CC=1 SCAR=6.9
Pod undocumented
187 0 my($ua, $method, $url, $callback) = @_;
188 0 my $r = HTTP::Request->new($method, resolve_url($url));
189 0 my $response = $ua->request($r, $callback);
190 0 lwp_trace($response);
191 }
192  
193 0 ▶sub hostport {
Subroutine hostport: not called CC=6 SCAR=37.4
Pod undocumented
194 0 ▶ my $config = shift || Apache::Test::config();
Condition0%0/3
shift() || Apache::Test::config()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || Apache::Test::config()
shift() Apache::Test::config()
Truth table0%0/3
A || B
A= shift()B= Apache::Test::config()
ABresult
1X1
011
000
195 0 my $vars = $config->{vars};
196 local $vars->{scheme} =
197 0 ▶ $Apache::TestRequest::Scheme || $vars->{scheme};
Condition0%0/3
$Apache::TestRequest::Scheme || $vars->{'scheme'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Apache::TestRequest::Scheme || $vars->{'scheme'}
$Apache::TestRequest::Scheme $vars->{'scheme'}
Truth table0%0/3
A || B
A= $Apache::TestRequest::SchemeB= $vars->{'scheme'}
ABresult
1X1
011
000
198 0 my $hostport = $config->hostport;
199  
200 0 my $default_hostport = join ':', $vars->{servername}, $vars->{port};
201 0 ▶ if (my $module = $Apache::TestRequest::Module) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = $Apache::TestRequest::Module) 0 0 0
202 $hostport = $module eq 'default'
203 ? $default_hostport
204 0 ▶ : $config->{vhosts}->{$module}->{hostport};
Branch: 1 branch
BranchTrueFalseTotal
$module eq 'default' ? : 0 0 0
205 }
206  
207 0 ▶ $hostport || $default_hostport;
Branch: 1 branch
BranchTrueFalseTotal
$hostport or $default_hostport 0 0 0
208 }
209  
210 0 ▶sub resolve_url {
Subroutine resolve_url: not called CC=6 SCAR=37.4
Pod undocumented
211 0 my $url = shift;
212 0 ▶ Carp::croak("no url passed") unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 0 0
213  
214 0 ▶ return $url if $url =~ m,^(\w+):/,;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[^(\w+):/] 0 0 0
215 0 ▶ $url = "/$url" unless $url =~ m,^/,;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 0 0
216  
217 0 my $vars = Apache::Test::vars();
218  
219 local $vars->{scheme} =
220 0 ▶ $Apache::TestRequest::Scheme || $vars->{scheme} || 'http';
Condition0%0/5
$Apache::TestRequest::Scheme || $vars->{'scheme'} || 'http'
l!l
00
$Apache::TestRequest::Scheme or $vars->{"scheme"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Apache::TestRequest::Scheme || $vars->{'scheme'} || 'http'
$Apache::TestRequest::Scheme $vars->{"scheme"}
Truth table0%0/3
A or B
A= $Apache::TestRequest::SchemeB= $vars->{"scheme"}
ABresult
1X1
011
000
221  
222 0 scheme_fixup($vars->{scheme});
223  
224 0 my $hostport = hostport();
225  
226 0 return "$vars->{scheme}://$hostport$url";
227 }
228  
229 1 my %wanted_args = map {$_, 1} qw(username password realm content filename
230 redirect_ok cert);
231  
232 1 ▶sub wanted_args {
Subroutine wanted_args: called CC=1 SCAR=0.0
Pod undocumented
233 1 \%wanted_args;
234 }
235  
236 0 ▶sub redirect_ok {
Subroutine redirect_ok: not called CC=5 SCAR=34.0
Pod documented
237 0 my $self = shift;
238 0 ▶ if ($have_lwp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_lwp) 0 0 0
239 # Return user setting or let LWP handle it.
240 0 ▶ return $RedirectOK if defined $RedirectOK;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Apache::TestRequest::RedirectOK 0 0 0
241 0 return $self->SUPER::redirect_ok(@_);
242 }
243  
244 # No LWP. We don't support redirect on POST.
245 0 ▶ return 0 if $self->method eq 'POST';
Branch: 1 branch
BranchTrueFalseTotal
if $self->method eq "POST" 0 0 0
246 # Return user setting or our internal calculation.
247 0 ▶ return $RedirectOK if defined $RedirectOK;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Apache::TestRequest::RedirectOK 0 0 0
248 0 return $REDIR;
249 }
250  
251 1 my %credentials;
252  
253 #subclass LWP::UserAgent
254 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
255 0 my $self = shift->SUPER::new(@_);
256  
257 0 lwp_debug(); #init from %ENV (set by Apache::TestRun)
258  
259 0 my $config = Apache::Test::config();
260 0 ▶ if (my $proxy = $config->configure_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $config->configure_proxy) 0 0 0
261 #t/TEST -proxy
262 0 $self->proxy(http => "http://$proxy");
263 }
264  
265 0 $self->timeout(UA_TIMEOUT);
266  
267 0 $self;
268 }
269  
270 0 ▶sub credentials {
Subroutine credentials: not called CC=1 SCAR=6.9
Pod documented
271 0 my $self = shift;
272 0 return $self->get_basic_credentials(@_);
273 }
274  
275 0 ▶sub get_basic_credentials {
Subroutine get_basic_credentials: not called CC=4 SCAR=30.0
Pod documented
276 0 my($self, $realm, $uri, $proxy) = @_;
277  
278 0 for ($realm, '__ALL__') {
279 0 ▶ next unless $_ && $credentials{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $_ and $credentials{$_} 0 0 0
Condition0%0/3
$_ and $credentials{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and $credentials{$_}
$_ $credentials{$_}
Truth table0%0/3
A and B
A= $_B= $credentials{$_}
ABresult
0X0
100
111
280 0 return @{ $credentials{$_} };
281 }
282  
283 0 return (undef,undef);
284 }
285  
286 0 ▶sub vhost_socket {
Subroutine vhost_socket: not called CC=5 SCAR=34.0
Pod undocumented
287 0 my $module = shift;
288 0 ▶ local $Apache::TestRequest::Module = $module if $module;
Branch: 1 branch
BranchTrueFalseTotal
if $module 0 0 0
289  
290 0 my $hostport = hostport(Apache::Test::config());
291  
292 0 my($host, $port) = split ':', $hostport;
293 0 my(%args) = (PeerAddr => $host, PeerPort => $port);
294  
295 0 ▶ if ($module and ($module =~ /ssl/ || $module eq 'h2')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module and $module =~ /ssl/ || $module eq 'h2') { } 0 0 0
Condition0%0/6
$module and $module =~ /ssl/ || $module eq "h2"
!ll&&!rl&&r
000
$module =~ /ssl/ or $module eq "h2"
l!l&&r!l&&!r
000
MC/DC0%0/3
$module and $module =~ /ssl/ || $module eq "h2"
$module $module =~ /ssl/ $module eq "h2"
Truth table0%0/4
A and B or C
A= $moduleB= $module =~ /ssl/C= $module eq "h2"
ABCresult
0XX0
1000
11X1
1011
296 0 require IO::Socket::SSL;
297 # Add all conn_opts to args
298 0 map {$args{$_} = $conn_opts->{$_}} keys %{$conn_opts};
299 0 return IO::Socket::SSL->new(%args, Timeout => UA_TIMEOUT);
300 }
301 else {
302 0 require IO::Socket;
303 0 return IO::Socket::INET->new(%args);
304 }
305 }
306  
307 #IO::Socket::SSL::getline does not correctly handle OpenSSL *_WANT_*.
308 #Could care less about performance here, just need a getline()
309 #that returns the same results with or without ssl.
310 #Inspired from Net::SSLeay::ssl_read_all().
311 my %getline = (
312 0 ▶ 'IO::Socket::SSL' => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
313 0 my $self = shift;
314 # _get_ssl_object in IO::Socket::SSL only meant for internal use!
315 # But we need to compensate for unsufficient getline impl there.
316 0 my $ssl = $self->_get_ssl_object;
317 0 my ($got, $rv, $errs);
318 0 my $reply = '';
319
320 0 while (1) {
321 0 ($got, $rv) = Net::SSLeay::read($ssl, 1);
322 0 ▶ if (! defined $got) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got) 0 0 0
323 0 my $err = Net::SSLeay::get_error($ssl, $rv);
324 0 ▶ if ($err != Net::SSLeay::ERROR_WANT_READ() and
Branch: 1 branch
BranchTrueFalseTotal
if ($err != Net::SSLeay::ERROR_WANT_READ() and $err != Net::SSLeay::ERROR_WANT_WRITE()) 0 0 0
Condition0%0/3
$err != Net::SSLeay::ERROR_WANT_READ() and $err != Net::SSLeay::ERROR_WANT_WRITE()
!ll&&!rl&&r
000
MC/DC0%0/2
$err != Net::SSLeay::ERROR_WANT_READ() and $err != Net::SSLeay::ERROR_WANT_WRITE()
$err != Net::SSLeay::ERROR_WANT_READ() $err != Net::SSLeay::ERROR_WANT_WRITE()
Truth table0%0/3
A and B
A= $err != Net::SSLeay::ERROR_WANT_READ()B= $err != Net::SSLeay::ERROR_WANT_WRITE()
ABresult
0X0
100
111
325 $err != Net::SSLeay::ERROR_WANT_WRITE()) {
326 0 $errs = Net::SSLeay::print_errs('SSL_read');
327 0 last;
328 }
329 0 next;
330 }
331 0 ▶ last if $got eq ''; # EOF
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "" 0 0 0
332 0 $reply .= $got;
333 0 ▶ last if $got eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "\n" 0 0 0
334 }
335  
336 0 ▶ wantarray ? ($reply, $errs) : $reply;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
337 },
338 1 );
339  
340 0 ▶sub getline {
Subroutine getline: not called CC=2 SCAR=17.9
Pod undocumented
341 0 my $sock = shift;
342 0 my $class = ref $sock;
343 0 ▶ my $method = $getline{$class} || 'getline';
Condition0%0/2
$getline{$class} || 'getline'
l!l
00
MC/DC0%0/1
$getline{$class} || 'getline'
$getline{$class}
Truth table0%0/2
A
A= $getline{$class}
Aresult
11
00
344 0 $sock->$method();
345 }
346  
347 0 ▶sub socket_trace {
Subroutine socket_trace: not called CC=2 SCAR=17.9
Pod undocumented
348 0 my $sock = shift;
349 0 ▶ return unless $sock->can('get_peer_certificate');
Branch: 1 branch
BranchTrueFalseTotal
unless $sock->can("get_peer_certificate") 0 0 0
350  
351 #like having some -v info
352 0 my $cert = $sock->get_peer_certificate;
353 0 print "#Cipher: ", $sock->get_cipher, "\n";
354 0 print "#Peer DN: ", $cert->subject_name, "\n";
355 }
356  
357 0 ▶sub prepare {
Subroutine prepare: not called CC=9 SCAR=45.0
Pod undocumented
358 0 my $url = shift;
359  
360 0 ▶ if ($have_lwp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_lwp) { } 0 0 0
361 0 user_agent();
362 0 $url = resolve_url($url);
363 }
364 else {
365 0 ▶ lwp_debug() if $ENV{APACHE_TEST_DEBUG_LWP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"APACHE_TEST_DEBUG_LWP"} 0 0 0
366 }
367  
368 0 my($pass, $keep) = Apache::TestConfig::filter_args(\@_, \%wanted_args);
369  
370 0 %credentials = ();
371 0 ▶ if (defined $keep->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $keep->{"username"}) 0 0 0
372 $credentials{$keep->{realm} || '__ALL__'} =
373 0 ▶ [$keep->{username}, $keep->{password}];
Condition0%0/2
$keep->{'realm'} || '__ALL__'
l!l
00
MC/DC0%0/1
$keep->{'realm'} || '__ALL__'
$keep->{'realm'}
Truth table0%0/2
A
A= $keep->{'realm'}
Aresult
11
00
374 }
375 0 ▶ if (defined(my $content = $keep->{content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $content = $keep->{"content"})) 0 0 0
376 0 ▶ if ($content eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($content eq '-') { } 0 0 0
elsif ($content =~ /^x(\d+)$/) { } 0 0 0
377 0 $content = join '', <STDIN>;
378 }
379 elsif ($content =~ /^x(\d+)$/) {
380 0 $content = 'a' x $1;
381 }
382 0 push @$pass, content => $content;
383 }
384 0 ▶ if (exists $keep->{cert}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keep->{"cert"}) 0 0 0
385 0 set_client_cert($keep->{cert});
386 }
387  
388 0 return ($url, $pass, $keep);
389 }
390  
391 0 ▶sub UPLOAD {
Subroutine UPLOAD: not called CC=3 SCAR=24.8
Pod documented
392 0 my($url, $pass, $keep) = prepare(@_);
393  
394 local $RedirectOK = exists $keep->{redirect_ok}
395 ? $keep->{redirect_ok}
396 0 ▶ : $RedirectOK;
Branch: 1 branch
BranchTrueFalseTotal
exists $keep->{'redirect_ok'} ? : 0 0 0
397  
398 0 ▶ if ($keep->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep->{'filename'}) { } 0 0 0
399 0 return upload_file($url, $keep->{filename}, $pass);
400 }
401 else {
402 0 return upload_string($url, $keep->{content});
403 }
404 }
405  
406 0 ▶sub UPLOAD_BODY {
Subroutine UPLOAD_BODY: not called CC=1 SCAR=6.9
Pod documented
407 0 UPLOAD(@_)->content;
408 }
409  
410 0 ▶sub UPLOAD_BODY_ASSERT {
Subroutine UPLOAD_BODY_ASSERT: not called CC=1 SCAR=6.9
Pod documented
411 0 content_assert(UPLOAD(@_));
412 }
413  
414 #lwp only supports files
415 0 ▶sub upload_string {
Subroutine upload_string: not called CC=1 SCAR=6.9
Pod undocumented
416 0 my($url, $data) = @_;
417  
418 0 my $CRLF = "\015\012";
419 0 my $bound = 742617000027;
420 0 my $req = HTTP::Request->new(POST => $url);
421  
422 0 my $content = join $CRLF,
423 "--$bound",
424 "Content-Disposition: form-data; name=\"HTTPUPLOAD\"; filename=\"b\"",
425 "Content-Type: text/plain", "",
426 $data, "--$bound--", "";
427  
428 0 $req->header("Content-Length", length($content));
429 0 $req->content_type("multipart/form-data; boundary=$bound");
430 0 $req->content($content);
431  
432 0 $UA->request($req);
433 }
434  
435 0 ▶sub upload_file {
Subroutine upload_file: not called CC=1 SCAR=6.9
Pod undocumented
436 0 my($url, $file, $args) = @_;
437  
438 0 my $content = [@$args, filename => [$file]];
439  
440 0 $UA->request(HTTP::Request::Common::POST($url,
441 Content_Type => 'form-data',
442 Content => $content,
443 ));
444 }
445  
446 #useful for POST_HEAD and $DebugLWP (see below)
447 0 ▶sub lwp_as_string {
Subroutine lwp_as_string: not called CC=5 SCAR=34.0
Pod undocumented
448 0 my($r, $want_body) = @_;
449 0 my $content = $r->content;
450  
451 0 ▶ unless ($r->isa('HTTP::Request') or
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->isa("HTTP::Request") or $r->header("Content-Length") or $r->header("Transfer-Encoding")) 0 0 0
Condition0%0/6
$r->isa("HTTP::Request") or $r->header("Content-Length") or $r->header("Transfer-Encoding")
l!l&&r!l&&!r
000
$r->isa("HTTP::Request") or $r->header("Content-Length")
l!l&&r!l&&!r
000
MC/DC0%0/3
$r->isa("HTTP::Request") or $r->header("Content-Length") or $r->header("Transfer-Encoding")
$r->isa("HTTP::Request") $r->header("Content-Length") $r->header("Transfer-Encoding")
Truth table0%0/4
A or B or C
A= $r->isa("HTTP::Request")B= $r->header("Content-Length")C= $r->header("Transfer-Encoding")
ABCresult
1XX1
01X1
0011
0000
452 $r->header('Content-Length') or
453 $r->header('Transfer-Encoding'))
454 {
455 0 $r->header('Content-Length' => length $content);
456 0 $r->header('X-Content-length-note' => 'added by Apache::TestRequest');
457 }
458  
459 0 ▶ $r->content('') unless $want_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_body 0 0 0
460  
461 0 (my $string = $r->as_string) =~ s/^/\#/mg;
462 0 $r->content($content); #reset
463 0 $string;
464 }
465  
466 1 $DebugLWP = 0; #1 == print METHOD URL and header response for all requests
467 #2 == #1 + response body
468 #other == passed to LWP::Debug->import
469  
470 0 ▶sub lwp_debug {
Subroutine lwp_debug: not called CC=5 SCAR=34.0
Pod undocumented
471 package main; #wtf: else package in perldb changes
472 0 ▶ my $val = $_[0] || $ENV{APACHE_TEST_DEBUG_LWP};
Condition0%0/3
$_[0] || $ENV{'APACHE_TEST_DEBUG_LWP'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] || $ENV{'APACHE_TEST_DEBUG_LWP'}
$_[0] $ENV{'APACHE_TEST_DEBUG_LWP'}
Truth table0%0/3
A || B
A= $_[0]B= $ENV{'APACHE_TEST_DEBUG_LWP'}
ABresult
1X1
011
000
473  
474 0 ▶ return unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
475  
476 0 ▶ if ($val =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\d+$/) { } 0 0 0
477 0 $Apache::TestRequest::DebugLWP = $val;
478 0 return "\$Apache::TestRequest::DebugLWP = $val\n";
479 }
480 else {
481 0 ▶ my(@args) = @_ ? @_ : split /\s+/, $val;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
482 0 require LWP::Debug;
483 0 LWP::Debug->import(@args);
484 0 return "LWP::Debug->import(@args)\n";
485 }
486 }
487  
488 0 ▶sub lwp_trace {
Subroutine lwp_trace: not called CC=3 SCAR=24.8
Pod undocumented
489 0 my $r = shift;
490  
491 0 ▶ unless ($r->request->protocol) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->request->protocol) 0 0 0
492 #lwp always sends a request, but never sets
493 #$r->request->protocol, happens deeper in the
494 #LWP::Protocol::http* modules
495 0 ▶ my $proto = user_agent_request_num($r) ? "1.1" : "1.0";
Branch: 1 branch
BranchTrueFalseTotal
user_agent_request_num($r) ? : 0 0 0
496 0 $r->request->protocol("HTTP/$proto");
497 }
498  
499 0 my $want_body = $DebugLWP > 1;
500 0 print "#lwp request:\n",
501 lwp_as_string($r->request, $want_body);
502  
503 0 print "#server response:\n",
504 lwp_as_string($r, $want_body);
505 }
506  
507 0 ▶sub lwp_call {
Subroutine lwp_call: not called CC=16 SCAR=56.1
Pod undocumented
508 0 my($name, $shortcut) = (shift, shift);
509  
510 0 my $r = (\&{$name})->(@_);
511  
512 0 ▶ Carp::croak("$name(@_) didn't return a response object") unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
513  
514 0 my $error = "";
515 0 ▶ unless ($shortcut) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shortcut) 0 0 0
516 #GET, HEAD, POST
517 0 ▶ if ($r->method eq "POST" && !defined($r->header("Content-Length"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->method eq "POST" and not defined $r->header("Content-Length")) 0 0 0
Condition0%0/3
$r->method eq "POST" and not defined $r->header("Content-Length")
!ll&&!rl&&r
000
MC/DC0%0/2
$r->method eq "POST" and not defined $r->header("Content-Length")
$r->method eq "POST" not defined $r->header("Content-Length")
Truth table0%0/3
A and not(B)
A= $r->method eq "POST"B= not defined $r->header("Content-Length")
ABresult
0X0
100
111
518 0 $r->header('Content-Length' => length($r->content));
519 }
520 0 ▶ $r = $UA ? $UA->request($r) : $r;
Branch: 1 branch
BranchTrueFalseTotal
$UA ? : 0 0 0
521 0 my $proto = $r->protocol;
522 0 ▶ if (defined($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto) 0 0 0
523 0 ▶ if ($proto !~ /^HTTP\/(\d\.\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proto =~ m[^HTTP/(\d\.\d)$]) 0 0 0
524 0 $error = "response had no protocol (is LWP broken or something?)";
525 }
526 0 ▶ if ($1 ne "1.0" && $1 ne "1.1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne "1.0" and $1 ne "1.1") 0 0 0
Condition0%0/3
$1 ne "1.0" and $1 ne "1.1"
!ll&&!rl&&r
000
MC/DC0%0/2
$1 ne "1.0" and $1 ne "1.1"
$1 ne "1.0" $1 ne "1.1"
Truth table0%0/3
A and B
A= $1 ne "1.0"B= $1 ne "1.1"
ABresult
0X0
100
111
527 $error = "response had protocol HTTP/$1 (headers not sent?)"
528 0 ▶ unless ($1 eq "0.9" && $ENV{APACHE_TEST_HTTP_09_OK});
Branch: 1 branch
BranchTrueFalseTotal
unless $1 eq "0.9" and $ENV{"APACHE_TEST_HTTP_09_OK"} 0 0 0
Condition0%0/3
$1 eq "0.9" and $ENV{"APACHE_TEST_HTTP_09_OK"}
!ll&&!rl&&r
000
MC/DC0%0/2
$1 eq "0.9" and $ENV{"APACHE_TEST_HTTP_09_OK"}
$1 eq "0.9" $ENV{"APACHE_TEST_HTTP_09_OK"}
Truth table0%0/3
A and B
A= $1 eq "0.9"B= $ENV{"APACHE_TEST_HTTP_09_OK"}
ABresult
0X0
100
111
529 }
530 }
531 }
532  
533 0 ▶ if ($DebugLWP and not $shortcut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Apache::TestRequest::DebugLWP and not $shortcut) 0 0 0
Condition0%0/3
$Apache::TestRequest::DebugLWP and not $shortcut
!ll&&!rl&&r
000
MC/DC0%0/2
$Apache::TestRequest::DebugLWP and not $shortcut
$Apache::TestRequest::DebugLWP not $shortcut
Truth table0%0/3
A and not(B)
A= $Apache::TestRequest::DebugLWPB= not $shortcut
ABresult
0X0
100
111
534 0 lwp_trace($r);
535 }
536  
537 0 ▶ Carp::croak($error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
538  
539 0 ▶ return $shortcut ? $r->$shortcut() : $r;
Branch: 1 branch
BranchTrueFalseTotal
$shortcut ? : 0 0 0
540 }
541  
542 0 ▶my %shortcuts = (RC => sub { shift->code },
Subroutine __ANON__: not called CC=1 SCAR=6.9
543 0 ▶ OK => sub { shift->is_success },
Subroutine __ANON__: not called CC=1 SCAR=6.9
544 0 ▶ STR => sub { shift->as_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
545 0 ▶ HEAD => sub { lwp_as_string(shift, 0) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
546 0 ▶ BODY => sub { shift->content },
Subroutine __ANON__: not called CC=1 SCAR=6.9
547 0 ▶ BODY_ASSERT => sub { content_assert(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
548 1 );
549  
550 1 for my $name (@EXPORT) {
551 6 ▶ my $package = $have_lwp ?
Branch: 1 branch
BranchTrueFalseTotal
$have_lwp ? : 6 0 6
552 'HTTP::Request::Common': 'Apache::TestClient';
553  
554 6 my $method = join '::', $package, $name;
555 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
556  
557 6 ▶ next unless defined &$method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$method 0 6 6
558  
559 0 ▶ *$name = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
560 0 my($url, $pass, $keep) = prepare(@_);
561 local $RedirectOK = exists $keep->{redirect_ok}
562 ? $keep->{redirect_ok}
563 0 ▶ : $RedirectOK;
Branch: 1 branch
BranchTrueFalseTotal
exists $keep->{'redirect_ok'} ? : 0 0 0
564 0 return lwp_call($method, undef, $url, @$pass);
565 6 };
566  
567 6 ▶ while (my($shortcut, $cv) = each %shortcuts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %shortcuts) 36 6 42
568 36 my $alias = join '_', $name, $shortcut;
569 0 ▶ *$alias = sub { lwp_call($name, $cv, @_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
570 }
571 }
572  
573 1 my @export_std = @EXPORT;
574 1 for my $method (@export_std) {
575 6 push @EXPORT, map { join '_', $method, $_ } keys %shortcuts;
576 }
577  
578 1 push @EXPORT, qw(UPLOAD UPLOAD_BODY UPLOAD_BODY_ASSERT);
579  
580 0 ▶sub to_string {
Subroutine to_string: not called CC=2 SCAR=17.9
Pod undocumented
581 0 my $obj = shift;
582 0 ▶ ref($obj) ? $obj->as_string : $obj;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj ? : 0 0 0
583 }
584  
585 # request an interpreter instance and use this interpreter id to
586 # select the same interpreter in requests below
587 0 ▶sub same_interp_tie {
Subroutine same_interp_tie: not called CC=2 SCAR=17.9
Pod undocumented
588 0 my($url) = @_;
589  
590 0 my $res = GET($url, INTERP_KEY, 'tie');
591 0 ▶ unless ($res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->code == 200) 0 0 0
592 0 die sprintf "failed to init the same_handler data (url=%s). " .
593 "Failed with code=%s, response:\n%s",
594 $url, $res->code, $res->content;
595 }
596 0 my $same_interp = $res->header(INTERP_KEY);
597  
598 0 return $same_interp;
599 }
600  
601 # run the request though the selected perl interpreter, by polling
602 # until we found it
603 # currently supports only GET, HEAD, PUT, POST subs
604 0 ▶sub same_interp_do {
Subroutine same_interp_do: not called CC=11 SCAR=48.8
Pod undocumented
605 0 my($same_interp, $sub, $url, @args) = @_;
606  
607 0 ▶ die "must pass an interpreter id, obtained via same_interp_tie()"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $same_interp and $same_interp 0 0 0
Condition0%0/3
defined $same_interp and $same_interp
!ll&&!rl&&r
000
MC/DC0%0/2
defined $same_interp and $same_interp
defined $same_interp $same_interp
Truth table0%0/3
A and B
A= defined $same_interpB= $same_interp
ABresult
0X0
100
111
608 unless defined $same_interp and $same_interp;
609  
610 0 push @args, (INTERP_KEY, $same_interp);
611  
612 0 my $res = '';
613 0 my $times = 0;
614 0 my $found_same_interp = '';
615 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$found_same_interp or do { do { $res = &$sub($url, @args); die "no result" unless $res; my $code = $res->code; if ($code == 200) { $found_same_interp = $res->header("X-PerlInterpreter") || ""; } elsif ($code == 404) { (); } else { die sprintf("failed to run the request (url=%s):\ncode=%s, response:\n%s", $url, $code, $res->content); } ; unless ($found_same_interp eq $same_interp) { $found_same_interp = ""; } ; if ($times++ > 200) { die "unable to find interp $same_interp\n"; } } } 0 0 0
616 #loop until we get a response from our interpreter instance
617 0 $res = $sub->($url, @args);
618 0 ▶ die "no result" unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
619 0 my $code = $res->code;
620 0 ▶ if ($code == 200) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
elsif ($code == 404) { } 0 0 0
621 0 ▶ $found_same_interp = $res->header(INTERP_KEY) || '';
Condition0%0/2
$res->header('X-PerlInterpreter') || ''
l!l
00
MC/DC0%0/1
$res->header('X-PerlInterpreter') || ''
$res->header('X-PerlInterpreter')
Truth table0%0/2
A
A= $res->header('X-PerlInterpreter')
Aresult
11
00
622 }
623 elsif ($code == 404) {
624 # try again
625 }
626 else {
627 0 die sprintf "failed to run the request (url=%s):\n" .
628 "code=%s, response:\n%s", $url, $code, $res->content;
629 }
630  
631 0 ▶ unless ($found_same_interp eq $same_interp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_same_interp eq $same_interp) 0 0 0
632 0 $found_same_interp = '';
633 }
634  
635 0 ▶ if ($times++ > TRY_TIMES) { #prevent endless loop
Branch: 1 branch
BranchTrueFalseTotal
if ($times++ > 200) 0 0 0
636 0 die "unable to find interp $same_interp\n";
637 }
638 } until ($found_same_interp);
639  
640 0 ▶ return $found_same_interp ? $res : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found_same_interp ? : 0 0 0
641 }
642  
643  
644 0 ▶sub set_client_cert {
Subroutine set_client_cert: not called CC=6 SCAR=37.4
Pod undocumented
645 0 my $name = shift;
646 0 my $vars = Apache::Test::vars();
647 0 my $dir = join '/', $vars->{sslca}, $vars->{sslcaorg};
648  
649 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
650 0 my ($cert, $key) = ("$dir/certs/$name.crt", "$dir/keys/$name.pem");
651 # IO::Socket:SSL raw socket compatibility
652 0 $conn_opts->{SSL_cert_file} = $cert;
653 0 $conn_opts->{SSL_key_file} = $key;
654 0 ▶ if ($LWP::VERSION >= 6.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LWP::VERSION >= 6) 0 0 0
655 # IO::Socket:SSL doesn't look at environment variables
656 0 ▶ if ($UA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UA) { } 0 0 0
657 0 $UA->ssl_opts(SSL_cert_file => $cert);
658 0 $UA->ssl_opts(SSL_key_file => $key);
659 } else {
660 0 user_agent(ssl_opts => { SSL_cert_file => $cert,
661 SSL_key_file => $key });
662 }
663 }
664 }
665 else {
666 # IO::Socket:SSL raw socket compatibility
667 0 $conn_opts->{SSL_cert_file} = undef;
668 0 $conn_opts->{SSL_key_file} = undef;
669 0 ▶ if ($LWP::VERSION >= 6.0 and $UA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LWP::VERSION >= 6 and $UA) 0 0 0
Condition0%0/3
$LWP::VERSION >= 6 and $UA
!ll&&!rl&&r
000
MC/DC0%0/2
$LWP::VERSION >= 6 and $UA
$LWP::VERSION >= 6 $UA
Truth table0%0/3
A and B
A= $LWP::VERSION >= 6B= $UA
ABresult
0X0
100
111
670 0 $UA->ssl_opts(SSL_cert_file => undef);
671 0 $UA->ssl_opts(SSL_key_file => undef);
672 }
673 }
674 }
675  
676 # Only for IO::Socket:SSL raw socket compatibility,
677 # when using user_agent() already done in its
678 # constructor.
679 0 ▶sub set_ca_cert {
Subroutine set_ca_cert: not called CC=1 SCAR=6.9
Pod undocumented
680 0 my $vars = Apache::Test::vars();
681 0 my $cafile = "$vars->{sslca}/$vars->{sslcaorg}/certs/ca.crt";
682 0 $conn_opts->{SSL_ca_file} = $cafile;
683 }
684  
685 #want news: urls to work with the LWP shortcuts
686 #but cant find a clean way to override the default nntp port
687 #by brute force we trick Net::NTTP into calling FixupNNTP::new
688 #instead of IO::Socket::INET::new, we fixup the args then forward
689 #to IO::Socket::INET::new
690  
691 #also want KeepAlive on for Net::HTTP
692 #XXX libwww-perl 5.53_xx has: LWP::UserAgent->new(keep_alive => 1);
693  
694 0 ▶sub install_net_socket_new {
Subroutine install_net_socket_new: not called CC=4 SCAR=26.6
Pod undocumented
695 0 my($module, $code) = @_;
696  
697 0 ▶ return unless Apache::Test::have_module($module);
Branch: 1 branch
BranchTrueFalseTotal
unless Apache::Test::have_module($module) 0 0 0
698  
699 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
700  
701 0 my $new;
702 0 my $isa = \@{"$module\::ISA"};
703  
704 0 for (@$isa) {
705 0 ▶ last if $new = $_->can('new');
Branch: 1 branch
BranchTrueFalseTotal
if $new = $_->can("new") 0 0 0
706 }
707  
708 0 my $fixup_class = "Apache::TestRequest::$module";
709 0 unshift @$isa, $fixup_class;
710  
711 0 ▶ *{"$fixup_class\::new"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
712 0 my $class = shift;
713 0 my $args = {@_};
714 0 $code->($args);
715 0 return $new->($class, %$args);
716 0 };
717 }
718  
719 my %scheme_fixups = (
720 0 ▶ 'news' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
721 0 ▶ return if $INC{'Net/NNTP.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Net/NNTP.pm"} 0 0 0
722 0 eval {
723 0 ▶ install_net_socket_new('Net::NNTP' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
724 0 my $args = shift;
725 0 my($host, $port) = split ':',
726 Apache::TestRequest::hostport();
727 0 $args->{PeerPort} = $port;
728 0 $args->{PeerAddr} = $host;
729 0 });
730 };
731 },
732 1 );
733  
734 0 ▶sub scheme_fixup {
Subroutine scheme_fixup: not called CC=2 SCAR=17.9
Pod undocumented
735 0 my $scheme = shift;
736 0 my $fixup = $scheme_fixups{$scheme};
737 0 ▶ return unless $fixup;
Branch: 1 branch
BranchTrueFalseTotal
unless $fixup 0 0 0
738 0 $fixup->();
739 }
740  
741 # when the client side simply prints the response body which should
742 # include the test's output, we need to make sure that the request
743 # hasn't failed, or the test will be skipped instead of indicating the
744 # error.
745 0 ▶sub content_assert {
Subroutine content_assert: not called CC=2 SCAR=17.9
Pod undocumented
746 0 my $res = shift;
747  
748 0 ▶ return $res->content if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
749  
750 0 die join "\n",
751 "request has failed (the response code was: " . $res->code . ")",
752 "see t/logs/error_log for more details\n";
753 }
754  
755 1 1;
756  
757 =head1 NAME
758  
759 Apache::TestRequest - Send requests to your Apache test server
760  
761 =head1 SYNOPSIS
762  
763 use Apache::Test qw(ok have_lwp);
764 use Apache::TestRequest qw(GET POST);
765 use Apache::Constants qw(HTTP_OK);
766  
767 plan tests => 1, have_lwp;
768  
769 my $res = GET '/test.html';
770 ok $res->code == HTTP_OK, "Request is ok";
771  
772 =head1 DESCRIPTION
773  
774 B<Apache::TestRequest> provides convenience functions to allow you to
775 make requests to your Apache test server in your test scripts. It
776 subclasses C<LWP::UserAgent>, so that you have access to all if its
777 methods, but also exports a number of useful functions likely useful
778 for majority of your test requests. Users of the old C<Apache::test>
779 (or C<Apache::testold>) module, take note! Herein lie most of the
780 functions you'll need to use to replace C<Apache::test> in your test
781 suites.
782  
783 Each of the functions exported by C<Apache::TestRequest> uses an
784 C<LWP::UserAgent> object to submit the request and retrieve its
785 results. The return value for many of these functions is an
786 HTTP::Response object. See L<HTTP::Response|HTTP::Response> for
787 documentation of its methods, which you can use in your tests. For
788 example, use the C<code()> and C<content()> methods to test the
789 response code and content of your request. Using C<GET>, you can
790 perform a couple of tests using these methods like this:
791  
792 use Apache::Test qw(ok have_lwp);
793 use Apache::TestRequest qw(GET POST);
794 use Apache::Constants qw(HTTP_OK);
795  
796 plan tests => 2, have_lwp;
797  
798 my $uri = "/test.html?foo=1&bar=2";
799 my $res = GET $uri;
800 ok $res->code == HTTP_OK, "Check that the request was OK";
801 ok $res->content eq "foo => 1, bar => 2", "Check its content";
802  
803 Note that you can also use C<Apache::TestRequest> with
804 C<Test::Builder> and its derivatives, including C<Test::More>:
805  
806 use Test::More;
807 # ...
808 is $res->code, HTTP_OK, "Check that the request was OK";
809 is $res->content, "foo => 1, bar => 2", "Check its content";
810  
811 =head1 CONFIGURATION FUNCTION
812  
813 You can tell C<Apache::TestRequest> what kind of C<LWP::UserAgent>
814 object to use for its convenience functions with C<user_agent()>. This
815 function uses its arguments to construct an internal global
816 C<LWP::UserAgent> object that will be used for all subsequent requests
817 made by the convenience functions. The arguments it takes are the same
818 as for the C<LWP::UserAgent> constructor. See the
819 C<L<LWP::UserAgent|LWP::UserAgent>> documentation for a complete list.
820  
821 The C<user_agent()> function only creates the internal
822 C<LWP::UserAgent> object the first time it is called. Since this
823 function is called internally by C<Apache::TestRequest>, you should
824 always use the C<reset> parameter to force it to create a new global
825 C<LWP::UserAgent> Object:
826  
827 Apache::TestRequest::user_agent(reset => 1, %params);
828  
829 C<user_agent()> differs from C<< LWP::UserAgent->new >> in two
830 additional ways. First, it supports an additional parameter,
831 C<keep_alive>, which enables connection persistence, where the same
832 connection is used to process multiple requests (and, according to the
833 C<L<LWP::UserAgent|LWP::UserAgent>> documentation, has the effect of
834 loading and enabling the new experimental HTTP/1.1 protocol module).
835  
836 And finally, the semantics of the C<requests_redirectable> parameter is
837 different than for C<LWP::UserAgent> in that you can pass it a boolean
838 value as well as an array for C<LWP::UserAgent>. To force
839 C<Apache::TestRequest> not to follow redirects in any of its convenience
840 functions, pass a false value to C<requests_redirectable>:
841  
842 Apache::TestRequest::user_agent(reset => 1,
843 requests_redirectable => 0);
844  
845 If LWP is not installed, then you can still pass in an array reference
846 as C<LWP::UserAgent> expects. C<Apache::TestRequest> will examine the
847 array and allow redirects if the array contains more than one value or
848 if there is only one value and that value is not "POST":
849  
850 # Always allow redirection.
851 my $redir = have_lwp() ? [qw(GET HEAD POST)] : 1;
852 Apache::TestRequest::user_agent(reset => 1,
853 requests_redirectable => $redir);
854  
855 But note that redirection will B<not> work with C<POST> unless LWP is
856 installed. It's best, therefore, to check C<have_lwp> before running
857 tests that rely on a redirection from C<POST>.
858  
859 Sometimes it is desireable to have C<Apache::TestRequest> remember
860 cookies sent by the pages you are testing and send them back to the
861 server on subsequent requests. This is especially necessary when
862 testing pages whose functionality relies on sessions or the presence
863 of preferences stored in cookies.
864  
865 By default, C<LWP::UserAgent> does B<not> remember cookies between
866 requests. You can tell it to remember cookies between request by
867 adding:
868  
869 Apache::TestRequest::user_agent(cookie_jar => {});
870  
871 before issuing the requests.
872  
873  
874 =head1 FUNCTIONS
875  
876 C<Apache::TestRequest> exports a number of functions that will likely
877 prove convenient for use in the majority of your request tests.
878  
879  
880  
881  
882 =head2 Optional Parameters
883  
884 Each function also takes a number of optional arguments.
885  
886 =over 4
887  
888 =item redirect_ok
889  
890 By default a request will follow redirects retrieved from the server. To
891 prevent this behavior, pass a false value to a C<redirect_ok>
892 parameter:
893  
894 my $res = GET $uri, redirect_ok => 0;
895  
896 Alternately, if all of your tests need to disable redirects, tell
897 C<Apache::TestRequest> to use an C<LWP::UserAgent> object that
898 disables redirects:
899  
900 Apache::TestRequest::user_agent( reset => 1,
901 requests_redirectable => 0 );
902  
903 =item cert
904  
905 If you need to force an SSL request to use a particular SSL
906 certificate, pass the name of the certificate via the C<cert>
907 parameter:
908  
909 my $res = GET $uri, cert => 'my_cert';
910  
911 =item content
912  
913 If you need to add content to your request, use the C<content>
914 parameter:
915  
916 my $res = GET $uri, content => 'hello world!';
917  
918 =item filename
919  
920 The name of a local file on the file system to be sent to the Apache
921 test server via C<UPLOAD()> and its friends.
922  
923 =back
924  
925 =head2 The Functions
926  
927 =head3 GET
928  
929 my $res = GET $uri;
930  
931 Sends a simple GET request to the Apache test server. Returns an
932 C<HTTP::Response> object.
933  
934 You can also supply additional headers to be sent with the request by
935 adding their name/value pairs after the C<url> parameter, for example:
936  
937 my $res = GET $url, 'Accept-Language' => 'de,en-us,en;q=0.5';
938  
939 =head3 GET_STR
940  
941 A shortcut function for C<GET($uri)-E<gt>as_string>.
942  
943 =head3 GET_BODY
944  
945 A shortcut function for C<GET($uri)-E<gt>content>.
946  
947 =head3 GET_BODY_ASSERT
948  
949 Use this function when your test is outputting content that you need
950 to check, and you want to make sure that the request was successful
951 before comparing the contents of the request. If the request was
952 unsuccessful, C<GET_BODY_ASSERT> will return an error
953 message. Otherwise it will simply return the content of the request
954 just as C<GET_BODY> would.
955  
956 =head3 GET_OK
957  
958 A shortcut function for C<GET($uri)-E<gt>is_success>.
959  
960 =head3 GET_RC
961  
962 A shortcut function for C<GET($uri)-E<gt>code>.
963  
964 =head3 GET_HEAD
965  
966 Throws out the content of the request, and returns the string
967 representation of the request. Since the body has been thrown out, the
968 representation will consist solely of the headers. Furthermore,
969 C<GET_HEAD> inserts a "#" at the beginning of each line of the return
970 string, so that the contents are suitable for printing to STDERR
971 during your tests without interfering with the workings of
972 C<Test::Harness>.
973  
974 =head3 HEAD
975  
976 my $res = HEAD $uri;
977  
978 Sends a HEAD request to the Apache test server. Returns an
979 C<HTTP::Response> object.
980  
981 =head3 HEAD_STR
982  
983 A shortcut function for C<HEAD($uri)-E<gt>as_string>.
984  
985 =head3 HEAD_BODY
986  
987 A shortcut function for C<HEAD($uri)-E<gt>content>. Of course, this
988 means that it will likely return nothing.
989  
990 =head3 HEAD_BODY_ASSERT
991  
992 Use this function when your test is outputting content that you need
993 to check, and you want to make sure that the request was successful
994 before comparing the contents of the request. If the request was
995 unsuccessful, C<HEAD_BODY_ASSERT> will return an error
996 message. Otherwise it will simply return the content of the request
997 just as C<HEAD_BODY> would.
998  
999 =head3 HEAD_OK
1000  
1001 A shortcut function for C<GET($uri)-E<gt>is_success>.
1002  
1003 =head3 HEAD_RC
1004  
1005 A shortcut function for C<GET($uri)-E<gt>code>.
1006  
1007 =head3 HEAD_HEAD
1008  
1009 Throws out the content of the request, and returns the string
1010 representation of the request. Since the body has been thrown out, the
1011 representation will consist solely of the headers. Furthermore,
1012 C<GET_HEAD> inserts a "#" at the beginning of each line of the return
1013 string, so that the contents are suitable for printing to STDERR
1014 during your tests without interfering with the workings of
1015 C<Test::Harness>.
1016  
1017 =head3 PUT
1018  
1019 my $res = PUT $uri;
1020  
1021 Sends a simple PUT request to the Apache test server. Returns an
1022 C<HTTP::Response> object.
1023  
1024 =head3 PUT_STR
1025  
1026 A shortcut function for C<PUT($uri)-E<gt>as_string>.
1027  
1028 =head3 PUT_BODY
1029  
1030 A shortcut function for C<PUT($uri)-E<gt>content>.
1031  
1032 =head3 PUT_BODY_ASSERT
1033  
1034 Use this function when your test is outputting content that you need
1035 to check, and you want to make sure that the request was successful
1036 before comparing the contents of the request. If the request was
1037 unsuccessful, C<PUT_BODY_ASSERT> will return an error
1038 message. Otherwise it will simply return the content of the request
1039 just as C<PUT_BODY> would.
1040  
1041 =head3 PUT_OK
1042  
1043 A shortcut function for C<PUT($uri)-E<gt>is_success>.
1044  
1045 =head3 PUT_RC
1046  
1047 A shortcut function for C<PUT($uri)-E<gt>code>.
1048  
1049 =head3 PUT_HEAD
1050  
1051 Throws out the content of the request, and returns the string
1052 representation of the request. Since the body has been thrown out, the
1053 representation will consist solely of the headers. Furthermore,
1054 C<PUT_HEAD> inserts a "#" at the beginning of each line of the return
1055 string, so that the contents are suitable for printing to STDERR
1056 during your tests without interfering with the workings of
1057 C<Test::Harness>.
1058  
1059 =head3 POST
1060  
1061 my $res = POST $uri, [ arg => $val, arg2 => $val ];
1062  
1063 Sends a POST request to the Apache test server and returns an
1064 C<HTTP::Response> object. An array reference of parameters passed as
1065 the second argument will be submitted to the Apache test server as the
1066 POST content. Parameters corresponding to those documented in
1067 L<Optional Parameters|/Optional
1068 Parameters> can follow the optional array reference of parameters, or after
1069 C<$uri>.
1070  
1071 To upload a chunk of data, simply use:
1072  
1073 my $res = POST $uri, content => $data;
1074  
1075 =head3 POST_STR
1076  
1077 A shortcut function for C<POST($uri, @args)-E<gt>content>.
1078  
1079 =head3 POST_BODY
1080  
1081 A shortcut function for C<POST($uri, @args)-E<gt>content>.
1082  
1083 =head3 POST_BODY_ASSERT
1084  
1085 Use this function when your test is outputting content that you need
1086 to check, and you want to make sure that the request was successful
1087 before comparing the contents of the request. If the request was
1088 unsuccessful, C<POST_BODY_ASSERT> will return an error
1089 message. Otherwise it will simply return the content of the request
1090 just as C<POST_BODY> would.
1091  
1092 =head3 POST_OK
1093  
1094 A shortcut function for C<POST($uri, @args)-E<gt>is_success>.
1095  
1096 =head3 POST_RC
1097  
1098 A shortcut function for C<POST($uri, @args)-E<gt>code>.
1099  
1100 =head3 POST_HEAD
1101  
1102 Throws out the content of the request, and returns the string
1103 representation of the request. Since the body has been thrown out, the
1104 representation will consist solely of the headers. Furthermore,
1105 C<POST_HEAD> inserts a "#" at the beginning of each line of the return
1106 string, so that the contents are suitable for printing to STDERR
1107 during your tests without interfering with the workings of
1108 C<Test::Harness>.
1109  
1110 =head3 UPLOAD
1111  
1112 my $res = UPLOAD $uri, \@args, filename => $filename;
1113  
1114 Sends a request to the Apache test server that includes an uploaded
1115 file. Other POST parameters can be passed as a second argument as an
1116 array reference.
1117  
1118 C<Apache::TestRequest> will read in the contents of the file named via
1119 the C<filename> parameter for submission to the server. If you'd
1120 rather, you can submit use the C<content> parameter instead of
1121 C<filename>, and its value will be submitted to the Apache server as
1122 file contents:
1123  
1124 my $res = UPLOAD $uri, undef, content => "This is file content";
1125  
1126 The name of the file sent to the server will simply be "b". Note that
1127 in this case, you cannot pass other POST arguments to C<UPLOAD()> --
1128 they would be ignored.
1129  
1130 =head3 UPLOAD_BODY
1131  
1132 A shortcut function for C<UPLOAD($uri, @params)-E<gt>content>.
1133  
1134 =head3 UPLOAD_BODY_ASSERT
1135  
1136 Use this function when your test is outputting content that you need
1137 to check, and you want to make sure that the request was successful
1138 before comparing the contents of the request. If the request was
1139 unsuccessful, C<UPLOAD_BODY_ASSERT> will return an error
1140 message. Otherwise it will simply return the content of the request
1141 just as C<UPLOAD_BODY> would.
1142  
1143 =head3 OPTIONS
1144  
1145 my $res = OPTIONS $uri;
1146  
1147 Sends an C<OPTIONS> request to the Apache test server. Returns an
1148 C<HTTP::Response> object with the I<Allow> header, indicating which
1149 methods the server supports. Possible methods include C<OPTIONS>,
1150 C<GET>, C<HEAD> and C<POST>. This function thus can be useful for
1151 testing what options the Apache server supports. Consult the HTTPD 1.1
1152 specification, section 9.2, at
1153 I<http://www.faqs.org/rfcs/rfc2616.html> for more information.
1154  
1155  
1156  
1157  
1158  
1159 =head2 URL Manipulation Functions
1160  
1161 C<Apache::TestRequest> also includes a few helper functions to aid in
1162 the creation of urls used in the functions above.
1163  
1164  
1165  
1166 =head3 C<module2path>
1167  
1168 $path = Apache::TestRequest::module2path($module_name);
1169  
1170 Convert a module name to a path, safe for use in the various request
1171 methods above. e.g. C<::> can't be used in URLs on win32. For example:
1172  
1173 $path = Apache::TestRequest::module2path('Foo::Bar');
1174  
1175 returns:
1176  
1177 /Foo__Bar
1178  
1179  
1180  
1181  
1182 =head3 C<module2url>
1183  
1184 $url = Apache::TestRequest::module2url($module);
1185 $url = Apache::TestRequest::module2url($module, \%options);
1186  
1187 Convert a module name to a full URL including the current
1188 configurations C<hostname:port> and sets C<module> accordingly.
1189  
1190 $url = Apache::TestRequest::module2url('Foo::Bar');
1191  
1192 returns:
1193  
1194 http://$hostname:$port/Foo__Bar
1195  
1196 The default scheme used is C<http>. You can override this by passing
1197 your preferred scheme into an optional second param. For example:
1198  
1199 $module = 'MyTestModule::TestHandler';
1200 $url = Apache::TestRequest::module2url($module, {scheme => 'https'});
1201  
1202 returns:
1203  
1204 https://$hostname:$port/MyTestModule__TestHandler
1205  
1206 You may also override the default path with a path of your own:
1207  
1208 $module = 'MyTestModule::TestHandler';
1209 $url = Apache::TestRequest::module2url($module, {path => '/foo'});
1210  
1211 returns:
1212  
1213 http://$hostname:$port/foo
1214  
1215  
1216  
1217  
1218  
1219 =head1 ENVIRONMENT VARIABLES
1220  
1221 The following environment variables can affect the behavior of
1222 C<Apache::TestRequest>:
1223  
1224 =over
1225  
1226 =item APACHE_TEST_PRETEND_NO_LWP
1227  
1228 If the environment variable C<APACHE_TEST_PRETEND_NO_LWP> is set to a
1229 true value, C<Apache::TestRequest> will pretend that LWP is not
1230 available so one can test whether the test suite will survive on a
1231 system which doesn't have libwww-perl installed.
1232  
1233 =item APACHE_TEST_HTTP_09_OK
1234  
1235 If the environment variable C<APACHE_TEST_HTTP_09_OK> is set to a
1236 true value, C<Apache::TestRequest> will allow HTTP/0.9 responses
1237 from the server to proceed. The default behavior is to die if
1238 the response protocol is not either HTTP/1.0 or HTTP/1.1.
1239  
1240 =back
1241  
1242 =head1 SEE ALSO
1243  
1244 L<Apache::Test|Apache::Test> is the main Apache testing module. Use it
1245 to set up your tests, create a plan, and to ensure that you have the
1246 Apache version and modules you need.
1247  
1248 Use L<Apache::TestMM|Apache::TestMM> in your I<Makefile.PL> to set up
1249 your distribution for testing.
1250  
1251 =head1 AUTHOR
1252  
1253 Doug MacEachern with contributions from Geoffrey Young, Philippe
1254 M. Chiasson, Stas Bekman and others. Documentation by David Wheeler.
1255  
1256 Questions can be asked at the test-dev <at> httpd.apache.org list. For
1257 more information see: I<http://httpd.apache.org/test/> and
1258 I<http://perl.apache.org/docs/general/testing/testing.html>.
« lib/Apache/TestConfig.pm ↑ Summary blib/lib/Apache/TestHarnessPHP.pm »