blib/lib/Dancer/Test.pm

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