blib/lib/Test2/Thunderhorse.pm

filter: Statementstmt 91.3% 74 / 81 Branchbran 79.1% 19 / 24 Conditioncond 44.4% 8 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 80.1% 125 / 156 CC 19 SCAR 10.6
CC
19
Cov
82%
_build_client
5.2
_http
4.0
http_header_is
3.3
CRAP 2.9
« blib/lib/Thunderhorse/Module/Middleware.pm ↑ Summary blib/lib/Thunderhorse/Context.pm »
1 package Test2::Thunderhorse;
2 19 $Test2::Thunderhorse::VERSION = '0.107';
3 19 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 19 ▶use Test2::API qw(context);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use PAGI::Test::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our @EXPORT = qw(
12 pagi_run
13 http
14 http_status_is
15 http_header_is
16 http_text_is
17 websocket
18 sse
19 );
20  
21 die 'Error: Test2::Thunderhorse loaded in a PAGI environment'
22 19 ▶ if ($ENV{PAGI_ENV} // 'test') ne 'test';
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PAGI_ENV"} // "test") ne "test" 0 19 19
Condition50%1/2
$ENV{"PAGI_ENV"} // "test"
l!l
019
MC/DC0%0/1
$ENV{"PAGI_ENV"} // "test"
$ENV{"PAGI_ENV"}
Truth table50%1/2
A
A= $ENV{"PAGI_ENV"}
Aresult
11
00
23 19 $ENV{PAGI_ENV} = 'test';
24  
25 19 our $THIS_CLIENT;
26 19 my $LAST_HTTP;
27 my $LAST_WS;
28 19 my $LAST_SSE;
29  
30 108 ▶sub _build_client ($app, %args)
Subroutine _build_client: called CC=5 SCAR=16.5
31 {
32 108 ▶ return $THIS_CLIENT if defined $THIS_CLIENT;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Test2::Thunderhorse::THIS_CLIENT 0 108 108
33  
34 108 ▶ if (ref $app eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $app eq 'ARRAY') { } 1 107 108
elsif (ref $app eq 'HASH') { } 0 107 107
35 1 %args = ($app->@[1, $app->$#*], app => $app->[0]->run, %args);
36 }
37 elsif (ref $app eq 'HASH') {
38 0 %args = ($app->%*, %args);
39 }
40 else {
41 107 $args{app} = $app->run;
42 }
43  
44 $args{raise_app_exceptions} = true
45 108 ▶ unless exists $args{raise_app_exceptions};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"raise_app_exceptions"} 107 1 108
46  
47 108 return PAGI::Test::Client->new(%args);
48 }
49  
50 2 ▶sub pagi_run ($app, $code)
Subroutine pagi_run: called CC=1 SCAR=0.0
Pod documented
51 {
52 2 my $client = _build_client($app, lifespan => true);
53 2 local $THIS_CLIENT = $client;
54  
55 2 $client->start;
56 2 $code->();
57 2 $client->stop;
58  
59 2 return $client->state;
60 }
61  
62 99 ▶sub _http ($app, $http_request, %args)
Subroutine _http: called CC=4 SCAR=13.9
63 {
64 # Extract method and path
65 99 my $method = lc $http_request->method;
66 99 my $path = $http_request->uri->path_query;
67  
68 # Extract headers
69 99 my %headers;
70 $http_request->headers->scan(
71 19 ▶ sub ($key, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 19 push $headers{$key}->@*, $value;
73 }
74 99 );
75  
76 99 ▶ $args{headers} = \%headers
Branch: 1 branch
BranchTrueFalseTotal
if %headers 9 90 99
77 if %headers;
78  
79 # Extract body for POST/PUT/PATCH
80 99 my $content = $http_request->content;
81 99 ▶ $args{body} = $content
Branch: 1 branch
BranchTrueFalseTotal
if length $content // "" 5 94 99
Condition50%1/2
length $content // ""
l!l
990
MC/DC0%0/1
length $content // ""
length $content
Truth table50%1/2
A
A= length $content
Aresult
11
00
82 if length $content // '';
83  
84 99 $LAST_HTTP = _build_client($app)->$method($path, %args);
85  
86 97 return $LAST_HTTP;
87 }
88  
89 138 ▶sub http (@args)
Subroutine http: called CC=3 SCAR=11.1
Pod documented
90 {
91 138 ▶ return $LAST_HTTP // croak 'no last http in Test2::Thunderhorse'
Branch: 1 branch
BranchTrueFalseTotal
if @args == 0 39 99 138
Condition50%1/2
$LAST_HTTP // croak("no last http in Test2::Thunderhorse")
l!l
390
MC/DC0%0/1
$LAST_HTTP // croak("no last http in Test2::Thunderhorse")
$LAST_HTTP
Truth table50%1/2
A
A= $LAST_HTTP
Aresult
11
00
92 if @args == 0;
93  
94 99 return _http @args;
95 }
96  
97 94 ▶sub http_status_is ($expected)
Subroutine http_status_is: called CC=1 SCAR=0.0
Pod documented
98 {
99 94 my $ctx = context();
100  
101 94 my $got = $LAST_HTTP->status;
102 94 my $pass = $got == $expected;
103  
104 94 $ctx->ok($pass, 'status ok', ["expected: $expected", "got: $got"]);
105  
106 94 $ctx->release;
107 94 return $pass;
108 }
109  
110 56 ▶sub http_text_is ($expected)
Subroutine http_text_is: called CC=2 SCAR=7.0
Pod documented
111 {
112 56 my $ctx = context();
113  
114 56 ▶ my $got = $LAST_HTTP->text // '<NULL>';
Condition50%1/2
$LAST_HTTP->text // "<NULL>"
l!l
560
MC/DC0%0/1
$LAST_HTTP->text // "<NULL>"
$LAST_HTTP->text
Truth table50%1/2
A
A= $LAST_HTTP->text
Aresult
11
00
115 56 my $pass = $got eq $expected;
116  
117 56 $ctx->ok($pass, 'body ok', ["expected: $expected", "got: $got"]);
118  
119 56 $ctx->release;
120 56 return $pass;
121 }
122  
123 25 ▶sub http_header_is ($header, $expected)
Subroutine http_header_is: called CC=3 SCAR=12.0
Pod documented
124 {
125 25 my $ctx = context();
126  
127 25 my $got = $LAST_HTTP->header($header);
128 25 ▶ my $pass = defined($got) && defined($expected) && $got eq $expected;
Condition33%2/6
defined $got && defined $expected && $got eq $expected
!ll&&!rl&&r
0025
defined $got and defined $expected
!ll&&!rl&&r
0025
MC/DC0%0/3
defined $got && defined $expected && $got eq $expected
defined $got defined $expected $got eq $expected
Truth table25%1/4
A and B && C
A= defined $gotB= defined $expectedC= $got eq $expected
ABCresult
0XX0
10X0
1100
1111
129  
130 25 $ctx->ok($pass, "$header header ok", ["expected: $expected", "got: $got"]);
131  
132 25 $ctx->release;
133 25 return $pass;
134 }
135  
136 3 ▶sub _websocket ($app, $path, @args)
Subroutine _websocket: called CC=2 SCAR=9.2
137 {
138 3 $LAST_WS = _build_client($app)->websocket($path, @args);
139  
140 3 ▶ if ($LAST_WS->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST_WS->is_closed) 0 3 3
141 0 my $ctx = context();
142 0 $ctx->fail("Connecting to websocket $path failed");
143 0 $ctx->release;
144 }
145  
146 3 return $LAST_WS;
147 }
148  
149 13 ▶sub websocket (@args)
Subroutine websocket: called CC=3 SCAR=11.1
Pod documented
150 {
151 13 ▶ return $LAST_WS // croak 'no last websocket in Test2::Thunderhorse'
Branch: 1 branch
BranchTrueFalseTotal
if @args == 0 10 3 13
Condition50%1/2
$LAST_WS // croak("no last websocket in Test2::Thunderhorse")
l!l
100
MC/DC0%0/1
$LAST_WS // croak("no last websocket in Test2::Thunderhorse")
$LAST_WS
Truth table50%1/2
A
A= $LAST_WS
Aresult
11
00
152 if @args == 0;
153  
154 3 return _websocket @args;
155 }
156  
157 4 ▶sub _sse ($app, $path, @args)
Subroutine _sse: called CC=2 SCAR=9.2
158 {
159 4 $LAST_SSE = _build_client($app)->sse($path, @args);
160  
161 4 ▶ if ($LAST_SSE->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST_SSE->is_closed) 0 4 4
162 0 my $ctx = context();
163 0 $ctx->fail("Connecting to sse $path failed");
164 0 $ctx->release;
165 }
166  
167 4 return $LAST_SSE;
168 }
169  
170 14 ▶sub sse (@args)
Subroutine sse: called CC=3 SCAR=11.1
Pod documented
171 {
172 14 ▶ return $LAST_SSE // croak 'no last sse in Test2::Thunderhorse'
Branch: 1 branch
BranchTrueFalseTotal
if @args == 0 10 4 14
Condition50%1/2
$LAST_SSE // croak("no last sse in Test2::Thunderhorse")
l!l
100
MC/DC0%0/1
$LAST_SSE // croak("no last sse in Test2::Thunderhorse")
$LAST_SSE
Truth table50%1/2
A
A= $LAST_SSE
Aresult
11
00
173 if @args == 0;
174  
175 4 return _sse @args;
176 }
177  
178 19 1;
179  
180 __END__
« blib/lib/Thunderhorse/Module/Middleware.pm ↑ Summary blib/lib/Thunderhorse/Context.pm »