lib/PAGI/App/Redirect.pm

filter: Statementstmt 100% 28 / 28 Branchbran 83.3% 5 / 6 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 94.4% 51 / 54 CC 7 SCAR 11.0
CC
7
Cov
98%
__ANON__
5.0
new
3.0
to_app
1.0
CRAP 3.0
« lib/PAGI/Middleware/Head.pm ↑ Summary blib/lib/PAGI/Middleware/Builder.pm »
1 package PAGI::App::Redirect;
2 1 $PAGI::App::Redirect::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PAGI::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding UTF-8
9  
10 =head1 NAME
11  
12 PAGI::App::Redirect - URL redirect app for the dynamic case
13  
14 =head1 SYNOPSIS
15  
16 # The static case is just a response value (preferred):
17 use PAGI::Response;
18 $router->mount('/old' => PAGI::Response->redirect('/new', 301));
19  
20 # PAGI::App::Redirect is for the dynamic case — a coderef target
21 # and/or query-string preservation:
22 use PAGI::App::Redirect;
23 my $app = PAGI::App::Redirect->new(
24 to => sub { my ($scope) = @_; compute_target($scope) },
25 status => 302,
26 preserve_query => 1,
27 )->to_app;
28  
29 =cut
30  
31 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
32 6 my ($class, %args) = @_;
33  
34 return bless {
35 to => $args{to},
36 status => $args{status} // 302,
37 6 ▶ preserve_query => $args{preserve_query} // 1,
Condition100%4/4
$args{"status"} // 302
l!l
24
$args{"preserve_query"} // 1
l!l
15
MC/DC100%1/1
$args{"status"} // 302
$args{"status"}
MC/DC100%1/1
$args{"preserve_query"} // 1
$args{"preserve_query"}
Truth table100%2/2
A
A= $args{"status"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"preserve_query"}
Aresult
11
00
38 }, $class;
39 }
40  
41 6 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
42 6 my ($self) = @_;
43  
44 6 my $to = $self->{to};
45 6 my $status = $self->{status};
46 6 my $preserve_query = $self->{preserve_query};
47  
48 6 ▶ return async sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
49 6 my ($scope, $receive, $send) = @_;
50 6 ▶ my $location = ref $to eq 'CODE' ? $to->($scope) : $to;
Branch: 1 branch
BranchTrueFalseTotal
ref $to eq 'CODE' ? : 2 4 6
51  
52 6 ▶ if ($preserve_query && $scope->{query_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_query and $scope->{"query_string"}) 1 5 6
Condition100%3/3
$preserve_query and $scope->{"query_string"}
!ll&&!rl&&r
141
MC/DC100%2/2
$preserve_query and $scope->{"query_string"}
$preserve_query $scope->{"query_string"}
Truth table100%3/3
A and B
A= $preserve_queryB= $scope->{"query_string"}
ABresult
0X0
100
111
53 1 ▶ my $sep = $location =~ /\?/ ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$location =~ /\?/ ? : 0 1 1
54 1 $location .= $sep . $scope->{query_string};
55 }
56  
57 await PAGI::Response->new($scope)
58 ->content_type('text/plain')
59 ->redirect($location, $status)
60 6 ->respond($send);
61 6 };
62 }
63  
64 1 1;
65  
66 __END__
« lib/PAGI/Middleware/Head.pm ↑ Summary blib/lib/PAGI/Middleware/Builder.pm »