blib/lib/Dancer/Test.pm

filter: Statementstmt 92.1% 234 / 254 Branchbran 84.3% 54 / 64 Conditioncond 72.4% 42 / 58 MC/DCmcdc 51.5% 17 / 33 Subroutinesub 89.5% 43 / 48 Podpod 89.4% 17 / 19 totaltotal 85.5% 407 / 476 CC 61 SCAR 25.0
CC
61
Cov
88%
dancer_response
34.8
response_redirect_location_is
6.0
response_redirect_location_like
6.0
CRAP 12.2
« blib/lib/Dancer/Logger/File.pm ↑ Summary blib/lib/Dancer/FileUtils.pm »
1 package Dancer::Test;
2 85 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Test helpers to test a Dancer application
4 85 $Dancer::Test::VERSION = '1.3513';
5 # test helpers for Dancer apps
6  
7 85 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 85 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 85 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 85 ▶use Test::More import => [ '!pass' ];
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 85 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 85 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 85 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 85 ▶use Dancer ':syntax', ':tests';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 85 ▶use Dancer::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 85 ▶use Dancer::Deprecation;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 85 ▶use Dancer::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 85 ▶use Dancer::Request::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 85 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 85 ▶use Dancer::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 85 ▶use Dancer::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 85 ▶use Dancer::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 85 ▶use Dancer::FileUtils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 85 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 85 ▶use vars '@EXPORT';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 85 @EXPORT = qw(
31 route_exists
32 route_doesnt_exist
33  
34 response_exists
35 response_doesnt_exist
36  
37 response_status_is
38 response_status_isnt
39  
40 response_content_is
41 response_content_isnt
42 response_content_is_deeply
43 response_content_like
44 response_content_unlike
45  
46 response_is_file
47 response_headers_are_deeply
48 response_headers_include
49 response_redirect_location_is
50 response_redirect_location_like
51  
52 dancer_response
53  
54 read_logs
55 );
56  
57 sub import {
58 84 ▶ my ($class, %options) = @_;
Subroutine import: called CC=2 SCAR=6.9
59 84 ▶ $options{appdir} ||= '.';
Condition100%2/2
$options{"appdir"} ||= "."
l!l
183
MC/DC100%1/1
$options{"appdir"} ||= "."
$options{"appdir"}
Truth table100%2/2
A
A= $options{"appdir"}
Aresult
11
00
60  
61 # mimic PSGI env
62 84 $ENV{SERVERNAME} = 'localhost';
63 84 $ENV{HTTP_HOST} = 'localhost';
64 84 $ENV{SERVER_PORT} = 80;
65 84 $ENV{'psgi.url_scheme'} = 'http';
66  
67 84 my ($package, $script) = caller;
68 84 $class->export_to_level(1, $class, @EXPORT);
69  
70 84 Dancer::_init_script_dir($options{appdir});
71 84 Dancer::Config->load;
72  
73 # set a default session engine for tests
74 84 setting 'session' => 'simple';
75  
76 # capture logs for testing
77 84 setting 'logger' => 'capture';
78 84 setting 'log' => 'debug';
79 }
80  
81 # Route Registry
82  
83 sub _isa {
84 1194 ▶ my ( $reference, $classname ) = @_;
Subroutine _isa: called CC=2 SCAR=7.1
85 1194 ▶ return blessed $reference && $reference->isa($classname);
Condition66%2/3
builtin::blessed($reference) && $reference->isa($classname)
!ll&&!rl&&r
1177017
MC/DC50%1/2
builtin::blessed($reference) && $reference->isa($classname)
builtin::blessed($reference) $reference->isa($classname)
Truth table66%2/3
A && B
A= builtin::blessed($reference)B= $reference->isa($classname)
ABresult
0X0
100
111
86 }
87  
88 sub _req_to_response {
89 424 ▶ my $req = shift;
Subroutine _req_to_response: called CC=3 SCAR=11.0
90  
91 # already a response object
92 424 ▶ return $req if _isa($req, 'Dancer::Response');
Branch: 1 branch
BranchTrueFalseTotal
if _isa($req, "Dancer::Response") 8 416 424
93  
94 416 ▶ return dancer_response( ref $req eq 'ARRAY' ? @$req : ( 'GET', $req ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $req eq 'ARRAY' ? : 379 37 416
95 }
96  
97 sub _req_label {
98 274 ▶ my $req = shift;
Subroutine _req_label: called CC=3 SCAR=11.0
99  
100 274 ▶ return _isa($req, 'Dancer::Response') ? 'response object'
Branch: 2 branches
BranchTrueFalseTotal
_isa($req, 'Dancer::Response') ? : 7 267 274
ref $req eq 'ARRAY' ? : 249 18 267
101 : ref $req eq 'ARRAY' ? join( ' ', @$req )
102 : "GET $req";
103 }
104  
105 sub expand_req {
106 54 ▶ my $req = shift;
Subroutine expand_req: called CC=2 SCAR=7.2
Pod undocumented
107 54 ▶ return ref $req eq 'ARRAY' ? @$req : ( 'GET', $req );
Branch: 1 branch
BranchTrueFalseTotal
ref $req eq 'ARRAY' ? : 54 0 54
108 }
109  
110 sub route_exists {
111 43 ▶ my ($req, $test_name) = @_;
Subroutine route_exists: called CC=2 SCAR=6.9
Pod documented
112 43 my $tb = Test::Builder->new;
113  
114 43 my ($method, $path) = expand_req($req);
115 43 ▶ $test_name ||= "a route exists for $method $path";
Condition100%2/2
$test_name ||= "a route exists for $method $path"
l!l
736
MC/DC100%1/1
$test_name ||= "a route exists for $method $path"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
116  
117 43 $req = Dancer::Request->new_for_request($method => $path);
118 43 return $tb->ok(defined(Dancer::App->find_route_through_apps($req)), $test_name);
119 }
120  
121 sub route_doesnt_exist {
122 4 ▶ my ($req, $test_name) = @_;
Subroutine route_doesnt_exist: called CC=2 SCAR=6.9
Pod documented
123 4 my $tb = Test::Builder->new;
124  
125 4 my ($method, $path) = expand_req($req);
126 4 ▶ $test_name ||= "no route exists for $method $path";
Condition100%2/2
$test_name ||= "no route exists for $method $path"
l!l
22
MC/DC100%1/1
$test_name ||= "no route exists for $method $path"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
127  
128 4 $req = Dancer::Request->new_for_request($method => $path);
129 4 return $tb->ok(!defined(Dancer::App->find_route_through_apps($req)), $test_name);
130 }
131  
132 # Response status
133  
134 sub response_exists {
135 0 ▶ Dancer::Deprecation->deprecated(
Subroutine response_exists: not called CC=1 SCAR=6.9
Pod documented
136 fatal => 1,
137 feature => 'response_exists',
138 reason => 'Use response_status_isnt and check for status 404.'
139 );
140 }
141  
142 sub response_doesnt_exist {
143 0 ▶ Dancer::Deprecation->deprecated(
Subroutine response_doesnt_exist: not called CC=1 SCAR=6.9
Pod documented
144 fatal => 1,
145 feature => 'response_doesnt_exist',
146 reason => 'Use response_status_is and check for status 404.',
147 );
148 }
149  
150 sub response_status_is {
151 159 ▶ my ($req, $status, $test_name) = @_;
Subroutine response_status_is: called CC=2 SCAR=7.0
Pod documented
152 159 ▶ $test_name ||= "response status is $status for " . _req_label($req);
Condition66%2/3
$test_name ||= "response status is $status for " . _req_label($req)
l!l&&r!l&&!r
71880
MC/DC0%0/2
$test_name ||= "response status is $status for " . _req_label($req)
$test_name "response status is $status for " . _req_label($req)
Truth table66%2/3
A ||= B
A= $test_nameB= "response status is $status for " . _req_label($req)
ABresult
1X1
011
000
153  
154 159 my $response = _req_to_response($req);
155 159 my $tb = Test::Builder->new;
156 159 return $tb->is_eq($response->status, $status, $test_name);
157 }
158  
159 sub response_status_isnt {
160 4 ▶ my ($req, $status, $test_name) = @_;
Subroutine response_status_isnt: called CC=2 SCAR=7.0
Pod documented
161 4 ▶ $test_name ||= "response status is not $status for " . _req_label($req);
Condition66%2/3
$test_name ||= "response status is not $status for " . _req_label($req)
l!l&&r!l&&!r
130
MC/DC0%0/2
$test_name ||= "response status is not $status for " . _req_label($req)
$test_name "response status is not $status for " . _req_label($req)
Truth table66%2/3
A ||= B
A= $test_nameB= "response status is not $status for " . _req_label($req)
ABresult
1X1
011
000
162  
163 4 my $response = _req_to_response($req);
164 4 my $tb = Test::Builder->new;
165 4 $tb->isnt_eq( $response->{status}, $status, $test_name );
166 }
167  
168 # Response content
169  
170 sub response_content_is {
171 121 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_content_is: called CC=2 SCAR=6.9
Pod documented
172 121 ▶ $test_name ||= "response content looks good for " . _req_label($req);
Condition100%2/2
$test_name ||= 'response content looks good for ' . _req_label($req)
l!l
4081
MC/DC100%1/1
$test_name ||= 'response content looks good for ' . _req_label($req)
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
173  
174 121 my $response = _req_to_response($req);
175 121 my $tb = Test::Builder->new;
176 121 return $tb->is_eq( $response->{content}, $matcher, $test_name );
177 }
178  
179 sub response_content_isnt {
180 4 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_content_isnt: called CC=2 SCAR=7.0
Pod documented
181 4 ▶ $test_name ||= "response content looks good for " . _req_label($req);
Condition50%1/2
$test_name ||= 'response content looks good for ' . _req_label($req)
l!l
04
MC/DC0%0/1
$test_name ||= 'response content looks good for ' . _req_label($req)
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
182  
183 4 my $response = _req_to_response($req);
184 4 my $tb = Test::Builder->new;
185 4 return $tb->isnt_eq( $response->{content}, $matcher, $test_name );
186 }
187  
188 sub response_content_like {
189 38 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_content_like: called CC=2 SCAR=6.9
Pod documented
190 38 ▶ $test_name ||= "response content looks good for " . _req_label($req);
Condition100%2/2
$test_name ||= 'response content looks good for ' . _req_label($req)
l!l
1721
MC/DC100%1/1
$test_name ||= 'response content looks good for ' . _req_label($req)
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
191  
192 38 my $response = _req_to_response($req);
193 38 my $tb = Test::Builder->new;
194 38 return $tb->like( $response->{content}, $matcher, $test_name );
195 }
196  
197 sub response_content_unlike {
198 4 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_content_unlike: called CC=2 SCAR=7.0
Pod documented
199 4 ▶ $test_name ||= "response content looks good for " , _req_label($req);
Condition50%1/2
$test_name ||= "response content looks good for "
l!l
04
MC/DC0%0/1
$test_name ||= "response content looks good for "
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
200  
201 4 my $response = _req_to_response($req);
202 4 my $tb = Test::Builder->new;
203 4 return $tb->unlike( $response->{content}, $matcher, $test_name );
204 }
205  
206 sub response_content_is_deeply {
207 59 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_content_is_deeply: called CC=2 SCAR=6.9
Pod documented
208 59 ▶ $test_name ||= "response content looks good for " . _req_label($req);
Condition100%2/2
$test_name ||= 'response content looks good for ' . _req_label($req)
l!l
950
MC/DC100%1/1
$test_name ||= 'response content looks good for ' . _req_label($req)
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
209  
210 59 local $Test::Builder::Level = $Test::Builder::Level + 1;
211 59 my $response = _req_to_response($req);
212 59 is_deeply $response->{content}, $matcher, $test_name;
213 }
214  
215 sub response_is_file {
216 1 ▶ my ($req, $test_name) = @_;
Subroutine response_is_file: called CC=2 SCAR=7.0
Pod undocumented
217 1 ▶ $test_name ||= "a file is returned for " . _req_label($req);
Condition50%1/2
$test_name ||= 'a file is returned for ' . _req_label($req)
l!l
01
MC/DC0%0/1
$test_name ||= 'a file is returned for ' . _req_label($req)
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
218  
219 1 my $response = _get_file_response($req);
220 1 my $tb = Test::Builder->new;
221 1 return $tb->ok(defined($response), $test_name);
222 }
223  
224 sub response_headers_are_deeply {
225 7 ▶ my ($req, $expected, $test_name) = @_;
Subroutine response_headers_are_deeply: called CC=2 SCAR=6.9
Pod documented
226 7 ▶ $test_name ||= "headers are as expected for " . _req_label($req);
Condition100%2/2
$test_name ||= 'headers are as expected for ' . _req_label($req)
l!l
43
MC/DC100%1/1
$test_name ||= 'headers are as expected for ' . _req_label($req)
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
227  
228 7 local $Test::Builder::Level = $Test::Builder::Level + 1;
229 7 my $response = _req_to_response($req);
230  
231 7 is_deeply(
232 _sort_headers( $response->headers_to_array ),
233 _sort_headers( $expected ),
234 $test_name
235 );
236 }
237  
238 # Sort arrayref of headers (turn it into a list of arrayrefs, sort by the header
239 # & value, then turn it back into an arrayref)
240 sub _sort_headers {
241 14 ▶ my @originalheaders = @{ shift() }; # take a copy we can modify
Subroutine _sort_headers: called CC=3 SCAR=11.0
242 14 my @headerpairs;
243 14 ▶ while (my ($header, $value) = splice @originalheaders, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@originalheaders, 0, 2)) 40 14 54
244 40 push @headerpairs, [ $header, $value ];
245 }
246  
247 # We have an array of arrayrefs holding header => value pairs; sort them by
248 # header then value, and return them flattened back into an arrayref
249 return [
250 40 map { @$_ }
251 14 ▶ sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] }
Branch: 1 branch
BranchTrueFalseTotal
$Dancer::Test::a->[0] cmp $Dancer::Test::b->[0] or $Dancer::Test::a->[1] cmp $Dancer::Test::b->[1] 0 35 35
252 @headerpairs
253 ];
254 }
255  
256  
257 sub response_headers_include {
258 27 ▶ my ($req, $expected, $test_name) = @_;
Subroutine response_headers_include: called CC=2 SCAR=6.9
Pod documented
259 27 ▶ $test_name ||= "headers include expected data for " . _req_label($req);
Condition100%2/2
$test_name ||= 'headers include expected data for ' . _req_label($req)
l!l
819
MC/DC100%1/1
$test_name ||= 'headers include expected data for ' . _req_label($req)
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
260 27 my $tb = Test::Builder->new;
261  
262 27 my $response = _req_to_response($req);
263 27 return $tb->ok(_include_in_headers($response->headers_to_array, $expected), $test_name);
264 }
265  
266 sub response_redirect_location_is {
267 0 ▶ my ($req, $expected, $test_name) = @_;
Subroutine response_redirect_location_is: not called CC=2 SCAR=17.9
Pod documented
268 0 ▶ $test_name ||= "redirect location looks good for " . _req_label($req);
Condition0%0/2
$test_name ||= 'redirect location looks good for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'redirect location looks good for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
269 0 my $tb = Test::Builder->new;
270  
271 0 my $response = _req_to_response($req);
272 0 return $tb->is_eq($response->header('location'), $expected, $test_name);
273 }
274  
275 sub response_redirect_location_like {
276 0 ▶ my ($req, $matcher, $test_name) = @_;
Subroutine response_redirect_location_like: not called CC=2 SCAR=17.9
Pod documented
277 0 ▶ $test_name ||= "redirect location looks good for " . _req_label($req);
Condition0%0/2
$test_name ||= 'redirect location looks good for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'redirect location looks good for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
278 0 my $tb = Test::Builder->new;
279  
280 0 my $response = _req_to_response($req);
281 0 return $tb->like($response->header('location'), $matcher, $test_name);
282 }
283  
284  
285 # make sure the given header sublist is included in the full headers array
286 sub _include_in_headers {
287 27 ▶ my ($full_headers, $expected_subset) = @_;
Subroutine _include_in_headers: called CC=3 SCAR=11.0
288  
289 # walk through all the expected header pairs, make sure
290 # they exist with the same value in the full_headers list
291 # return false as soon as one is not.
292 27 ▶ for (my $i=0; $i<scalar(@$expected_subset); $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$expected_subset) 45 27 72
293 45 my ($name, $value) = ($expected_subset->[$i], $expected_subset->[$i + 1]);
294 45 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _check_header($full_headers, $name, $value) 0 45 45
295 unless _check_header($full_headers, $name, $value);
296 }
297  
298 # we've found all the expected pairs in the $full_headers list
299 27 return 1;
300 }
301  
302 sub _check_header {
303 45 ▶ my ($headers, $key, $value) = @_;
Subroutine _check_header: called CC=4 SCAR=14.0
304 45 ▶ for (my $i=0; $i<scalar(@$headers); $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$headers) 112 0 112
305 112 my ($name, $val) = ($headers->[$i], $headers->[$i + 1]);
306 112 ▶ return 1 if $name eq $key && $value eq $val;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $key and $value eq $val 45 67 112
Condition100%3/3
$name eq $key and $value eq $val
!ll&&!rl&&r
59845
MC/DC100%2/2
$name eq $key and $value eq $val
$name eq $key $value eq $val
Truth table100%3/3
A and B
A= $name eq $keyB= $value eq $val
ABresult
0X0
100
111
307 }
308 0 return 0;
309 }
310  
311 sub dancer_response {
312 496 ▶ my ($method, $path, $args) = @_;
Subroutine dancer_response: called CC=33 SCAR=35.5
Pod documented
313 496 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
36460
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
314 496 my $extra_env = {};
315  
316 496 ▶ if ($method =~ /^(?:PUT|POST)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:PUT|POST)$/) 28 468 496
317  
318 28 my ($content, $content_type);
319  
320 28 ▶ if ( $args->{body} and $args->{files} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args->{'body'} and $args->{'files'}) { } 0 28 28
elsif ($args->{'body'}) { } 13 15 28
elsif ($args->{'files'}) { } 3 12 15
Condition66%2/3
$args->{"body"} and $args->{"files"}
!ll&&!rl&&r
15130
MC/DC0%0/2
$args->{"body"} and $args->{"files"}
$args->{"body"} $args->{"files"}
Truth table66%2/3
A and B
A= $args->{"body"}B= $args->{"files"}
ABresult
0X0
100
111
321 # XXX: When fixing this, update this method's POD
322 0 croak 'dancer_response() does not support both body and files';
323 }
324 elsif ( $args->{body} ) {
325 13 $content = $args->{body};
326 $content_type = $args->{content_type}
327 13 ▶ || 'text/plain';
Condition100%2/2
$args->{'content_type'} || 'text/plain'
l!l
211
MC/DC100%1/1
$args->{'content_type'} || 'text/plain'
$args->{'content_type'}
Truth table100%2/2
A
A= $args->{'content_type'}
Aresult
11
00
328  
329 # coerce hashref into an url-encoded string
330 13 ▶ if ( ref($content) && ( ref($content) eq 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content and ref $content eq "HASH") 3 10 13
Condition66%2/3
ref $content and ref $content eq "HASH"
!ll&&!rl&&r
1003
MC/DC50%1/2
ref $content and ref $content eq "HASH"
ref $content ref $content eq "HASH"
Truth table66%2/3
A and B
A= ref $contentB= ref $content eq "HASH"
ABresult
0X0
100
111
331 3 my @tokens;
332 3 ▶ while ( my ( $name, $value ) = each %{$content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$content;}) 3 3 6
333 3 $name = _url_encode($name);
334 3 ▶ my @values = ref $value eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 2 3
335 3 for my $value (@values) {
336 4 $value = _url_encode($value);
337 4 push @tokens, "${name}=${value}";
338 }
339 }
340 3 $content = join( '&', @tokens );
341 3 $content_type = 'application/x-www-form-urlencoded';
342 }
343 }
344 elsif ( $args->{files} ) {
345 3 $content_type = 'multipart/form-data; boundary=----BOUNDARY';
346 3 foreach my $file (@{$args->{files}}){
347 5 ▶ $file->{content_type} ||= 'text/plain';
Condition50%1/2
$file->{"content_type"} ||= "text/plain"
l!l
05
MC/DC0%0/1
$file->{"content_type"} ||= "text/plain"
$file->{"content_type"}
Truth table50%1/2
A
A= $file->{"content_type"}
Aresult
11
00
348 5 $content .= qq/------BOUNDARY\r\n/;
349 5 $content .= qq/Content-Disposition: form-data; name="$file->{name}"; filename="$file->{filename}"\r\n/;
350 5 $content .= qq/Content-Type: $file->{content_type}\r\n\r\n/;
351 5 ▶ if ( $file->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{'data'}) { } 1 4 5
352 1 $content .= $file->{data};
353 } else {
354 4 open my $fh, '<', $file->{filename};
355 4 ▶ if ( -B $file->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-B $file->{"filename"}) 0 4 4
356 0 binmode $fh;
357 }
358 4 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 4 4 8
359 4 $content .= $_;
360 }
361 }
362 5 $content .= "\r\n";
363 }
364 3 $content .= "------BOUNDARY";
365 }
366  
367 28 my $l = 0;
368 28 ▶ $l = length $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 16 12 28
369 28 open my $in, '<', \$content;
370 28 $extra_env->{'CONTENT_LENGTH'} = $l;
371 28 ▶ $extra_env->{'CONTENT_TYPE'} = $content_type || "";
Condition100%2/2
$content_type || ''
l!l
1612
MC/DC100%1/1
$content_type || ''
$content_type
Truth table100%2/2
A
A= $content_type
Aresult
11
00
372 28 $extra_env->{'psgi.input'} = $in;
373 }
374  
375 496 my ($params, $body, $headers) = @$args{qw(params body headers)};
376  
377 496 ▶ $headers = HTTP::Headers->new(@{$headers||[]})
Branch: 2 branches
BranchTrueFalseTotal
unless _isa($headers, "HTTP::Headers") 494 2 496
$headers or [] 477 17 494
378 unless _isa($headers, "HTTP::Headers");
379  
380 496 ▶ if ($headers->header('Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->header("Content-Type")) 16 480 496
381 16 $extra_env->{'CONTENT_TYPE'} = $headers->header('Content-Type');
382 }
383  
384 # handle all the keys of Request::_build_request_env():
385 496 for my $key (qw( user_agent host accept_language accept_charset
386 accept_encoding keep_alive connection accept accept_type referer
387 x_requested_with )) {
388 5456 my $k = sprintf("HTTP_%s", uc $key);
389 $extra_env->{$k} = $headers->{$key}
390 5456 ▶ if exists $headers->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $headers->{$key} 2 5454 5456
391 }
392  
393 # fake the REQUEST_URI
394 # TODO deal with the params
395 496 ▶ unless( $extra_env->{REQUEST_URI} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extra_env->{"REQUEST_URI"}) 496 0 496
396 496 $extra_env->{REQUEST_URI} = $path;
397 496 ▶ if ( $method eq 'GET' and $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET" and $params) 8 488 496
Condition100%3/3
$method eq "GET" and $params
!ll&&!rl&&r
694198
MC/DC100%2/2
$method eq "GET" and $params
$method eq "GET" $params
Truth table100%3/3
A and B
A= $method eq "GET"B= $params
ABresult
0X0
100
111
398 $extra_env->{REQUEST_URI} .=
399 8 '?' . join '&', map { join '=', $_, $params->{$_} }
400 sort keys %$params;
401 }
402 }
403  
404 496 my $request = Dancer::Request->new_for_request(
405 $method => $path,
406 $params, $body, $headers, $extra_env
407 );
408  
409 # first, reset the current state
410 496 Dancer::SharedData->reset_all();
411  
412 # then store the request
413 496 Dancer::SharedData->request($request);
414  
415 # XXX this is a hack!!
416 496 ▶ $request = Dancer::Serializer->process_request($request)
Branch: 1 branch
BranchTrueFalseTotal
if "Dancer::App"->current->setting("serializer") 33 463 496
417 if Dancer::App->current->setting('serializer');
418  
419 496 my $get_action = Dancer::Handler::render_request($request);
420 496 my $response = Dancer::SharedData->response();
421  
422 496 ▶ $response->content('') if $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 2 494 496
423 496 Dancer::SharedData->reset_response();
424 496 ▶ return $response if $get_action;
Branch: 1 branch
BranchTrueFalseTotal
if $get_action 496 0 496
425 0 ▶ (defined $response && $response->exists) ? return $response : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $response && $response->exists ? : 0 0 0
Condition0%0/3
defined $response and $response->exists
!ll&&!rl&&r
000
MC/DC0%0/2
defined $response and $response->exists
defined $response $response->exists
Truth table0%0/3
A and B
A= defined $responseB= $response->exists
ABresult
0X0
100
111
426 }
427  
428 # private
429  
430 sub _url_encode {
431 7 ▶ my $string = shift;
Subroutine _url_encode: called CC=1 SCAR=0.0
432 7 $string =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
433 7 return $string;
434 }
435  
436 sub _get_file_response {
437 7 ▶ my ($req) = @_;
Subroutine _get_file_response: called CC=1 SCAR=0.0
438  
439 7 my ($method, $path, $params) = expand_req($req);
440 7 my $request = Dancer::Request->new_for_request($method => $path, $params);
441 7 Dancer::SharedData->request($request);
442 7 return Dancer::Renderer::get_file_response();
443 }
444  
445 sub _get_handler_response {
446 0 ▶ my ($req) = @_;
Subroutine _get_handler_response: not called CC=1 SCAR=6.9
447 0 my ($method, $path, $params) = expand_req($req);
448 0 my $request = Dancer::Request->new_for_request($method => $path, $params);
449 0 return Dancer::Handler->handle_request($request);
450 }
451  
452 sub read_logs {
453 2 ▶ return Dancer::Logger::Capture->trap->read;
Subroutine read_logs: called CC=1 SCAR=0.0
Pod documented
454 }
455  
456  
457 85 1;
458  
459 __END__
« blib/lib/Dancer/Logger/File.pm ↑ Summary blib/lib/Dancer/FileUtils.pm »