Apache.pm

filter: Statementstmt 79.0% 113 / 143 Branchbran 41.6% 15 / 36 Conditioncond 47.3% 18 / 38 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 95.2% 20 / 21 Podpod 40.0% 2 / 5 totaltotal 64.1% 170 / 265 CC 36 SCAR 24.4
CC
36
Cov
67%
DEBUG
20.4
NYI_DEBUG
20.0
execute_handler
17.5
CRAP 11.4
↑ Summary Apache/Emulation.pm »
1 # Mock::Apache - a package to mock the mod_perl 1.x environment
2 #
3 # Method descriptions are taken from my book: "Mod_perl Pocket Reference",
4 # Andrew Ford, O'Reilly & Associates, 2001, 0-596-00047-2.
5 # Page references in the comments (marked "MPPR pNN") refer to the book.
6 #
7 # Copyright (C) 2013, Andrew Ford. All rights reserved.
8 # This library is free software; you can redistribute it and/or modify
9 # it under the same terms as Perl itself.
10  
11 package Mock::Apache;
12  
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Apache::ConfigParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Module::Loaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use parent 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 5 __PACKAGE__->mk_accessors(qw(server));
27  
28 5 our $VERSION = "0.09";
29 5 our $DEBUG;
30  
31 5 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=13.9
32 5 Readonly our @APACHE_CLASSES
33 => qw( Apache Apache::SubRequest Apache::Server Apache::Connection
34 Apache::File Apache::Log Apache::Table Apache::URI Apache::Util
35 Apache::Constants Apache::ModuleConfig Apache::Symbol
36 Apache::Request Apache::Upload Apache::Cookie );
37  
38 # Lie about the Apache::* modules being loaded
39 mark_as_loaded($_)
40 5 for @APACHE_CLASSES;
41  
42 # alias the DEBUG() and NYI_DEBUG() functions into each class
43 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 *{"${_}::DEBUG"} = \&DEBUG for @APACHE_CLASSES;
45 5 *{"${_}::NYI_DEBUG"} = \&NYI_DEBUG for @APACHE_CLASSES;
46  
47 5 $ENV{MOD_PERL} = 'CGI-Perl/1.1';
48 }
49  
50 # These packages need to come after the Apache::* modules have been
51 # marked as loaded, to avoid the real Apache::* classes being dragged
52 # in.
53 5 ▶use Mock::Apache::Emulation;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 5 ▶use Mock::Apache::RemoteClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57 5 Readonly our $DEFAULT_HOSTNAME => 'server.example.com';
58 5 Readonly our $DEFAULT_ADDR => '22.22.22.22';
59 5 Readonly our $DEFAULT_ADMIN => 'webmaster';
60  
61 # Default locations (RedHat-inspired)
62  
63 5 Readonly our $DEFAULT_SERVER_ROOT => '/etc/httpd';
64 5 Readonly our $DEFAULT_DOCUMENT_ROOT => '/var/www/html';
65  
66 # I am still playing with the API to Mock::Apache.
67 # I envisage having methods to:
68 # * set up the mock server
69 # * run a request through the server
70 # * create an apache request object
71  
72 # Set up a mock Apache server
73  
74 4 ▶sub setup_server {
Subroutine setup_server: called CC=11 SCAR=27.6
Pod documented
75 4 my ($class, %params) = @_;
76  
77 4 my $cfg = Apache::ConfigParser->new;
78  
79 4 ▶ if (my $config_file = $params{config_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_file = $params{"config_file"}) 1 3 4
80 1 $cfg->parse_file($config_file);
81 }
82  
83 4 $DEBUG = delete $params{DEBUG};
84  
85 4 ▶ $params{document_root} ||= _get_config_value($cfg, 'DocumentRoot', $DEFAULT_DOCUMENT_ROOT);
Condition33%1/3
$params{"document_root"} ||= _get_config_value($cfg, "DocumentRoot", $Mock::Apache::DEFAULT_DOCUMENT_ROOT)
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{"document_root"} ||= _get_config_value($cfg, "DocumentRoot", $Mock::Apache::DEFAULT_DOCUMENT_ROOT)
$params{"document_root"} _get_config_value($cfg, "DocumentRoot", $Mock::Apache::DEFAULT_DOCUMENT_ROOT)
Truth table33%1/3
A ||= B
A= $params{"document_root"}B= _get_config_value($cfg, "DocumentRoot", $Mock::Apache::DEFAULT_DOCUMENT_ROOT)
ABresult
1X1
011
000
86 4 ▶ $params{server_root} ||= _get_config_value($cfg, 'ServerRoot', $DEFAULT_SERVER_ROOT);
Condition33%1/3
$params{"server_root"} ||= _get_config_value($cfg, "ServerRoot", $Mock::Apache::DEFAULT_SERVER_ROOT)
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{"server_root"} ||= _get_config_value($cfg, "ServerRoot", $Mock::Apache::DEFAULT_SERVER_ROOT)
$params{"server_root"} _get_config_value($cfg, "ServerRoot", $Mock::Apache::DEFAULT_SERVER_ROOT)
Truth table33%1/3
A ||= B
A= $params{"server_root"}B= _get_config_value($cfg, "ServerRoot", $Mock::Apache::DEFAULT_SERVER_ROOT)
ABresult
1X1
011
000
87 4 ▶ $params{server_hostname} ||= $DEFAULT_HOSTNAME;
Condition66%2/3
$params{"server_hostname"} ||= $Mock::Apache::DEFAULT_HOSTNAME
l!l&&r!l&&!r
130
MC/DC0%0/2
$params{"server_hostname"} ||= $Mock::Apache::DEFAULT_HOSTNAME
$params{"server_hostname"} $Mock::Apache::DEFAULT_HOSTNAME
Truth table66%2/3
A ||= B
A= $params{"server_hostname"}B= $Mock::Apache::DEFAULT_HOSTNAME
ABresult
1X1
011
000
88 4 ▶ $params{server_port} ||= 80;
Condition50%1/2
$params{"server_port"} ||= 80
l!l
04
MC/DC0%0/1
$params{"server_port"} ||= 80
$params{"server_port"}
Truth table50%1/2
A
A= $params{"server_port"}
Aresult
11
00
89 $params{server_admin} ||= _get_config_value($cfg, 'ServerAdmin',
90 4 ▶ $DEFAULT_ADMIN . '@' . $params{server_hostname});
Condition33%1/3
$params{"server_admin"} ||= _get_config_value($cfg, "ServerAdmin", $Mock::Apache::DEFAULT_ADMIN . '@' . $params{"server_hostname"})
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{"server_admin"} ||= _get_config_value($cfg, "ServerAdmin", $Mock::Apache::DEFAULT_ADMIN . '@' . $params{"server_hostname"})
$params{"server_admin"} _get_config_value($cfg, "ServerAdmin", $Mock::Apache::DEFAULT_ADMIN . '@' . $params{"server_hostname"})
Truth table33%1/3
A ||= B
A= $params{"server_admin"}B= _get_config_value($cfg, "ServerAdmin", $Mock::Apache::DEFAULT_ADMIN . '@' . $params{"server_hostname"})
ABresult
1X1
011
000
91 4 ▶ $params{gid} ||= getgrnam('apache') || 48;
Condition40%2/5
$params{"gid"} ||= getgrnam "apache" || 48
l!l&&r!l&&!r
040
getgrnam 'apache' || 48
l!l
04
MC/DC0%0/2
$params{"gid"} ||= getgrnam "apache" || 48
$params{"gid"} getgrnam 'apache'
Truth table0%0/3
A ||= B
A= $params{"gid"}B= getgrnam 'apache'
ABresult
1X1
011
000
92 4 ▶ $params{uid} ||= getpwnam('apache') || 48;
Condition40%2/5
$params{"uid"} ||= getpwnam "apache" || 48
l!l&&r!l&&!r
040
getpwnam 'apache' || 48
l!l
04
MC/DC0%0/2
$params{"uid"} ||= getpwnam "apache" || 48
$params{"uid"} getpwnam 'apache'
Truth table0%0/3
A ||= B
A= $params{"uid"}B= getpwnam 'apache'
ABresult
1X1
011
000
93  
94  
95 4 my $self = bless { %params }, $class;
96  
97 4 $self->{server} = $Apache::server = Apache::Server->new($self, %params);
98  
99 4 return $self;
100 }
101  
102 12 ▶sub _get_config_value {
Subroutine _get_config_value: called CC=3 SCAR=11.0
103 12 my ($config, $directive, $default) = @_;
104  
105 12 ▶ if ($config and my @dirs = $config->find_down_directive_names($directive)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config and my(@dirs) = $config->find_down_directive_names($directive)) 1 11 12
Condition66%2/3
$config and my(@dirs) = $config->find_down_directive_names($directive)
!ll&&!rl&&r
0111
MC/DC50%1/2
$config and my(@dirs) = $config->find_down_directive_names($directive)
$config my(@dirs) = $config->find_down_directive_names($directive)
Truth table66%2/3
A and B
A= $configB= my(@dirs) = $config->find_down_directive_names($directive)
ABresult
0X0
100
111
106 1 return $dirs[0]->value;
107 }
108 11 return $default;
109 }
110  
111 4 ▶sub mock_client {
Subroutine mock_client: called CC=1 SCAR=0.0
Pod undocumented
112 4 my ($self, %params) = @_;
113  
114 4 return Mock::Apache::RemoteClient->new(%params, mock_apache => $self);
115 }
116  
117  
118 # $mock_apache->execute_handler($handler, $request)
119 # $mock_apache->execute_handler($handler, $client, $request)
120  
121 4 ▶sub execute_handler {
Subroutine execute_handler: called CC=14 SCAR=28.6
Pod documented
122 4 my ($self, $handler, $client) = (shift, shift, shift);
123  
124 4 my $request;
125 4 ▶ if (ref $client and $client->isa('Apache')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $client and $client->isa("Apache")) 3 1 4
Condition66%2/3
ref $client and $client->isa("Apache")
!ll&&!rl&&r
013
MC/DC50%1/2
ref $client and $client->isa("Apache")
ref $client $client->isa("Apache")
Truth table66%2/3
A and B
A= ref $clientB= $client->isa("Apache")
ABresult
0X0
100
111
126 3 $request = $client;
127 3 $client = $client->_mock_client;
128 }
129 4 ▶ croak "no mock client specified"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $client and $client->isa("Mock::Apache::RemoteClient") 0 4 4
Condition33%1/3
ref $client and $client->isa("Mock::Apache::RemoteClient")
!ll&&!rl&&r
004
MC/DC0%0/2
ref $client and $client->isa("Mock::Apache::RemoteClient")
ref $client $client->isa("Mock::Apache::RemoteClient")
Truth table33%1/3
A and B
A= ref $clientB= $client->isa("Mock::Apache::RemoteClient")
ABresult
0X0
100
111
130 unless ref $client and $client->isa('Mock::Apache::RemoteClient');
131  
132 4 ▶ if (!ref $handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $handler) 0 4 4
133 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 0 $handler = \&{$handler};
135 }
136  
137 4 ▶ $request ||= $client->new_request(@_);
Condition66%2/3
$request ||= $client->new_request(@_)
l!l&&r!l&&!r
310
MC/DC0%0/2
$request ||= $client->new_request(@_)
$request $client->new_request(@_)
Truth table66%2/3
A ||= B
A= $requestB= $client->new_request(@_)
ABresult
1X1
011
000
138  
139 4 my $saved_debug = $Mock::Apache::DEBUG;
140 4 local $Mock::Apache::DEBUG = 0;
141  
142 4 local($ENV{REMOTE_ADDR}) = $request->subprocess_env('REMOTE_ADDR');
143 4 local($ENV{REMOTE_HOST}) = $request->subprocess_env('REMOTE_HOST');
144  
145 4 local $Apache::request = $request;
146  
147 4 my $rc = eval {
148 4 local $Mock::Apache::DEBUG = $saved_debug;
149 4 local *STDOUT;
150 4 tie *STDOUT, 'IO::Scalar', \$request->{_output};
151 4 $handler->($request);
152 };
153 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
154 0 printf STDERR "handler failed: $@\n";
155 0 $request->status_line('500 Internal server error');
156 }
157  
158 4 my $status = $request->status;
159 4 ▶ if (!$status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 4 0 4
160 4 ▶ if ($rc == &Apache::Constants::OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rc == &Apache::Constants::OK) { } 4 0 4
elsif ($rc == &Apache::Constants::MOVED) { } 0 0 0
161 4 $request->status_line(($status = &Apache::Constants::HTTP_OK) . ' ok');
162 }
163 elsif ($rc == &Apache::Constants::MOVED) {
164 0 $request->status_line(($status = &Apache::Constants::HTTP_MOVED_PERMANENTLY) . ' moved permanently');
165 }
166 }
167 4 ▶ (my $message = $request->status_line || '') =~ s/^... //;
Condition50%1/2
$request->status_line || ''
l!l
40
MC/DC0%0/1
$request->status_line || ''
$request->status_line
Truth table50%1/2
A
A= $request->status_line
Aresult
11
00
168 4 my $headers = HTTP::Headers->new;
169 4 ▶ if (!$request->header_out('content-length')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->header_out("content-length")) 4 0 4
170 4 $request->header_out('content-length', length($request->_output));
171 }
172 4 ▶ while (my($field, $value) = each %{$request->headers_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$request->headers_out;}) 5 4 9
173 5 $headers->push_header($field, $value);
174 }
175 4 my $output = $request->_output;
176  
177 4 return HTTP::Response->new( $status, $message, $headers, $output );
178 }
179  
180 89 ▶sub DEBUG {
Subroutine DEBUG: called CC=5 SCAR=30.1
Pod undocumented
181 89 my ($message, @args) = @_;
182  
183 89 ▶ return unless $Mock::Apache::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mock::Apache::DEBUG 89 0 89
184 0 ▶ $message .= "\n" unless $message =~ qr{\n$};
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ qr/\n$/ 0 0 0
185 0 printf STDERR "DEBUG: $message", @args;
186 0 ▶ if ($DEBUG > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mock::Apache::DEBUG > 1) 0 0 0
187 0 my ($package, $file, $line, $subr) = ((caller(1))[0..2], (caller(2))[3]);
188 0 ▶ if ($file eq __FILE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq __FILE__) 0 0 0
189 0 ($package, $file, $line, $subr) = ((caller(2))[0..2], (caller(3))[3]);
190 }
191 0 my $dir = getcwd;
192 0 $file =~ s{^$dir/}{};
193 0 print STDERR " from $subr at line $line of $file\n";
194 }
195  
196 0 return;
197 }
198  
199 0 ▶sub NYI_DEBUG {
Subroutine NYI_DEBUG: not called CC=4 SCAR=30.0
Pod undocumented
200 0 my ($message, @args) = @_;
201  
202 0 ▶ $message .= "\n" unless $message =~ qr{\n$};
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ qr/\n$/ 0 0 0
203 0 printf STDERR "DEBUG: $message", @args;
204  
205 0 my $carp_level = 1;
206 0 my ($package, $file, $line, $subr) = ((caller(1))[0..2], (caller(2))[3]);
207 0 ▶ if ($file eq __FILE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq __FILE__) 0 0 0
208 0 $carp_level++;
209 0 ($package, $file, $line, $subr) = ((caller(2))[0..2], (caller(3))[3]);
210 }
211 0 ▶ if ($DEBUG > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mock::Apache::DEBUG > 1) 0 0 0
212 0 my $dir = getcwd;
213 0 $file =~ s{^$dir/}{};
214 0 print STDERR " from $subr at line $line of $file";
215 }
216 0 $DB::single = 1;
217 0 local $Carp::CarpLevel = $carp_level;
218 0 croak((caller(1))[3] . " - NOT YET IMPLEMENTED");
219 }
220  
221 5 1;
222  
223 __END__
↑ Summary Apache/Emulation.pm »