blib/lib/Punk/Test.pm

filter: Statementstmt 82.9% 588 / 709 Branchbran 65.3% 226 / 346 Conditioncond 59.0% 111 / 188 MC/DCmcdc 29.8% 31 / 104 Subroutinesub 91.6% 77 / 84 Podpod 100% 42 / 42 totaltotal 72.9% 1075 / 1473 CC 269 SCAR 25.6
CC
269
Cov
74%
_ws_inprocess
39.2
_build_env
30.9
message_like
30.0
CRAP 12.9
« blib/lib/Punk/DevError.pm ↑ Summary blib/lib/Punk/App.pm »
1 package Punk::Test;
2  
3 35 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.69';
8  
9 35 ▶use Test::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use File::Raw::XML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use Punk::Test::WS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Punk::Test::WS::Conn ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # An in-process test client for a compiled Punk application: one object is
19 # one browser - it keeps a cookie jar, so sessions, CSRF and flash flows
20 # test the way they run. Every assertion returns $self for chaining and
21 # reports through the one Test::Builder singleton, so it works under
22 # Test::More and Test2 alike.
23  
24 35 my $TB = Test::Builder->new;
25 35 my %APPS;
26  
27 # An application under test runs in the `test` environment unless something
28 # already said which. It is where a 500 still says what the handler died
29 # with: in production that goes to the log and the body is a fixed message,
30 # and an assertion cannot read a log.
31 35 ▶$ENV{PUNK_ENV} = 'test' unless defined $ENV{PUNK_ENV};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"PUNK_ENV"} 31 4 35
32  
33 89 ▶sub new {
Subroutine new: called CC=11 SCAR=25.3
Pod documented
34 89 my ($class, $app, %opts) = @_;
35 89 ▶ my $app_class = ref $app ? undef : $app;
Branch: 1 branch
BranchTrueFalseTotal
ref $app ? : 9 80 89
36 89 ▶ if (ref $app ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app ne "CODE") 80 9 89
37 80 my $name = $app;
38 80 ▶ unless ($name->can('to_app')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name->can("to_app")) 1 79 80
39 1 ▶ eval "require $name"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $name" or die "Punk::Test: could not load ${name}: $@" 0 1 1
40 or die "Punk::Test: could not load $name: $@";
41 }
42 79 ▶ $name->can('to_app')
Branch: 1 branch
BranchTrueFalseTotal
$name->can("to_app") or die "Punk::Test: $name has no to_app - not a Punk app?\n" 79 0 79
43 or die "Punk::Test: $name has no to_app - not a Punk app?\n";
44 # to_app compiles once per class, and two clients against one app
45 # (two browsers, one server) is a thing tests want - so the frozen
46 # coderef is shared
47 79 ▶ $app = $APPS{$name} //= $name->to_app;
Condition66%2/3
$APPS{$name} //= $name->to_app
l!l&&r!l&&!r
5740
MC/DC0%0/2
$APPS{$name} //= $name->to_app
$APPS{$name} $name->to_app
Truth table66%2/3
A //= B
A= $APPS{$name}B= $name->to_app
ABresult
1X1
011
000
48 }
49 return bless {
50 app => $app,
51 class => $app_class,
52 jar => {},
53 headers => {},
54 timeout => $opts{timeout} // 5,
55 max_bytes => $opts{max_bytes} // 1_000_000,
56 csrf_cookie => $opts{csrf_cookie} // 'csrf',
57 88 ▶ csrf_header => $opts{csrf_header} // 'X-CSRF-Token',
Condition50%4/8
$opts{"timeout"} // 5
l!l
088
$opts{"max_bytes"} // 1000000
l!l
088
$opts{"csrf_cookie"} // "csrf"
l!l
088
$opts{"csrf_header"} // "X-CSRF-Token"
l!l
088
MC/DC0%0/1
$opts{"timeout"} // 5
$opts{"timeout"}
MC/DC0%0/1
$opts{"max_bytes"} // 1000000
$opts{"max_bytes"}
MC/DC0%0/1
$opts{"csrf_cookie"} // "csrf"
$opts{"csrf_cookie"}
MC/DC0%0/1
$opts{"csrf_header"} // "X-CSRF-Token"
$opts{"csrf_header"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"max_bytes"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"csrf_cookie"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"csrf_header"}
Aresult
11
00
58 res => undef,
59 }, $class;
60 }
61  
62 # ---- requests ----------------------------------------------------------------
63  
64 329 ▶sub get_ok { my $self = shift; $self->_request_ok('GET', @_) }
Subroutine get_ok: called CC=1 SCAR=0.0
Pod documented
65 126 ▶sub post_ok { my $self = shift; $self->_request_ok('POST', @_) }
Subroutine post_ok: called CC=1 SCAR=0.0
Pod documented
66 0 ▶sub put_ok { my $self = shift; $self->_request_ok('PUT', @_) }
Subroutine put_ok: not called CC=1 SCAR=6.9
Pod documented
67 0 ▶sub patch_ok { my $self = shift; $self->_request_ok('PATCH', @_) }
Subroutine patch_ok: not called CC=1 SCAR=6.9
Pod documented
68 0 ▶sub delete_ok { my $self = shift; $self->_request_ok('DELETE', @_) }
Subroutine delete_ok: not called CC=1 SCAR=6.9
Pod documented
69 4 ▶sub head_ok { my $self = shift; $self->_request_ok('HEAD', @_) }
Subroutine head_ok: called CC=1 SCAR=0.0
Pod documented
70 0 ▶sub options_ok { my $self = shift; $self->_request_ok('OPTIONS', @_) }
Subroutine options_ok: not called CC=1 SCAR=6.9
Pod documented
71 19 ▶sub request_ok { my $self = shift; $self->_request_ok(@_) }
Subroutine request_ok: called CC=1 SCAR=0.0
Pod documented
72  
73 # Sign a user straight into the jar - the signed session cookie is minted
74 # through the app's own session config, so tests reach guarded pages without
75 # driving a login flow first. Takes an id or a user row; needs a client
76 # built from a class name (the app object holds the session secret).
77 4 ▶sub login_as {
Subroutine login_as: called CC=8 SCAR=20.8
Pod documented
78 4 my ($self, $user) = @_;
79 my $class = $self->{class}
80 4 ▶ or die "Punk::Test: login_as needs a client built from a class "
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->{"class"} or die "Punk::Test: login_as needs a client built from a class name, not a coderef\n" 3 1 4
81 . "name, not a coderef\n";
82 3 my $app = $class->punk_app;
83 3 ▶ my $auth = $app->{auth} || {};
Condition100%2/2
$app->{'auth'} || {}
l!l
21
MC/DC100%1/1
$app->{'auth'} || {}
$app->{'auth'}
Truth table100%2/2
A
A= $app->{'auth'}
Aresult
11
00
84 3 ▶ my $key = $auth->{session_key} // 'user_id';
Condition100%2/2
$auth->{"session_key"} // "user_id"
l!l
21
MC/DC100%1/1
$auth->{"session_key"} // "user_id"
$auth->{"session_key"}
Truth table100%2/2
A
A= $auth->{"session_key"}
Aresult
11
00
85 3 ▶ my $idf = ($auth->{fields} || {})->{id} // 'id';
Condition100%4/4
+($auth->{"fields"} || {})->{"id"} // "id"
l!l
21
$auth->{'fields'} || {}
l!l
21
MC/DC100%1/1
+($auth->{"fields"} || {})->{"id"} // "id"
$auth->{'fields'}
Truth table100%2/2
A
A= $auth->{'fields'}
Aresult
11
00
86 3 ▶ my $id = ref $user eq 'HASH' ? $user->{$idf} : $user;
Branch: 1 branch
BranchTrueFalseTotal
ref $user eq 'HASH' ? : 1 2 3
87 3 ▶ defined $id or die "Punk::Test: login_as needs a user row or id\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $id or die "Punk::Test: login_as needs a user row or id\n" 3 0 3
88 # _seal_session, not _seal: an application with `session store => ...`
89 # keeps the payload server-side, so a cookie with the session sealed into
90 # it is no session at all there. This writes it wherever the application
91 # will look.
92 3 my ($name, $value) = Punk::Session::_seal_session($app, { $key => $id });
93 3 $self->{jar}{$name} = $value;
94 3 return $self;
95 }
96  
97 # A header on every request from here on, the way the jar holds a cookie:
98 # a credential a client carries, an Accept a whole file's worth of requests
99 # needs. undef removes one; no arguments gives back the lot.
100 #
101 # $t->request_header(Authorization => "Bearer $key");
102 # $t->request_header(Authorization => undef);
103 #
104 # A request's own `headers` still wins, so one call can be made an exception
105 # without unsetting and setting again around it. Named for the request side
106 # because `header` is the last response's, and one name for both directions
107 # is how a test comes to assert what it just sent.
108 7 ▶sub request_header {
Subroutine request_header: called CC=4 SCAR=13.9
Pod documented
109 7 my ($self, %h) = @_;
110 7 ▶ return { %{ $self->{headers} } } unless %h;
Branch: 1 branch
BranchTrueFalseTotal
unless %h 1 6 7
111 6 for my $k (keys %h) {
112 7 ▶ if (defined $h{$k}) { $self->{headers}{$k} = $h{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h{$k}) { } 6 1 7
113 1 else { delete $self->{headers}{$k} }
114 }
115 6 return $self;
116 }
117  
118 162 ▶sub _urlenc {
Subroutine _urlenc: called CC=3 SCAR=11.2
119 162 my ($s) = @_;
120 162 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 162 162
121 162 ▶ utf8::encode($s) if utf8::is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($s) 0 162 162
122 162 $s =~ s/([^A-Za-z0-9_.~-])/sprintf '%%%02X', ord $1/ge;
123 162 return $s;
124 }
125  
126 # A path, or [ 'book', id => 1 ] naming a route.
127 #
128 # This is what turns a wrong route name into a failing test rather than a
129 # 404 in production: url_for croaks on a name no route carries, and the
130 # croak arrives here, at the request that named it.
131 484 ▶sub _resolve_path {
Subroutine _resolve_path: called CC=6 SCAR=18.6
132 484 my ($self, $path) = @_;
133 484 ▶ return $path unless ref $path eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "ARRAY" 478 6 484
134 6 my ($name, @args) = @$path;
135 my $class = $self->{class}
136 6 ▶ or die "Punk::Test: [ '$name', ... ] names a route, which needs the "
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->{"class"} or die "Punk::Test: [ '${name}', ... ] names a route, which needs the " . 'application class - construct the client with ' . "Punk::Test->new('MyApp') rather than with a coderef\n" 5 1 6
137 . "application class - construct the client with "
138 . "Punk::Test->new('MyApp') rather than with a coderef\n";
139 5 ▶ my $app = $class->can('punk_app') ? $class->punk_app : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('punk_app') ? : 5 0 5
140 5 ▶ $app && $app->can('url_for')
Branch: 1 branch
BranchTrueFalseTotal
$app and $app->can("url_for") or die "Punk::Test: [ '${name}', ... ] needs Punk 0.31 or newer\n" 5 0 5
Condition33%1/3
$app and $app->can("url_for")
!ll&&!rl&&r
005
MC/DC0%0/2
$app and $app->can("url_for")
$app $app->can("url_for")
Truth table33%1/3
A and B
A= $appB= $app->can("url_for")
ABresult
0X0
100
111
141 or die "Punk::Test: [ '$name', ... ] needs Punk 0.31 or newer\n";
142 5 return $app->url_for($name, @args);
143 }
144  
145 476 ▶sub _build_env {
Subroutine _build_env: called CC=30 SCAR=34.3
146 476 my ($self, $method, $path, %o) = @_;
147 476 ▶ my $query = $o{query} // '';
Condition100%2/2
$o{"query"} // ""
l!l
1475
MC/DC100%1/1
$o{"query"} // ""
$o{"query"}
Truth table100%2/2
A
A= $o{"query"}
Aresult
11
00
148 476 ▶ ($path, $query) = ($1, $2) if !length $query && $path =~ /\A([^?]*)\?(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if not length $query and $path =~ /\A([^?]*)\?(.*)\z/s 12 464 476
Condition100%3/3
not length $query and $path =~ /\A([^?]*)\?(.*)\z/s
!ll&&!rl&&r
146312
MC/DC100%2/2
not length $query and $path =~ /\A([^?]*)\?(.*)\z/s
not length $query $path =~ /\A([^?]*)\?(.*)\z/s
Truth table100%3/3
not(A) and B
A= not length $queryB= $path =~ /\A([^?]*)\?(.*)\z/s
ABresult
0X0
100
111
149  
150 476 my ($body, $type) = ('', $o{type});
151 476 my ($in, $clen);
152 476 ▶ if (exists $o{upload}) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $o{'upload'}) { } 19 457 476
elsif (exists $o{'json'}) { } 20 437 457
elsif (exists $o{'xml'}) { } 7 430 437
elsif (exists $o{'form'}) { } 44 386 430
elsif (defined $o{'body'}) { } 16 370 386
153 19 for my $k (qw(json xml body type)) {
154 die "Punk::Test: upload and $k do not combine - an upload "
155 70 ▶ . "is multipart/form-data\n" if exists $o{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o{$k} 4 66 70
156 }
157 15 ($in, $clen, $type) = $self->_multipart_input($o{form}, $o{upload});
158 }
159 elsif (exists $o{json}) {
160 20 $body = File::Raw::JSON::file_json_encode($o{json});
161 20 ▶ $type //= 'application/json';
Condition50%1/2
$type //= "application/json"
l!l
020
MC/DC0%0/1
$type //= "application/json"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
162 }
163 elsif (exists $o{xml}) {
164 # a Document or Node serialises itself; anything else is markup the
165 # caller wrote, including the malformed strings a test wants to send
166 7 ▶ $body = ref $o{xml} ? $o{xml}->to_string : $o{xml};
Branch: 1 branch
BranchTrueFalseTotal
ref $o{'xml'} ? : 1 6 7
167 7 ▶ $type //= 'application/xml';
Condition50%1/2
$type //= "application/xml"
l!l
07
MC/DC0%0/1
$type //= "application/xml"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
168 }
169 elsif (exists $o{form}) {
170 44 my $f = $o{form};
171 44 $body = join '&', map { _urlenc($_) . '=' . _urlenc($f->{$_}) }
172 sort keys %$f;
173 44 ▶ $type //= 'application/x-www-form-urlencoded';
Condition50%1/2
$type //= "application/x-www-form-urlencoded"
l!l
044
MC/DC0%0/1
$type //= "application/x-www-form-urlencoded"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
174 }
175 elsif (defined $o{body}) {
176 16 $body = $o{body};
177 }
178  
179 470 ▶ unless ($in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in) 457 13 470
180 457 ▶ open $in, '<', \$body or die "Punk::Test: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $in, "<", \$body or die "Punk::Test: $!" 457 0 457
181 457 ▶ $clen = length $body if length $body or defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if length $body or defined $type 87 370 457
Condition100%3/3
length $body or defined $type
l!l&&r!l&&!r
861370
MC/DC100%2/2
length $body or defined $type
length $body defined $type
Truth table100%3/3
A or B
A= length $bodyB= defined $type
ABresult
1X1
011
000
182 }
183 470 my $env = {
184 REQUEST_METHOD => $method,
185 PATH_INFO => $path,
186 QUERY_STRING => $query,
187 SERVER_NAME => 'localhost',
188 SERVER_PORT => 80,
189 SERVER_PROTOCOL => 'HTTP/1.1',
190 HTTP_HOST => 'localhost',
191 'psgi.url_scheme' => 'http',
192 'psgi.input' => $in,
193 'psgi.errors' => \*STDERR,
194 'psgi.version' => [ 1, 1 ],
195 'psgi.streaming' => 1,
196 };
197 470 ▶ if (defined $clen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clen) 100 370 470
198 100 $env->{CONTENT_LENGTH} = $clen;
199 100 ▶ $env->{CONTENT_TYPE} = $type // '';
Condition50%1/2
$type // ""
l!l
1000
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
200 }
201 # Sticky headers first, so a single request's `headers` can still override
202 # one of them - the narrower statement wins, as it does everywhere else.
203 470 for my $h ($self->{headers}, $o{headers}) {
204 940 ▶ next unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 341 599 940
205 599 for my $k (keys %$h) {
206 296 my $ek = uc $k;
207 296 $ek =~ tr/-/_/;
208 296 ▶ $ek = "HTTP_$ek"
Branch: 1 branch
BranchTrueFalseTotal
unless $ek eq "CONTENT_TYPE" or $ek eq "CONTENT_LENGTH" 296 0 296
Condition33%1/3
$ek eq "CONTENT_TYPE" or $ek eq "CONTENT_LENGTH"
l!l&&r!l&&!r
00296
MC/DC0%0/2
$ek eq "CONTENT_TYPE" or $ek eq "CONTENT_LENGTH"
$ek eq "CONTENT_TYPE" $ek eq "CONTENT_LENGTH"
Truth table33%1/3
A or B
A= $ek eq "CONTENT_TYPE"B= $ek eq "CONTENT_LENGTH"
ABresult
1X1
011
000
209 unless $ek eq 'CONTENT_TYPE' || $ek eq 'CONTENT_LENGTH';
210 296 $env->{$ek} = $h->{$k};
211 }
212 }
213 470 ▶ if ($o{csrf}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"csrf"}) 4 466 470
214 4 my $tok = $self->csrf_token;
215 4 ▶ if (defined $tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tok) { } 4 0 4
216 4 my $ek = uc $self->{csrf_header};
217 4 $ek =~ tr/-/_/;
218 4 $env->{"HTTP_$ek"} = $tok;
219 }
220 else {
221 0 $TB->diag("csrf => 1, but the jar holds no "
222 . "'$self->{csrf_cookie}' cookie yet - GET a page first");
223 }
224 }
225 470 ▶ if (my $jar = $self->_jar_header) { $env->{HTTP_COOKIE} = $jar }
Branch: 1 branch
BranchTrueFalseTotal
if (my $jar = $self->_jar_header) 95 375 470
226 470 ▶ %$env = (%$env, %{ $o{env} }) if $o{env};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"env"} 49 421 470
227 470 return $env;
228 }
229  
230 # ---- multipart ---------------------------------------------------------------
231  
232 # The `upload` option: form pairs and upload specs encoded as
233 # multipart/form-data. The body is never assembled - the parts become a
234 # list of items (header strings whole, file parts read a buffer at a
235 # time) drained through psgi.input - so a test of the server's spill
236 # path costs this process one read buffer, not the file.
237  
238 35 my @_BND = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
239 12 ▶sub _mp_boundary { join '', 'PunkTest', map { $_BND[int rand @_BND] } 1 .. 16 }
Subroutine _mp_boundary: called CC=1 SCAR=0.0
240  
241 # A name or filename into a Content-Disposition quoted-string: quotes and
242 # newlines percent-encoded, everything else (UTF-8 included) sent raw,
243 # which is RFC 7578 and what browsers do. The server-side parser stores
244 # what arrives; it does not decode.
245 38 ▶sub _mp_name {
Subroutine _mp_name: called CC=3 SCAR=11.0
246 38 my ($s) = @_;
247 38 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 38 38
248 38 ▶ utf8::encode($s) if utf8::is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($s) 1 37 38
249 38 $s =~ s/(["\r\n])/sprintf '%%%02X', ord $1/ge;
250 38 return $s;
251 }
252  
253 # Whether one content source contains the boundary. A file is scanned in
254 # chunks with a boundary-length overlap, because the hit this exists to
255 # catch is exactly the one that straddles a read.
256 22 ▶sub _mp_contains {
Subroutine _mp_contains: called CC=6 SCAR=18.0
257 22 my ($src, $bnd) = @_;
258 22 ▶ return index($$src, $bnd) >= 0 if ref $src;
Branch: 1 branch
BranchTrueFalseTotal
if ref $src 20 2 22
259 2 ▶ open my $fh, '<:raw', $src or die "Punk::Test: cannot open '$src': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $src or die "Punk::Test: cannot open '${src}': $!" 2 0 2
260 2 my ($tail, $keep) = ('', length($bnd) - 1);
261 2 ▶ while (read $fh, my $chunk, 65536) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, 65536) 5 2 7
262 5 $chunk = $tail . $chunk;
263 5 ▶ return 1 if index($chunk, $bnd) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($chunk, $bnd) != -1 0 5 5
264 5 ▶ $tail = length $chunk > $keep ? substr($chunk, -$keep) : $chunk;
Branch: 1 branch
BranchTrueFalseTotal
length $chunk > $keep ? : 4 1 5
265 }
266 2 return 0;
267 }
268  
269 15 ▶sub _multipart_input {
Subroutine _multipart_input: called CC=27 SCAR=33.5
270 15 my ($self, $form, $upload) = @_;
271 15 ▶ die "Punk::Test: upload takes a hashref of field => arrayref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $upload eq "HASH" 0 15 15
272 unless ref $upload eq 'HASH';
273 15 my (@parts, @scan);
274 15 ▶ for my $k (sort keys %{ $form || {} }) {
Condition100%2/2
$form || {}
l!l
411
MC/DC100%1/1
$form || {}
$form
Truth table100%2/2
A
A= $form
Aresult
11
00
275 4 my $val = $form->{$k};
276 4 ▶ $val = '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 4 4
277 4 ▶ utf8::encode($val) if utf8::is_utf8($val);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($val) 0 4 4
278 4 push @parts, { name => $k, content => $val };
279 4 push @scan, \$val;
280 }
281 15 for my $field (sort keys %$upload) {
282 17 my $v = $upload->{$field};
283 17 ▶ die "Punk::Test: upload => { $field => ... } takes an arrayref: "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" and @$v 1 16 17
Condition66%2/3
ref $v eq "ARRAY" and @$v
!ll&&!rl&&r
1016
MC/DC50%1/2
ref $v eq "ARRAY" and @$v
ref $v eq "ARRAY" @$v
Truth table66%2/3
A and B
A= ref $v eq "ARRAY"B= @$v
ABresult
0X0
100
111
284 . "a path or a scalar ref of content, then optional filename "
285 . "and type\n" unless ref $v eq 'ARRAY' && @$v;
286 16 ▶ for my $spec (ref $v->[0] eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v->[0] eq 'ARRAY' ? : 2 14 16
287 18 my ($src, $fname, $ftype) = @$spec;
288 18 ▶ my %p = (name => $field,
Condition100%2/2
$ftype // "application/octet-stream"
l!l
315
MC/DC100%1/1
$ftype // "application/octet-stream"
$ftype
Truth table100%2/2
A
A= $ftype
Aresult
11
00
289 type => $ftype // 'application/octet-stream');
290 18 ▶ if (ref $src eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src eq 'SCALAR') { } 15 3 18
291 15 ▶ my $bytes = $$src // '';
Condition50%1/2
$$src // ""
l!l
150
MC/DC0%0/1
$$src // ""
$$src
Truth table50%1/2
A
A= $$src
Aresult
11
00
292 15 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 0 15 15
293 15 $p{content} = $bytes;
294 15 ▶ $p{filename} = $fname // $field;
Condition66%2/3
$fname // $field
l!l&&r!l&&!r
1320
MC/DC0%0/2
$fname // $field
$fname $field
Truth table66%2/3
A // B
A= $fnameB= $field
ABresult
1X1
011
000
295 15 push @scan, \$bytes;
296 }
297 else {
298 3 ▶ die "Punk::Test: upload => { $field => ... }: no file at '"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src and -f $src 1 2 3
Condition60%3/5
defined $src and -f $src
!ll&&!rl&&r
012
$src // ""
l!l
10
MC/DC50%1/2
defined $src and -f $src
defined $src -f $src
MC/DC0%0/1
$src // ""
$src
Truth table66%2/3
A and B
A= defined $srcB= -f $src
ABresult
0X0
100
111
Truth table50%1/2
A
A= $src
Aresult
11
00
299 . ($src // '') . "'\n" unless defined $src && -f $src;
300 2 $p{path} = $src;
301 2 $p{size} = -s $src;
302 2 ▶ $p{filename} = $fname // ($src =~ m{([^/\\]+)\z})[0];
Condition66%2/3
$fname // ($src =~ m(([^/\\]+)\z))[0]
l!l&&r!l&&!r
110
MC/DC0%0/2
$fname // ($src =~ m(([^/\\]+)\z))[0]
$fname ($src =~ m(([^/\\]+)\z))[0]
Truth table66%2/3
A // B
A= $fnameB= ($src =~ m(([^/\\]+)\z))[0]
ABresult
1X1
011
000
303 2 push @scan, $src;
304 }
305 17 push @parts, \%p;
306 }
307 }
308  
309 # The boundary must not appear in any part. Verified, not hoped:
310 # scanning also proves every named file readable before the request
311 # starts, so a bad path fails here and not as a truncated body.
312 13 my $bnd = _mp_boundary();
313 13 ▶ for (my $tries = 1; grep { _mp_contains($_, $bnd) } @scan; $tries++) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {_mp_contains($_, $bnd);} @scan) 1 13 14
314 1 ▶ die "Punk::Test: could not pick a boundary absent from the "
Branch: 1 branch
BranchTrueFalseTotal
if $tries >= 16 0 1 1
315 . "content\n" if $tries >= 16;
316 1 $bnd = _mp_boundary();
317 }
318  
319 13 my ($len, @items) = (0);
320 13 for my $p (@parts) {
321 my $hdr = "--$bnd\r\nContent-Disposition: form-data; name=\""
322 21 . _mp_name($p->{name}) . '"';
323 21 ▶ if (exists $p->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"filename"}) 17 4 21
324 17 $hdr .= '; filename="' . _mp_name($p->{filename}) . '"'
325 . "\r\nContent-Type: $p->{type}";
326 }
327 21 $hdr .= "\r\n\r\n";
328 21 ▶ if (defined $p->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{'path'}) { } 2 19 21
329 open my $fh, '<:raw', $p->{path}
330 2 ▶ or die "Punk::Test: cannot open '$p->{path}': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $p->{"path"} or die qq[Punk::Test: cannot open '$p->{"path"}': $!] 2 0 2
331 2 push @items, $hdr, { fh => $fh };
332 2 $len += $p->{size};
333 }
334 else {
335 19 push @items, $hdr, $p->{content};
336 19 $len += length $p->{content};
337 }
338 21 push @items, "\r\n";
339 21 $len += length($hdr) + 2;
340 }
341 13 push @items, "--$bnd--\r\n";
342 13 $len += length $items[-1];
343  
344 13 push @Punk::Test::_MPInput::QUEUE, \@items;
345 13 ▶ open my $in, '<:via(Punk::Test::_MPInput)', \(my $empty = '')
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:via(Punk::Test::_MPInput)", \(my $empty = "") or die "Punk::Test: $!" 13 0 13
346 or die "Punk::Test: $!";
347 13 return ($in, $len, "multipart/form-data; boundary=$bnd");
348 }
349  
350 478 ▶sub _request_ok {
Subroutine _request_ok: called CC=7 SCAR=21.6
351 478 my ($self, $method, $path, %o) = @_;
352 478 local $Test::Builder::Level = $Test::Builder::Level + 2;
353 478 $path = $self->_resolve_path($path);
354 475 my $env = $self->_build_env($method, $path, %o);
355 469 my $r = eval { $self->{app}->($env) };
356 469 my $err = $@;
357 469 ▶ if (ref $r eq 'CODE') { # a psgi.streaming response
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "CODE") 2 467 469
358 2 $r = eval { $self->_drive_streaming($r) };
359 2 ▶ $err ||= $@;
Condition33%1/3
$err ||= $@
l!l&&r!l&&!r
002
MC/DC0%0/2
$err ||= $@
$err $@
Truth table33%1/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
360 }
361 469 ▶ if (ref $r eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq 'ARRAY') { } 469 0 469
362 469 $self->_set_response($r);
363 469 ▶ $TB->ok(1, $o{name} // "$method $path");
Condition100%2/2
$o{"name"} // "$method $path"
l!l
60409
MC/DC100%1/1
$o{"name"} // "$method $path"
$o{"name"}
Truth table100%2/2
A
A= $o{"name"}
Aresult
11
00
364 }
365 else {
366 0 $self->{res} = undef;
367 0 ▶ $TB->ok(0, $o{name} // "$method $path");
Condition0%0/2
$o{"name"} // "$method $path"
l!l
00
MC/DC0%0/1
$o{"name"} // "$method $path"
$o{"name"}
Truth table0%0/2
A
A= $o{"name"}
Aresult
11
00
368 0 ▶ $TB->diag($err ? "the application died: $err"
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
369 : 'the application did not return a PSGI response');
370 }
371 469 return $self;
372 }
373  
374 # Drive a delayed-response coderef to completion, collecting the writes.
375 3 ▶sub _drive_streaming {
Subroutine _drive_streaming: called CC=2 SCAR=7.5
376 3 my ($self, $code) = @_;
377 3 my ($status, $headers, @writes, $closed);
378 3 my $max = $self->{max_bytes};
379 3 my $got = 0;
380 10 ▶ my $writer = Punk::Test::_Writer->new(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
381 10 $got += length $_[0];
382 10 ▶ die "Punk::Test: stream exceeded $max bytes without closing\n"
Branch: 1 branch
BranchTrueFalseTotal
if $got > $max 0 10 10
383 if $got > $max;
384 10 push @writes, $_[0];
385 3 }, \$closed);
386 3 ▶ my $ret = $code->(sub {
Subroutine __ANON__: called CC=2 SCAR=9.9
387 3 my ($triplet) = @_;
388 3 ($status, $headers) = @$triplet[0, 1];
389 3 ▶ return $writer if @$triplet < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @$triplet < 3 3 0 3
390 0 push @writes, @{ $triplet->[2] };
391 0 $closed = 1;
392 0 return;
393 3 });
394 3 ▶ return [ $status, $headers // [], [ join '', @writes ] ];
Condition50%1/2
$headers // []
l!l
30
MC/DC0%0/1
$headers // []
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
395 }
396  
397 470 ▶sub _set_response {
Subroutine _set_response: called CC=12 SCAR=24.9
398 470 my ($self, $r) = @_;
399 470 my $body = $r->[2];
400 470 ▶ if (ref $body eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 445 25 470
elsif (builtin::blessed($body) and $body->can('getline')) { } 8 17 25
401 445 $body = join '', grep { defined } @$body;
402 }
403 ▶ elsif (Scalar::Util::blessed($body) && $body->can('getline')) {
Condition66%2/3
builtin::blessed($body) and $body->can("getline")
!ll&&!rl&&r
1708
MC/DC50%1/2
builtin::blessed($body) and $body->can("getline")
builtin::blessed($body) $body->can("getline")
Truth table66%2/3
A and B
A= builtin::blessed($body)B= $body->can("getline")
ABresult
0X0
100
111
404 8 my @chunks; # a PSGI body object (send_file)
405 8 ▶ while (defined(my $chunk = $body->getline)) { push @chunks, $chunk }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $body->getline)) 9 8 17
406 8 eval { $body->close };
407 8 $body = join '', @chunks;
408 }
409 else { # a filehandle (Punk::Static)
410 17 local $/;
411 17 my $fh = $body;
412 17 ▶ $body = <$fh> // '';
Condition50%1/2
readline $fh // ""
l!l
170
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
413 17 eval { $fh->close };
414 }
415 470 $self->{res} = { status => $r->[0], headers => $r->[1], body => $body };
416 470 delete $self->{$_} for qw(json xml);
417 470 my @h = @{ $r->[1] };
418 470 ▶ while (my ($k, $v) = splice @h, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@h, 0, 2)) 1201 470 1671
419 1201 ▶ next unless lc $k eq 'set-cookie';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $k eq "set-cookie" 1130 71 1201
420 71 ▶ my ($name, $value) = $v =~ /\A\s*([^=;\s]+)=([^;]*)/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $v =~ /\A\s*([^=;\s]+)=([^;]*)/ or next 71 0 71
421 71 ▶ if ($value eq '' || $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '' or $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i) { } 3 68 71
Condition66%2/3
$value eq "" or $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i
l!l&&r!l&&!r
3068
MC/DC50%1/2
$value eq "" or $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i
$value eq "" $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i
Truth table66%2/3
A or B
A= $value eq ""B= $v =~ /;\s*Max-Age\s*=\s*0\s*(?:;|\z)/i
ABresult
1X1
011
000
422 3 delete $self->{jar}{$name};
423 }
424 else {
425 68 $self->{jar}{$name} = $value;
426 }
427 }
428 470 return;
429 }
430  
431 476 ▶sub _jar_header {
Subroutine _jar_header: called CC=2 SCAR=6.9
432 476 my ($self) = @_;
433 476 my $jar = $self->{jar};
434 476 ▶ return unless %$jar;
Branch: 1 branch
BranchTrueFalseTotal
unless %$jar 378 98 476
435 98 return join '; ', map { "$_=$jar->{$_}" } sort keys %$jar;
436 }
437  
438 # ---- the response ------------------------------------------------------------
439  
440 5 ▶sub status { $_[0]{res} ? $_[0]{res}{status} : undef }
Subroutine status: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'res'} ? : 5 0 5
441 26 ▶sub body { $_[0]{res} ? $_[0]{res}{body} : undef }
Subroutine body: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'res'} ? : 26 0 26
442  
443 118 ▶sub header {
Subroutine header: called CC=4 SCAR=13.9
Pod documented
444 118 my ($self, $name) = @_;
445 118 ▶ return unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 118 118
446 118 my @h = @{ $self->{res}{headers} };
447 118 ▶ while (my ($k, $v) = splice @h, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@h, 0, 2)) 300 17 317
448 300 ▶ return $v if lc $k eq lc $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $k eq lc $name 101 199 300
449 }
450 17 return undef;
451 }
452  
453 155 ▶sub json {
Subroutine json: called CC=3 SCAR=11.3
Pod documented
454 155 my ($self) = @_;
455 155 ▶ return unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 155 155
456 155 ▶ return $self->{json} //= eval { File::Raw::JSON::file_json_decode($self->{res}{body}) };
Condition66%2/3
$self->{"json"} //= eval { do { File::Raw::JSON::file_json_decode($self->{"res"}{"body"}) } }
l!l&&r!l&&!r
511040
MC/DC0%0/2
$self->{"json"} //= eval { do { File::Raw::JSON::file_json_decode($self->{"res"}{"body"}) } }
$self->{"json"} eval { do { File::Raw::JSON::file_json_decode($self->{"res"}{"body"}) } }
Truth table66%2/3
A //= B
A= $self->{"json"}B= eval { do { File::Raw::JSON::file_json_decode($self->{"res"}{"body"}) } }
ABresult
1X1
011
000
457 }
458  
459 8 ▶sub xml {
Subroutine xml: called CC=3 SCAR=11.0
Pod documented
460 8 my ($self) = @_;
461 8 ▶ return unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 8 8
462 8 ▶ return $self->{xml} //= eval {
Condition100%3/3
$self->{"xml"} //= eval { do { File::Raw::XML::file_xml_decode($self->{"res"}{"body"}) } }
l!l&&r!l&&!r
341
MC/DC100%2/2
$self->{"xml"} //= eval { do { File::Raw::XML::file_xml_decode($self->{"res"}{"body"}) } }
$self->{"xml"} eval { do { File::Raw::XML::file_xml_decode($self->{"res"}{"body"}) } }
Truth table100%3/3
A //= B
A= $self->{"xml"}B= eval { do { File::Raw::XML::file_xml_decode($self->{"res"}{"body"}) } }
ABresult
1X1
011
000
463 5 File::Raw::XML::file_xml_decode($self->{res}{body});
464 };
465 }
466  
467 5 ▶sub cookie { $_[0]{jar}{ $_[1] } }
Subroutine cookie: called CC=1 SCAR=0.0
Pod documented
468  
469 6 ▶sub csrf_token { $_[0]{jar}{ $_[0]{csrf_cookie} } }
Subroutine csrf_token: called CC=1 SCAR=0.0
Pod documented
470  
471 1 ▶sub reset_session { %{ $_[0]{jar} } = (); $_[0] }
Subroutine reset_session: called CC=1 SCAR=0.0
Pod documented
472  
473 # ---- assertions --------------------------------------------------------------
474  
475 0 ▶sub _no_response {
Subroutine _no_response: not called CC=1 SCAR=6.9
476 0 my ($self, $name) = @_;
477 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
478 0 $TB->ok(0, $name);
479 0 $TB->diag('no response - did the last *_ok request fail?');
480 0 return $self;
481 }
482  
483 311 ▶sub status_is {
Subroutine status_is: called CC=4 SCAR=14.0
Pod documented
484 311 my ($self, $want, $name) = @_;
485 311 local $Test::Builder::Level = $Test::Builder::Level + 1;
486 311 ▶ $name //= "status is $want";
Condition100%2/2
$name //= "status is $want"
l!l
139172
MC/DC100%1/1
$name //= "status is $want"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
487 311 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 311 311
488 311 ▶ $TB->is_num($self->{res}{status}, $want, $name)
Branch: 1 branch
BranchTrueFalseTotal
$TB->is_num($self->{"res"}{"status"}, $want, $name) or $TB->diag('body: ' . $self->_body_head) 311 0 311
489 or $TB->diag('body: ' . $self->_body_head);
490 311 return $self;
491 }
492  
493 10 ▶sub status_isnt {
Subroutine status_isnt: called CC=3 SCAR=11.0
Pod documented
494 10 my ($self, $want, $name) = @_;
495 10 local $Test::Builder::Level = $Test::Builder::Level + 1;
496 10 ▶ $name //= "status is not $want";
Condition100%2/2
$name //= "status is not $want"
l!l
91
MC/DC100%1/1
$name //= "status is not $want"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
497 10 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 10 10
498 10 $TB->isnt_num($self->{res}{status}, $want, $name);
499 10 return $self;
500 }
501  
502 72 ▶sub header_is {
Subroutine header_is: called CC=4 SCAR=13.9
Pod documented
503 72 my ($self, $h, $want, $name) = @_;
504 72 local $Test::Builder::Level = $Test::Builder::Level + 1;
505 72 ▶ $name //= "$h header is " . (defined $want ? "'$want'" : 'undef');
Branch: 1 branch
BranchTrueFalseTotal
defined $want ? : 56 5 61
Condition100%2/2
$name //= "$h header is " . (defined $want ? "'${want}'" : "undef")
l!l
1161
MC/DC100%1/1
$name //= "$h header is " . (defined $want ? "'${want}'" : "undef")
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
506 72 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 72 72
507 72 $TB->is_eq($self->header($h), $want, $name);
508 72 return $self;
509 }
510  
511 10 ▶sub header_like {
Subroutine header_like: called CC=4 SCAR=14.0
Pod documented
512 10 my ($self, $h, $re, $name) = @_;
513 10 local $Test::Builder::Level = $Test::Builder::Level + 1;
514 10 ▶ $name //= "$h header matches";
Condition100%2/2
$name //= "$h header matches"
l!l
28
MC/DC100%1/1
$name //= "$h header matches"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
515 10 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 10 10
516 10 ▶ $TB->like($self->header($h) // '', $re, $name);
Condition50%1/2
$self->header($h) // ""
l!l
100
MC/DC0%0/1
$self->header($h) // ""
$self->header($h)
Truth table50%1/2
A
A= $self->header($h)
Aresult
11
00
517 10 return $self;
518 }
519  
520 1 ▶sub header_exists {
Subroutine header_exists: called CC=3 SCAR=11.2
Pod documented
521 1 my ($self, $h, $name) = @_;
522 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
523 1 ▶ $name //= "$h header exists";
Condition50%1/2
$name //= "$h header exists"
l!l
01
MC/DC0%0/1
$name //= "$h header exists"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
524 1 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 1 1
525 1 $TB->ok(defined $self->header($h), $name);
526 1 return $self;
527 }
528  
529 76 ▶sub content_is {
Subroutine content_is: called CC=3 SCAR=11.0
Pod documented
530 76 my ($self, $want, $name) = @_;
531 76 local $Test::Builder::Level = $Test::Builder::Level + 1;
532 76 ▶ $name //= 'exact body';
Condition100%2/2
$name //= "exact body"
l!l
4630
MC/DC100%1/1
$name //= "exact body"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
533 76 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 76 76
534 76 $TB->is_eq($self->{res}{body}, $want, $name);
535 76 return $self;
536 }
537  
538 31 ▶sub content_like {
Subroutine content_like: called CC=3 SCAR=11.0
Pod documented
539 31 my ($self, $re, $name) = @_;
540 31 local $Test::Builder::Level = $Test::Builder::Level + 1;
541 31 ▶ $name //= 'body matches';
Condition100%2/2
$name //= "body matches"
l!l
2110
MC/DC100%1/1
$name //= "body matches"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
542 31 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 31 31
543 31 $TB->like($self->{res}{body}, $re, $name);
544 31 return $self;
545 }
546  
547 8 ▶sub content_unlike {
Subroutine content_unlike: called CC=3 SCAR=11.0
Pod documented
548 8 my ($self, $re, $name) = @_;
549 8 local $Test::Builder::Level = $Test::Builder::Level + 1;
550 8 ▶ $name //= 'body does not match';
Condition100%2/2
$name //= "body does not match"
l!l
62
MC/DC100%1/1
$name //= "body does not match"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
551 8 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 8 8
552 8 $TB->unlike($self->{res}{body}, $re, $name);
553 8 return $self;
554 }
555  
556 # RFC 6901: '' is the whole document, '/a/0/b' walks in. Returns
557 # (found, value).
558 137 ▶sub _ptr_get {
Subroutine _ptr_get: called CC=10 SCAR=24.6
559 137 my ($doc, $ptr) = @_;
560 137 ▶ return (1, $doc) if !defined $ptr || $ptr eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ptr or $ptr eq "" 2 135 137
Condition66%2/3
not defined $ptr or $ptr eq ""
l!l&&r!l&&!r
02135
MC/DC50%1/2
not defined $ptr or $ptr eq ""
not defined $ptr $ptr eq ""
Truth table66%2/3
not(A) or B
A= not defined $ptrB= $ptr eq ""
ABresult
1X1
011
000
561 135 ▶ return (0, undef) unless $ptr =~ s{\A/}{};
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr =~ s[\A/][] 0 135 135
562 135 for my $tok (split m{/}, $ptr, -1) {
563 208 $tok =~ s/~1/\//g;
564 208 $tok =~ s/~0/~/g;
565 208 ▶ if (ref $doc eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $doc eq 'HASH') { } 178 30 208
elsif (ref $doc eq 'ARRAY') { } 30 0 30
566 178 ▶ return (0, undef) unless exists $doc->{$tok};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $doc->{$tok} 0 178 178
567 178 $doc = $doc->{$tok};
568 }
569 elsif (ref $doc eq 'ARRAY') {
570 30 ▶ return (0, undef) unless $tok =~ /\A\d+\z/ && $tok <= $#$doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $tok =~ /\A\d+\z/ and $tok <= $doc->$#* 0 30 30
Condition33%1/3
$tok =~ /\A\d+\z/ and $tok <= $doc->$#*
!ll&&!rl&&r
0030
MC/DC0%0/2
$tok =~ /\A\d+\z/ and $tok <= $doc->$#*
$tok =~ /\A\d+\z/ $tok <= $doc->$#*
Truth table33%1/3
A and B
A= $tok =~ /\A\d+\z/B= $tok <= $doc->$#*
ABresult
0X0
100
111
571 30 $doc = $doc->[$tok];
572 }
573 0 else { return (0, undef) }
574 }
575 135 return (1, $doc);
576 }
577  
578 135 ▶sub _json_at {
Subroutine _json_at: called CC=3 SCAR=13.7
579 135 my ($self, $ptr, $name) = @_;
580 135 local $Test::Builder::Level = $Test::Builder::Level + 1;
581 135 my $doc = $self->json;
582 135 ▶ unless (defined $doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc) 0 135 135
583 0 $TB->ok(0, $name);
584 0 $TB->diag('the body did not decode as JSON: ' . $self->_body_head);
585 0 return;
586 }
587 135 my ($found, $got) = _ptr_get($doc, $ptr);
588 135 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 135 135
589 0 $TB->ok(0, $name);
590 0 $TB->diag("nothing at JSON pointer '$ptr' in " . $self->_body_head);
591 0 return;
592 }
593 135 return \$got;
594 }
595  
596 102 ▶sub json_is {
Subroutine json_is: called CC=6 SCAR=18.1
Pod documented
597 102 my ($self, $ptr, $want, $name) = @_;
598 102 local $Test::Builder::Level = $Test::Builder::Level + 1;
599 102 ▶ $name //= "json at '$ptr'";
Condition100%2/2
$name //= "json at '${ptr}'"
l!l
6834
MC/DC100%1/1
$name //= "json at '${ptr}'"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
600 102 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 102 102
601 102 ▶ my $slot = $self->_json_at($ptr, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $slot = $self->_json_at($ptr, $name) or return $self 102 0 102
602 102 my $got = $$slot;
603 102 ▶ if (ref $got || ref $want) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got or ref $want) { } 7 95 102
Condition66%2/3
ref $got or ref $want
l!l&&r!l&&!r
7095
MC/DC50%1/2
ref $got or ref $want
ref $got ref $want
Truth table66%2/3
A or B
A= ref $gotB= ref $want
ABresult
1X1
011
000
604 # deep-compare through canonical JSON, wrapped so scalars encode
605 7 $TB->is_eq(File::Raw::JSON::file_json_encode([$got], sort_keys => 1),
606 File::Raw::JSON::file_json_encode([$want], sort_keys => 1), $name);
607 }
608 else {
609 95 $TB->is_eq($got, $want, $name);
610 }
611 102 return $self;
612 }
613  
614 6 ▶sub json_has {
Subroutine json_has: called CC=4 SCAR=14.0
Pod documented
615 6 my ($self, $ptr, $name) = @_;
616 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
617 6 ▶ $name //= "json has '$ptr'";
Condition100%2/2
$name //= "json has '${ptr}'"
l!l
24
MC/DC100%1/1
$name //= "json has '${ptr}'"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
618 6 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 6 6
619 6 ▶ my $slot = $self->_json_at($ptr, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $slot = $self->_json_at($ptr, $name) or return $self 6 0 6
620 6 $TB->ok(1, $name);
621 6 return $self;
622 }
623  
624 27 ▶sub json_like {
Subroutine json_like: called CC=6 SCAR=19.1
Pod documented
625 27 my ($self, $ptr, $re, $name) = @_;
626 27 local $Test::Builder::Level = $Test::Builder::Level + 1;
627 27 ▶ $name //= "json at '$ptr' matches";
Condition100%2/2
$name //= "json at '${ptr}' matches"
l!l
1512
MC/DC100%1/1
$name //= "json at '${ptr}' matches"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
628 27 ▶ return $self->_no_response($name) unless $self->{res};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"res"} 0 27 27
629 27 ▶ my $slot = $self->_json_at($ptr, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $slot = $self->_json_at($ptr, $name) or return $self 27 0 27
630 27 ▶ $TB->like(defined $$slot && !ref $$slot ? $$slot : '', $re, $name);
Branch: 1 branch
BranchTrueFalseTotal
defined $$slot && !ref($$slot) ? : 27 0 27
Condition33%1/3
defined $$slot and not ref $$slot
!ll&&!rl&&r
0027
MC/DC0%0/2
defined $$slot and not ref $$slot
defined $$slot not ref $$slot
Truth table33%1/3
A and not(B)
A= defined $$slotB= not ref $$slot
ABresult
0X0
100
111
631 27 return $self;
632 }
633  
634 0 ▶sub _body_head {
Subroutine _body_head: not called CC=4 SCAR=30.0
635 0 my ($self) = @_;
636 0 ▶ my $b = $self->{res} ? $self->{res}{body} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'res'} ? : 0 0 0
637 0 ▶ $b = substr($b, 0, 500) . '...' if length $b > 500;
Branch: 1 branch
BranchTrueFalseTotal
if length $b > 500 0 0 0
638 0 ▶ return length $b ? $b : '(empty body)';
Branch: 1 branch
BranchTrueFalseTotal
length $b ? : 0 0 0
639 }
640  
641 # ---- server-sent events ------------------------------------------------------
642  
643 1 ▶sub sse_ok {
Subroutine sse_ok: called CC=11 SCAR=29.3
Pod documented
644 1 my ($self, $path, %o) = @_;
645 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
646 1 $path = $self->_resolve_path($path);
647 1 ▶ my $name = $o{name} // "SSE $path";
Condition50%1/2
$o{"name"} // "SSE $path"
l!l
01
MC/DC0%0/1
$o{"name"} // "SSE $path"
$o{"name"}
Truth table50%1/2
A
A= $o{"name"}
Aresult
11
00
648 1 ▶ $o{headers}{Accept} //= 'text/event-stream';
Condition50%1/2
$o{"headers"}{"Accept"} //= "text/event-stream"
l!l
01
MC/DC0%0/1
$o{"headers"}{"Accept"} //= "text/event-stream"
$o{"headers"}{"Accept"}
Truth table50%1/2
A
A= $o{"headers"}{"Accept"}
Aresult
11
00
649 1 my $env = $self->_build_env(GET => $path, %o);
650 1 $env->{'psgi.streaming'} = 1;
651 1 delete $self->{sse};
652 1 my $r = eval { $self->{app}->($env) };
653 1 my $err = $@;
654 1 ▶ if (ref $r eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "CODE") 1 0 1
655 1 $r = eval { $self->_drive_streaming($r) };
656 1 ▶ $err ||= $@;
Condition33%1/3
$err ||= $@
l!l&&r!l&&!r
001
MC/DC0%0/2
$err ||= $@
$err $@
Truth table33%1/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
657 }
658 1 ▶ unless (ref $r eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $r eq "ARRAY") 0 1 1
659 0 $self->{res} = undef;
660 0 $TB->ok(0, $name);
661 0 ▶ $TB->diag($err ? "the application died: $err"
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
662 : 'no response from the sse route');
663 0 return $self;
664 }
665 1 $self->_set_response($r);
666 1 ▶ my $ct = $self->header('Content-Type') // '';
Condition50%1/2
$self->header("Content-Type") // ""
l!l
10
MC/DC0%0/1
$self->header("Content-Type") // ""
$self->header("Content-Type")
Truth table50%1/2
A
A= $self->header("Content-Type")
Aresult
11
00
667 1 ▶ if ($r->[0] == 200 && $ct =~ m{\Atext/event-stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[0] == 200 and $ct =~ m[\Atext/event-stream]) { } 1 0 1
Condition33%1/3
$r->[0] == 200 and $ct =~ m[\Atext/event-stream]
!ll&&!rl&&r
001
MC/DC0%0/2
$r->[0] == 200 and $ct =~ m[\Atext/event-stream]
$r->[0] == 200 $ct =~ m[\Atext/event-stream]
Truth table33%1/3
A and B
A= $r->[0] == 200B= $ct =~ m[\Atext/event-stream]
ABresult
0X0
100
111
668 1 $self->{sse} = _parse_sse($self->{res}{body});
669 1 $TB->ok(1, $name);
670 }
671 else {
672 0 $TB->ok(0, $name);
673 0 ▶ $TB->diag("expected a 200 text/event-stream, got $r->[0] "
Condition0%0/2
$ct || '(no content type)'
l!l
00
MC/DC0%0/1
$ct || '(no content type)'
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
674 . ($ct || '(no content type)') . ': ' . $self->_body_head);
675 }
676 1 return $self;
677 }
678  
679 # The spec's dispatch loop: fields accumulate, a blank line dispatches.
680 1 ▶sub _parse_sse {
Subroutine _parse_sse: called CC=11 SCAR=24.2
681 1 my ($bytes) = @_;
682 1 my (@events, @comments, %cur);
683 1 for my $line (split /\n/, $bytes, -1) {
684 13 $line =~ s/\r\z//;
685 13 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 5 8 13
686 5 ▶ push @events, {%cur} if %cur;
Branch: 1 branch
BranchTrueFalseTotal
if %cur 4 1 5
687 5 %cur = ();
688 5 next;
689 }
690 8 ▶ if ($line =~ /\A:\s?(.*)\z/s) { push @comments, $1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A:\s?(.*)\z/s) 1 7 8
691 7 my ($field, $value) = $line =~ /\A([^:]+)(?::\s?(.*))?\z/s;
692 7 ▶ $value //= '';
Condition50%1/2
$value //= ""
l!l
70
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
693 7 ▶ if ($field eq 'data') { $cur{data} = defined $cur{data}
Branch: 4 branches
BranchTrueFalseTotal
if ($field eq 'data') { } 5 2 7
elsif ($field eq 'event') { } 1 1 2
elsif ($field eq 'id') { } 1 0 1
elsif ($field eq 'retry') { } 0 0 0
694 5 ▶ ? "$cur{data}\n$value" : $value }
Branch: 1 branch
BranchTrueFalseTotal
defined $cur{'data'} ? : 1 4 5
695 1 elsif ($field eq 'event') { $cur{event} = $value }
696 1 elsif ($field eq 'id') { $cur{id} = $value }
697 0 elsif ($field eq 'retry') { $cur{retry} = $value }
698 }
699 1 return { events => \@events, comments => \@comments };
700 }
701  
702 2 ▶sub sse_events { $_[0]{sse} ? $_[0]{sse}{events} : undef }
Subroutine sse_events: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'sse'} ? : 2 0 2
703 1 ▶sub sse_comments { $_[0]{sse} ? $_[0]{sse}{comments} : undef }
Subroutine sse_comments: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'sse'} ? : 1 0 1
704  
705 5 ▶sub _sse_event {
Subroutine _sse_event: called CC=3 SCAR=14.7
706 5 my ($self, $i, $name) = @_;
707 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
708 5 ▶ unless ($self->{sse}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sse"}) 0 5 5
709 0 $TB->ok(0, $name);
710 0 $TB->diag('no SSE stream - did sse_ok pass?');
711 0 return;
712 }
713 5 my $ev = $self->{sse}{events}[$i];
714 5 ▶ unless ($ev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ev) 0 5 5
715 0 $TB->ok(0, $name);
716 $TB->diag("no event $i - the stream held "
717 0 . scalar(@{ $self->{sse}{events} }) . ' event(s)');
718 0 return;
719 }
720 5 return $ev;
721 }
722  
723 1 ▶sub sse_event_is {
Subroutine sse_event_is: called CC=7 SCAR=23.8
Pod documented
724 1 my ($self, $i, $event, $data, $name) = @_;
725 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
726 1 ▶ $name //= "event $i is $event";
Condition50%1/2
$name //= "event $i is $event"
l!l
01
MC/DC0%0/1
$name //= "event $i is $event"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
727 1 ▶ my $ev = $self->_sse_event($i, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $ev = $self->_sse_event($i, $name) or return $self 1 0 1
728 1 ▶ if (($ev->{event} // '') eq $event) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ev->{'event'} // '') eq $event) { } 1 0 1
Condition50%1/2
$ev->{"event"} // ""
l!l
10
MC/DC0%0/1
$ev->{"event"} // ""
$ev->{"event"}
Truth table50%1/2
A
A= $ev->{"event"}
Aresult
11
00
729 1 ▶ $TB->is_eq($ev->{data} // '', $data, $name);
Condition50%1/2
$ev->{"data"} // ""
l!l
10
MC/DC0%0/1
$ev->{"data"} // ""
$ev->{"data"}
Truth table50%1/2
A
A= $ev->{"data"}
Aresult
11
00
730 }
731 else {
732 0 $TB->ok(0, $name);
733 0 ▶ $TB->diag("event $i is named '" . ($ev->{event} // '')
Condition0%0/2
$ev->{"event"} // ""
l!l
00
MC/DC0%0/1
$ev->{"event"} // ""
$ev->{"event"}
Truth table0%0/2
A
A= $ev->{"event"}
Aresult
11
00
734 . "', not '$event'");
735 }
736 1 return $self;
737 }
738  
739 2 ▶sub sse_data_is {
Subroutine sse_data_is: called CC=4 SCAR=14.0
Pod documented
740 2 my ($self, $i, $data, $name) = @_;
741 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
742 2 ▶ $name //= "event $i data";
Condition100%2/2
$name //= "event $i data"
l!l
11
MC/DC100%1/1
$name //= "event $i data"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
743 2 ▶ my $ev = $self->_sse_event($i, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $ev = $self->_sse_event($i, $name) or return $self 2 0 2
744 2 ▶ $TB->is_eq($ev->{data} // '', $data, $name);
Condition50%1/2
$ev->{"data"} // ""
l!l
20
MC/DC0%0/1
$ev->{"data"} // ""
$ev->{"data"}
Truth table50%1/2
A
A= $ev->{"data"}
Aresult
11
00
745 2 return $self;
746 }
747  
748 2 ▶sub sse_json_is {
Subroutine sse_json_is: called CC=9 SCAR=28.6
Pod documented
749 2 my ($self, $i, $ptr, $want, $name) = @_;
750 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
751 2 ▶ $name //= "event $i json at '$ptr'";
Condition50%1/2
$name //= "event $i json at '${ptr}'"
l!l
02
MC/DC0%0/1
$name //= "event $i json at '${ptr}'"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
752 2 ▶ my $ev = $self->_sse_event($i, $name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $ev = $self->_sse_event($i, $name) or return $self 2 0 2
753 2 ▶ my $doc = eval { File::Raw::JSON::file_json_decode($ev->{data} // '') };
Condition50%1/2
$ev->{"data"} // ""
l!l
20
MC/DC0%0/1
$ev->{"data"} // ""
$ev->{"data"}
Truth table50%1/2
A
A= $ev->{"data"}
Aresult
11
00
754 2 ▶ unless (defined $doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc) 0 2 2
755 0 $TB->ok(0, $name);
756 0 ▶ $TB->diag("event $i data is not JSON: " . ($ev->{data} // ''));
Condition0%0/2
$ev->{"data"} // ""
l!l
00
MC/DC0%0/1
$ev->{"data"} // ""
$ev->{"data"}
Truth table0%0/2
A
A= $ev->{"data"}
Aresult
11
00
757 0 return $self;
758 }
759 2 my ($found, $got) = _ptr_get($doc, $ptr);
760 2 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 2 2
761 0 $TB->ok(0, $name);
762 0 $TB->diag("nothing at JSON pointer '$ptr' in event $i");
763 0 return $self;
764 }
765 2 ▶ if (ref $got || ref $want) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got or ref $want) { } 0 2 2
Condition33%1/3
ref $got or ref $want
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $got or ref $want
ref $got ref $want
Truth table33%1/3
A or B
A= ref $gotB= ref $want
ABresult
1X1
011
000
766 0 $TB->is_eq(File::Raw::JSON::file_json_encode([$got], sort_keys => 1),
767 File::Raw::JSON::file_json_encode([$want], sort_keys => 1), $name);
768 }
769 else {
770 2 $TB->is_eq($got, $want, $name);
771 }
772 2 return $self;
773 }
774  
775 # ---- websockets --------------------------------------------------------------
776  
777 # Whether the live (forked Hyperman) transport can run here at all - tests
778 # use this to SKIP, the same way t/1011-ws-live.t does.
779 2 ▶sub ws_live_available {
Subroutine ws_live_available: called CC=2 SCAR=7.0
Pod documented
780 2 ▶ return eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Hyperman; require Punk::WebSocket; Punk::WebSocket::_hm_available() } } ? : 2 0 2
781 2 require Hyperman;
782 2 require Punk::WebSocket;
783 2 Punk::WebSocket::_hm_available();
784 } ? 1 : 0;
785 }
786  
787 5 ▶sub websocket_ok {
Subroutine websocket_ok: called CC=14 SCAR=31.6
Pod documented
788 5 my ($self, $path, %o) = @_;
789 5 local $Test::Builder::Level = $Test::Builder::Level + 1;
790 5 $path = $self->_resolve_path($path);
791 5 ▶ my $name = $o{name} // "websocket $path";
Condition50%1/2
$o{"name"} // "websocket $path"
l!l
05
MC/DC0%0/1
$o{"name"} // "websocket $path"
$o{"name"}
Truth table50%1/2
A
A= $o{"name"}
Aresult
11
00
792 5 $self->_ws_teardown;
793  
794 5 ▶ if (!$o{live}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o{"live"}) 5 0 5
795 5 my ($conn, $hdr, $key, $status) = $self->_ws_inprocess($path, %o);
796 5 ▶ if ($status == 101) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 101) 3 2 5
797 3 my ($acc) = $hdr =~ /Sec-WebSocket-Accept: (\S+)/i;
798 3 $self->{ws} = $conn;
799 3 $self->{ws_hdr} = $hdr;
800 3 ▶ $TB->is_eq($acc // '', Punk::Test::WS::accept_key($key), $name);
Condition50%1/2
$acc // ""
l!l
30
MC/DC0%0/1
$acc // ""
$acc
Truth table50%1/2
A
A= $acc
Aresult
11
00
801 3 return $self;
802 }
803 2 ▶ $conn->close if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 2 0 2
804 2 ▶ if ($status != 501) { # 501 = this transport cannot upgrade; go live
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 501) 1 1 2
805 1 $TB->ok(0, $name);
806 1 ▶ $TB->diag($status
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 1 0 1
807 ? "the upgrade was answered with a $status:\n$hdr"
808 : 'no bytes came back from the upgrade request');
809 1 return $self;
810 }
811 }
812  
813 1 ▶ unless (ws_live_available()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ws_live_available()) 0 1 1
814 0 $TB->ok(0, $name);
815 0 $TB->diag('this route needs the live transport (Hyperman 0.11+); '
816 . 'guard the test with Punk::Test::ws_live_available');
817 0 return $self;
818 }
819 1 my ($conn, $hdr, $key) = $self->_ws_live($path, %o);
820 1 ▶ unless ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 0 1 1
821 0 $TB->ok(0, $name);
822 0 ▶ $TB->diag($hdr // 'could not connect to the live worker');
Condition0%0/2
$hdr // "could not connect to the live worker"
l!l
00
MC/DC0%0/1
$hdr // "could not connect to the live worker"
$hdr
Truth table0%0/2
A
A= $hdr
Aresult
11
00
823 0 return $self;
824 }
825 1 ▶ if ($hdr =~ m{\AHTTP/1\.1 101 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ m[\AHTTP/1\.1 101 ]) { } 1 0 1
826 1 my ($acc) = $hdr =~ /Sec-WebSocket-Accept: (\S+)/i;
827 1 $self->{ws} = $conn;
828 1 $self->{ws_hdr} = $hdr;
829 1 ▶ $TB->is_eq($acc // '', Punk::Test::WS::accept_key($key), $name);
Condition50%1/2
$acc // ""
l!l
10
MC/DC0%0/1
$acc // ""
$acc
Truth table50%1/2
A
A= $acc
Aresult
11
00
830 }
831 else {
832 0 $conn->close;
833 0 $TB->ok(0, $name);
834 0 my ($line) = split /\r\n/, $hdr;
835 0 ▶ $TB->diag('the upgrade was answered with: ' . ($line // '(nothing)'));
Condition0%0/2
$line // "(nothing)"
l!l
00
MC/DC0%0/1
$line // "(nothing)"
$line
Truth table0%0/2
A
A= $line
Aresult
11
00
836 }
837 1 return $self;
838 }
839  
840 # In-process transport: the app runs in a fork with one end of a socketpair
841 # as psgix.io - so a blocking => 1 route streams to us in real time while
842 # this side sends and asserts interactively. The child answers non-upgrade
843 # triplets (a guard's 403, the 501 of a route this transport cannot carry)
844 # as minimal HTTP so the parent can see why.
845 5 ▶sub _ws_inprocess {
Subroutine _ws_inprocess: called CC=13 SCAR=36.7
846 5 my ($self, $path, %o) = @_;
847 my ($env, $key) = Punk::Test::WS::upgrade_env(
848 5 path => $path, query => $o{query}, protocol => $o{protocol});
849 5 ▶ if (my $h = $o{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $o{"headers"}) 1 4 5
850 1 for my $k (keys %$h) {
851 1 my $ek = uc $k; $ek =~ tr/-/_/;
852 1 $env->{"HTTP_$ek"} = $h->{$k};
853 }
854 }
855 5 ▶ if (my $jar = $self->_jar_header) { $env->{HTTP_COOKIE} = $jar }
Branch: 1 branch
BranchTrueFalseTotal
if (my $jar = $self->_jar_header) 3 2 5
856  
857 5 ▶ socketpair(my $ours, my $theirs, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $ours, my $theirs, 1, 1, 0 or die "Punk::Test: socketpair: $!" 5 0 5
858 or die "Punk::Test: socketpair: $!";
859 5 ▶ my $pid = fork // die "Punk::Test: fork: $!";
Condition50%1/2
fork // die("Punk::Test: fork: $!")
l!l
50
MC/DC0%0/1
fork // die("Punk::Test: fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
860 5 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 5 5
861 # the child owns no test state: keep it off the TAP pipe entirely,
862 # and leave through _exit so no END blocks or destructors run
863 0 close $ours;
864 0 open STDIN, '<', '/dev/null';
865 0 open STDOUT, '>', '/dev/null';
866 0 open STDERR, '>', '/dev/null';
867 0 $env->{'psgix.io'} = $theirs;
868 0 my $r = eval { $self->{app}->($env) };
869 0 ▶ if (ref $r eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'ARRAY') { } 0 0 0
elsif (not defined $r) { } 0 0 0
870 # a successful blocking upgrade already spoke on the socket and
871 # returns the empty 200 sentinel; anything else (a guard's 403,
872 # the 501 of a route this transport cannot carry, a plain route
873 # answering) is relayed as minimal HTTP so the parent sees why
874 my $body = ref $r->[2] eq 'ARRAY'
875 0 ▶ ? join('', grep { defined } @{ $r->[2] }) : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $r->[2] eq 'ARRAY' ? : 0 0 0
876 0 ▶ syswrite $theirs, "HTTP/1.1 $r->[0] X\r\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $r->[0] == 200 and not length $body 0 0 0
Condition0%0/3
$r->[0] == 200 and not length $body
!ll&&!rl&&r
000
MC/DC0%0/2
$r->[0] == 200 and not length $body
$r->[0] == 200 not length $body
Truth table0%0/3
A and not(B)
A= $r->[0] == 200B= not length $body
ABresult
0X0
100
111
877 . "Content-Length: " . length($body) . "\r\n"
878 . "\r\n$body"
879 unless $r->[0] == 200 && !length $body;
880 }
881 elsif (!defined $r) {
882 0 syswrite $theirs, "HTTP/1.1 500 X\r\n\r\n";
883 }
884 0 POSIX::_exit(0);
885 }
886 5 close $theirs;
887 my $conn = Punk::Test::WS::Conn->new(
888 5 sock => $ours, timeout => $self->{timeout}, pid => $pid);
889 5 my $hdr = $conn->read_headers;
890 5 my ($status) = $hdr =~ m{\AHTTP/1\.1 (\d+)};
891 5 ▶ return ($conn, $hdr, $key, $status // 0);
Condition50%1/2
$status // 0
l!l
50
MC/DC0%0/1
$status // 0
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
892 }
893  
894 # Live transport: one forked Hyperman worker per client, started on first
895 # use, speaking real TCP - fully interactive, and the same machinery as
896 # t/1011-ws-live.t.
897 1 ▶sub _ws_live {
Subroutine _ws_live: called CC=5 SCAR=17.3
898 1 my ($self, $path, %o) = @_;
899 1 ▶ my $host = $self->_live_host or return (undef, 'live worker failed to start');
Branch: 1 branch
BranchTrueFalseTotal
my $host = $self->_live_host or return undef, "live worker failed to start" 1 0 1
900 1 require IO::Socket::INET;
901 1 ▶ my $sock = IO::Socket::INET->new(PeerAddr => $host)
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $host) or return undef, "connect ${host}: $!" 1 0 1
902 or return (undef, "connect $host: $!");
903 1 $sock->autoflush(1);
904 0 my @extra = map { my $k = $_; "$k: $o{headers}{$k}" }
905 1 ▶ keys %{ $o{headers} || {} };
Condition50%1/2
$o{'headers'} || {}
l!l
01
MC/DC0%0/1
$o{'headers'} || {}
$o{'headers'}
Truth table50%1/2
A
A= $o{'headers'}
Aresult
11
00
906 0 ▶ if (my $jar = $self->_jar_header) { push @extra, "Cookie: $jar" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $jar = $self->_jar_header) 0 1 1
907 my ($req, $key) = Punk::Test::WS::handshake_request(
908 host => $host, path => $path, protocol => $o{protocol},
909 1 extra => \@extra);
910 1 syswrite $sock, $req;
911 my $conn = Punk::Test::WS::Conn->new(
912 1 sock => $sock, timeout => $self->{timeout});
913 1 my $hdr = $conn->read_headers;
914 1 return ($conn, $hdr, $key);
915 }
916  
917 1 ▶sub _live_host {
Subroutine _live_host: called CC=6 SCAR=20.9
918 1 my ($self) = @_;
919 1 ▶ return $self->{live}{host} if $self->{live};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"live"} 0 1 1
920 1 require IO::Socket::INET;
921 1 my $port = 27000 + ($$ % 400);
922 1 ▶ my $pid = fork // die "Punk::Test: fork: $!";
Condition50%1/2
fork // die("Punk::Test: fork: $!")
l!l
10
MC/DC0%0/1
fork // die("Punk::Test: fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
923 1 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 1 1
924 0 open STDIN, '<', '/dev/null';
925 0 open STDOUT, '>', '/dev/null';
926 0 open STDERR, '>', '/dev/null';
927 0 eval {
928 0 Hyperman->run(app => $self->{app}, host => '127.0.0.1',
929 port => $port, workers => 1);
930 };
931 0 POSIX::_exit(0);
932 }
933 1 my $host = "127.0.0.1:$port";
934 1 for (1 .. 100) {
935 2 my $s = IO::Socket::INET->new(PeerAddr => $host);
936 2 ▶ if ($s) { close $s; $self->{live} = { pid => $pid, host => $host };
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 1 1 2
937 1 return $host }
938 1 select undef, undef, undef, 0.1;
939 }
940 0 kill 'KILL', $pid;
941 0 waitpid $pid, 0;
942 0 return;
943 }
944  
945 6 ▶sub send_ok {
Subroutine send_ok: called CC=5 SCAR=17.8
Pod documented
946 6 my ($self, $payload, %o) = @_;
947 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
948 6 ▶ my $name = $o{name} // 'send';
Condition50%1/2
$o{"name"} // "send"
l!l
06
MC/DC0%0/1
$o{"name"} // "send"
$o{"name"}
Truth table50%1/2
A
A= $o{"name"}
Aresult
11
00
949 6 ▶ unless ($self->{ws}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ws"}) 0 6 6
950 0 $TB->ok(0, $name);
951 0 $TB->diag('no websocket - did websocket_ok pass?');
952 0 return $self;
953 }
954 my $n = $self->{ws}->send_frame(
955 6 ▶ opcode => $o{binary} ? 2 : ($o{opcode} // 1),
Branch: 1 branch
BranchTrueFalseTotal
$o{'binary'} ? : 1 5 6
Condition50%1/2
$o{"opcode"} // 1
l!l
05
MC/DC0%0/1
$o{"opcode"} // 1
$o{"opcode"}
Truth table50%1/2
A
A= $o{"opcode"}
Aresult
11
00
956 payload => $payload);
957 6 $TB->ok(defined $n, $name);
958 6 return $self;
959 }
960  
961 # The next data frame's payload, reassembling fragments, skipping pongs.
962 6 ▶sub _next_message {
Subroutine _next_message: called CC=5 SCAR=16.8
963 6 my ($self) = @_;
964 6 my $payload = '';
965 6 while (1) {
966 6 ▶ my $f = $self->{ws}->read_frame or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $f = $self->{"ws"}->read_frame or return undef 6 0 6
967 6 ▶ next if $f->{opcode} == 10; # a pong
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"opcode"} == 10 0 6 6
968 6 ▶ return { close => 1, frame => $f } if $f->{opcode} == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"opcode"} == 8 0 6 6
969 6 $payload .= $f->{payload};
970 6 ▶ return { payload => $payload, frame => $f } if $f->{fin};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"fin"} 6 0 6
971 }
972 }
973  
974 6 ▶sub message_is {
Subroutine message_is: called CC=5 SCAR=19.5
Pod documented
975 6 my ($self, $want, $name) = @_;
976 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
977 6 ▶ $name //= 'message';
Condition100%2/2
$name //= "message"
l!l
51
MC/DC100%1/1
$name //= "message"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
978 6 ▶ unless ($self->{ws}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ws"}) 0 6 6
979 0 $TB->ok(0, $name);
980 0 $TB->diag('no websocket - did websocket_ok pass?');
981 0 return $self;
982 }
983 6 my $m = $self->_next_message;
984 0 ▶ if (!$m) { $TB->ok(0, $name); $TB->diag('no frame arrived (timeout or EOF)') }
Branch: 2 branches
BranchTrueFalseTotal
if (not $m) { } 0 6 6
elsif ($m->{'close'}) { } 0 6 6
985 0 elsif ($m->{close}) { $TB->ok(0, $name); $TB->diag('the server closed instead') }
986 6 else { $TB->is_eq($m->{payload}, $want, $name) }
987 6 return $self;
988 }
989  
990 0 ▶sub message_like {
Subroutine message_like: not called CC=5 SCAR=34.0
Pod documented
991 0 my ($self, $re, $name) = @_;
992 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
993 0 ▶ $name //= 'message matches';
Condition0%0/2
$name //= "message matches"
l!l
00
MC/DC0%0/1
$name //= "message matches"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
994 0 ▶ unless ($self->{ws}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ws"}) 0 0 0
995 0 $TB->ok(0, $name);
996 0 $TB->diag('no websocket - did websocket_ok pass?');
997 0 return $self;
998 }
999 0 my $m = $self->_next_message;
1000 0 ▶ if (!$m) { $TB->ok(0, $name); $TB->diag('no frame arrived (timeout or EOF)') }
Branch: 2 branches
BranchTrueFalseTotal
if (not $m) { } 0 0 0
elsif ($m->{'close'}) { } 0 0 0
1001 0 elsif ($m->{close}) { $TB->ok(0, $name); $TB->diag('the server closed instead') }
1002 0 else { $TB->like($m->{payload}, $re, $name) }
1003 0 return $self;
1004 }
1005  
1006 4 ▶sub finish_ok {
Subroutine finish_ok: called CC=6 SCAR=19.4
Pod documented
1007 4 my ($self, $name) = @_;
1008 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
1009 4 ▶ $name //= 'clean close';
Condition50%1/2
$name //= "clean close"
l!l
04
MC/DC0%0/1
$name //= "clean close"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
1010 4 ▶ unless ($self->{ws}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ws"}) 0 4 4
1011 0 $TB->ok(0, $name);
1012 0 $TB->diag('no websocket - did websocket_ok pass?');
1013 0 return $self;
1014 }
1015 4 $self->{ws}->send_frame(opcode => 8, payload => pack('n', 1000));
1016 4 my $echoed = 0;
1017 4 ▶ while (my $f = $self->{ws}->read_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->{"ws"}->read_frame) 4 0 4
1018 4 ▶ if ($f->{opcode} == 8) { $echoed = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"opcode"} == 8) 4 0 4
1019 }
1020 4 $TB->ok($echoed, $name);
1021 4 ▶ $TB->diag('the close was never echoed') unless $echoed;
Branch: 1 branch
BranchTrueFalseTotal
unless $echoed 0 4 4
1022 4 $self->_ws_teardown;
1023 4 return $self;
1024 }
1025  
1026 96 ▶sub _ws_teardown {
Subroutine _ws_teardown: called CC=2 SCAR=6.9
1027 96 my ($self) = @_;
1028 96 ▶ if (my $ws = delete $self->{ws}) { $ws->close }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ws = delete $self->{"ws"}) 4 92 96
1029 96 delete $self->{ws_hdr};
1030 96 return;
1031 }
1032  
1033 87 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
1034 87 my ($self) = @_;
1035 87 $self->_ws_teardown;
1036 87 ▶ if (my $live = delete $self->{live}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $live = delete $self->{"live"}) 1 86 87
1037 1 kill 'TERM', $live->{pid};
1038 1 waitpid $live->{pid}, 0;
1039 }
1040 87 return;
1041 }
1042  
1043 # psgi.input for a multipart request: a real PerlIO handle - the parser
1044 # reads it with PerlIO_read, which a tied handle never sees - whose FILL
1045 # drains the item list built by _multipart_input. @QUEUE hands the list
1046 # to PUSHED, because open() gives a via layer no arguments of ours.
1047 package
1048 Punk::Test::_MPInput;
1049  
1050 35 our @QUEUE;
1051 13 ▶sub PUSHED { bless { items => shift @QUEUE }, $_[0] }
Subroutine PUSHED: called CC=1 SCAR=0.0
1052 75 ▶sub FILL {
Subroutine FILL: called CC=5 SCAR=16.4
1053 75 my ($self) = @_;
1054 75 my $items = $self->{items};
1055 75 ▶ while (@$items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$items) 77 0 77
1056 77 my $it = $items->[0];
1057 77 ▶ unless (ref $it) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $it) 70 7 77
1058 70 shift @$items;
1059 70 ▶ return $it if length $it;
Branch: 1 branch
BranchTrueFalseTotal
if length $it 70 0 70
1060 0 next;
1061 }
1062 7 my $n = read $it->{fh}, my $chunk, 65536;
1063 7 ▶ return $chunk if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 5 2 7
1064 2 close $it->{fh};
1065 2 shift @$items;
1066 }
1067 0 return undef;
1068 }
1069  
1070 # The psgi.streaming writer handed to a delayed response.
1071 package
1072 Punk::Test::_Writer;
1073 3 ▶sub new { bless { w => $_[1], c => $_[2] }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
1074 10 ▶sub write { $_[0]{w}->($_[1]) }
Subroutine write: called CC=1 SCAR=0.0
1075 3 ▶sub close { ${ $_[0]{c} } = 1 }
Subroutine close: called CC=1 SCAR=0.0
1076  
1077 package Punk::Test;
1078  
1079 35 1;
1080  
1081 __END__
« blib/lib/Punk/DevError.pm ↑ Summary blib/lib/Punk/App.pm »